LPCOpen Platform for LPC112X microcontrollers  112X
LPCOpen Platform for the NXP LPC112X family of Microcontrollers
uart_112x.c
Go to the documentation of this file.
1 /*
2  * @brief LPC1125 UART chip driver
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 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /*****************************************************************************
39  * Public types/enumerations/variables
40  ****************************************************************************/
41 
42 /*****************************************************************************
43  * Private functions
44  ****************************************************************************/
45 
46 /*****************************************************************************
47  * Public functions
48  ****************************************************************************/
49 
50 /* Initializes the pUART peripheral */
52 {
55 
56  /* Enable FIFOs by default, reset them */
58 
59  /* Default 8N1, with DLAB disabled */
61 
62  /* Disable fractional divider */
63  pUART->FDR = 0x10;
64 }
65 
66 /* De-initializes the pUART peripheral */
68 {
70 }
71 
72 /* Transmit a byte array through the UART peripheral (non-blocking) */
73 int Chip_UART_Send(LPC_UART_T *pUART, const void *data, int numBytes)
74 {
75  int sent = 0;
76  uint8_t *p8 = (uint8_t *) data;
77 
78  /* Send until the transmit FIFO is full or out of bytes */
79  while ((sent < numBytes) &&
80  ((Chip_UART_ReadLineStatus(pUART) & UART_LSR_THRE) != 0)) {
81  Chip_UART_SendByte(pUART, *p8);
82  p8++;
83  sent++;
84  }
85 
86  return sent;
87 }
88 
89 /* Transmit a byte array through the UART peripheral (blocking) */
90 int Chip_UART_SendBlocking(LPC_UART_T *pUART, const void *data, int numBytes)
91 {
92  int pass, sent = 0;
93  uint8_t *p8 = (uint8_t *) data;
94 
95  while (numBytes > 0) {
96  pass = Chip_UART_Send(pUART, p8, numBytes);
97  numBytes -= pass;
98  sent += pass;
99  p8 += pass;
100  }
101 
102  return sent;
103 }
104 
105 /* Read data through the UART peripheral (non-blocking) */
106 int Chip_UART_Read(LPC_UART_T *pUART, void *data, int numBytes)
107 {
108  int readBytes = 0;
109  uint8_t *p8 = (uint8_t *) data;
110 
111  /* Send until the transmit FIFO is full or out of bytes */
112  while ((readBytes < numBytes) &&
113  ((Chip_UART_ReadLineStatus(pUART) & UART_LSR_RDR) != 0)) {
114  *p8 = Chip_UART_ReadByte(pUART);
115  p8++;
116  readBytes++;
117  }
118 
119  return readBytes;
120 }
121 
122 /* Read data through the UART peripheral (blocking) */
123 int Chip_UART_ReadBlocking(LPC_UART_T *pUART, void *data, int numBytes)
124 {
125  int pass, readBytes = 0;
126  uint8_t *p8 = (uint8_t *) data;
127 
128  while (readBytes < numBytes) {
129  pass = Chip_UART_Read(pUART, p8, numBytes);
130  numBytes -= pass;
131  readBytes += pass;
132  p8 += pass;
133  }
134 
135  return readBytes;
136 }
137 
138 /* Determines and sets best dividers to get a target bit rate */
139 uint32_t Chip_UART_SetBaud(LPC_UART_T *pUART, uint32_t baudrate)
140 {
141  uint32_t div, divh, divl, clkin;
142 
143  /* Determine UART clock in rate without FDR */
144  clkin = Chip_Clock_GetMainClockRate();
145  div = clkin / (baudrate * 16);
146 
147  /* High and low halves of the divider */
148  divh = div / 256;
149  divl = div - (divh * 256);
150 
152  Chip_UART_SetDivisorLatches(pUART, divl, divh);
154 
155  /* Fractional FDR alreadt setup for 1 in UART init */
156 
157  return clkin / div;
158 }
159 
160 /* UART receive-only interrupt handler for ring buffers */
162 {
163  /* New data will be ignored if data not popped in time */
164  while (Chip_UART_ReadLineStatus(pUART) & UART_LSR_RDR) {
165  uint8_t ch = Chip_UART_ReadByte(pUART);
166  RingBuffer_Insert(pRB, &ch);
167  }
168 }
169 
170 /* UART transmit-only interrupt handler for ring buffers */
172 {
173  uint8_t ch;
174 
175  /* Fill FIFO until full or until TX ring buffer is empty */
176  while ((Chip_UART_ReadLineStatus(pUART) & UART_LSR_THRE) != 0 &&
177  RingBuffer_Pop(pRB, &ch)) {
178  Chip_UART_SendByte(pUART, ch);
179  }
180 }
181 
182 /* Populate a transmit ring buffer and start UART transmit */
183 uint32_t Chip_UART_SendRB(LPC_UART_T *pUART, RINGBUFF_T *pRB, const void *data, int bytes)
184 {
185  uint32_t ret;
186  uint8_t *p8 = (uint8_t *) data;
187 
188  /* Don't let UART transmit ring buffer change in the UART IRQ handler */
190 
191  /* Move as much data as possible into transmit ring buffer */
192  ret = RingBuffer_InsertMult(pRB, p8, bytes);
193  Chip_UART_TXIntHandlerRB(pUART, pRB);
194 
195  /* Add additional data to transmit ring buffer if possible */
196  ret += RingBuffer_InsertMult(pRB, (p8 + ret), (bytes - ret));
197 
198  /* Enable UART transmit interrupt */
200 
201  return ret;
202 }
203 
204 /* Copy data from a receive ring buffer */
205 int Chip_UART_ReadRB(LPC_UART_T *pUART, RINGBUFF_T *pRB, void *data, int bytes)
206 {
207  (void) pUART;
208 
209  return RingBuffer_PopMult(pRB, (uint8_t *) data, bytes);
210 }
211 
212 /* UART receive/transmit interrupt handler for ring buffers */
214 {
215  /* Handle transmit interrupt if enabled */
216  if (pUART->IER & UART_IER_THREINT) {
217  Chip_UART_TXIntHandlerRB(pUART, pTXRB);
218 
219  /* Disable transmit interrupt if the ring buffer is empty */
220  if (RingBuffer_IsEmpty(pTXRB)) {
222  }
223  }
224 
225  /* Handle receive interrupt */
226  Chip_UART_RXIntHandlerRB(pUART, pRXRB);
227 }
228 
229 /* Determines and sets best dividers to get a target baud rate */
230 uint32_t Chip_UART_SetBaudFDR(LPC_UART_T *pUART, uint32_t baud)
231 {
232  uint32_t sdiv = 0, sm = 1, sd = 0;
233  uint32_t pclk, m, d;
234  uint32_t odiff = -1UL; /* old best diff */
235 
236  /* Get base clock for the corresponding UART */
238 
239  /* Loop through all possible fractional divider values */
240  for (m = 1; odiff && m < 16; m++) {
241  for (d = 0; d < m; d++) {
242  uint32_t diff, div;
243  uint64_t dval = (((uint64_t) pclk << 28) * m) / (baud * (m + d));
244 
245  /* Lower 32-bit of dval has diff */
246  diff = (uint32_t) dval;
247  /* Upper 32-bit of dval has div */
248  div = (uint32_t) (dval >> 32);
249 
250  /* Closer to next div */
251  if ((int)diff < 0) {
252  diff = -diff;
253  div ++;
254  }
255 
256  /* Check if new value is worse than old or out of range */
257  if (odiff < diff || !div || (div >> 16) || (div < 3 && d)) {
258  continue;
259  }
260 
261  /* Store the new better values */
262  sdiv = div;
263  sd = d;
264  sm = m;
265  odiff = diff;
266 
267  /* On perfect match, break loop */
268  if(!diff) {
269  break;
270  }
271  }
272  }
273 
274  /* Return 0 if a vaild divisor is not possible */
275  if (!sdiv) {
276  return 0;
277  }
278 
279  /* Update UART registers */
283 
284  /* Set best fractional divider */
285  pUART->FDR = (UART_FDR_MULVAL(sm) | UART_FDR_DIVADDVAL(sd));
286 
287  /* Return actual baud rate */
288  return ((pclk >> 4) * sm) / (sdiv * (sm + sd));
289 }