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