1 /*----------------------------------------------------------------------------
3 * Purpose: serial port handling for LPC17xx
5 *----------------------------------------------------------------------------
6 * This software is supplied "AS IS" without any warranties, express,
7 * implied or statutory, including but not limited to the implied
8 * warranties of fitness for purpose, satisfactory quality and
9 * noninfringement. Keil extends you a royalty-free right to reproduce
10 * and distribute executable files created using this software for use
11 * on NXP Semiconductors LPC microcontroller devices only. Nothing else
12 * gives you the right to use this software.
14 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
15 *---------------------------------------------------------------------------*/
16 #include <board.h> // LPC17xx definitions
17 #include "lpc_types.h"
21 /*----------------------------------------------------------------------------
22 Defines for ring buffers
23 *---------------------------------------------------------------------------*/
24 #define SER_BUF_SIZE (128) // serial buffer in bytes (power 2)
25 #define SER_BUF_MASK (SER_BUF_SIZE-1ul) // buffer size mask
27 /* Buffer read / write macros */
28 #define SER_BUF_RESET(serBuf) (serBuf.rdIdx = serBuf.wrIdx = 0)
29 #define SER_BUF_WR(serBuf, dataIn) (serBuf.data[SER_BUF_MASK & serBuf.wrIdx++] = (dataIn))
30 #define SER_BUF_RD(serBuf) (serBuf.data[SER_BUF_MASK & serBuf.rdIdx++])
31 #define SER_BUF_EMPTY(serBuf) (serBuf.rdIdx == serBuf.wrIdx)
32 #define SER_BUF_FULL(serBuf) (serBuf.rdIdx == serBuf.wrIdx+1)
33 #define SER_BUF_COUNT(serBuf) (SER_BUF_MASK & (serBuf.wrIdx - serBuf.rdIdx))
36 typedef struct __SER_BUF_T {
37 unsigned char data[SER_BUF_SIZE];
42 unsigned long ser_txRestart; // NZ if TX restart is required
43 unsigned short ser_lineState; // ((msr << 8) | (lsr))
44 SER_BUF_T ser_out; // Serial data buffers
47 /*----------------------------------------------------------------------------
49 *---------------------------------------------------------------------------*/
50 void ser_OpenPort (char portNum) {
55 NVIC_DisableIRQ(UART0_IRQn);
56 PINCON->PINSEL0 &= ~0x000000F0;
57 PINCON->PINSEL0 |= 0x00000050; /* RxD0 is P0.3 and TxD0 is P0.2 */
62 NVIC_DisableIRQ(UART1_IRQn);
63 PINCON->PINSEL4 &= ~0x0000000F;
64 PINCON->PINSEL4 |= 0x0000000A; /* Enable RxD1 P2.1, TxD1 P2.0 */
69 /*----------------------------------------------------------------------------
71 *---------------------------------------------------------------------------*/
72 void ser_ClosePort (char portNum ) {
76 PINCON->PINSEL0 &= ~0x000000F0;
77 /* Disable the interrupt in the VIC and UART controllers */
79 NVIC_DisableIRQ(UART0_IRQn);
84 PINCON->PINSEL4 &= ~0x0000000F;
85 /* Disable the interrupt in the VIC and UART controllers */
87 NVIC_DisableIRQ(UART1_IRQn);
92 /*----------------------------------------------------------------------------
93 initialize the serial port
94 *---------------------------------------------------------------------------*/
95 void ser_InitPort0 (unsigned long baudrate, unsigned int databits,
96 unsigned int parity, unsigned int stopbits) {
98 unsigned char lcr_p, lcr_s, lcr_d;
100 unsigned int pclkdiv, pclk;
103 case 5: // 5 Data bits
106 case 6: // 6 Data bits
109 case 7: // 7 Data bits
112 case 8: // 8 Data bits
119 case 1: // 1,5 Stop bits
120 case 2: // 2 Stop bits
123 case 0: // 1 Stop bit
130 case 1: // Parity Odd
133 case 2: // Parity Even
136 case 3: // Parity Mark
139 case 4: // Parity Space
142 case 0: // Parity None
148 SER_BUF_RESET(ser_out); // reset out buffer
149 SER_BUF_RESET(ser_in); // reset in buffer
151 /* Bit 6~7 is for UART0 */
152 pclkdiv = (SC->PCLKSEL0 >> 6) & 0x03;
158 pclk = SystemFrequency/4;
161 pclk = SystemFrequency;
164 pclk = SystemFrequency/2;
167 pclk = SystemFrequency/8;
171 dll = (pclk/16)/baudrate ; /*baud rate */
172 UART0->FDR = 0; // Fractional divider not used
173 UART0->LCR = 0x80 | lcr_d | lcr_p | lcr_s; // Data bits, Parity, Stop bit
174 UART0->DLL = dll; // Baud Rate depending on PCLK
175 UART0->DLM = (dll >> 8); // High divisor latch
176 UART0->LCR = 0x00 | lcr_d | lcr_p | lcr_s; // DLAB = 0
177 UART0->IER = 0x03; // Enable TX/RX interrupts
179 UART0->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
180 ser_txRestart = 1; // TX fifo is empty
182 /* Enable the UART Interrupt */
183 NVIC_EnableIRQ(UART0_IRQn);
187 /*----------------------------------------------------------------------------
188 initialize the serial port
189 *---------------------------------------------------------------------------*/
190 void ser_InitPort1 (unsigned long baudrate, unsigned int databits,
191 unsigned int parity, unsigned int stopbits) {
193 unsigned char lcr_p, lcr_s, lcr_d;
195 unsigned int pclkdiv, pclk;
198 case 5: // 5 Data bits
201 case 6: // 6 Data bits
204 case 7: // 7 Data bits
207 case 8: // 8 Data bits
214 case 1: // 1,5 Stop bits
215 case 2: // 2 Stop bits
218 case 0: // 1 Stop bit
225 case 1: // Parity Odd
228 case 2: // Parity Even
231 case 3: // Parity Mark
234 case 4: // Parity Space
237 case 0: // Parity None
243 SER_BUF_RESET(ser_out); // reset out buffer
244 SER_BUF_RESET(ser_in); // reset in buffer
246 /* Bit 8,9 are for UART1 */
247 pclkdiv = (SC->PCLKSEL0 >> 8) & 0x03;
253 pclk = SystemFrequency/4;
256 pclk = SystemFrequency;
259 pclk = SystemFrequency/2;
262 pclk = SystemFrequency/8;
266 dll = (pclk/16)/baudrate ; /*baud rate */
267 UART1->FDR = 0; // Fractional divider not used
268 UART1->LCR = 0x80 | lcr_d | lcr_p | lcr_s; // Data bits, Parity, Stop bit
269 UART1->DLL = dll; // Baud Rate depending on PCLK
270 UART1->DLM = (dll >> 8); // High divisor latch
271 UART1->LCR = 0x00 | lcr_d | lcr_p | lcr_s; // DLAB = 0
272 UART1->IER = 0x03; // Enable TX/RX interrupts
274 UART1->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
275 ser_txRestart = 1; // TX fifo is empty
277 /* Enable the UART Interrupt */
278 NVIC_EnableIRQ(UART1_IRQn);
282 /*----------------------------------------------------------------------------
283 read data from serial port
284 *---------------------------------------------------------------------------*/
285 int ser_Read (char *buffer, const int *length) {
286 int bytesToRead, bytesRead;
288 /* Read *length bytes, block if *bytes are not avaialable */
289 bytesToRead = *length;
290 bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length);
291 bytesRead = bytesToRead;
293 while (bytesToRead--) {
294 while (SER_BUF_EMPTY(ser_in)); // Block until data is available if none
295 *buffer++ = SER_BUF_RD(ser_in);
300 /*----------------------------------------------------------------------------
301 write data to the serial port
302 *---------------------------------------------------------------------------*/
303 int ser_Write (char portNum, const char *buffer, int *length) {
304 int bytesToWrite, bytesWritten;
306 // Write *length bytes
307 bytesToWrite = *length;
308 bytesWritten = bytesToWrite;
310 while (!SER_BUF_EMPTY(ser_out)); // Block until space is available if none
311 while (bytesToWrite) {
312 SER_BUF_WR(ser_out, *buffer++); // Read Rx FIFO to buffer
320 UART0->THR = SER_BUF_RD(ser_out); // Write to the Tx Register
324 UART1->THR = SER_BUF_RD(ser_out); // Write to the Tx Register
328 return (bytesWritten);
331 /*----------------------------------------------------------------------------
332 check if character(s) are available at the serial interface
333 *---------------------------------------------------------------------------*/
334 void ser_AvailChar (int *availChar) {
336 *availChar = SER_BUF_COUNT(ser_in);
340 /*----------------------------------------------------------------------------
341 read the line state of the serial port
342 *---------------------------------------------------------------------------*/
343 void ser_LineState (unsigned short *lineState) {
345 *lineState = ser_lineState;
350 /*----------------------------------------------------------------------------
351 serial port 0 interrupt
352 *---------------------------------------------------------------------------*/
353 void UART0_IRQHandler(void)
355 volatile unsigned long iir;
359 if ((iir & 0x4) || (iir & 0xC)) { // RDA or CTI pending
360 while (UART0->LSR & 0x01) { // Rx FIFO is not empty
361 SER_BUF_WR(ser_in, UART0->RBR); // Read Rx FIFO to buffer
364 if ((iir & 0x2)) { // TXMIS pending
365 if (SER_BUF_COUNT(ser_out) != 0) {
366 UART0->THR = SER_BUF_RD(ser_out); // Write to the Tx FIFO
373 ser_lineState = UART0->LSR & 0x1E; // update linestate
377 /*----------------------------------------------------------------------------
378 serial port 1 interrupt
379 *---------------------------------------------------------------------------*/
380 void UART1_IRQHandler(void)
382 volatile unsigned long iir;
386 if ((iir & 0x4) || (iir & 0xC)) { // RDA or CTI pending
387 while (UART1->LSR & 0x01) { // Rx FIFO is not empty
388 SER_BUF_WR(ser_in, UART1->RBR); // Read Rx FIFO to buffer
391 if ((iir & 0x2)) { // TXMIS pending
392 if (SER_BUF_COUNT(ser_out) != 0) {
393 UART1->THR = SER_BUF_RD(ser_out); // Write to the Tx FIFO
400 ser_lineState = ((UART1->MSR<<8)|UART1->LSR) & 0xE01E; // update linestate