this is using the mbed os version 5-13-1
Embed:
(wiki syntax)
Show/hide line numbers
debug2.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file debug.c 00004 * @brief Source file containing logic responsible for: 00005 * @details 00006 * 00007 ****************************************************************************** 00008 * @author TTP 00009 * @copyright (c) 2017 TTP (The Technology Partnership) plc 00010 * 00011 ****************************************************************************** 00012 */ 00013 00014 //-------- Includes ----------------------------------------------------------// 00015 00016 #include "debug.h" 00017 #include <mbed.h> 00018 //#include "clocks.h" 00019 00020 //-------- Defines -----------------------------------------------------------// 00021 00022 #define FILE_CODE "dbgm" 00023 #define ERROR "err" 00024 00025 #define DBG_RX_BUFFER_SIZE_BYTES (1024) 00026 #define DBG_RX_BUFFER_MASK (DBG_RX_BUFFER_SIZE_BYTES - 1) 00027 00028 #define DBG_TX_BUFFER_SIZE_BYTES (1024) 00029 00030 #if (DBG_RX_BUFFER_SIZE_BYTES & DBG_RX_BUFFER_MASK) 00031 #error Rx buffer size is not power of 2 00032 #endif 00033 00034 //-------- Constants & enums -------------------------------------------------// 00035 00036 00037 //-------- Structs & typedefs ------------------------------------------------// 00038 00039 //-------- Global Variables --------------------------------------------------// 00040 00041 /** 00042 *@brief Global array of length ::DBG_TX_BUFFER_SIZE_BYTES to store 00043 * the current debug print statement of the firmware. 00044 */ 00045 static char g_dbg_buffer[DBG_TX_BUFFER_SIZE_BYTES]; 00046 00047 //-------- Static Variables --------------------------------------------------// 00048 00049 static const char dbg_tx_terminate_string[] = {'\n', '\r'}; ///<Array of characters representing the terminate sequence of a debug transmit string 00050 00051 static uint8_t current_debug_level; ///<Variable keeping track of the current debug level 00052 00053 static char fw_ver_string[REL_FW_VER_STRING_LEN_BYTES + SVN_FW_VER_STRING_LEN_BYTES]; 00054 static uint8_t fw_ver_bytes[5]; 00055 00056 static data_mode_e dbg_data_mode; 00057 00058 //Rx buffer to be used for both ASCII and binary mode 00059 static uint8_t dbg_rx_buffer[DBG_RX_BUFFER_SIZE_BYTES]; 00060 static volatile uint16_t dbg_rx_pos; 00061 static volatile uint32_t dbg_rx_count; 00062 static volatile uint16_t dbg_rx_cmd_count; 00063 00064 //static char g_dbg_buffer[DBG_TX_BUFFER_SIZE_BYTES]; 00065 00066 //-------- Static function prototypes ----------------------------------------// 00067 00068 static void set_fw_ver_string(void); 00069 static void set_fw_ver_bytes(void); 00070 static char get_rx_byte_from_buffer(void); 00071 static const char* debug_level_to_string(uint8_t debug_level); 00072 00073 //-------- Function implementations ------------------------------------------// 00074 #ifdef BOX_MICRO_CODE 00075 static inline void disable_dbg_rx_intrup(void) 00076 { 00077 __HAL_PCD_DISABLE(&hpcd_USB_OTG_FS); 00078 } 00079 00080 static inline void enable_dbg_rx_intrup(void) 00081 { 00082 __HAL_PCD_ENABLE(&hpcd_USB_OTG_FS); 00083 } 00084 #endif 00085 /** 00086 * @brief Starting function to supervise the 00087 * - Initialisation of UART2 responsible for acting as the debug port for the firmware 00088 * - Initialisation of the UART2 transmit and receive buffers 00089 * - Initial read from UART2 00090 * 00091 * @param None 00092 * @retval None 00093 */ 00094 //#define DEBUG_ENABLED 00095 void initialise_debug(uint8_t debug_level=NONE) 00096 { 00097 memset(g_dbg_buffer, 0, sizeof(g_dbg_buffer)); 00098 00099 //. Set the version strings 00100 set_fw_ver_string(); 00101 set_fw_ver_bytes(); 00102 #ifdef DEBUG_ENABLED 00103 current_debug_level = (LOG | ERR | TXT | DBG); //NONE; // 00104 #else 00105 current_debug_level = debug_level; // 00106 #endif 00107 dbg_data_mode = DATA_ASCII; 00108 dbg_rx_pos = 0; 00109 dbg_rx_count = 0; 00110 dbg_rx_cmd_count = 0; 00111 } 00112 00113 /** 00114 * @brief 00115 * @param 00116 * @param 00117 * @retval 00118 */ 00119 const char* get_dbg_tx_terminate_string (void) 00120 { 00121 if (dbg_data_mode == DATA_ASCII) 00122 { 00123 return dbg_tx_terminate_string; 00124 } 00125 else 00126 { 00127 return NULL; 00128 } 00129 } 00130 00131 /** 00132 * @brief 00133 * @param 00134 * @param 00135 * @retval 00136 */ 00137 uint8_t get_dbg_tx_terminate_string_len (void) 00138 { 00139 if (dbg_data_mode == DATA_ASCII) 00140 { 00141 return sizeof(dbg_tx_terminate_string); 00142 } 00143 else 00144 { 00145 return 0; 00146 } 00147 } 00148 00149 static const char* debug_level_to_string(uint8_t debug_level) 00150 { 00151 switch(debug_level) 00152 { 00153 case LOG: 00154 return "LOG"; 00155 00156 case ERR: 00157 return "ERR"; 00158 00159 case DBG: 00160 return "DBG"; 00161 00162 case TXT: 00163 return "TXT"; 00164 00165 default: 00166 return "INV"; //Invalid 00167 } 00168 } 00169 00170 00171 //void dbg_printf(uint8_t debug_level, ...) 00172 //{ 00173 // dbg_print(FILE_CODE, __LINE__, debug_level, ...); 00174 //} 00175 void dbg_print(const char *file_code, uint16_t line_number, 00176 uint8_t debug_level, const char *text, ...) 00177 { 00178 //const char *file_code, uint16_t line_number, 00179 00180 if (current_debug_level & debug_level) 00181 { 00182 //Re-init the gloabl debug buffer 00183 memset(g_dbg_buffer, 0, sizeof(g_dbg_buffer)); 00184 uint16_t len_bytes = 0; 00185 00186 if (debug_level != TXT) 00187 { 00188 //sprintf(g_dbg_buffer, "|%s|%10u|", debug_level_to_string(debug_level), Kernel::get_ms_count()); 00189 sprintf(g_dbg_buffer, "|%s|%s|%4d|%10u|", debug_level_to_string(debug_level), file_code, line_number, Kernel::get_ms_count()); 00190 len_bytes = strlen(g_dbg_buffer); 00191 } 00192 00193 va_list args; 00194 va_start(args, text); 00195 vsnprintf(&g_dbg_buffer[len_bytes], (sizeof(g_dbg_buffer) - len_bytes - 2), text, args); 00196 printf("%s", g_dbg_buffer); 00197 va_end(args); 00198 00199 len_bytes = strlen(g_dbg_buffer); 00200 g_dbg_buffer[len_bytes++] = '\r'; 00201 g_dbg_buffer[len_bytes++] = '\n'; 00202 #ifdef WRITE_TO_FILE_ENABLED 00203 if (debug_level == LOG || debug_level == ERROR) 00204 { 00205 write_to_file(SYS_LOG, (const uint8_t *)g_dbg_buffer, len_bytes); 00206 } 00207 00208 if (dbg_data_mode == DATA_ASCII) 00209 { 00210 transmit_bytes_over_usb((uint8_t *)g_dbg_buffer, len_bytes); 00211 } 00212 #endif 00213 } 00214 } 00215 00216 /** 00217 Function used to send data during firmware update (in binary mode) 00218 */ 00219 #ifdef WRITE_TO_FILE_ENABLED 00220 void debug_send_data(uint8_t *pData, uint16_t length) 00221 { 00222 transmit_bytes_over_usb(pData, length); 00223 } 00224 #endif 00225 00226 /** 00227 * @brief Function to set the debug level. Currently only 2 debug levels 00228 * are provided as mentioned in ::debug_log_level_e. 00229 * LOG is constantly enabled 00230 * 00231 * @param (uint8_t) debug_level 00232 * @retval None 00233 */ 00234 void set_debug_level(uint8_t debug_level) 00235 { 00236 current_debug_level = (LOG | ERR | TXT | debug_level); 00237 dbg_printf(LOG, "Current debug level set to %02x", current_debug_level); 00238 } 00239 00240 /** 00241 * @brief Function to get the current debug level set. 00242 * 00243 * @param None 00244 * @retval (uint8_t) current_debug_level 00245 */ 00246 uint8_t get_debug_level(void) 00247 { 00248 return current_debug_level; 00249 } 00250 00251 /** 00252 * @brief Function to set the micro firmware version 00253 * Firmware version follows the following format: 00254 * major.minor.patch.svn_revision 00255 * 00256 * If 'svnversion' is not installed on the machine, the svn 00257 * revision is returned null. In that instance, the svn revision 00258 * is set to n.a 00259 * 00260 * @param None 00261 * @retval None 00262 */ 00263 static void set_fw_ver_string(void) 00264 { 00265 memset(fw_ver_string, 0, sizeof(fw_ver_string)); 00266 00267 memcpy(fw_ver_string, REL_FW_VER_STRING, REL_FW_VER_STRING_LEN_BYTES); 00268 00269 uint8_t rel_fw_string_len_bytes = strlen(REL_FW_VER_STRING); 00270 00271 fw_ver_string[rel_fw_string_len_bytes] = '.'; 00272 #ifdef BOX_MICRO_CODE 00273 if (strlen(SVN_FW_VERSION_STRING) == 0) 00274 { 00275 //svnversion is not installed on the machine 00276 memcpy(&fw_ver_string[rel_fw_string_len_bytes + 1], "n.a", 4); 00277 } 00278 else 00279 { 00280 memcpy(&fw_ver_string[rel_fw_string_len_bytes + 1], SVN_FW_VERSION_STRING, SVN_FW_VER_STRING_LEN_BYTES); 00281 } 00282 #endif 00283 } 00284 00285 /** 00286 * @brief Function to get the firmware version 00287 * 00288 * @param None 00289 * @retval (const char *) fw_ver_string 00290 */ 00291 const char* get_fw_ver_string(void) 00292 { 00293 return fw_ver_string; 00294 } 00295 00296 static void set_fw_ver_bytes(void) 00297 { 00298 memset(fw_ver_bytes, 0, sizeof(fw_ver_bytes)); 00299 00300 uint16_t val = 0; 00301 sscanf(fw_ver_string, "%hhx.%hhx.%hhx.%hx", &fw_ver_bytes[0], &fw_ver_bytes[1], &fw_ver_bytes[2], &val); 00302 00303 fw_ver_bytes[3] = (val >> 8) & 0xff; 00304 fw_ver_bytes[4] = val & 0xff; 00305 } 00306 00307 const uint8_t* get_fw_ver_bytes(void) 00308 { 00309 return fw_ver_bytes; 00310 } 00311 00312 void debug_setDataMode(data_mode_e mode) 00313 { 00314 dbg_data_mode = mode; 00315 } 00316 00317 void print_debug_hex(const uint8_t* p_buffer, uint16_t len) 00318 { 00319 //print out the rxvd EDM bytes 00320 00321 uint16_t quo = len / 8; 00322 uint16_t rem = len % 8; 00323 00324 uint16_t i = 0; 00325 uint16_t j = 0; 00326 for (i = 0; i < quo; i++) 00327 { 00328 dbg_printf(LOG, "[%3d]: %02x %02x %02x %02x %02x %02x %02x %02x", j, 00329 p_buffer[j + 0], p_buffer[j + 1], p_buffer[j + 2], p_buffer[j + 3], 00330 p_buffer[j + 4], p_buffer[j + 5], p_buffer[j + 6], p_buffer[j + 7]); 00331 j += 8; 00332 } 00333 00334 if (rem == 7) 00335 { 00336 dbg_printf(LOG, "[%3d]: %02x %02x %02x %02x %02x %02x %02x", j, 00337 p_buffer[j + 0], p_buffer[j + 1], p_buffer[j + 2], p_buffer[j + 3], 00338 p_buffer[j + 4], p_buffer[j + 5], p_buffer[j + 6]); 00339 } 00340 else if (rem == 6) 00341 { 00342 dbg_printf(LOG, "[%3d]: %02x %02x %02x %02x %02x %02x", j, 00343 p_buffer[j + 0], p_buffer[j + 1], p_buffer[j + 2], p_buffer[j + 3], 00344 p_buffer[j + 4], p_buffer[j + 5]); 00345 } 00346 else if (rem == 5) 00347 { 00348 dbg_printf(LOG, "[%3d]: %02x %02x %02x %02x %02x", j, 00349 p_buffer[j + 0], p_buffer[j + 1], p_buffer[j + 2], p_buffer[j + 3], 00350 p_buffer[j + 4]); 00351 } 00352 else if (rem == 4) 00353 { 00354 dbg_printf(LOG, "[%3d]: %02x %02x %02x %02x", j, 00355 p_buffer[j + 0], p_buffer[j + 1], p_buffer[j + 2], p_buffer[j + 3]); 00356 } 00357 else if (rem == 3) 00358 { 00359 dbg_printf(LOG, "[%3d]: %02x %02x %02x", j, 00360 p_buffer[j + 0], p_buffer[j + 1], p_buffer[j + 2]); 00361 } 00362 else if (rem == 2) 00363 { 00364 dbg_printf(LOG, "[%3d]: %02x %02x", j, p_buffer[j + 0], p_buffer[j + 1]); 00365 } 00366 else if (rem == 1) 00367 { 00368 dbg_printf(LOG, "[%3d]: %02x", j, p_buffer[j]); 00369 } 00370 00371 dbg_printf(LOG, ""); 00372 }
Generated on Wed Jul 13 2022 05:40:26 by
1.7.2