From 69443613fc2f5f80baf1233954ea8b5b3b86916c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 13 Jan 2018 21:33:19 +0100 Subject: [PATCH] some more stack shrinking --- debug.c | 34 ++++++---------------------------- debug.h | 25 ++++++++++++++++++++----- framework/unit_registry.c | 4 ++-- gex.mk | 2 +- platform/debug_uart.c | 17 +++++++++-------- platform/debug_uart.h | 2 ++ utils/ini_writer.c | 12 +++++++++++- utils/ini_writer.h | 9 ++++++++- 8 files changed, 59 insertions(+), 46 deletions(-) diff --git a/debug.c b/debug.c index 9bc2af2..001df58 100644 --- a/debug.c +++ b/debug.c @@ -8,7 +8,7 @@ #if USE_DEBUG_UART // debug printf -int _DO_PRINTF(const char *format, ...) +void _DO_PRINTF(const char *format, ...) { va_list args; int len; @@ -25,8 +25,6 @@ int _DO_PRINTF(const char *format, ...) _write_r(NULL, 2, dbg_buf, (size_t) len); va_end(args); - - return len; } /** @@ -34,19 +32,10 @@ int _DO_PRINTF(const char *format, ...) * @param string - buffer to print * @param len - number of bytes to print */ -void PUTSN(const char *string, size_t len) -{ - if (len == 0) len = strlen(string); - _write_r(NULL, 2, string, (size_t) len); -} - -/** - * Puts a newline - * - */ -void PUTNL(void) +void PUTSN(const char *string, uint16_t len) { - _write_r(NULL, 2, "\r\n", 2); + if (len == 0) len = (uint16_t) strlen(string); + debug_write(string, len); } /** @@ -54,22 +43,11 @@ void PUTNL(void) * @param string - string to print, zero-terminated * @return number of characters printed */ -int PUTS(const char *string) +void PUTS(const char *string) { size_t len = strlen(string); - _write_r(NULL, 2, string, len); - return (int) len; + debug_write(string, (uint16_t) len); } -/** - * Print one character to debug uart - * @param ch - character ASCII code - * @return the character code - */ -int PUTCHAR(int ch) -{ - _write_r(NULL, 2, &ch, 1); - return ch; // or EOF -} #endif diff --git a/debug.h b/debug.h index ddaa470..6c9ebd4 100644 --- a/debug.h +++ b/debug.h @@ -11,11 +11,26 @@ #if USE_DEBUG_UART -int _DO_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); +extern void debug_write(const char *buf, uint16_t len); + +void _DO_PRINTF(const char *format, ...) __attribute__((format(printf,1,2))) ; +void PUTSN(const char *string, uint16_t len); +void PUTS(const char *string); + +static inline void PUTNL(void) +{ + debug_write("\r\n", 2); +} + +/** + * Print one character to debug uart + * @param ch - character ASCII code + * @return the character code + */ +static inline void PUTCHAR(char ch) +{ + debug_write(&ch, 1); +} #define PRINTF(format, ...) do { \ if (VA_ARG_COUNT(__VA_ARGS__) == 0) { \ diff --git a/framework/unit_registry.c b/framework/unit_registry.c index 27ef88e..3773250 100644 --- a/framework/unit_registry.c +++ b/framework/unit_registry.c @@ -364,12 +364,12 @@ static void export_unit_do(UlistEntry *li, IniWriter *iw) { // special message for failed unit die to resource if (pUnit->status == E_RESOURCE_NOT_AVAILABLE) { - iw_comment(iw, "!!! %s not available, already held by %s", + iw_commentf(iw, "!!! %s not available, already held by %s", rsc_get_name(pUnit->failed_rsc), rsc_get_owner_name(pUnit->failed_rsc)); } else { - iw_comment(iw, "!!! %s", error_get_message(pUnit->status)); + iw_commentf(iw, "!!! %s", error_get_message(pUnit->status)); } iw_cmt_newline(iw); } diff --git a/gex.mk b/gex.mk index d7d1b1e..e3bf971 100644 --- a/gex.mk +++ b/gex.mk @@ -57,7 +57,7 @@ GEX_CFLAGS = \ -MD -Wno-redundant-decls -Wno-unused-parameter \ -Wno-unused-variable -Wno-inline \ -fmerge-constants -fmerge-all-constants -Wno-implicit-fallthrough \ - -fno-exceptions -finline-small-functions -findirect-inlining -Wno-strict-aliasing -Wno-float-equal -Wno-discarded-qualifiers + -fno-exceptions -finline-small-functions -findirect-inlining -Wno-strict-aliasing -Wno-float-equal -Wno-discarded-qualifiers -fstack-usage GEX_CDEFS_BASE = \ -D__weak="__attribute__((weak))" \ diff --git a/platform/debug_uart.c b/platform/debug_uart.c index 6759286..6f266cd 100644 --- a/platform/debug_uart.c +++ b/platform/debug_uart.c @@ -106,18 +106,19 @@ void DebugUart_PreInit(void) LL_USART_Enable(DEBUG_USART); } -/** Debug print, used by debug / newlib */ -ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len) +void debug_write(const char *buf, uint16_t len) { - (void)rptr; - - uint8_t *buff = buf; - - for (uint32_t i = 0; i < len; i++) { + for (uint16_t i = 0; i < len; i++) { while (!LL_USART_IsActiveFlag_TC(DEBUG_USART)); - LL_USART_TransmitData8(DEBUG_USART, *buff++); + LL_USART_TransmitData8(DEBUG_USART, (uint8_t) *buf++); } +} +/** Debug print, used by debug / newlib */ +ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len) +{ + (void)rptr; + debug_write(buf, len); return len; } diff --git a/platform/debug_uart.h b/platform/debug_uart.h index bd171f9..7971d82 100644 --- a/platform/debug_uart.h +++ b/platform/debug_uart.h @@ -8,4 +8,6 @@ void DebugUart_PreInit(void); void DebugUart_Init(void); +void debug_write(const char *buf, uint16_t len); + #endif //GEX_DEBUG_UART_H diff --git a/utils/ini_writer.c b/utils/ini_writer.c index 7c35064..299d564 100644 --- a/utils/ini_writer.c +++ b/utils/ini_writer.c @@ -82,7 +82,7 @@ void iw_section(IniWriter *iw, const char *format, ...) iw_string(iw, "]\r\n"); } -void iw_comment(IniWriter *iw, const char *format, ...) +void iw_commentf(IniWriter *iw, const char *format, ...) { if (iw->count == 0) return; if (!SystemSettings.ini_comments) return; @@ -92,6 +92,16 @@ void iw_comment(IniWriter *iw, const char *format, ...) iw_newline(iw); } +void iw_comment(IniWriter *iw, const char *text) +{ + if (iw->count == 0) return; + if (!SystemSettings.ini_comments) return; + + iw_string(iw, "# "); + iw_string(iw, text); + iw_newline(iw); +} + void iw_hdr_comment(IniWriter *iw, const char *format, ...) { if (iw->count == 0) return; diff --git a/utils/ini_writer.h b/utils/ini_writer.h index 7790dab..0ae4ca3 100644 --- a/utils/ini_writer.h +++ b/utils/ini_writer.h @@ -85,13 +85,20 @@ void iw_sprintf(IniWriter *iw, const char *format, ...) void iw_section(IniWriter *iw, const char *format, ...) __attribute__((format(printf,2,3))); +/** + * Try to write a INI comment # blah\r\n + * @param iw - iniwriter handle + * @param text - format, like printf + */ +void iw_comment(IniWriter *iw, const char *text); + /** * Try to write a INI comment # blah\r\n * @param iw - iniwriter handle * @param format - format, like printf * @param ... - replacements */ -void iw_comment(IniWriter *iw, const char *format, ...) +void iw_commentf(IniWriter *iw, const char *format, ...) __attribute__((format(printf,2,3))); /**