diff --git a/debug.c b/debug.c index 7ef2342..db31482 100644 --- a/debug.c +++ b/debug.c @@ -17,8 +17,15 @@ int PRINTF(const char *format, ...) va_start(args, format); /*convert into string at buff[0] of length iw*/ len = (int)fixup_vsnprintf(&dbg_buf[0], DBG_BUF_LEN, format, args); + + if (len >= DBG_BUF_LEN) { + dbg_buf[DBG_BUF_LEN-1] = 0; + len = DBG_BUF_LEN-1; + } + _write_r(NULL, 2, dbg_buf, (size_t) len); va_end(args); + return len; } @@ -33,6 +40,15 @@ void PUTSN(const char *string, size_t len) _write_r(NULL, 2, string, (size_t) len); } +/** + * Puts a newline + * + */ +void PUTNL(void) +{ + _write_r(NULL, 2, "\r\n", 2); +} + /** * Print a string to debug uart * @param string - string to print, zero-terminated @@ -56,26 +72,4 @@ int PUTCHAR(int ch) return ch; // or EOF } -/** - * Print string to debug uart, add newline if missing (printf-like) - * @param format - * @param ... - */ -void dbg(const char *format, ...) -{ - va_list args; - int len; - char dbg_buf[DBG_BUF_LEN]; - - va_start(args, format); - len = (int)VSNPRINTF(&dbg_buf[0], DBG_BUF_LEN, format, args); - _write_r(NULL, 2, dbg_buf, (size_t) len); - - // add newline if not present - if (dbg_buf[len-1] != '\n') - _write_r(NULL, 2, "\r\n", 2); - - va_end(args); -} - #endif diff --git a/debug.h b/debug.h index d61ff96..20bdff2 100644 --- a/debug.h +++ b/debug.h @@ -11,13 +11,14 @@ #if USE_DEBUG_UART -void dbg(const char *format, ...) __attribute__((format(printf,1,2))) ; - int PRINTF(const char *format, ...) __attribute__((format(printf,1,2))) ; void PUTSN(const char *string, size_t len); int PUTS(const char *string); +void PUTNL(void); int PUTCHAR(int ch); +#define dbg(format, ...) do { PRINTF(format, ##__VA_ARGS__); PUTNL(); } while (0) + #else #define dbg(format, ...) do {} while (0) #define PRINTF(format, ...) do {} while (0) diff --git a/freertos.c b/freertos.c index af2b0a9..07b0e57 100644 --- a/freertos.c +++ b/freertos.c @@ -71,7 +71,7 @@ uint32_t jobRunnerBuffer[ TSK_STACK_JOBRUNNER ]; osStaticThreadDef_t jobRunnerControlBlock; osMessageQId queSchedHandle; -uint8_t myQueue02Buffer[ HP_SCHED_CAPACITY * sizeof( struct sched_que_item ) ]; +uint8_t myQueue02Buffer[ JOB_QUEUE_CAPACITY * sizeof( struct sched_que_item ) ]; osStaticMessageQDef_t myQueue02ControlBlock; osMessageQId queRxDataHandle; @@ -180,7 +180,7 @@ void MX_FREERTOS_Init(void) { /* Create the queue(s) */ /* definition and creation of queSchedHP */ - osMessageQStaticDef(queSchedHP, HP_SCHED_CAPACITY, struct sched_que_item, myQueue02Buffer, &myQueue02ControlBlock); + osMessageQStaticDef(queSchedHP, JOB_QUEUE_CAPACITY, struct sched_que_item, myQueue02Buffer, &myQueue02ControlBlock); queSchedHandle = osMessageCreate(osMessageQ(queSchedHP), NULL); /* definition and creation of queRxData */ diff --git a/gex.mk b/gex.mk index c56b500..37b9b0f 100644 --- a/gex.mk +++ b/gex.mk @@ -61,17 +61,10 @@ GEX_CDEFS_BASE = \ -D__packed="__attribute__((__packed__))" \ -DUSE_FULL_LL_DRIVER \ -# TODO implement debug build choice - enable or disable debugging -ifeq '1' '1' -GEX_CDEFS = $(GEX_CDEFS_BASE) \ - -DUSE_FULL_ASSERT=1 \ - -DVERBOSE_ASSERT=1 \ - -DDEBUG_VFS=0 \ - -DDEBUG_FLASH_WRITE=0 \ - -DVERBOSE_HARDFAULT=1 \ - -DUSE_STACK_MONITOR=1 \ - -DUSE_DEBUG_UART=1 -else + + +ifeq '$(DISABLE_DEBUG)' '1' + GEX_CDEFS = $(GEX_CDEFS_BASE) \ -DUSE_FULL_ASSERT=0 \ -DVERBOSE_ASSERT=0 \ @@ -80,11 +73,26 @@ GEX_CDEFS = $(GEX_CDEFS_BASE) \ -DVERBOSE_HARDFAULT=0 \ -DUSE_STACK_MONITOR=0 \ -DUSE_DEBUG_UART=0 + +else + +GEX_CDEFS = $(GEX_CDEFS_BASE) \ + -DUSE_FULL_ASSERT=1 \ + -DVERBOSE_ASSERT=1 \ + -DDEBUG_VFS=0 \ + -DDEBUG_FLASH_WRITE=0 \ + -DVERBOSE_HARDFAULT=1 \ + -DUSE_STACK_MONITOR=1 \ + -DUSE_DEBUG_UART=1 + endif -# TODO implement debug build choice - enable or disable MSC -ifeq '1' '1' +ifeq '$(DISABLE_MSC)' '1' + +GEX_CDEFS += -DDISABLE_MSC + +else GEX_SOURCES += \ User/USB/usbd_storage_if.c \ @@ -96,6 +104,4 @@ GEX_SOURCES += \ GEX_SRC_DIR += \ User/vfs -else -GEX_CDEFS += -DDISABLE_MSC endif diff --git a/gex_hooks.c b/gex_hooks.c index 3db152a..bc5d4ec 100644 --- a/gex_hooks.c +++ b/gex_hooks.c @@ -39,7 +39,7 @@ void GEX_PreInit(void) DebugUart_PreInit(); dbg("\r\n\033[37;1m*** GEX "GEX_VERSION" on "GEX_PLATFORM" ***\033[m"); - dbg("Build "__DATE__" "__TIME__"\r\n"); + dbg("Build "__DATE__" "__TIME__); plat_init(); diff --git a/platform/plat_compat.h b/platform/plat_compat.h index aa4437c..5f57084 100644 --- a/platform/plat_compat.h +++ b/platform/plat_compat.h @@ -5,69 +5,39 @@ #ifndef GEX_PLAT_COMPAT_H #define GEX_PLAT_COMPAT_H -// -------- Buffers and stack sizes --------- +// -------- Static buffers --------- +#define TSK_STACK_MAIN 150 // USB / VFS task stack size +#define TSK_STACK_MSG 180 // TF message handler task stack size +#define TSK_STACK_JOBRUNNER 100 // Job runner task stack size -// FreeRTOS thread stacks (in 4-byte words) -#define TSK_STACK_MAIN 160 -#define TSK_STACK_MSG 200 -#define TSK_STACK_JOBRUNNER 128 +#define BULKREAD_MAX_CHUNK 256 // Bulk read buffer -// Size of the snprintf buffer for debug messages -// (this is on stack to avoid races) -#define DBG_BUF_LEN 80 +#define FLASH_SAVE_BUF_LEN 256 // Static buffer for saving to flash -// Bulk read/write -#define BULK_LST_TIMEOUT_MS 200 // timeout for the bulk transaction to expire -#define BULKREAD_MAX_CHUNK 512 // this is a static buffer +#define JOB_QUEUE_CAPACITY 4 // Job runner queue size (16 bytes each) +#define RX_QUE_CAPACITY 10 // TinyFrame rx queue size (64 bytes each) -// Error message buffer size (on stack) -#define ERR_MSG_STR_LEN 32 +#define TF_MAX_PAYLOAD_RX 512 // TF max Rx payload +#define TF_SENDBUF_LEN 64 // TF transmit buffer (can be less than a full frame) -// Static buffer for saving to flash -#define FLASH_SAVE_BUF_LEN 256 +#define TF_MAX_ID_LST 4 // Frame ID listener count +#define TF_MAX_TYPE_LST 6 // Frame Type listener count +#define TF_MAX_GEN_LST 1 // Generic listener count -// Number of job runner slots -#define HP_SCHED_CAPACITY 5 +#define USBD_MAX_STR_DESC_SIZ 64 // Descriptor conversion buffer (used for converting ASCII to UTF-16, must be 2x the size of the longest descriptor) +#define MSC_MEDIA_PACKET 512 // Mass storage sector size (packet) -// Number of message queue slots (64 bytes each) -#define RX_QUE_CAPACITY 10 +#define INI_KEY_MAX 20 // Ini parser key buffer +#define INI_VALUE_MAX 30 // Ini parser value buffer +// -------- Stack buffers ---------- +#define DBG_BUF_LEN 80 // Size of the snprintf buffer for debug messages +#define ERR_MSG_STR_LEN 32 // Error message buffer size +#define IWBUFFER_LEN 80 // Ini writer buffer for sprintf -// ------ TinyFrame config ------ - -// Maximum received payload size (static buffer) -// Larger payloads will be rejected. -#define TF_MAX_PAYLOAD_RX 640 - -// Size of the sending buffer. Larger payloads will be split to pieces and sent -// in multiple calls to the write function. This can be lowered to reduce RAM usage. -#define TF_SENDBUF_LEN 64 - -// --- Listener counts - determine sizes of the static slot tables --- - -// Frame ID listeners (wait for response / multi-part message) -#define TF_MAX_ID_LST 4 -// Frame Type listeners (wait for frame with a specific first payload byte) -#define TF_MAX_TYPE_LST 6 -// Generic listeners (fallback if no other listener catches it) -#define TF_MAX_GEN_LST 1 - -// Timeout for receiving & parsing a frame -// ticks = number of calls to TF_Tick() -#define TF_PARSER_TIMEOUT_TICKS 250 - - -// --- Mass Storage / USB config --- - -#define USBD_MAX_STR_DESC_SIZ 128 -#define MSC_MEDIA_PACKET 512 - - -// INI buffer sizes -#define INI_KEY_MAX 20 -#define INI_VALUE_MAX 30 -// ini writer -#define IWBUFFER_LEN 128 +// -------- Timeouts ------------ +#define TF_PARSER_TIMEOUT_TICKS 100 // Timeout for receiving & parsing a frame +#define BULK_LST_TIMEOUT_MS 200 // timeout for the bulk transaction to expire diff --git a/stm32_assert.c b/stm32_assert.c index 9d56d0c..8b58d74 100644 --- a/stm32_assert.c +++ b/stm32_assert.c @@ -9,7 +9,7 @@ */ void __attribute__((noreturn)) abort_msg(const char *msg, const char *filename, uint32_t line) { - dbg("\r\n\033[31m%s:\033[m %s:%"PRIu32"\r\n", msg, filename, line); + dbg("\r\n\033[31m%s:\033[m %s:%"PRIu32, msg, filename, line); vPortEnterCritical(); StatusLed_On(STATUS_FAULT); while(1); @@ -23,7 +23,7 @@ void __attribute__((noreturn)) abort_msg(const char *msg, const char *filename, */ void warn_msg(const char *msg, const char *filename, uint32_t line) { - dbg("\r\n\033[33m%s:\033[m %s:%"PRIu32"\r\n", msg, filename, line); + dbg("\r\n\033[33m%s:\033[m %s:%"PRIu32, msg, filename, line); } /** diff --git a/utils/stacksmon.c b/utils/stacksmon.c index cc8d753..99e4e5a 100644 --- a/utils/stacksmon.c +++ b/utils/stacksmon.c @@ -81,7 +81,7 @@ void stackmon_check_canaries(void) for (uint32_t i = 0; i < nextidx; i++) { struct stackhandle *stack = &stacks[i]; if (stack->buffer[0] != 0xA5) { - dbg("\r\n\033[31;1m!!!! STACK \"%s\" OVERRUN - CANARY IS DEAD !!!!\033[m\r\n", stack->description); + dbg("\r\n\033[31;1m!!!! STACK \"%s\" OVERRUN - CANARY IS DEAD !!!!\033[m", stack->description); stackmon_dump(); trap("ABORT"); } diff --git a/vfs/vfs_manager.c b/vfs/vfs_manager.c index 9d2bbde..aac5716 100644 --- a/vfs/vfs_manager.c +++ b/vfs/vfs_manager.c @@ -575,6 +575,7 @@ static void file_data_handler(uint32_t sector, const uint8_t *buf, uint32_t num_ file_transfer_state.size_transferred); vfs_printf_nonl("\033[31m", 5); + // FIXME this seems wrong vfs_printf_nonl((const char *) buf, VFS_SECTOR_SIZE * num_of_sectors); vfs_printf_nonl("\033[0m\r\n", 6);