debug_frmwrk.c (10321B)
1 /********************************************************************** 2 * $Id$ debug_frmwrk.c 2010-05-21 3 *//** 4 * @file debug_frmwrk.c 5 * @brief Contains some utilities that used for debugging through UART 6 * @version 2.0 7 * @date 21. May. 2010 8 * @author NXP MCU SW Application Team 9 * 10 * Copyright(C) 2010, NXP Semiconductor 11 * All rights reserved. 12 * 13 *********************************************************************** 14 * Software that is described herein is for illustrative purposes only 15 * which provides customers with programming information regarding the 16 * products. This software is supplied "AS IS" without any warranties. 17 * NXP Semiconductors assumes no responsibility or liability for the 18 * use of the software, conveys no license or title under any patent, 19 * copyright, or mask work right to the product. NXP Semiconductors 20 * reserves the right to make changes in the software without 21 * notification. NXP Semiconductors also make no representation or 22 * warranty that such application will be suitable for the specified 23 * use without further testing or modification. 24 * Permission to use, copy, modify, and distribute this software and its 25 * documentation is hereby granted, under NXP Semiconductors' 26 * relevant copyright in the software, without fee, provided that it 27 * is used in conjunction with NXP Semiconductors microcontrollers. This 28 * copyright, permission, and disclaimer notice must appear in all copies of 29 * this code. 30 **********************************************************************/ 31 32 #include "debug_frmwrk.h" 33 #include "lpc17xx_pinsel.h" 34 35 /* If this source file built with example, the LPC17xx FW library configuration 36 * file in each example directory ("lpc17xx_libcfg.h") must be included, 37 * otherwise the default FW library configuration file must be included instead 38 */ 39 #ifdef __BUILD_WITH_EXAMPLE__ 40 #include "lpc17xx_libcfg.h" 41 #else 42 #include "lpc17xx_libcfg_default.h" 43 #endif /* __BUILD_WITH_EXAMPLE__ */ 44 45 #ifdef _DBGFWK 46 /* Debug framework */ 47 48 void (*_db_msg)(LPC_UART_TypeDef *UARTx, const void *s); 49 void (*_db_msg_)(LPC_UART_TypeDef *UARTx, const void *s); 50 void (*_db_char)(LPC_UART_TypeDef *UARTx, uint8_t ch); 51 void (*_db_dec)(LPC_UART_TypeDef *UARTx, uint8_t decn); 52 void (*_db_dec_16)(LPC_UART_TypeDef *UARTx, uint16_t decn); 53 void (*_db_dec_32)(LPC_UART_TypeDef *UARTx, uint32_t decn); 54 void (*_db_hex)(LPC_UART_TypeDef *UARTx, uint8_t hexn); 55 void (*_db_hex_16)(LPC_UART_TypeDef *UARTx, uint16_t hexn); 56 void (*_db_hex_32)(LPC_UART_TypeDef *UARTx, uint32_t hexn); 57 uint8_t (*_db_get_char)(LPC_UART_TypeDef *UARTx); 58 59 60 /*********************************************************************//** 61 * @brief Puts a character to UART port 62 * @param[in] UARTx Pointer to UART peripheral 63 * @param[in] ch Character to put 64 * @return None 65 **********************************************************************/ 66 void UARTPutChar (LPC_UART_TypeDef *UARTx, uint8_t ch) 67 { 68 UART_Send(UARTx, &ch, 1, BLOCKING); 69 } 70 71 72 /*********************************************************************//** 73 * @brief Get a character to UART port 74 * @param[in] UARTx Pointer to UART peripheral 75 * @return character value that returned 76 **********************************************************************/ 77 uint8_t UARTGetChar (LPC_UART_TypeDef *UARTx) 78 { 79 uint8_t tmp = 0; 80 UART_Receive(UARTx, &tmp, 1, BLOCKING); 81 return(tmp); 82 } 83 84 85 /*********************************************************************//** 86 * @brief Puts a string to UART port 87 * @param[in] UARTx Pointer to UART peripheral 88 * @param[in] str string to put 89 * @return None 90 **********************************************************************/ 91 void UARTPuts(LPC_UART_TypeDef *UARTx, const void *str) 92 { 93 uint8_t *s = (uint8_t *) str; 94 95 while (*s) 96 { 97 UARTPutChar(UARTx, *s++); 98 } 99 } 100 101 102 /*********************************************************************//** 103 * @brief Puts a string to UART port and print new line 104 * @param[in] UARTx Pointer to UART peripheral 105 * @param[in] str String to put 106 * @return None 107 **********************************************************************/ 108 void UARTPuts_(LPC_UART_TypeDef *UARTx, const void *str) 109 { 110 UARTPuts (UARTx, str); 111 UARTPuts (UARTx, "\n\r"); 112 } 113 114 115 /*********************************************************************//** 116 * @brief Puts a decimal number to UART port 117 * @param[in] UARTx Pointer to UART peripheral 118 * @param[in] decnum Decimal number (8-bit long) 119 * @return None 120 **********************************************************************/ 121 void UARTPutDec(LPC_UART_TypeDef *UARTx, uint8_t decnum) 122 { 123 uint8_t c1=decnum%10; 124 uint8_t c2=(decnum/10)%10; 125 uint8_t c3=(decnum/100)%10; 126 UARTPutChar(UARTx, '0'+c3); 127 UARTPutChar(UARTx, '0'+c2); 128 UARTPutChar(UARTx, '0'+c1); 129 } 130 131 /*********************************************************************//** 132 * @brief Puts a decimal number to UART port 133 * @param[in] UARTx Pointer to UART peripheral 134 * @param[in] decnum Decimal number (8-bit long) 135 * @return None 136 **********************************************************************/ 137 void UARTPutDec16(LPC_UART_TypeDef *UARTx, uint16_t decnum) 138 { 139 uint8_t c1=decnum%10; 140 uint8_t c2=(decnum/10)%10; 141 uint8_t c3=(decnum/100)%10; 142 uint8_t c4=(decnum/1000)%10; 143 uint8_t c5=(decnum/10000)%10; 144 UARTPutChar(UARTx, '0'+c5); 145 UARTPutChar(UARTx, '0'+c4); 146 UARTPutChar(UARTx, '0'+c3); 147 UARTPutChar(UARTx, '0'+c2); 148 UARTPutChar(UARTx, '0'+c1); 149 } 150 151 /*********************************************************************//** 152 * @brief Puts a decimal number to UART port 153 * @param[in] UARTx Pointer to UART peripheral 154 * @param[in] decnum Decimal number (8-bit long) 155 * @return None 156 **********************************************************************/ 157 void UARTPutDec32(LPC_UART_TypeDef *UARTx, uint32_t decnum) 158 { 159 uint8_t c1=decnum%10; 160 uint8_t c2=(decnum/10)%10; 161 uint8_t c3=(decnum/100)%10; 162 uint8_t c4=(decnum/1000)%10; 163 uint8_t c5=(decnum/10000)%10; 164 uint8_t c6=(decnum/100000)%10; 165 uint8_t c7=(decnum/1000000)%10; 166 uint8_t c8=(decnum/10000000)%10; 167 uint8_t c9=(decnum/100000000)%10; 168 uint8_t c10=(decnum/1000000000)%10; 169 UARTPutChar(UARTx, '0'+c10); 170 UARTPutChar(UARTx, '0'+c9); 171 UARTPutChar(UARTx, '0'+c8); 172 UARTPutChar(UARTx, '0'+c7); 173 UARTPutChar(UARTx, '0'+c6); 174 UARTPutChar(UARTx, '0'+c5); 175 UARTPutChar(UARTx, '0'+c4); 176 UARTPutChar(UARTx, '0'+c3); 177 UARTPutChar(UARTx, '0'+c2); 178 UARTPutChar(UARTx, '0'+c1); 179 } 180 181 /*********************************************************************//** 182 * @brief Puts a hex number to UART port 183 * @param[in] UARTx Pointer to UART peripheral 184 * @param[in] hexnum Hex number (8-bit long) 185 * @return None 186 **********************************************************************/ 187 void UARTPutHex (LPC_UART_TypeDef *UARTx, uint8_t hexnum) 188 { 189 uint8_t nibble, i; 190 191 UARTPuts(UARTx, "0x"); 192 i = 1; 193 do { 194 nibble = (hexnum >> (4*i)) & 0x0F; 195 UARTPutChar(UARTx, (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble)); 196 } while (i--); 197 } 198 199 200 /*********************************************************************//** 201 * @brief Puts a hex number to UART port 202 * @param[in] UARTx Pointer to UART peripheral 203 * @param[in] hexnum Hex number (16-bit long) 204 * @return None 205 **********************************************************************/ 206 void UARTPutHex16 (LPC_UART_TypeDef *UARTx, uint16_t hexnum) 207 { 208 uint8_t nibble, i; 209 210 UARTPuts(UARTx, "0x"); 211 i = 3; 212 do { 213 nibble = (hexnum >> (4*i)) & 0x0F; 214 UARTPutChar(UARTx, (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble)); 215 } while (i--); 216 } 217 218 /*********************************************************************//** 219 * @brief Puts a hex number to UART port 220 * @param[in] UARTx Pointer to UART peripheral 221 * @param[in] hexnum Hex number (32-bit long) 222 * @return None 223 **********************************************************************/ 224 void UARTPutHex32 (LPC_UART_TypeDef *UARTx, uint32_t hexnum) 225 { 226 uint8_t nibble, i; 227 228 UARTPuts(UARTx, "0x"); 229 i = 7; 230 do { 231 nibble = (hexnum >> (4*i)) & 0x0F; 232 UARTPutChar(UARTx, (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble)); 233 } while (i--); 234 } 235 236 ///*********************************************************************//** 237 // * @brief print function that supports format as same as printf() 238 // * function of <stdio.h> library 239 // * @param[in] None 240 // * @return None 241 // **********************************************************************/ 242 //void _printf (const char *format, ...) 243 //{ 244 // static char buffer[512 + 1]; 245 // va_list vArgs; 246 // char *tmp; 247 // va_start(vArgs, format); 248 // vsprintf((char *)buffer, (char const *)format, vArgs); 249 // va_end(vArgs); 250 // 251 // _DBG(buffer); 252 //} 253 254 /*********************************************************************//** 255 * @brief Initialize Debug frame work through initializing UART port 256 * @param[in] None 257 * @return None 258 **********************************************************************/ 259 void debug_frmwrk_init(void) 260 { 261 UART_CFG_Type UARTConfigStruct; 262 PINSEL_CFG_Type PinCfg; 263 264 #if (USED_UART_DEBUG_PORT==0) 265 /* 266 * Initialize UART0 pin connect 267 */ 268 PinCfg.Funcnum = 1; 269 PinCfg.OpenDrain = 0; 270 PinCfg.Pinmode = 0; 271 PinCfg.Pinnum = 2; 272 PinCfg.Portnum = 0; 273 PINSEL_ConfigPin(&PinCfg); 274 PinCfg.Pinnum = 3; 275 PINSEL_ConfigPin(&PinCfg); 276 277 #elif (USED_UART_DEBUG_PORT==1) 278 /* 279 * Initialize UART1 pin connect 280 */ 281 PinCfg.Funcnum = 1; 282 PinCfg.OpenDrain = 0; 283 PinCfg.Pinmode = 0; 284 PinCfg.Pinnum = 15; 285 PinCfg.Portnum = 0; 286 PINSEL_ConfigPin(&PinCfg); 287 PinCfg.Pinnum = 16; 288 PINSEL_ConfigPin(&PinCfg); 289 #endif 290 291 /* Initialize UART Configuration parameter structure to default state: 292 * Baudrate = 9600bps 293 * 8 data bit 294 * 1 Stop bit 295 * None parity 296 */ 297 UART_ConfigStructInit(&UARTConfigStruct); 298 299 // Re-configure baudrate to 115200bps 300 UARTConfigStruct.Baud_rate = 115200; 301 302 // Initialize DEBUG_UART_PORT peripheral with given to corresponding parameter 303 UART_Init((LPC_UART_TypeDef *)DEBUG_UART_PORT, &UARTConfigStruct); 304 305 // Enable UART Transmit 306 UART_TxCmd((LPC_UART_TypeDef *)DEBUG_UART_PORT, ENABLE); 307 308 _db_msg = UARTPuts; 309 _db_msg_ = UARTPuts_; 310 _db_char = UARTPutChar; 311 _db_hex = UARTPutHex; 312 _db_hex_16 = UARTPutHex16; 313 _db_hex_32 = UARTPutHex32; 314 _db_dec = UARTPutDec; 315 _db_dec_16 = UARTPutDec16; 316 _db_dec_32 = UARTPutDec32; 317 _db_get_char = UARTGetChar; 318 } 319 #endif /*_DBGFWK */ 320 321 322 /* --------------------------------- End Of File ------------------------------ */