From a20acf4031c9207f895c7ae6e04c3f75f90a2774 Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sat, 20 Apr 2024 15:20:18 +0200 Subject: [PATCH 1/2] Implement error header --- srv/error.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 srv/error.h diff --git a/srv/error.h b/srv/error.h new file mode 100644 index 0000000..2616e6b --- /dev/null +++ b/srv/error.h @@ -0,0 +1,50 @@ +/** @file error.h + * Module providing error handling macros + * + * The module implements the assert macro intended to allow catching critical + * errors during debug while having no influence in the release code as well as + * multiple tool macros to handles other kinds of errors + */ + +#ifndef _ERROR_H_ +#define _ERROR_H_ + +//--includes-------------------------------------------------------------------- + +#include "debug.h" + +#define ERROR_ASSERT 1 + + +//--type definitions------------------------------------------------------------ + +#define error_to_string(arg) #arg + +#if ERROR_ASSERT +#define error_assert(cond) \ + do { \ + if (!(cond)) { \ + debug_error("%s:l%u:%s: %s", __FILE__, __LINE__, __func__, \ + error_to_string(cond)); \ + while (true) {} \ + } \ + } while (0) +#else +#define error_assert(cond) do {} while (0) +#endif + +#if ERROR_ASSERT +#define error_trigger(msg, ...) \ + do { \ + debug_error(msg __VA_OPT__(,) __VA_ARGS__); \ + while (true) {} \ + } while (0) +#else +#define error_trigger(msg) do {} while (0) +#endif + + +//--functions------------------------------------------------------------------- + +#endif //_ERROR_H_ + -- 2.45.2 From 47a06a2a398965fcc97b8a70b05df5a732865f45 Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sat, 20 Apr 2024 15:43:17 +0200 Subject: [PATCH 2/2] Add asserts in existing code --- srv/dma_cbuf.c | 10 +++++++++- srv/dma_mbuf.c | 11 ++++++++++- srv/format.c | 7 ++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/srv/dma_cbuf.c b/srv/dma_cbuf.c index 22f1fe4..718c4b6 100644 --- a/srv/dma_cbuf.c +++ b/srv/dma_cbuf.c @@ -8,6 +8,7 @@ //--includes-------------------------------------------------------------------- #include "dma_cbuf.h" +#include "error.h" //--local definitions----------------------------------------------------------- @@ -31,7 +32,9 @@ void dma_cbuf_configure(volatile struct DmaCircBuffer* buffer, const struct DmaParam* param, enum DmaConfig priority, volatile void* raw_buffer, uint16_t buffer_size) { - #warning "check for null ptr" + error_assert(buffer != nullptr); + error_assert(param != nullptr); + error_assert(raw_buffer != nullptr); buffer->buffer = raw_buffer; @@ -52,6 +55,9 @@ void dma_cbuf_configure(volatile struct DmaCircBuffer* buffer, uint32_t dma_cbuf_read_byte(volatile struct DmaCircBuffer* buffer, uint8_t* byte) { + error_assert(buffer != nullptr); + error_assert(byte != nullptr); + //retreive the current end of the buffer based on the DMA's progress uint16_t end = buffer->size - dma_get_remaining(buffer->dma, buffer->channel); @@ -86,6 +92,8 @@ uint32_t dma_cbuf_read_byte(volatile struct DmaCircBuffer* buffer, static void cbuf_callback(enum DmaIRQSource src, volatile void* param) { (void)src; //only transfer complete expected + error_assert(param != nullptr); + volatile struct DmaCircBuffer* buffer = param; buffer->dma_looped = true; } diff --git a/srv/dma_mbuf.c b/srv/dma_mbuf.c index 4f2255c..a5896b6 100644 --- a/srv/dma_mbuf.c +++ b/srv/dma_mbuf.c @@ -8,6 +8,7 @@ //--includes-------------------------------------------------------------------- #include "dma_mbuf.h" +#include "error.h" //--local definitions----------------------------------------------------------- @@ -30,7 +31,9 @@ void dma_mbuf_configure(volatile struct DmaMultiBuffer* buffer, const struct DmaParam* param, enum DmaConfig priority, void* raw_buffer, uint16_t buffer_size) { - #warning "check for null ptr" + error_assert(buffer != nullptr); + error_assert(param != nullptr); + error_assert(raw_buffer != nullptr); buffer->raw_buffer = raw_buffer; @@ -51,6 +54,8 @@ void dma_mbuf_configure(volatile struct DmaMultiBuffer* buffer, uint32_t dma_mbuf_write_byte(volatile struct DmaMultiBuffer* buffer, uint8_t byte) { + error_assert(buffer != nullptr); + //if the current buffer is full, give up if (buffer->byte_index >= buffer->buffer_size) { return 1; @@ -65,6 +70,8 @@ uint32_t dma_mbuf_write_byte(volatile struct DmaMultiBuffer* buffer, uint32_t dma_mbuf_switch(volatile struct DmaMultiBuffer* buffer) { + error_assert(buffer != nullptr); + //no data to send, stop here if (buffer->byte_index == 0) { return 0; @@ -100,6 +107,8 @@ uint32_t dma_mbuf_switch(volatile struct DmaMultiBuffer* buffer) static void mbuf_callback(enum DmaIRQSource src, volatile void* param) { (void)src; //only transfer complete expected + error_assert(param != nullptr); + volatile struct DmaMultiBuffer* buffer = param; dma_stop(buffer->dma, buffer->channel); buffer->dma_running = false; diff --git a/srv/format.c b/srv/format.c index ccf392e..df78bd3 100644 --- a/srv/format.c +++ b/srv/format.c @@ -7,6 +7,7 @@ //--includes-------------------------------------------------------------------- #include "format.h" +#include "error.h" //--local definitions----------------------------------------------------------- @@ -68,6 +69,8 @@ uint32_t format_vfctprintf(FormatCallback callback, void* arg, */ static uint32_t buffer_write(uint8_t byte, void* arg) { + error_assert(arg != nullptr); + struct BufferArg* buffer_arg = (struct BufferArg*)arg; if (buffer_arg->byte_index >= buffer_arg->buffer_size) { return 1; @@ -87,6 +90,8 @@ static uint32_t buffer_write(uint8_t byte, void* arg) static uint32_t render_format(FormatCallback callback, void* arg, const char* restrict format, va_list va) { + error_assert(format != nullptr); + uint8_t width = 0; bool in_format = false; bool zero_padding = false; @@ -148,7 +153,7 @@ static uint32_t render_format(FormatCallback callback, void* arg, width += (*c - '0'); continue; default: - //TODO manage error + error_trigger("unsupported option: %c in %s", c, format); break; } } else { -- 2.45.2