Basic debug library

Dependencies:   CBuffer

Dependents:   modem_ref_helper

Committer:
Jeej
Date:
Wed May 17 13:56:09 2017 +0000
Revision:
2:9e2ab0547cd0
Parent:
1:ad0727902991
Child:
3:f4a6a6a28dec
Added buffer bypass.

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