Basic debug library

Dependencies:   CBuffer

Dependents:   modem_ref_helper

Committer:
Jeej
Date:
Thu Aug 01 08:32:35 2019 +0000
Revision:
9:87d9cc850af1
Parent:
8:87a867b8a129
Updated for mbed-os 5.13

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeej 0:1d3ed1850649 1 #ifndef DBG_H
Jeej 0:1d3ed1850649 2 #define DBG_H
Jeej 0:1d3ed1850649 3
Jeej 3:f4a6a6a28dec 4 #include "mbed.h"
Jeej 3:f4a6a6a28dec 5
Jeej 0:1d3ed1850649 6 // Enable the debug prints.
Jeej 0:1d3ed1850649 7 #define __PRINT_ENABLED__
Jeej 0:1d3ed1850649 8
Jeej 0:1d3ed1850649 9 // Reboot the device on fatal error, else trap the processor.
Jeej 0:1d3ed1850649 10 #define __REBOOT_ON_ASSERT__
Jeej 0:1d3ed1850649 11
Jeej 0:1d3ed1850649 12 // Flush the debug queue if full to avoid losing traces (can impact performances)
Jeej 0:1d3ed1850649 13 //#define __FLUSH_IF_FULL__
Jeej 0:1d3ed1850649 14
Jeej 0:1d3ed1850649 15 // Force flush until an EOL character ('\n')
Jeej 0:1d3ed1850649 16 //#define __FORCE_LINE_INTEGRITY__
Jeej 0:1d3ed1850649 17
Jeej 2:9e2ab0547cd0 18 // Force flush, do not bufferise trace
Jeej 2:9e2ab0547cd0 19 //#define __FORCE_FLUSH__
Jeej 0:1d3ed1850649 20
Jeej 0:1d3ed1850649 21 #ifdef __PRINT_ENABLED__
Jeej 0:1d3ed1850649 22 #define DBG_OPEN(...) dbg_open(__VA_ARGS__)
Jeej 0:1d3ed1850649 23 #define FLUSH() dbg_flush()
Jeej 0:1d3ed1850649 24
Jeej 0:1d3ed1850649 25 // Enable or disable trace levels
Jeej 0:1d3ed1850649 26 #define _USER_PRINTS_
Jeej 0:1d3ed1850649 27 #define _PRINT_ERRORS_
Jeej 0:1d3ed1850649 28 #define _PRINT_WARNINGS_
Jeej 0:1d3ed1850649 29 #define _PRINT_INFOS_
Jeej 1:ad0727902991 30 //#define _PRINT_DEBUG_
Jeej 0:1d3ed1850649 31 //#define _PRINT_FUNCTIONS_ // Print the function name where FPRINT is called
Jeej 0:1d3ed1850649 32 //#define _TRACE_MALLOC_
Jeej 0:1d3ed1850649 33
Jeej 0:1d3ed1850649 34 #else
Jeej 0:1d3ed1850649 35 #define DBG_OPEN(led) dbg_set_led(led)
Jeej 0:1d3ed1850649 36 #define FLUSH();
Jeej 0:1d3ed1850649 37 #endif
Jeej 0:1d3ed1850649 38
Jeej 0:1d3ed1850649 39
Jeej 0:1d3ed1850649 40 #define RAM_START_ADDRESS (0x20000000)
Jeej 0:1d3ed1850649 41 #define RAM_END_ADDRESS (0x40000000)
Jeej 0:1d3ed1850649 42
Jeej 0:1d3ed1850649 43 #define ASSERT(...) dbg_assert(__VA_ARGS__)
Jeej 0:1d3ed1850649 44
Jeej 0:1d3ed1850649 45
Jeej 0:1d3ed1850649 46 #ifdef _USER_PRINTS_
Jeej 0:1d3ed1850649 47 #define PRINT(...) dbg_print(__VA_ARGS__)
Jeej 0:1d3ed1850649 48 #define PRINT_DATA(...) dbg_print_data(__VA_ARGS__)
Jeej 0:1d3ed1850649 49 #else
Jeej 0:1d3ed1850649 50 #define PRINT(...);
Jeej 0:1d3ed1850649 51 #define PRINT_DATA(...);
Jeej 0:1d3ed1850649 52 #endif
Jeej 0:1d3ed1850649 53
Jeej 0:1d3ed1850649 54
Jeej 0:1d3ed1850649 55 #ifdef _PRINT_ERRORS_
Jeej 8:87a867b8a129 56 #define EPRINT(format,...) dbg_print("ERROR: "format, __VA_ARGS__)
Jeej 0:1d3ed1850649 57 #else
Jeej 0:1d3ed1850649 58 #define EPRINT(...);
Jeej 0:1d3ed1850649 59 #endif
Jeej 0:1d3ed1850649 60
Jeej 0:1d3ed1850649 61
Jeej 0:1d3ed1850649 62 #ifdef _PRINT_WARNINGS_
Jeej 8:87a867b8a129 63 #define WARNING(w,format,...) do { if(!(w)) { dbg_print("WARNING: "format, __VA_ARGS__); } } while(0)
Jeej 0:1d3ed1850649 64 #else
Jeej 8:87a867b8a129 65 #define WARNING(...);
Jeej 0:1d3ed1850649 66 #endif
Jeej 0:1d3ed1850649 67
Jeej 0:1d3ed1850649 68
Jeej 0:1d3ed1850649 69 #ifdef _PRINT_FUNCTIONS_
Jeej 0:1d3ed1850649 70 #define FPRINT(format,...) dbg_print("%s"format,__FUNCTION__,##__VA_ARGS__)
Jeej 0:1d3ed1850649 71 #else
Jeej 0:1d3ed1850649 72 #define FPRINT(...);
Jeej 0:1d3ed1850649 73 #endif
Jeej 0:1d3ed1850649 74
Jeej 0:1d3ed1850649 75
Jeej 0:1d3ed1850649 76 #ifdef _PRINT_INFOS_
Jeej 0:1d3ed1850649 77 #define IPRINT(...) dbg_print("INFO: "__VA_ARGS__)
Jeej 0:1d3ed1850649 78 #else
Jeej 0:1d3ed1850649 79 #define IPRINT(...);
Jeej 0:1d3ed1850649 80 #endif
Jeej 0:1d3ed1850649 81
Jeej 0:1d3ed1850649 82
Jeej 0:1d3ed1850649 83 #ifdef _PRINT_DEBUG_
Jeej 0:1d3ed1850649 84 #define DPRINT(...) dbg_print(__VA_ARGS__)
Jeej 0:1d3ed1850649 85 #define DPRINT_DATA(...) dbg_print_data(__VA_ARGS__)
Jeej 0:1d3ed1850649 86 #else
Jeej 0:1d3ed1850649 87 #define DPRINT(...);
Jeej 0:1d3ed1850649 88 #define DPRINT_DATA(...);
Jeej 0:1d3ed1850649 89 #endif
Jeej 0:1d3ed1850649 90
Jeej 0:1d3ed1850649 91
Jeej 0:1d3ed1850649 92 #ifdef _TRACE_MALLOC_
Jeej 0:1d3ed1850649 93 #define MALLOC(s) dbg_malloc((s),__FUNCTION__,__LINE__)
Jeej 0:1d3ed1850649 94 #define REALLOC(p,s) dbg_realloc((p),(s),__FUNCTION__,__LINE__)
Jeej 0:1d3ed1850649 95 #define FREE(p) dbg_free((p),__FUNCTION__,__LINE__)
Jeej 0:1d3ed1850649 96 #else
Jeej 0:1d3ed1850649 97 #define MALLOC(s) malloc(s)
Jeej 0:1d3ed1850649 98 #define REALLOC(p,s) realloc(p,s)
Jeej 0:1d3ed1850649 99 #define FREE(p) free(p)
Jeej 0:1d3ed1850649 100 #endif
Jeej 0:1d3ed1850649 101
Jeej 0:1d3ed1850649 102 void dbg_open(PinName led = NC, PinName tx = USBTX, PinName rx = USBRX);
Jeej 0:1d3ed1850649 103 void dbg_close( void );
Jeej 0:1d3ed1850649 104
Jeej 0:1d3ed1850649 105 void dbg_set_led(PinName led = NC, PinName dummy1 = NC, PinName dummy2 = NC);
Jeej 0:1d3ed1850649 106
Jeej 0:1d3ed1850649 107 /**
Jeej 0:1d3ed1850649 108 Asserts and trap processor.
Jeej 0:1d3ed1850649 109
Jeej 0:1d3ed1850649 110 @param bool Assert condition
Jeej 0:1d3ed1850649 111 @param const char* Assert message format
Jeej 0:1d3ed1850649 112 @param ... Assert message parameters
Jeej 0:1d3ed1850649 113 @return void
Jeej 0:1d3ed1850649 114 */
Jeej 0:1d3ed1850649 115 void dbg_assert(bool test, const char* format, ...);
Jeej 0:1d3ed1850649 116
Jeej 0:1d3ed1850649 117 /**
Jeej 0:1d3ed1850649 118 Prints a message to the debug port.
Jeej 0:1d3ed1850649 119
Jeej 0:1d3ed1850649 120 @param const char* Message format
Jeej 0:1d3ed1850649 121 @param ... Message parameters
Jeej 0:1d3ed1850649 122 @return void
Jeej 0:1d3ed1850649 123 */
Jeej 0:1d3ed1850649 124 void dbg_print(const char* format, ...);
Jeej 0:1d3ed1850649 125
Jeej 0:1d3ed1850649 126 /**
Jeej 0:1d3ed1850649 127 Flushes the pending debug messages.
Jeej 0:1d3ed1850649 128
Jeej 0:1d3ed1850649 129 @param void
Jeej 0:1d3ed1850649 130 @return void
Jeej 0:1d3ed1850649 131 */
Jeej 0:1d3ed1850649 132 void dbg_flush( void );
Jeej 0:1d3ed1850649 133
Jeej 0:1d3ed1850649 134 /**
Jeej 0:1d3ed1850649 135 Prints malloc parameters.
Jeej 0:1d3ed1850649 136
Jeej 0:1d3ed1850649 137 @param size_t Size to allocate
Jeej 0:1d3ed1850649 138 @param const char* Current function name
Jeej 0:1d3ed1850649 139 @param uint32_t Current file line
Jeej 0:1d3ed1850649 140 @return void* Pointer to allocated memory
Jeej 0:1d3ed1850649 141 */
Jeej 0:1d3ed1850649 142 void* dbg_malloc(size_t size, const char* f, uint32_t l);
Jeej 0:1d3ed1850649 143
Jeej 0:1d3ed1850649 144 /**
Jeej 0:1d3ed1850649 145 Prints realloc parameters.
Jeej 0:1d3ed1850649 146
Jeej 0:1d3ed1850649 147 @param void* Pointer to reallocate
Jeej 0:1d3ed1850649 148 @param size_t New size to allocate
Jeej 0:1d3ed1850649 149 @param const char* Current function name
Jeej 0:1d3ed1850649 150 @param uint32_t Current file line
Jeej 0:1d3ed1850649 151 @return void* Pointer to allocated memory
Jeej 0:1d3ed1850649 152 */
Jeej 0:1d3ed1850649 153 void* dbg_realloc(void* p, size_t size, const char* f, uint32_t l);
Jeej 0:1d3ed1850649 154
Jeej 0:1d3ed1850649 155 /**
Jeej 0:1d3ed1850649 156 Prints free parameters.
Jeej 0:1d3ed1850649 157
Jeej 0:1d3ed1850649 158 @param void* Pointer to memory to free
Jeej 0:1d3ed1850649 159 @param const char* Current function name
Jeej 0:1d3ed1850649 160 @param uint32_t Current file line
Jeej 0:1d3ed1850649 161 @return void
Jeej 0:1d3ed1850649 162 */
Jeej 0:1d3ed1850649 163 void dbg_free(void* p, const char* f, uint32_t l);
Jeej 0:1d3ed1850649 164
Jeej 0:1d3ed1850649 165 void dbg_print_data(const char* before, const char* format, uint8_t* data, uint8_t length, const char* after);
Jeej 0:1d3ed1850649 166
Jeej 7:8e75991f65e5 167 void dbg_set_rx_callback(void (*rx_isr)(char c));
Jeej 7:8e75991f65e5 168
Jeej 0:1d3ed1850649 169 #endif // DBG_H