| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uart.c
Go to the documentation of this file.
1 /*
2 * @brief UART example
3 * This example show how to use the UART in 3 modes : Polling, Interrupt and DMA
4 *
5 * @note
6 * Copyright(C) NXP Semiconductors, 2012
7 * All rights reserved.
8 *
9 * @par
10 * Software that is described herein is for illustrative purposes only
11 * which provides customers with programming information regarding the
12 * LPC products. This software is supplied "AS IS" without any warranties of
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
14 * all warranties, express or implied, including all implied warranties of
15 * merchantability, fitness for a particular purpose and non-infringement of
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
17 * or liability for the use of the software, conveys no license or rights under any
18 * patent, copyright, mask work right, or any other intellectual property rights in
19 * or to any products. NXP Semiconductors reserves the right to make changes
20 * in the software without notification. NXP Semiconductors also makes no
21 * representation or warranty that such application will be suitable for the
22 * specified use without further testing or modification.
23 *
24 * @par
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation is hereby granted, under NXP Semiconductors' and its
27 * licensor's relevant copyrights in the software, without fee, provided that it
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
29 * copyright, permission, and disclaimer notice must appear in all copies of
30 * this code.
31 */
32
33 #include "chip.h"
34 #include "board.h"
35
68 /*****************************************************************************
69 * Private types/enumerations/variables
70 ****************************************************************************/
71 #if defined(BOARD_HITEX_EVA_18504350)
72 #define UARTNum 0
73
74 #elif defined (BOARD_KEIL_MCB_18574357)
75 #define UARTNum 3
76
77 #elif defined (BOARD_NGX_XPLORER_18304330)
78 #define UARTNum 0
79
80 #else
81 #error No UART selected for undefined board
82 #endif
83
84 #if (UARTNum == 0)
85 #define LPC_UART LPC_USART0
86 #define UARTx_IRQn USART0_IRQn
87 #define UARTx_IRQHandler UART0_IRQHandler
88 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART0_Tx
89 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART0_Rx
90 #elif (UARTNum == 1)
91 #define LPC_UART LPC_UART1
92 #define UARTx_IRQn UART1_IRQn
93 #define UARTx_IRQHandler UART1_IRQHandler
94 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART1_Tx
95 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART1_Rx
96 #elif (UARTNum == 2)
97 #define LPC_UART LPC_USART2
98 #define UARTx_IRQn USART2_IRQn
99 #define UARTx_IRQHandler UART2_IRQHandler
100 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART2_Tx
101 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART2_Rx
102 #elif (UARTNum == 3)
103 #define LPC_UART LPC_USART3
104 #define UARTx_IRQn USART3_IRQn
105 #define UARTx_IRQHandler UART3_IRQHandler
106 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART3_Tx
107 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART3_Rx
108 #endif
109
110 static uint8_t uartABComplete[] = "UART Auto-Baudrate synchronized! \r\n";
111
112 /* Uart Polling variables and functions declaration */
113 static uint8_t uartPolling_menu1[] = "Hello NXP Semiconductors \r\n";
114 static uint8_t uartPolling_menu2[] = "UART polling mode demo \r\n";
115 static uint8_t uartPolling_menu3[] = "\r\nUART demo terminated!";
116 static uint8_t uartPolling_menu4[] = "\r\nPress number 1-3 to choose UART running mode:\r\n"
117 "\t 1: Polling Mode \r\n"
118 "\t 2: Interrupt Mode \r\n"
119 "\t 3: DMA Mode \r\n";
120 static uint8_t uartPolling_menu5[] = "\r\nPolling mode is running now! Please press \'c\' and choose another mode \r\n";
121
122 /* Uart Interrupt variables and functions declaration */
123 static uint8_t uart_interrupt_menu[] =
124 "UART Interrupt mode demo ! \r\nPress '1' to '4' to display 4 menus \r\nPress 'x'to exit UART interrupt mode \r\n";
125 static uint8_t uart_interrupt_menu1[] = "UART interrupt menu 1 \r\n";
126 static uint8_t uart_interrupt_menu2[] = "UART interrupt menu 2 \r\n";
127 static uint8_t uart_interrupt_menu3[] = "UART interrupt menu 3 \r\n";
128 static uint8_t uart_interrupt_menu4[] = "UART interrupt menu 4 \r\n";
129 /* static uint8_t rxUartIntBuf[1]; */
130
131 #define DMA_TIMEOUT 0xA000000
132
133 /* DMA variables and functions declaration */
134 static uint8_t dmaChannelNumTx, dmaChannelNumRx;
135 static uint8_t uartDMA_menu[] = "Hello NXP Semiconductors (DMA mode)\r\n"
136 "UART DMA mode demo ! Please type 'hello NXP' to return\r\n";
137
138 static volatile uint32_t channelTC; /* Terminal Counter flag for Channel */
139 static volatile uint32_t channelTCErr;
140 static FunctionalState isDMATx = ENABLE;
141
142 /*****************************************************************************
143 * Public types/enumerations/variables
144 ****************************************************************************/
145
146 /*****************************************************************************
147 * Private functions
148 ****************************************************************************/
149
150 /* Initialize DMA for UART, enable DMA controller and enable DMA interrupt */
151 void App_DMA_Init(void)
152 {
153 /* Initialize GPDMA controller */
154 Chip_GPDMA_Init(LPC_GPDMA);
155 /* Setting GPDMA interrupt */
156 NVIC_DisableIRQ(DMA_IRQn);
157 NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
158 NVIC_EnableIRQ(DMA_IRQn);
159 }
160
161 /* DeInitialize DMA for UART, free transfer channels and disable DMA interrupt */
162 void App_DMA_DeInit(void)
163 {
164 Chip_DMA_Stop(LPC_GPDMA, dmaChannelNumTx);
165 Chip_DMA_Stop(LPC_GPDMA, dmaChannelNumRx);
166 NVIC_DisableIRQ(DMA_IRQn);
167 }
168
169 /* DMA routine for example_uart */
170 void App_DMA_Test(void)
171 {
172 uint8_t receiveBuffer[16];
173
174 App_DMA_Init();
175 dmaChannelNumTx = Chip_DMA_GetFreeChannel(LPC_GPDMA, _GPDMA_CONN_UART_Tx);
176
177 isDMATx = ENABLE;
178 channelTC = channelTCErr = 0;
179 Chip_DMA_Transfer(LPC_GPDMA, dmaChannelNumTx,
180 (uint32_t) &uartDMA_menu[0],
181 _GPDMA_CONN_UART_Tx,
182 GPDMA_TRANSFERTYPE_M2P_CONTROLLER_DMA,
183 sizeof(uartDMA_menu));
184 while (!channelTC) {}
185
186 dmaChannelNumRx = Chip_DMA_GetFreeChannel(LPC_GPDMA, _GPDMA_CONN_UART_Rx);
187 isDMATx = DISABLE;
188 channelTC = channelTCErr = 0;
189 Chip_DMA_Transfer(LPC_GPDMA, dmaChannelNumRx,
190 _GPDMA_CONN_UART_Rx,
191 (uint32_t) &receiveBuffer[0],
192 GPDMA_TRANSFERTYPE_P2M_CONTROLLER_DMA,
193 10);
194 while (!channelTC) {}
195
196 isDMATx = ENABLE;
197 channelTC = channelTCErr = 0;
198 Chip_DMA_Transfer(LPC_GPDMA, dmaChannelNumTx,
199 (uint32_t) &receiveBuffer[0],
200 _GPDMA_CONN_UART_Tx,
201 GPDMA_TRANSFERTYPE_M2P_CONTROLLER_DMA,
202 10);
203 while (!channelTC) {}
204
205 App_DMA_DeInit();
206 }
207
208 /* Print Welcome Screen Menu subroutine by Interrupt mode */
209 void Print_Menu_Interrupt(LPC_USART_T *UARTx)
210 {
211 uint32_t tmp, tmp2;
212 uint8_t *pDat;
213
214 tmp = sizeof(uart_interrupt_menu);
215 tmp2 = 0;
216 pDat = (uint8_t *) &uart_interrupt_menu[0];
217 while (tmp) {
218 tmp2 = Chip_UART_Interrupt_Transmit(UARTx, pDat, tmp);
219 pDat += tmp2;
220 tmp -= tmp2;
221 }
222 }
223
224 /* Initialize Interrupt for UART */
225 void App_Interrupt_Init(void)
226 {
227 /* Enable UART Rx interrupt */
228 Chip_UART_IntConfig(LPC_UART, UART_INTCFG_RBR, ENABLE);
229 /* Enable UART line status interrupt */
230 Chip_UART_IntConfig(LPC_UART, UART_INTCFG_RLS, ENABLE);
231 /*
232 * Do not enable transmit interrupt here, since it is handled by
233 * UART_Send() function, just to reset Tx Interrupt state for the
234 * first time
235 */
236 Chip_UART_InitRingBuffer(LPC_UART);
237 /* Enable Interrupt for UART channel */
238 /* Priority = 1 */
239 NVIC_SetPriority(UARTx_IRQn, 1);
240 /* Enable Interrupt for UART channel */
241 NVIC_EnableIRQ(UARTx_IRQn);
242 }
243
244 /* DeInitialize Interrupt for UART */
245 void App_Interrupt_DeInit(void)
246 {
247 /* Disable UART Rx interrupt */
248 Chip_UART_IntConfig(LPC_UART, UART_INTCFG_RBR, DISABLE);
249 /* Disable UART line status interrupt */
250 Chip_UART_IntConfig(LPC_UART, UART_INTCFG_RLS, DISABLE);
251 /* Disable Interrupt for UART channel */
252 NVIC_DisableIRQ(UARTx_IRQn);
253 }
254
255 /* Interrupt routine for example_uart */
256 void App_Interrupt_Test(void)
257 {
258 uint8_t isExit = 0, userInput;
259 uint32_t len;
260 App_Interrupt_Init();
261
262 /* Print out uart interrupt menu */
263 Print_Menu_Interrupt(LPC_UART);
264
265 while (!isExit) {
266 len = 0;
267 while (len == 0) {
268 len = Chip_UART_Interrupt_Receive(LPC_UART, &userInput, 1);
269 }
270 if (userInput == '1') {
271 Chip_UART_Interrupt_Transmit(LPC_UART, (uint8_t *) &uart_interrupt_menu1[0], sizeof(uart_interrupt_menu1));
272 }
273 else if (userInput == '2') {
274 Chip_UART_Interrupt_Transmit(LPC_UART, (uint8_t *) &uart_interrupt_menu2[0], sizeof(uart_interrupt_menu2));
275 }
276 else if (userInput == '3') {
277 Chip_UART_Interrupt_Transmit(LPC_UART, (uint8_t *) &uart_interrupt_menu3[0], sizeof(uart_interrupt_menu3));
278 }
279 else if (userInput == '4') {
280 Chip_UART_Interrupt_Transmit(LPC_UART, (uint8_t *) &uart_interrupt_menu4[0], sizeof(uart_interrupt_menu4));
281 }
282 else if (( userInput == 'x') || ( userInput == 'X') ) {
283 isExit = 1;
284 }
285 }
286 App_Interrupt_DeInit();
287 }
288
289 /* Print Welcome menu by Polling mode */
290 void Print_Menu_Polling(void)
291 {
292 Chip_UART_Send(LPC_UART, uartPolling_menu1, sizeof(uartPolling_menu1), BLOCKING);
293 Chip_UART_Send(LPC_UART, uartPolling_menu2, sizeof(uartPolling_menu2), BLOCKING);
294 Chip_UART_Send(LPC_UART, uartPolling_menu5, sizeof(uartPolling_menu5), BLOCKING);
295 }
296
297 /*****************************************************************************
298 * Public functions
299 ****************************************************************************/
300
305 void DMA_IRQHandler(void)
306 {
307 uint8_t dmaChannelNum;
308 if (isDMATx) {
309 dmaChannelNum = dmaChannelNumTx;
310 }
311 else {
312 dmaChannelNum = dmaChannelNumRx;
313 }
314 if (Chip_DMA_Interrupt(LPC_GPDMA, dmaChannelNum) == SUCCESS) {
315 channelTC++;
316 }
317 else {
318 channelTCErr++;
319 }
320 }
321
326 void UARTx_IRQHandler(void)
327 {
328 Chip_UART_Interrupt_Handler(LPC_UART);
329 }
330
335 int main(void)
336 {
337 FlagStatus exitflag;
338 uint8_t buffer[10];
339 int ret = 0;
340 uint32_t len;
341
342 /* UART FIFO configuration Struct variable */
343 UART_FIFO_CFG_T UARTFIFOConfigStruct;
344 /* Auto baudrate configuration structure */
345 UART_AB_CFG_T ABConfig;
346
347 Board_Init();
348 Board_UART_Init(LPC_UART);
349
350 #if !((defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357) && UARTNum==3) || ((!(defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357))) && UARTNum==0))
351 Chip_UART_Init(LPC_UART);
352 Chip_UART_SetBaud(LPC_UART, 115200);
353 Chip_UART_ConfigData(LPC_UART, UART_DATABIT_8, UART_PARITY_NONE, UART_STOPBIT_1); /* Default 8-N-1 */
354
355 /* Enable UART Transmit */
356 Chip_UART_TxCmd(LPC_UART, ENABLE);
357 #endif
358
359 Chip_UART_FIFOConfigStructInit(LPC_UART, &UARTFIFOConfigStruct);
360
361 /* Enable DMA mode in UART */
362 UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE;
363 /* Initialize FIFO for UART0 peripheral */
364 Chip_UART_FIFOConfig(LPC_UART, &UARTFIFOConfigStruct);
365
366 /* Enable UART End of Auto baudrate interrupt */
367 Chip_UART_IntConfig(LPC_UART, UART_INTCFG_ABEO, ENABLE);
368 /* Enable UART Auto baudrate timeout interrupt */
369 Chip_UART_IntConfig(LPC_UART, UART_INTCFG_ABTO, ENABLE);
370
371 /* preemption = 1, sub-priority = 1 */
372 NVIC_SetPriority(UARTx_IRQn, 1);
373 /* Enable Interrupt for UART0 channel */
374 NVIC_EnableIRQ(UARTx_IRQn);
375
376 /* ---------------------- Auto baud rate section ----------------------- */
377 /* Configure Auto baud rate mode */
378 ABConfig.ABMode = UART_AUTOBAUD_MODE0;
379 ABConfig.AutoRestart = ENABLE;
380
381 /* Start auto baudrate mode */
382 Chip_UART_ABCmd(LPC_UART, &ABConfig, ENABLE);
383
384 /* Loop until auto baudrate mode complete */
385 while (Chip_UART_GetABEOStatus(LPC_UART) == RESET);
386
387 Chip_UART_Send(LPC_UART, uartABComplete, sizeof(uartABComplete), BLOCKING);
388
389 /* Disable UART Interrupt */
390 NVIC_DisableIRQ(UARTx_IRQn);
391
392 /* Print welcome screen */
393 Print_Menu_Polling();
394
395 exitflag = RESET;
396 /* Read some data from the buffer */
397 while (exitflag == RESET) {
398 len = 0;
399 while (len == 0) {
400 len = Chip_UART_Receive(LPC_UART, buffer, 1, NONE_BLOCKING);
401 }
402 if (buffer[0] == 27) {
403 /* ESC key, set exit flag */
404 Chip_UART_Send(LPC_UART, uartPolling_menu3, sizeof(uartPolling_menu3), BLOCKING);
405 ret = -1;
406 exitflag = SET;
407 }
408 else if (buffer[0] == 'c') {
409 Chip_UART_Send(LPC_UART, uartPolling_menu4, sizeof(uartPolling_menu4), BLOCKING);
410 len = 0;
411 while (len == 0) {
412 len = Chip_UART_Receive(LPC_UART, buffer, sizeof(buffer), NONE_BLOCKING);
413 if ((buffer[0] != '1') && (buffer[0] != '2') && (buffer[0] != '3')) {
414 len = 0;
415 }
416 }
417 switch (buffer[0]) {
418 case '1': /* Polling Mode */
419 Chip_UART_Send(LPC_UART, uartPolling_menu5, sizeof(uartPolling_menu5), BLOCKING);
420 break;
421
422 case '2': /* Interrupt Mode */
423 ret = 2;
424 /* Exitflag = SET; */
425 App_Interrupt_Test();
426 Print_Menu_Polling();
427 break;
428
429 case '3': /* DMA mode */
430 ret = 3;
431 App_DMA_Test();
432 Print_Menu_Polling();
433 break;
434 }
435 }
436 }
437
438 /* Wait for current transmission complete - THR must be empty */
439 while (Chip_UART_CheckBusy(LPC_UART) == SET) {}
440
441 /* DeInitialize UART0 peripheral */
442 Chip_UART_DeInit(LPC_UART);
443
444 return ret;
445 }
446