LPCOpen Platform
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 "chip.h"
34 #include "board.h"
35 
59 /*****************************************************************************
60  * Private types/enumerations/variables
61  ****************************************************************************/
62 #if defined(BOARD_HITEX_EVA_18504350)
63 #define UARTNum 0
64 
65 #elif defined (BOARD_KEIL_MCB_18574357)
66 #define UARTNum 3
67 
68 #elif defined (BOARD_NGX_XPLORER_18304330)
69 #define UARTNum 0
70 
71 #else
72 #error No UART selected for undefined board
73 #endif
74 
75 #if (UARTNum == 0)
76 #define LPC_UART LPC_USART0
77 #define UARTx_IRQn USART0_IRQn
78 #define UARTx_IRQHandler UART0_IRQHandler
79 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART0_Tx
80 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART0_Rx
81 #elif (UARTNum == 1)
82 #define LPC_UART LPC_UART1
83 #define UARTx_IRQn UART1_IRQn
84 #define UARTx_IRQHandler UART1_IRQHandler
85 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART1_Tx
86 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART1_Rx
87 #elif (UARTNum == 2)
88 #define LPC_UART LPC_USART2
89 #define UARTx_IRQn USART2_IRQn
90 #define UARTx_IRQHandler UART2_IRQHandler
91 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART2_Tx
92 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART2_Rx
93 #elif (UARTNum == 3)
94 #define LPC_UART LPC_USART3
95 #define UARTx_IRQn USART3_IRQn
96 #define UARTx_IRQHandler UART3_IRQHandler
97 #define _GPDMA_CONN_UART_Tx GPDMA_CONN_UART3_Tx
98 #define _GPDMA_CONN_UART_Rx GPDMA_CONN_UART3_Rx
99 #endif
100 
101 /* Uart Polling variables and functions declaration */
102 static uint8_t uartPolling_menu1[] = "Hello NXP Semiconductors \n\r";
103 static uint8_t uartPolling_menu2[] = "UART polling mode demo \n\r";
104 static uint8_t uartPolling_menu3[] = "\n\rUART demo terminated!";
105 static uint8_t uartPolling_menu4[] = "\n\rPress number 1-3 to choose UART running mode:\n\r"
106  "\t 1: Polling Mode \n\r"
107  "\t 2: Interrupt Mode \n\r"
108  "\t 3: DMA Mode \n\r";
109 static uint8_t uartPolling_menu5[] = "\n\rPolling mode is running now! Please press \'c\' and choose another mode \n\r";
110 
111 /* Uart Interrupt variables and functions declaration */
112 static uint8_t uart_interrupt_menu[] =
113  "UART Interrupt mode demo ! \n\rPress '1' to '4' to display 4 menus \n\rPress 'x'to exist uart interrupt mode \n\r";
114 static uint8_t uart_interrupt_menu1[] = "UART interrupt menu 1 \n\r";
115 static uint8_t uart_interrupt_menu2[] = "UART interrupt menu 2 \n\r";
116 static uint8_t uart_interrupt_menu3[] = "UART interrupt menu 3 \n\r";
117 static uint8_t uart_interrupt_menu4[] = "UART interrupt menu 4 \n\r";
118 /* static uint8_t rxUartIntBuf[1]; */
119 
120 #define DMA_TIMEOUT 0xA000000
121 
122 /* DMA variables and functions declaration */
124 static uint8_t uartDMA_menu[] = "Hello NXP Semiconductors (DMA mode)\n\r"
125  "UART DMA mode demo ! Please type 'hello NXP' to return\n\r";
126 
127 static volatile uint32_t channelTC; /* Terminal Counter flag for Channel */
128 static volatile uint32_t channelTCErr;
130 
131 /*****************************************************************************
132  * Public types/enumerations/variables
133  ****************************************************************************/
134 
135 /*****************************************************************************
136  * Private functions
137  ****************************************************************************/
138 
139 /* Initialize DMA for UART, enable DMA controller and enable DMA interrupt */
140 void App_DMA_Init(void)
141 {
142  /* Initialize GPDMA controller */
143  Chip_GPDMA_Init();
144  /* Setting GPDMA interrupt */
145  NVIC_DisableIRQ(DMA_IRQn);
146  NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
147  NVIC_EnableIRQ(DMA_IRQn);
148 }
149 
150 /* DeInitialize DMA for UART, free transfer channels and disable DMA interrupt */
151 void App_DMA_DeInit(void)
152 {
155  NVIC_DisableIRQ(DMA_IRQn);
156 }
157 
158 /* DMA routine for example_uart */
159 void App_DMA_Test(void)
160 {
161  uint8_t receiveBuffer[16];
162 
163  App_DMA_Init();
164  dmaChannelNumTx = Chip_DMA_GetFreeChannel(_GPDMA_CONN_UART_Tx);
165 
166  isDMATx = ENABLE;
167  channelTC = channelTCErr = 0;
169  (uint32_t) &uartDMA_menu[0],
170  _GPDMA_CONN_UART_Tx,
172  sizeof(uartDMA_menu));
173  while (!channelTC) {}
174 
175  dmaChannelNumRx = Chip_DMA_GetFreeChannel(_GPDMA_CONN_UART_Rx);
176  isDMATx = DISABLE;
177  channelTC = channelTCErr = 0;
179  _GPDMA_CONN_UART_Rx,
180  (uint32_t) &receiveBuffer[0],
182  10);
183  while (!channelTC) {}
184 
185  isDMATx = ENABLE;
186  channelTC = channelTCErr = 0;
188  (uint32_t) &receiveBuffer[0],
189  _GPDMA_CONN_UART_Tx,
191  10);
192  while (!channelTC) {}
193 
194  App_DMA_DeInit();
195 }
196 
197 /* Print Welcome Screen Menu subroutine by Interrupt mode */
199 {
200  uint32_t tmp, tmp2;
201  uint8_t *pDat;
202 
203  tmp = sizeof(uart_interrupt_menu);
204  tmp2 = 0;
205  pDat = (uint8_t *) &uart_interrupt_menu[0];
206  while (tmp) {
207  tmp2 = Chip_UART_Interrupt_Transmit(UARTx, pDat, tmp);
208  pDat += tmp2;
209  tmp -= tmp2;
210  }
211 }
212 
213 /* Initialize Interrupt for UART */
215 {
216  /* Enable UART Rx interrupt */
218  /* Enable UART line status interrupt */
220  /*
221  * Do not enable transmit interrupt here, since it is handled by
222  * UART_Send() function, just to reset Tx Interrupt state for the
223  * first time
224  */
226  /* Enable Interrupt for UART channel */
227  /* Priority = 1 */
228  NVIC_SetPriority(UARTx_IRQn, 1);
229  /* Enable Interrupt for UART channel */
230  NVIC_EnableIRQ(UARTx_IRQn);
231 }
232 
233 /* DeInitialize Interrupt for UART */
235 {
236  /* Disable UART Rx interrupt */
238  /* Disable UART line status interrupt */
240  /* Disable Interrupt for UART channel */
241  NVIC_DisableIRQ(UARTx_IRQn);
242 }
243 
244 /* Interrupt routine for example_uart */
246 {
247  uint8_t isExit = 0, userInput;
248  uint32_t len;
250 
251  /* Print out uart interrupt menu */
253 
254  while (!isExit) {
255  len = 0;
256  while (len == 0) {
257  len = Chip_UART_Interrupt_Receive(LPC_UART, &userInput, 1);
258  }
259  if (userInput == '1') {
261  }
262  else if (userInput == '2') {
264  }
265  else if (userInput == '3') {
267  }
268  else if (userInput == '4') {
270  }
271  else if (( userInput == 'x') || ( userInput == 'X') ) {
272  isExit = 1;
273  }
274  }
276 }
277 
278 /* Print Welcome menu by Polling mode */
280 {
284 }
285 
286 /*****************************************************************************
287  * Public functions
288  ****************************************************************************/
289 
294 void DMA_IRQHandler(void)
295 {
296  uint8_t dmaChannelNum;
297  if (isDMATx) {
298  dmaChannelNum = dmaChannelNumTx;
299  }
300  else {
301  dmaChannelNum = dmaChannelNumRx;
302  }
303  if (Chip_DMA_Interrupt(dmaChannelNum) == SUCCESS) {
304  channelTC++;
305  }
306  else {
307  channelTCErr++;
308  }
309 }
310 
316 {
318 }
319 
324 int main(void)
325 {
326  /* Chip_Stream_Polling_Ctx uart0_ctx; */
327  FlagStatus exitflag;
328  uint8_t buffer[10];
329  int ret = 0;
330  uint32_t len;
331 
332  /* UART FIFO configuration Struct variable */
333  UART_FIFO_CFG_Type UARTFIFOConfigStruct;
334 
335  Board_Init();
336 
338 
339 #if !((defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357) && UARTNum==3) || ((!(defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357))) && UARTNum==0))
341  Chip_UART_SetBaud(LPC_UART, 115200);
343 
344  /* Enable UART Transmit */
346 #endif
347 
348  Chip_UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
349 
350  /* Enable DMA mode in UART */
351  UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE;
352  /* Initialize FIFO for UART0 peripheral */
353  Chip_UART_FIFOConfig(LPC_UART, &UARTFIFOConfigStruct);
354 
355  /* Print welcome screen */
357 
358  exitflag = RESET;
359  /* Read some data from the buffer */
360  while (exitflag == RESET) {
361  len = 0;
362  while (len == 0) {
363  len = Chip_UART_Receive(LPC_UART, buffer, 1, NONE_BLOCKING);
364  }
365  if (buffer[0] == 27) {
366  /* ESC key, set exit flag */
368  ret = -1;
369  exitflag = SET;
370  }
371  else if (buffer[0] == 'c') {
373  len = 0;
374  while (len == 0) {
375  len = Chip_UART_Receive(LPC_UART, buffer, sizeof(buffer), NONE_BLOCKING);
376  if ((buffer[0] != '1') && (buffer[0] != '2') && (buffer[0] != '3')) {
377  len = 0;
378  }
379  }
380  switch (buffer[0]) {
381  case '1': /* Polling Mode */
383  break;
384 
385  case '2': /* Interrupt Mode */
386  ret = 2;
387  /* Exitflag = SET; */
390  break;
391 
392  case '3': /* DMA mode */
393  ret = 3;
394  App_DMA_Test();
396  break;
397  }
398  }
399  }
400 
401  /* Wait for current transmission complete - THR must be empty */
402  while (Chip_UART_CheckBusy(LPC_UART) == SET) {}
403 
404  /* DeInitialize UART0 peripheral */
406 
407  return ret;
408 }
409