| 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_rom.c
Go to the documentation of this file.
1 /*
2 * @brief Universal Asynchronous Receiver/Transmitter API in ROM (USART API ROM) 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
55 /*****************************************************************************
56 * Private types/enumerations/variables
57 ****************************************************************************/
58
59 #define APP_UART_RECEIVE_BUGGER_SIZE 9
60
61 static UART_HANDLE_T *uart;
62
63 static uint32_t umem[UART_ROM_MEM_SIZE];
64 static const char welcome[] = "LPC812 XPresso Board USART API ROM Example.\0";
65 static const char Polling_info[] = "UART Polling mode demo! Please type 'hello NXP' to continue.\0";
66 static const char Interrupt_info[] =
67 "\r\nUART Interrupt mode demo! Type a character to be echoed back or \'x\' to exit.\0";
68 static const char terminate[] = "\r\nDemo is terminated. Thanks for using.\0";
69
70 static char recv_buf[APP_UART_RECEIVE_BUGGER_SIZE];
71 static volatile uint8_t byte, test_stop = 0;
72 static volatile uint32_t tx_Done = 0;
73
74 /*****************************************************************************
75 * Public types/enumerations/variables
76 ****************************************************************************/
77
78 /*****************************************************************************
79 * Private functions
80 ****************************************************************************/
81
82 static void rx_callback(uint32_t err_code, uint32_t n);
83
84 static void tx_callback(uint32_t err_code, uint32_t n);
85
86 static void App_error_routine()
87 {
88 Board_LED_Set(0, true); /* Test fail */
89 __WFE();
90 }
91
92 static void App_Polling_Send(const char *send_data, uint32_t lenght)
93 {
94 UART_PARAM_T param;
95 param.buffer = (uint8_t *) send_data; /* the data to send */
96 param.size = lenght; /* size of the data */
97 param.transfer_mode = TX_MODE_SZERO_SEND_CRLF; /* send until zero-terminator of string + append CRLF */
98 param.driver_mode = DRIVER_MODE_POLLING; /* simple "polled" (blocking) transfer */
99 /* transmit the message */
100 if (LPC_UART_API->uart_put_line(uart, &param)) {
101 App_error_routine();
102 }
103 }
104
105 static void App_Polling_Receive(char *receive_buffer, uint32_t lenght)
106 {
107 UART_PARAM_T param;
108 param.buffer = (uint8_t *) receive_buffer; /* the buffer for receiving */
109 param.size = lenght; /* size of the buffer */
110 param.transfer_mode = RX_MODE_LF_RECVD; /* receive until get LF character of buffer is full */
111 param.driver_mode = DRIVER_MODE_POLLING; /* simple "polled" (blocking) transfer */
112 /* receive the message */
113 if (LPC_UART_API->uart_get_line(uart, &param)) {
114 App_error_routine();
115 }
116 }
117
118 static void App_Interrupt_Send(const char *send_data, uint32_t lenght)
119 {
120 UART_PARAM_T param;
121 param.buffer = (uint8_t *) send_data; /* the data to send */
122 param.size = lenght; /* size of the data */
123 param.transfer_mode = TX_MODE_SZERO_SEND_CRLF; /* send until zero-terminator of string + append CRLF */
124 param.driver_mode = DRIVER_MODE_INTERRUPT; /* Interrupt (non-blocking) transfer */
125 param.callback_func_pt = (UART_CALLBK_T) tx_callback; /* call back function when transmit is completed */
126 /* transmit the message */
127 if (LPC_UART_API->uart_put_line(uart, &param)) {
128 App_error_routine();
129 }
130 }
131
132 static void App_UART_API_Init()
133 {
134 uint32_t frg_mult;
135 /* Initialize the UARTs, Get the UART memory size needed */
136 volatile uint32_t mem = LPC_UART_API->uart_get_mem_size();
137 /* Configure the UART settings */
138 UART_CONFIG_T cfg = {0, /* U_PCLK frequency in Hz */
139 115200, /* Baud Rate in Hz */
140 1, /* 8N1 */
141 0, /* Asynchronous Mode */
142 NO_ERR_EN /* Enable No Errors */
143 };
144 cfg.sys_clk_in_hz = Chip_Clock_GetMainClockRate();
145 /* Perform a sanity check on the storage allocation */
146 if (UART_ROM_MEM_SIZE < (mem / sizeof(uint32_t))) {
147 while (1) {}
148 }
149
150
151
152 /* Setup the UART */
153 uart = LPC_UART_API->uart_setup((uint32_t) LPC_USART0, (uint8_t *) umem);
154
155 /* Check the API return value for a valid handle */
156 if (uart != NULL) {
157 /* initialize the UART with the configuration parameters */
158 frg_mult = LPC_UART_API->uart_init(uart, &cfg); /* API returns FRG MULT for cfg'd baud rate */
159 if (frg_mult) {
160 Chip_SYSCTL_SetUSARTFRGDivider(0xFF); /* value 0xFF should be always used */
161 Chip_SYSCTL_SetUSARTFRGMultiplier(frg_mult);
162 }
163 }
164 }
165
166 static void rx_callback(uint32_t err_code, uint32_t n)
167 {
168 App_Interrupt_Send( (char *) &byte, 1); /* echo the character back */
169 if (byte == 'x') {
170 test_stop = 1;
171 }
172 }
173
174 static void tx_callback(uint32_t err_code, uint32_t n)
175 {
176 tx_Done = 1;
177 }
178
179 /*****************************************************************************
180 * Public functions
181 ****************************************************************************/
182
187 void UART0_IRQHandler(void)
188 {
189 LPC_UART_API->uart_isr(uart);
190 }
191
197 int main(void)
198 {
199 volatile int k = 1;
200
201 UART_PARAM_T param;
202
203 Board_Init();
204
205 Board_UART_Init(LPC_USART0);
206
207 App_UART_API_Init();/* Initialize the UARTs 0 */
208
209 /* transmit the "welcome" message */
210 App_Polling_Send(welcome, sizeof(welcome));
211 App_Polling_Send(Polling_info, sizeof(Polling_info));
212 App_Polling_Receive(recv_buf, sizeof(recv_buf));
213 App_Polling_Send(recv_buf, sizeof(recv_buf));
214
215 /* Enable the IRQ for the UART */
216 NVIC_DisableIRQ(UART0_IRQn);
217 NVIC_ClearPendingIRQ(UART0_IRQn);
218 NVIC_EnableIRQ(UART0_IRQn);
219
220 App_Interrupt_Send(Interrupt_info, sizeof(Interrupt_info));
221 /* waiting for transmit is completed */
222 while (!tx_Done) {}
223
224 param.buffer = (uint8_t *) &byte;
225 param.size = sizeof(byte);
226 param.transfer_mode = RX_MODE_BUF_FULL;
227 param.driver_mode = DRIVER_MODE_INTERRUPT;
228 param.callback_func_pt = (UART_CALLBK_T) rx_callback;
229 while (!test_stop) {
230 LPC_UART_API->uart_get_line(uart, &param);
231 __WFE();
232 }
233
234 NVIC_DisableIRQ(UART0_IRQn);
235 App_Polling_Send(terminate, sizeof(terminate));
236
237 while(k);
238
239 return 0;
240 }
241