Basic debug library

Dependencies:   CBuffer

Dependents:   modem_ref_helper

Committer:
Jeej
Date:
Wed Apr 26 12:31:00 2017 +0000
Revision:
0:1d3ed1850649
Child:
1:ad0727902991
Initial version

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