| 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 interrupt example with ring buffers
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 "chip.h"
33 #include "board.h"
34 #include "string.h"
35
65 /*****************************************************************************
66 * Private types/enumerations/variables
67 ****************************************************************************/
68
69 /* Transmit and receive ring buffers */
70 STATIC RINGBUFF_T txring, rxring;
71
72 /* Ring buffer size */
73 #define UART_RB_SIZE 64
74
75 /* Transmit and receive buffers */
76 static uint8_t rxbuff[UART_RB_SIZE], txbuff[UART_RB_SIZE];
77
78 const char inst1[] = "LPC11xx UART example using ring buffers\r\n";
79 const char inst2[] = "Press a key to echo it back or ESC to quit\r\n";
80
81 /*****************************************************************************
82 * Public types/enumerations/variables
83 ****************************************************************************/
84
85 /*****************************************************************************
86 * Private functions
87 ****************************************************************************/
88
89 /*****************************************************************************
90 * Public functions
91 ****************************************************************************/
92
97 void UART_IRQHandler(void)
98 {
99 /* Want to handle any errors? Do it here. */
100
101 /* Use default ring buffer handler. Override this with your own
102 code if you need more capability. */
103 Chip_UART_IRQRBHandler(LPC_USART, &rxring, &txring);
104 }
105
110 int main(void)
111 {
112 uint8_t key;
113
114 Board_Init();
115 Board_UART_Init();
116
117 /* Setup UART for 115.2K8N1 */
118 Chip_UART_Init(LPC_USART);
119 Chip_UART_SetBaud(LPC_USART, 115200);
120 Chip_UART_ConfigData(LPC_USART, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT));
121 Chip_UART_SetupFIFOS(LPC_USART, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2));
122 Chip_UART_TXEnable(LPC_USART);
123
124 /* Before using the ring buffers, initialize them using the ring
125 buffer init function */
126 RingBuffer_Init(&rxring, rxbuff, 1, UART_RB_SIZE);
127 RingBuffer_Init(&txring, txbuff, 1, UART_RB_SIZE);
128
129 /* Enable receive data and line status interrupt */
130 Chip_UART_IntEnable(LPC_USART, (UART_IER_RBRINT | UART_IER_RLSINT));
131
132 /* preemption = 1, sub-priority = 1 */
133 NVIC_SetPriority(UART0_IRQn, 1);
134 NVIC_EnableIRQ(UART0_IRQn);
135
136 /* Initial message sent using blocking method to prevent ring
137 buffer overflow */
138 Chip_UART_SendBlocking(LPC_USART, inst1, sizeof(inst1) - 1);
139 Chip_UART_SendRB(LPC_USART, &txring, inst2, sizeof(inst2) - 1);
140
141 /* Poll the receive ring buffer for the ESC (ASCII 27) key */
142 key = 0;
143 while (key != 27) {
144 if (Chip_UART_ReadRB(LPC_USART, &rxring, &key, 1) > 0) {
145 /* Wrap value back around */
146 Chip_UART_SendRB(LPC_USART, &txring, (const uint8_t *) &key, 1);
147 }
148 }
149
150 /* DeInitialize UART0 peripheral */
151 NVIC_DisableIRQ(UART0_IRQn);
152 Chip_UART_DeInit(LPC_USART);
153
154 return 1;
155 }
156