| 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 Universal Asynchronous Receiver/Transmitter (UART) example
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights 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 "board.h"
33
62 /*****************************************************************************
63 * Private types/enumerations/variables
64 ****************************************************************************/
65
66 #define BUFFER_SIZE 0xF
67 #define UART_TEST_DEFAULT_BAUDRATE 115200
68 #define UART_TEST_REPEAT_NUMBER 0xF
69
70 static uint8_t TxBuf0[BUFFER_SIZE];
71 static uint8_t RxBuf1[BUFFER_SIZE];
72 static uint8_t RxBuf0[BUFFER_SIZE];
73 static uint32_t RxBufCnt1 = 0;
74 static uint32_t TxBufCnt1 = 0;
75 static volatile bool receiveCompleted = true;
76 static volatile bool sendCompleted = true;
77
78 /*****************************************************************************
79 * Public types/enumerations/variables
80 ****************************************************************************/
81
82 /*****************************************************************************
83 * Private functions
84 ****************************************************************************/
85
86 /* Buffer initialisation function */
87 static void bufferInit(uint8_t seed)
88 {
89 uint32_t i;
90 for (i = 0; i < BUFFER_SIZE; i++) {
91 TxBuf0[i] = i + seed;
92 RxBuf1[i] = 0;
93 RxBuf0[i] = 0;
94 }
95 }
96
97 /* Buffer check function */
98 static void bufferCheck()
99 {
100 uint32_t i;
101 for (i = 0; i < BUFFER_SIZE; i++) {
102 if (TxBuf0[i] != RxBuf0[i]) {
103 while (1) {
104 /* Data mismatch */
105 }
106 }
107 }
108 }
109
110 /* UART initialistaion function */
111 static void App_UART_Init(LPC_USART_T *pUART)
112 {
113 Board_UART_Init(pUART);
114 Chip_UART_ConfigData(pUART, UART_DATALEN_8, UART_PARITY_NONE, UART_STOPLEN_1);
115 Chip_UART_SetBaudRate(pUART, UART_TEST_DEFAULT_BAUDRATE);
116 Chip_UART_Init(pUART);
117 }
118
119 /*****************************************************************************
120 * Public functions
121 ****************************************************************************/
122
127 void UART1_IRQHandler(void)
128 {
129 uint8_t ch;
130 uint32_t IntStatus = Chip_UART_GetIntStatus(LPC_USART1);
131
132 if (IntStatus & RXRDY_INT) {
133 if(receiveCompleted == false) {
134 Chip_UART_ReceiveByte(LPC_USART1, &RxBuf1[RxBufCnt1++]);
135 if (RxBufCnt1 == BUFFER_SIZE) {
136 Chip_UART_IntEnable(LPC_USART1, RXRDY_INT, DISABLE);
137 receiveCompleted = true;
138 RxBufCnt1 = 0;
139 }
140 }
141 else {
142 Chip_UART_ReceiveByte(LPC_USART1, &ch);
143 }
144 }
145
146 if (IntStatus & TXRDY_INT) {
147 if (sendCompleted == false) {
148 Chip_UART_SendByte(LPC_USART1, RxBuf1[TxBufCnt1++]);
149 if (TxBufCnt1 == BUFFER_SIZE) {
150 Chip_UART_IntEnable(LPC_USART1, TXRDY_INT, DISABLE);
151 sendCompleted = true;
152 TxBufCnt1 = 0;
153 }
154 }
155 else {
156 return;
157 }
158
159 }
160 }
161
167 int main(void)
168 {
169 uint32_t i;
170 uint8_t repeat_num = UART_TEST_REPEAT_NUMBER;
171 volatile int j = 1;
172 uint8_t ch = 0;
173 uint32_t IntStatus;
174
175 /* Generic Initialization */
176 Board_Init();
177
178 Board_LED_Set(0, false);
179
180 /* Disable UART1 IRQ */
181 NVIC_DisableIRQ(UART1_IRQn);
182
183 /* Initialize the UARTs */
184 App_UART_Init(LPC_USART0);
185 App_UART_Init(LPC_USART1);
186
187 /* Custom Initialization */
188 Chip_UART_IntEnable(LPC_USART1, RXRDY_INT, DISABLE);
189 Chip_UART_IntEnable(LPC_USART1, TXRDY_INT, DISABLE);
190 NVIC_EnableIRQ(UART1_IRQn);
191
192 /* Data transfer loop */
193 while (repeat_num--) {
194
195 bufferInit(repeat_num);
196
197 /* Sending from UART0 (polling mode) to UART1 (interrupt mode) */
198 receiveCompleted = false;
199 Chip_UART_IntEnable(LPC_USART1, RXRDY_INT, ENABLE);
200 for (i = 0; i < BUFFER_SIZE; i++) {
201 while (Chip_UART_SendByte(LPC_USART0, TxBuf0[i]) != SUCCESS) {}
202 }
203 while (!receiveCompleted) {}
204
205 /* Clear Rx FIFO */
206 Chip_UART_ReceiveByte(LPC_USART0, &ch);
207
208 /* Sending from UART1 (interrupt mode) to UART0 (polling mode) */
209 sendCompleted = false;
210 Chip_UART_IntEnable(LPC_USART1, TXRDY_INT, ENABLE);
211 for (i = 0; i < BUFFER_SIZE; i++) {
212 while (Chip_UART_ReceiveByte(LPC_USART0, &RxBuf0[i]) != SUCCESS) {}
213 }
214 while (!sendCompleted) {}
215
216 bufferCheck();
217 }
218
219 NVIC_DisableIRQ(UART1_IRQn);
220
221 /* Test OK - Turn on Red LED */
222 Board_LED_Set(0, true);
223
224 /* Should not return */
225 while (j) {}
226
227 return 0;
228 }
229