1 /**********************************************************************
\r
2 * $Id$ debug_frmwrk.c 2011-06-02
\r
4 * @file debug_frmwrk.c
\r
5 * @brief Contains some utilities that used for debugging through UART
\r
7 * @date 02. June. 2011
\r
8 * @author NXP MCU SW Application Team
\r
10 * Copyright(C) 2011, NXP Semiconductor
\r
11 * All rights reserved.
\r
13 ***********************************************************************
\r
14 * Software that is described herein is for illustrative purposes only
\r
15 * which provides customers with programming information regarding the
\r
16 * products. This software is supplied "AS IS" without any warranties.
\r
17 * NXP Semiconductors assumes no responsibility or liability for the
\r
18 * use of the software, conveys no license or title under any patent,
\r
19 * copyright, or mask work right to the product. NXP Semiconductors
\r
20 * reserves the right to make changes in the software without
\r
21 * notification. NXP Semiconductors also make no representation or
\r
22 * warranty that such application will be suitable for the specified
\r
23 * use without further testing or modification.
\r
24 **********************************************************************/
\r
26 /* Peripheral group ----------------------------------------------------------- */
\r
27 /** @addtogroup DEBUG_FRMWRK
\r
31 #ifndef _DEBUG_FRMWRK_
\r
32 #define _DEBUG_FRMWRK_
\r
34 /* Includes ------------------------------------------------------------------- */
\r
35 #include "debug_frmwrk.h"
\r
36 #include "lpc18xx_scu.h"
\r
40 #ifdef CDC_DEBUG_MESSEGES
\r
42 #include "cdcuser.h"
\r
43 #include "CDCdemo.h"
\r
44 #include "lpc18xx_utils.h"
\r
48 /* Debug framework */
\r
50 void (*_db_msg)(LPC_USARTn_Type *UARTx, const void *s);
\r
51 void (*_db_msg_)(LPC_USARTn_Type *UARTx, const void *s);
\r
52 void (*_db_char)(LPC_USARTn_Type *UARTx, uint8_t ch);
\r
53 void (*_db_dec)(LPC_USARTn_Type *UARTx, uint8_t decn);
\r
54 void (*_db_dec_16)(LPC_USARTn_Type *UARTx, uint16_t decn);
\r
55 void (*_db_dec_32)(LPC_USARTn_Type *UARTx, uint32_t decn);
\r
56 void (*_db_hex)(LPC_USARTn_Type *UARTx, uint8_t hexn);
\r
57 void (*_db_hex_16)(LPC_USARTn_Type *UARTx, uint16_t hexn);
\r
58 void (*_db_hex_32)(LPC_USARTn_Type *UARTx, uint32_t hexn);
\r
59 uint8_t (*_db_get_char)(LPC_USARTn_Type *UARTx);
\r
63 /*********************************************************************//**
\r
64 * @brief Puts a character to UART port
\r
65 * @param[in] UARTx Pointer to UART peripheral
\r
66 * @param[in] ch Character to put
\r
68 **********************************************************************/
\r
69 void UARTPutChar (LPC_USARTn_Type *UARTx, uint8_t ch)
\r
71 UART_Send(UARTx, &ch, 1, BLOCKING);
\r
75 /*********************************************************************//**
\r
76 * @brief Get a character to UART port
\r
77 * @param[in] UARTx Pointer to UART peripheral
\r
78 * @return character value that returned
\r
79 **********************************************************************/
\r
80 uint8_t UARTGetChar (LPC_USARTn_Type *UARTx)
\r
83 UART_Receive(UARTx, &tmp, 1, BLOCKING);
\r
88 /*********************************************************************//**
\r
89 * @brief Puts a string to UART port
\r
90 * @param[in] UARTx Pointer to UART peripheral
\r
91 * @param[in] str string to put
\r
93 **********************************************************************/
\r
94 void UARTPuts(LPC_USARTn_Type *UARTx, const void *str)
\r
96 #ifdef CDC_DEBUG_MESSEGES
\r
98 num_of_bytes = strlen(str);
\r
99 timer_delay_us(num_of_bytes);
\r
101 USB_WriteEP (CDC_DEP_IN, (unsigned char *)str, num_of_bytes);
\r
103 uint8_t *s = (uint8_t *) str;
\r
107 UARTPutChar(UARTx, *s++);
\r
113 /*********************************************************************//**
\r
114 * @brief Puts a string to UART port and print new line
\r
115 * @param[in] UARTx Pointer to UART peripheral
\r
116 * @param[in] str String to put
\r
118 **********************************************************************/
\r
119 void UARTPuts_(LPC_USARTn_Type *UARTx, const void *str)
\r
121 UARTPuts (UARTx, str);
\r
122 UARTPuts (UARTx, "\n\r");
\r
126 /*********************************************************************//**
\r
127 * @brief Puts a decimal number to UART port
\r
128 * @param[in] UARTx Pointer to UART peripheral
\r
129 * @param[in] decnum Decimal number (8-bit long)
\r
131 **********************************************************************/
\r
132 void UARTPutDec(LPC_USARTn_Type *UARTx, uint8_t decnum)
\r
134 uint8_t c1=decnum%10;
\r
135 uint8_t c2=(decnum/10)%10;
\r
136 uint8_t c3=(decnum/100)%10;
\r
137 UARTPutChar(UARTx, '0'+c3);
\r
138 UARTPutChar(UARTx, '0'+c2);
\r
139 UARTPutChar(UARTx, '0'+c1);
\r
142 /*********************************************************************//**
\r
143 * @brief Puts a decimal number to UART port
\r
144 * @param[in] UARTx Pointer to UART peripheral
\r
145 * @param[in] decnum Decimal number (8-bit long)
\r
147 **********************************************************************/
\r
148 void UARTPutDec16(LPC_USARTn_Type *UARTx, uint16_t decnum)
\r
150 uint8_t c1=decnum%10;
\r
151 uint8_t c2=(decnum/10)%10;
\r
152 uint8_t c3=(decnum/100)%10;
\r
153 uint8_t c4=(decnum/1000)%10;
\r
154 uint8_t c5=(decnum/10000)%10;
\r
155 UARTPutChar(UARTx, '0'+c5);
\r
156 UARTPutChar(UARTx, '0'+c4);
\r
157 UARTPutChar(UARTx, '0'+c3);
\r
158 UARTPutChar(UARTx, '0'+c2);
\r
159 UARTPutChar(UARTx, '0'+c1);
\r
162 /*********************************************************************//**
\r
163 * @brief Puts a decimal number to UART port
\r
164 * @param[in] UARTx Pointer to UART peripheral
\r
165 * @param[in] decnum Decimal number (8-bit long)
\r
167 **********************************************************************/
\r
168 void UARTPutDec32(LPC_USARTn_Type *UARTx, uint32_t decnum)
\r
170 uint8_t c1=decnum%10;
\r
171 uint8_t c2=(decnum/10)%10;
\r
172 uint8_t c3=(decnum/100)%10;
\r
173 uint8_t c4=(decnum/1000)%10;
\r
174 uint8_t c5=(decnum/10000)%10;
\r
175 uint8_t c6=(decnum/100000)%10;
\r
176 uint8_t c7=(decnum/1000000)%10;
\r
177 uint8_t c8=(decnum/10000000)%10;
\r
178 uint8_t c9=(decnum/100000000)%10;
\r
179 uint8_t c10=(decnum/1000000000)%10;
\r
180 UARTPutChar(UARTx, '0'+c10);
\r
181 UARTPutChar(UARTx, '0'+c9);
\r
182 UARTPutChar(UARTx, '0'+c8);
\r
183 UARTPutChar(UARTx, '0'+c7);
\r
184 UARTPutChar(UARTx, '0'+c6);
\r
185 UARTPutChar(UARTx, '0'+c5);
\r
186 UARTPutChar(UARTx, '0'+c4);
\r
187 UARTPutChar(UARTx, '0'+c3);
\r
188 UARTPutChar(UARTx, '0'+c2);
\r
189 UARTPutChar(UARTx, '0'+c1);
\r
192 /*********************************************************************//**
\r
193 * @brief Puts a hex number to UART port
\r
194 * @param[in] UARTx Pointer to UART peripheral
\r
195 * @param[in] hexnum Hex number (8-bit long)
\r
197 **********************************************************************/
\r
198 void UARTPutHex (LPC_USARTn_Type *UARTx, uint8_t hexnum)
\r
202 UARTPuts(UARTx, "0x");
\r
205 nibble = (hexnum >> (4*i)) & 0x0F;
\r
206 UARTPutChar(UARTx, (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble));
\r
211 /*********************************************************************//**
\r
212 * @brief Puts a hex number to UART port
\r
213 * @param[in] UARTx Pointer to UART peripheral
\r
214 * @param[in] hexnum Hex number (16-bit long)
\r
216 **********************************************************************/
\r
217 void UARTPutHex16 (LPC_USARTn_Type *UARTx, uint16_t hexnum)
\r
221 UARTPuts(UARTx, "0x");
\r
224 nibble = (hexnum >> (4*i)) & 0x0F;
\r
225 UARTPutChar(UARTx, (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble));
\r
229 /*********************************************************************//**
\r
230 * @brief Puts a hex number to UART port
\r
231 * @param[in] UARTx Pointer to UART peripheral
\r
232 * @param[in] hexnum Hex number (32-bit long)
\r
234 **********************************************************************/
\r
235 void UARTPutHex32 (LPC_USARTn_Type *UARTx, uint32_t hexnum)
\r
239 UARTPuts(UARTx, "0x");
\r
242 nibble = (hexnum >> (4*i)) & 0x0F;
\r
243 UARTPutChar(UARTx, (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble));
\r
247 /*********************************************************************//**
\r
248 * @brief print function that supports format as same as printf()
\r
249 * function of <stdio.h> library
\r
250 * @param[in] format formated string to be print
\r
252 **********************************************************************/
\r
253 void lpc_printf (const char *format, ...)
\r
255 char buffer[512 + 1];
\r
257 va_start(vArgs, format);
\r
258 vsprintf((char *)buffer, (char const *)format, vArgs);
\r
264 /*********************************************************************//**
\r
265 * @brief Initialize Debug frame work through initializing UART port
\r
268 **********************************************************************/
\r
269 void debug_frmwrk_init(void)
\r
271 #ifdef UART_DEBUG_MESSEGES
\r
273 UART_CFG_Type UARTConfigStruct;
\r
275 #if (USED_UART_DEBUG_PORT==0)
\r
277 * Initialize UART0 pin connect NGX board
\r
279 scu_pinmux(0xF ,10 , MD_PDN|MD_EZI, FUNC1); // P6.4 UART0_TXD
\r
280 scu_pinmux(0xF ,11 , MD_PDN|MD_EZI, FUNC1); // P6.5 UART0_RXD
\r
281 #elif (USED_UART_DEBUG_PORT==1)
\r
283 * Initialize UART1 pin connect
\r
285 scu_pinmux(0x1 ,13 , MD_PDN, FUNC1); // PC.13 : UART1_TXD
\r
286 scu_pinmux(0x1 ,14 , MD_PLN|MD_EZI|MD_ZI, FUNC1); // PC.14 : UART1_RXD
\r
289 /* Initialize UART Configuration parameter structure to default state:
\r
290 * Baudrate = 9600bps
\r
295 UART_ConfigStructInit(&UARTConfigStruct);
\r
297 // Initialize DEBUG_UART_PORT peripheral with given to corresponding parameter
\r
298 UART_Init((LPC_USARTn_Type*)DEBUG_UART_PORT, &UARTConfigStruct);
\r
300 // Enable UART Transmit
\r
301 UART_TxCmd((LPC_USARTn_Type*)DEBUG_UART_PORT, ENABLE);
\r
303 #ifdef CDC_DEBUG_MESSEGES
\r
304 CDC_init(); //wait for usb enumeration
\r
308 _db_msg = UARTPuts;
\r
309 _db_msg_ = UARTPuts_;
\r
310 _db_char = UARTPutChar;
\r
311 _db_hex = UARTPutHex;
\r
312 _db_hex_16 = UARTPutHex16;
\r
313 _db_hex_32 = UARTPutHex32;
\r
314 _db_dec = UARTPutDec;
\r
315 _db_dec_16 = UARTPutDec16;
\r
316 _db_dec_32 = UARTPutDec32;
\r
317 _db_get_char = UARTGetChar;
\r
320 #endif /* _DEBUG_FRMWRK_ */
\r
326 /* --------------------------------- End Of File ------------------------------ */
\r