LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 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 "board.h"
34 
76 /*****************************************************************************
77  * Private types/enumerations/variables
78  ****************************************************************************/
79 
80 #if defined(BOARD_EA_DEVKIT_17884088)
81 #define UARTNum 0
82 #elif defined(BOARD_NXP_XPRESSO_1769)
83 #define UARTNum 3
84 #else
85 #error No UART selected for undefined board
86 #endif
87 
88 #if (UARTNum == 0)
89 #define LPC_UART LPC_UART0
90 #define UARTx_IRQn UART0_IRQn
91 #define UARTx_IRQHandler UART0_IRQHandler
92 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART0_Tx
93 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART0_Rx
94 #elif (UARTNum == 1)
95 #define LPC_UART LPC_UART1
96 #define UARTx_IRQn UART1_IRQn
97 #define UARTx_IRQHandler UART1_IRQHandler
98 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART1_Tx
99 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART1_Rx
100 #elif (UARTNum == 2)
101 #define LPC_UART LPC_UART2
102 #define UARTx_IRQn UART2_IRQn
103 #define UARTx_IRQHandler UART2_IRQHandler
104 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART2_Tx
105 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART2_Rx
106 #elif (UARTNum == 3)
107 #define LPC_UART LPC_UART3
108 #define UARTx_IRQn UART3_IRQn
109 #define UARTx_IRQHandler UART3_IRQHandler
110 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART3_Tx
111 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART3_Rx
112 #endif
113 
114 static uint8_t uartABComplete[] = "UART Auto-Baurate synchronized! \n\r";
115 
116 /* Uart Polling variables and functions declaration */
117 static uint8_t uartPolling_menu1[] = "Hello NXP Semiconductors \n\r";
118 static uint8_t uartPolling_menu2[] = "UART polling mode demo \n\r";
119 static uint8_t uartPolling_menu3[] = "\n\rUART demo terminated!";
120 static uint8_t uartPolling_menu4[] = "\n\rPress number 1-3 to choose UART running mode:\n\r"
121  "\t 1: Polling Mode \n\r"
122  "\t 2: Interrupt Mode \n\r"
123  "\t 3: DMA Mode \n\r";
124 static uint8_t uartPolling_menu5[] = "\n\rPolling mode is running now! Please press \'c\' and choose another mode \n\r";
125 
126 /* Uart Interrupt variables and functions declaration */
127 static uint8_t uart_interrupt_menu[] =
128  "UART Interrupt mode demo ! \n\rPress '1' to '4' to display 4 menus \n\rPress 'x'to exist uart interrupt mode \n\r";
129 static uint8_t uart_interrupt_menu1[] = "UART interrupt menu 1 \n\r";
130 static uint8_t uart_interrupt_menu2[] = "UART interrupt menu 2 \n\r";
131 static uint8_t uart_interrupt_menu3[] = "UART interrupt menu 3 \n\r";
132 static uint8_t uart_interrupt_menu4[] = "UART interrupt menu 4 \n\r";
133 /* static uint8_t rxUartIntBuf[1]; */
134 
135 #define DMA_TIMEOUT 0xA000000
136 
137 /* DMA variables and functions declaration */
139 static uint8_t uartDMA_menu[] = "Hello NXP Semiconductors (DMA mode)\n\r"
140  "UART DMA mode demo ! Please type 'hello NXP' to return\n\r";
141 
142 static volatile uint32_t channelTC; /* Terminal Counter flag for Channel */
143 static volatile uint32_t channelTCErr;
145 
146 /*****************************************************************************
147  * Public types/enumerations/variables
148  ****************************************************************************/
149 
150 /*****************************************************************************
151  * Private functions
152  ****************************************************************************/
153 
154 /* Initialize DMA for UART, enable DMA controller and enable DMA interrupt */
155 static void App_DMA_Init(void)
156 {
157  /* Initialize GPDMA controller */
159  /* Setting GPDMA interrupt */
160  NVIC_DisableIRQ(DMA_IRQn);
161  NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
162  NVIC_EnableIRQ(DMA_IRQn);
163 }
164 
165 /* DeInitialize DMA for UART, free transfer channels and disable DMA interrupt */
166 static void App_DMA_DeInit(void)
167 {
170  NVIC_DisableIRQ(DMA_IRQn);
171 }
172 
173 /* DMA routine for example_uart */
174 static void App_DMA_Test(void)
175 {
176  uint8_t receiveBuffer[16];
177 
178  App_DMA_Init();
179  dmaChannelNumTx = Chip_DMA_GetFreeChannel(LPC_GPDMA, _GPDMA_CONN_UART_Tx);
180 
181  isDMATx = ENABLE;
182  channelTC = channelTCErr = 0;
184  (uint32_t) &uartDMA_menu[0],
185  _GPDMA_CONN_UART_Tx,
187  sizeof(uartDMA_menu));
188  while (!channelTC) {}
189 
190  dmaChannelNumRx = Chip_DMA_GetFreeChannel(LPC_GPDMA, _GPDMA_CONN_UART_Rx);
191  isDMATx = DISABLE;
192  channelTC = channelTCErr = 0;
194  _GPDMA_CONN_UART_Rx,
195  (uint32_t) &receiveBuffer[0],
197  10);
198  while (!channelTC) {}
199 
200  isDMATx = ENABLE;
201  channelTC = channelTCErr = 0;
203  (uint32_t) &receiveBuffer[0],
204  _GPDMA_CONN_UART_Tx,
206  10);
207  while (!channelTC) {}
208 
209  App_DMA_DeInit();
210 }
211 
212 /* Print Welcome Screen Menu subroutine by Interrupt mode */
214 {
215  uint32_t tmp, tmp2;
216  uint8_t *pDat;
217 
218  tmp = sizeof(uart_interrupt_menu);
219  tmp2 = 0;
220  pDat = (uint8_t *) &uart_interrupt_menu[0];
221  while (tmp) {
222  tmp2 = Chip_UART_Interrupt_Transmit(UARTx, pDat, tmp);
223  pDat += tmp2;
224  tmp -= tmp2;
225  }
226 }
227 
228 /* Initialize Interrupt for UART */
229 static void App_Interrupt_Init(void)
230 {
231  /* Enable UART Rx interrupt */
233  /* Enable UART line status interrupt */
235  /*
236  * Do not enable transmit interrupt here, since it is handled by
237  * UART_Send() function, just to reset Tx Interrupt state for the
238  * first time
239  */
241  /* Enable Interrupt for UART channel */
242  /* Priority = 1 */
243  NVIC_SetPriority(UARTx_IRQn, 1);
244  /* Enable Interrupt for UART channel */
245  NVIC_EnableIRQ(UARTx_IRQn);
246 }
247 
248 /* DeInitialize Interrupt for UART */
249 static void App_Interrupt_DeInit(void)
250 {
251  /* Disable UART Rx interrupt */
253  /* Disable UART line status interrupt */
255  /* Disable Interrupt for UART channel */
256  NVIC_DisableIRQ(UARTx_IRQn);
257 }
258 
259 /* Interrupt routine for example_uart */
260 static void App_Interrupt_Test(void)
261 {
262  uint8_t isExit = 0, userInput;
263  uint32_t len;
265 
266  /* Print out uart interrupt menu */
268 
269  while (!isExit) {
270  len = 0;
271  while (len == 0) {
272  len = Chip_UART_Interrupt_Receive(LPC_UART, &userInput, 1);
273  }
274  if (userInput == '1') {
276  }
277  else if (userInput == '2') {
279  }
280  else if (userInput == '3') {
282  }
283  else if (userInput == '4') {
285  }
286  else if (( userInput == 'x') || ( userInput == 'X') ) {
287  isExit = 1;
288  }
289  }
291 }
292 
293 /* Print Welcome menu by Polling mode */
294 static void Print_Menu_Polling(void)
295 {
299 }
300 
301 /*****************************************************************************
302  * Public functions
303  ****************************************************************************/
304 
309 void DMA_IRQHandler(void)
310 {
311  uint8_t dmaChannelNum;
312  if (isDMATx) {
313  dmaChannelNum = dmaChannelNumTx;
314  }
315  else {
316  dmaChannelNum = dmaChannelNumRx;
317  }
318  if (Chip_DMA_Interrupt(LPC_GPDMA, dmaChannelNum) == SUCCESS) {
319  channelTC++;
320  }
321  else {
322  channelTCErr++;
323  }
324 }
325 
331 {
333 }
334 
339 int main(void)
340 {
341  FlagStatus exitflag;
342  uint8_t buffer[10];
343  int ret = 0;
344  uint32_t len;
345 
346  /* UART FIFO configuration Struct variable */
347  UART_FIFO_CFG_T UARTFIFOConfigStruct;
348  /* Auto baudrate configuration structure */
349  UART_AB_CFG_T ABConfig;
350 
351  Board_Init();
353 
354 #if (UARTNum != 0)
356  Chip_UART_SetBaud(LPC_UART, 115200);
358 
359  /* Enable UART Transmit */
361 #endif
362 
363  Chip_UART_FIFOConfigStructInit(LPC_UART, &UARTFIFOConfigStruct);
364 
365  /* Enable DMA mode in UART */
366  UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE;
367  /* Initialize FIFO for UART0 peripheral */
368  Chip_UART_FIFOConfig(LPC_UART, &UARTFIFOConfigStruct);
369 
370  /* Enable UART End of Auto baudrate interrupt */
372  /* Enable UART Auto baudrate timeout interrupt */
374 
375  /* preemption = 1, sub-priority = 1 */
376  NVIC_SetPriority(UARTx_IRQn, 1);
377  /* Enable Interrupt for UART0 channel */
378  NVIC_EnableIRQ(UARTx_IRQn);
379 
380  /* ---------------------- Auto baud rate section ----------------------- */
381  /* Configure Auto baud rate mode */
382  ABConfig.ABMode = UART_AUTOBAUD_MODE0;
383  ABConfig.AutoRestart = ENABLE;
384 
385  /* Start auto baudrate mode */
386  Chip_UART_ABCmd(LPC_UART, &ABConfig, ENABLE);
387 
388  /* Loop until auto baudrate mode complete */
389  while (Chip_UART_GetABEOStatus(LPC_UART) == RESET) {}
390 
392 
393  /* Disable UART Interrupt */
394  NVIC_DisableIRQ(UARTx_IRQn);
395 
396  /* Print welcome screen */
398 
399  exitflag = RESET;
400  /* Read some data from the buffer */
401  while (exitflag == RESET) {
402  len = 0;
403  while (len == 0) {
404  len = Chip_UART_Receive(LPC_UART, buffer, 1, NONE_BLOCKING);
405  }
406  if (buffer[0] == 27) {
407  /* ESC key, set exit flag */
409  ret = -1;
410  exitflag = SET;
411  }
412  else if (buffer[0] == 'c') {
414  len = 0;
415  while (len == 0) {
416  len = Chip_UART_Receive(LPC_UART, buffer, sizeof(buffer), NONE_BLOCKING);
417  if ((buffer[0] != '1') && (buffer[0] != '2') && (buffer[0] != '3')) {
418  len = 0;
419  }
420  }
421  switch (buffer[0]) {
422  case '1': /* Polling Mode */
424  break;
425 
426  case '2': /* Interrupt Mode */
427  ret = 2;
428  /* Exitflag = SET; */
431  break;
432 
433  case '3': /* DMA mode */
434  ret = 3;
435  App_DMA_Test();
437  break;
438  }
439  }
440  }
441 
442  /* Wait for current transmission complete - THR must be empty */
443  while (Chip_UART_CheckBusy(LPC_UART) == SET) {}
444 
445  /* DeInitialize UART0 peripheral */
447 
448  return ret;
449 }
450