Skip to content

Commit

Permalink
Pushing fixes to LiveLogTick, bounds checking for memory functions wr…
Browse files Browse the repository at this point in the history
…iting to FRAM within a setting, and elf binary file space reducing modifications to the Makefile
  • Loading branch information
maxieds committed Oct 15, 2020
1 parent 410a26a commit 93c9884
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 73 deletions.
126 changes: 66 additions & 60 deletions Firmware/Chameleon-Mini/LiveLogTick.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
#ifndef FLUSH_LOGS_ON_SPACE_ERROR
#define FLUSH_LOGS_ON_SPACE_ERROR (1)
#endif

typedef struct LogBlockListNode {
uint8_t *logBlockStart;
uint8_t logBlockSize;
struct LogBlockListNode *nextBlock;
uint8_t *logBlockDataStart;
uint8_t logBlockDataSize;
struct LogBlockListNode *nextBlock;
} LogBlockListNode;

#define LOG_BLOCK_LIST_NODE_SIZE (sizeof(LogBlockListNode) + 4 - (uint8_t) (sizeof(LogBlockListNode) % 4))
extern LogBlockListNode *LogBlockListBegin;
extern LogBlockListNode *LogBlockListEnd;
extern uint8_t LogBlockListElementCount;
Expand All @@ -44,75 +44,81 @@ INLINE void FreeLogBlocks(void);
INLINE bool AtomicLiveLogTick(void);
INLINE bool LiveLogTick(void);

INLINE bool
INLINE bool
AtomicAppendLogBlock(LogEntryEnum logCode, uint16_t sysTickTime, const uint8_t *logData, uint8_t logDataSize) {
bool status = true;
if ((logDataSize + 4 > LogMemLeft) && (LogMemPtr != LogMem)) {
if (FLUSH_LOGS_ON_SPACE_ERROR) {
LiveLogTick();
FreeLogBlocks();
}
status = false;
} else if (logDataSize + 4 <= LogMemLeft) {
LogBlockListNode *logBlock = (LogBlockListNode *) malloc(sizeof(LogBlockListNode));
logBlock->logBlockStart = LogMemPtr;
logBlock->logBlockSize = logDataSize + 4;
logBlock->nextBlock = NULL;
*(LogMemPtr++) = logCode;
*(LogMemPtr++) = logDataSize;
*(LogMemPtr++) = (uint8_t)(sysTickTime >> 8);
*(LogMemPtr++) = (uint8_t)(sysTickTime >> 0);
memcpy(LogMemPtr, logData, logDataSize);
LogMemPtr += logDataSize;
LogMemLeft -= logDataSize + 4;
if (LogBlockListBegin != NULL && LogBlockListEnd != NULL) {
LogBlockListEnd->nextBlock = logBlock;
LogBlockListEnd = logBlock;
} else {
LogBlockListBegin = LogBlockListEnd = logBlock;
}
++LogBlockListElementCount;
} else {
status = false;
}
return status;
bool status = true;
if((logDataSize + 4 + 3 + LOG_BLOCK_LIST_NODE_SIZE > LogMemLeft) && (LogMemPtr != LogMem)) {
if(FLUSH_LOGS_ON_SPACE_ERROR) {
LiveLogTick();
FreeLogBlocks();
}
status = false;
}
else if(logDataSize + 4 + 3 + LOG_BLOCK_LIST_NODE_SIZE <= LogMemLeft) {
uint8_t alignOffset = 4 - (uint8_t) (((uint16_t) LogMemPtr) % 4);
uint8_t *logBlockStart = LogMemPtr + alignOffset;
LogBlockListNode logBlock;
LogMemPtr += LOG_BLOCK_LIST_NODE_SIZE + alignOffset;
LogMemLeft -= LOG_BLOCK_LIST_NODE_SIZE + alignOffset;
logBlock.logBlockDataStart = LogMemPtr;
logBlock.logBlockDataSize = logDataSize + 4;
logBlock.nextBlock = 0;
*(LogMemPtr++) = (uint8_t) logCode;
*(LogMemPtr++) = logDataSize;
*(LogMemPtr++) = (uint8_t) (sysTickTime >> 8);
*(LogMemPtr++) = (uint8_t) (sysTickTime >> 0);
memcpy(LogMemPtr, logData, logDataSize);
LogMemPtr += logDataSize;
LogMemLeft -= logDataSize + 4;
memcpy(logBlockStart, &logBlock, sizeof(LogBlockListNode));
if(LogBlockListBegin != NULL && LogBlockListEnd != NULL) {
LogBlockListEnd->nextBlock = (LogBlockListNode *) logBlockStart;
LogBlockListEnd = (LogBlockListNode *) logBlockStart;
}
else {
LogBlockListBegin = LogBlockListEnd = (LogBlockListNode *) logBlockStart;
}
++LogBlockListElementCount;
}
else {
status = false;
}
return status;
}

INLINE void
FreeLogBlocks(void) {
LogMemPtr = &LogMem[0];
LogBlockListNode *logBlockCurrent = LogBlockListBegin;
LogBlockListNode *logBlockNext = NULL;
while (logBlockCurrent != NULL) {
logBlockNext = logBlockCurrent->nextBlock;
LogMemLeft += logBlockCurrent->logBlockSize;
free(logBlockCurrent);
logBlockCurrent = logBlockNext;
}
LogBlockListBegin = LogBlockListEnd = NULL;
LogBlockListElementCount = 0;
LogMemPtr = &LogMem[0];
LogMemLeft = LOG_SIZE;
LogBlockListBegin = LogBlockListEnd = NULL;
LogBlockListElementCount = 0;
}

INLINE bool
AtomicLiveLogTick(void) {
bool status;
status = LiveLogTick();
return status;
return LiveLogTick();
}

INLINE bool
LiveLogTick(void) {
bool status = LogBlockListBegin == NULL;
LogBlockListNode *logBlockCurrent = LogBlockListBegin;
while (logBlockCurrent != NULL && LogBlockListElementCount > 0) {
TerminalFlushBuffer();
TerminalSendBlock(logBlockCurrent->logBlockStart, logBlockCurrent->logBlockSize);
TerminalFlushBuffer();
logBlockCurrent = logBlockCurrent->nextBlock;
}
FreeLogBlocks();
LiveLogModePostTickCount = 0x00;
return status;
LogBlockListNode logBlockCurrent, *tempBlockPtr = NULL;
memcpy(&logBlockCurrent, LogBlockListBegin, sizeof(LogBlockListNode));
while(LogBlockListElementCount > 0) {
TerminalFlushBuffer();
TerminalSendBlock(logBlockCurrent.logBlockDataStart, logBlockCurrent.logBlockDataSize);
TerminalFlushBuffer();
tempBlockPtr = logBlockCurrent.nextBlock;
if(tempBlockPtr != NULL) {
memcpy(&logBlockCurrent, tempBlockPtr, sizeof(LogBlockListNode));
}
else {
break;
}
--LogBlockListElementCount;
}
FreeLogBlocks();
LiveLogModePostTickCount = 0;
return true;
}

#endif
26 changes: 15 additions & 11 deletions Firmware/Chameleon-Mini/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ SHELL = /bin/sh
#Supported configurations
SETTINGS += -DCONFIG_MF_CLASSIC_MINI_4B_SUPPORT
SETTINGS += -DCONFIG_MF_CLASSIC_1K_SUPPORT
SETTINGS += -DCONFIG_MF_CLASSIC_1K_7B_SUPPORT
#SETTINGS += -DCONFIG_MF_CLASSIC_1K_7B_SUPPORT
SETTINGS += -DCONFIG_MF_CLASSIC_4K_SUPPORT
SETTINGS += -DCONFIG_MF_CLASSIC_4K_7B_SUPPORT
#SETTINGS += -DCONFIG_MF_CLASSIC_4K_7B_SUPPORT
SETTINGS += -DCONFIG_MF_ULTRALIGHT_SUPPORT
SETTINGS += -DCONFIG_ISO14443A_SNIFF_SUPPORT
SETTINGS += -DCONFIG_ISO14443A_READER_SUPPORT
SETTINGS += -DCONFIG_NTAG215_SUPPORT
SETTINGS += -DCONFIG_VICINITY_SUPPORT
SETTINGS += -DCONFIG_SL2S2002_SUPPORT
SETTINGS += -DCONFIG_TITAGITSTANDARD_SUPPORT
#SETTINGS += -DCONFIG_NTAG215_SUPPORT
#SETTINGS += -DCONFIG_VICINITY_SUPPORT
#SETTINGS += -DCONFIG_SL2S2002_SUPPORT
#SETTINGS += -DCONFIG_TITAGITSTANDARD_SUPPORT
SETTINGS += -DCONFIG_ISO15693_SNIFF_SUPPORT
SETTINGS += -DCONFIG_EM4233_SUPPORT
#SETTINGS += -DCONFIG_EM4233_SUPPORT

#Support magic mode on mifare classic configuration
SETTINGS += -DSUPPORT_MF_CLASSIC_MAGIC_MODE
Expand Down Expand Up @@ -78,8 +78,10 @@ SETTINGS += -DDEFAULT_READER_THRESHOLD=400
SETTINGS += -DENABLE_EEPROM_SETTINGS

#Memory definitions and objcopy flags to include sections in binaries
#FLASH_DATA_ADDR = 0x10000 #Start of data section in flash
#FLASH_DATA_SIZE = 0x10000 #Size of data section in flash
FLASH_DATA_ADDR = 0x10000 #Start of data section in flash
FLASH_DATA_SIZE = 0x10000 #Size of data section in flash
FLASH_DATA_SIZE = 0x0FFFF #Size of data section in flash
FLASH_DATA_OBJCOPY = --set-section-flags=.flashdata="alloc,load"
SPM_HELPER_ADDR = 0x21FE0 #Start of SPM helper section. Should be last 32Byte in bootloader section
SPM_HELPER_OBJCOPY = --set-section-flags=.spmhelper="alloc,load"
Expand Down Expand Up @@ -107,10 +109,13 @@ SRC += Codec/ISO15693.c
SRC += Application/Vicinity.c Application/Sl2s2002.c Application/TITagitstandard.c Application/ISO15693-A.c Application/EM4233.c
SRC += $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS)
LUFA_PATH = ../LUFA
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -DFLASH_DATA_ADDR=$(FLASH_DATA_ADDR) -DFLASH_DATA_SIZE=$(FLASH_DATA_SIZE) \
CC_FLAGS = -g0 -DUSE_LUFA_CONFIG_HEADER -DFLASH_DATA_ADDR=$(FLASH_DATA_ADDR) -DFLASH_DATA_SIZE=$(FLASH_DATA_SIZE) \
-DSPM_HELPER_ADDR=$(SPM_HELPER_ADDR) -DBUILD_DATE=$(BUILD_DATE) -DCOMMIT_ID=\"$(COMMIT_ID)\" $(SETTINGS) \
-D__AVR_ATxmega128A4U__ -D__PROG_TYPES_COMPAT__ -DMAX_ENDPOINT_INDEX=4 \
-std=gnu99 -Werror=implicit-function-declaration
-std=gnu99 -Werror=implicit-function-declaration \
-fno-inline-small-functions -fshort-enums -fpack-struct \
-ffunction-sections -Wl,--gc-sections --data-sections -ffunction-sections \
-Wl,-relax -fno-split-wide-types -fno-tree-scev-cprop
LD_FLAGS = $(CC_FLAGS) -Wl,--section-start=.flashdata=$(FLASH_DATA_ADDR) -Wl,--section-start=.spmhelper=$(SPM_HELPER_ADDR)
OBJDIR = Bin
OBJECT_FILES =
Expand All @@ -131,7 +136,6 @@ AVRDUDE_WRITE_EEPROM_LATEST = -U eeprom:w:Latest/Chameleon-Mini.eep

# Default target
all:
$(CROSS)-size --mcu=atmega128 -C $(TARGET).elf

# Include LUFA build script makefiles
include $(LUFA_PATH)/Build/lufa_core.mk
Expand Down
17 changes: 17 additions & 0 deletions Firmware/Chameleon-Mini/Memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ void MemoryReadBlock(void *Buffer, uint16_t Address, uint16_t ByteCount) {
FRAMRead(Buffer, Address, ByteCount);
}

void MemoryReadBlockInSetting(void* Buffer, uint16_t Address, uint16_t ByteCount)
{
if (ByteCount == 0 || Address >= MEMORY_SIZE_PER_SETTING)
return;
uint16_t ActualFRAMAddress = Address + GlobalSettings.ActiveSettingIdx * MEMORY_SIZE_PER_SETTING;
FRAMRead(Buffer, ActualFRAMAddress, ByteCount);
}

void MemoryWriteBlock(const void *Buffer, uint16_t Address, uint16_t ByteCount) {
if (ByteCount == 0)
return;
Expand All @@ -370,6 +378,15 @@ void MemoryWriteBlock(const void *Buffer, uint16_t Address, uint16_t ByteCount)
LEDHook(LED_MEMORY_CHANGED, LED_ON);
}

void MemoryWriteBlockInSetting(const void* Buffer, uint16_t Address, uint16_t ByteCount)
{
if (ByteCount == 0 || Address >= MEMORY_SIZE_PER_SETTING)
return;
uint16_t ActualFRAMAddress = Address + GlobalSettings.ActiveSettingIdx * MEMORY_SIZE_PER_SETTING;
FRAMWrite(Buffer, ActualFRAMAddress, ByteCount);
LEDHook(LED_MEMORY_CHANGED, LED_ON);
}

void MemoryClear(void) {
FlashErase((uint32_t) GlobalSettings.ActiveSettingIdx * MEMORY_SIZE_PER_SETTING, MEMORY_SIZE_PER_SETTING);

Expand Down
6 changes: 4 additions & 2 deletions Firmware/Chameleon-Mini/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include "Common.h"

void MemoryInit(void);
void MemoryReadBlock(void *Buffer, uint16_t Address, uint16_t ByteCount);
void MemoryWriteBlock(const void *Buffer, uint16_t Address, uint16_t ByteCount);
void MemoryReadBlock(void* Buffer, uint16_t Address, uint16_t ByteCount);
void MemoryReadBlockInSetting(void* Buffer, uint16_t Address, uint16_t ByteCount);
void MemoryWriteBlock(const void* Buffer, uint16_t Address, uint16_t ByteCount);
void MemoryWriteBlockInSetting(const void* Buffer, uint16_t Address, uint16_t ByteCount);
void MemoryClear(void);


Expand Down

0 comments on commit 93c9884

Please sign in to comment.