8 #ifndef __LIVE_LOG_TICK_H__ 9 #define __LIVE_LOG_TICK_H__ 13 #include <avr/interrupt.h> 15 #include <util/atomic.h> 20 #include "Terminal/Terminal.h" 22 #define cli_memory() __asm volatile( "cli" ::: "memory" ) 23 #define sei_memory() __asm volatile( "sei" ::: "memory" ) 25 #ifndef FLUSH_LOGS_ON_SPACE_ERROR 26 #define FLUSH_LOGS_ON_SPACE_ERROR (1) 28 typedef struct LogBlockListNode {
29 uint8_t *logBlockDataStart;
30 uint8_t logBlockDataSize;
31 struct LogBlockListNode *nextBlock;
34 #define LOG_BLOCK_LIST_NODE_SIZE (sizeof(LogBlockListNode) + 4 - (uint8_t) (sizeof(LogBlockListNode) % 4)) 35 extern LogBlockListNode *LogBlockListBegin;
36 extern LogBlockListNode *LogBlockListEnd;
37 extern uint8_t LogBlockListElementCount;
39 #define LIVE_LOGGER_POST_TICKS (6) 40 extern uint8_t LiveLogModePostTickCount;
42 INLINE
bool AtomicAppendLogBlock(
LogEntryEnum logCode, uint16_t sysTickTime,
const uint8_t *logData, uint8_t logDataSize);
43 INLINE
void FreeLogBlocks(
void);
44 INLINE
bool AtomicLiveLogTick(
void);
45 INLINE
bool LiveLogTick(
void);
48 AtomicAppendLogBlock(
LogEntryEnum logCode, uint16_t sysTickTime,
const uint8_t *logData, uint8_t logDataSize) {
50 if ((logDataSize + 4 + 3 + LOG_BLOCK_LIST_NODE_SIZE > LogMemLeft) && (LogMemPtr != LogMem)) {
51 if (FLUSH_LOGS_ON_SPACE_ERROR) {
56 }
else if (logDataSize + 4 + 3 + LOG_BLOCK_LIST_NODE_SIZE <= LogMemLeft) {
57 uint8_t alignOffset = 4 - (uint8_t)(((uint16_t) LogMemPtr) % 4);
58 uint8_t *logBlockStart = LogMemPtr + alignOffset;
59 LogBlockListNode logBlock;
60 LogMemPtr += LOG_BLOCK_LIST_NODE_SIZE + alignOffset;
61 LogMemLeft -= LOG_BLOCK_LIST_NODE_SIZE + alignOffset;
62 logBlock.logBlockDataStart = LogMemPtr;
63 logBlock.logBlockDataSize = logDataSize + 4;
64 logBlock.nextBlock = 0;
65 *(LogMemPtr++) = (uint8_t) logCode;
66 *(LogMemPtr++) = logDataSize;
67 *(LogMemPtr++) = (uint8_t)(sysTickTime >> 8);
68 *(LogMemPtr++) = (uint8_t)(sysTickTime >> 0);
69 memcpy(LogMemPtr, logData, logDataSize);
70 LogMemPtr += logDataSize;
71 LogMemLeft -= logDataSize + 4;
72 memcpy(logBlockStart, &logBlock,
sizeof(LogBlockListNode));
73 if (LogBlockListBegin != NULL && LogBlockListEnd != NULL) {
74 LogBlockListEnd->nextBlock = (LogBlockListNode *) logBlockStart;
75 LogBlockListEnd = (LogBlockListNode *) logBlockStart;
77 LogBlockListBegin = LogBlockListEnd = (LogBlockListNode *) logBlockStart;
79 ++LogBlockListElementCount;
88 LogMemPtr = &LogMem[0];
89 LogMemLeft = LOG_SIZE;
90 LogBlockListBegin = LogBlockListEnd = NULL;
91 LogBlockListElementCount = 0;
95 AtomicLiveLogTick(
void) {
101 LogBlockListNode logBlockCurrent, *tempBlockPtr = NULL;
102 memcpy(&logBlockCurrent, LogBlockListBegin,
sizeof(LogBlockListNode));
103 while (LogBlockListElementCount > 0) {
104 TerminalFlushBuffer();
105 TerminalSendBlock(logBlockCurrent.logBlockDataStart, logBlockCurrent.logBlockDataSize);
106 TerminalFlushBuffer();
107 tempBlockPtr = logBlockCurrent.nextBlock;
108 if (tempBlockPtr != NULL) {
109 memcpy(&logBlockCurrent, tempBlockPtr,
sizeof(LogBlockListNode));
113 --LogBlockListElementCount;
116 LiveLogModePostTickCount = 0;
LogEntryEnum
Definition: Log.h:16