1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/****************************************************************************
* arch/arm/src/lpc313x/lpc313x_lowputc.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <debug.h>
#include <nuttx/arch.h>
#include "up_arch.h"
#include "up_internal.h"
#include "lpc313x_cgudrvr.h"
#include "lpc313x_uart.h"
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Is the UART enabled? */
#ifdef CONFIG_LPC313X_UART
# define HAVE_UART 1
/* Is it a serial console? */
# ifdef CONFIG_UART_SERIAL_CONSOLE
# define HAVE_CONSOLE 1
/* Is initialization performed by up_earlyserialinit()? Or is UART
* initialization suppressed?
*/
# if defined(CONFIG_USE_EARLYSERIALINIT) || defined(CONFIG_SUPPRESS_LPC313X_UART_CONFIG)
# undef NEED_LOWSETUP
# else
# define NEED_LOWSETUP 1
# endif
# else
# undef HAVE_CONSOLE
# undef NEED_LOWSETUP
# endif
#else
# undef HAVE_UART
# undef HAVE_CONSOLE
# undef NEED_LOWSETUP
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Variables
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: up_waittxready
****************************************************************************/
#ifdef HAVE_CONSOLE
static inline void up_waittxready(void)
{
int tmp;
/* Limit how long we will wait for the TX available condition */
for (tmp = 1000 ; tmp > 0 ; tmp--)
{
/* Check if the tranmitter holding register (THR) is empty */
if ((getreg32(LPC313X_UART_LSR) & UART_LSR_THRE) != 0)
{
/* The THR is empty, return */
break;
}
}
}
#endif
/****************************************************************************
* Name: up_configbaud
****************************************************************************/
#ifdef NEED_LOWSETUP
static inline void up_configbaud(void)
{
/* In a buckled-up, embedded system, there is no reason to constantly
* calculate the following. The calculation can be skipped if the
* MULVAL, DIVADDVAL, and DIVISOR values are provided in the configuration
* file.
*/
#ifndef CONFIG_LPC313X_UART_MULVAL
uint32_t qtrclk;
uint32_t regval;
/* Test values calculated for every multiplier/divisor combination */
uint32_t tdiv;
uint32_t terr;
int tmulval;
int tdivaddval;
/* Optimal multiplier/divider values */
uint32_t div = 0;
uint32_t err = 100000;
int mulval = 1;
int divaddval = 0;
/* Baud is generated using FDR and DLL-DLM registers
*
* baud = clock * (mulval/(mulval+divaddval) / (16 * div)
*
* Or
*
* div = (clock/16) * (mulval/(mulval+divaddval) / baud
*
* Where mulval = Fractional divider multiplier
* divaddval = Fractional divider pre-scale div
* div = DLL-DLM divisor
*/
/* Get UART block clock divided by 16 */
qtrclk = lpc313x_clkfreq(CLKID_UARTUCLK, DOMAINID_UART) >> 4;
/* Try every valid multiplier, tmulval (or until a perfect
* match is found).
*/
for (tmulval = 1 ; tmulval <= 15 && err > 0; tmulval++)
{
/* Try every valid pre-scale div, tdivaddval (or until a perfect
* match is found).
*/
for (tdivaddval = 0 ; tdivaddval <= 15 && err > 0; tdivaddval++)
{
/* Calculate the divisor with these fractional divider settings */
uint32_t tmp = (tmulval * qtrclk) / ((tmulval + tdivaddval));
tdiv = (tmp + (CONFIG_UART_BAUD>>1)) / CONFIG_UART_BAUD;
/* Check if this candidate divisor is within a valid range */
if (tdiv > 2 && tdiv < 0x10000)
{
/* Calculate the actual baud and the error */
uint32_t actualbaud = tmp / tdiv;
if (actualbaud <= CONFIG_UART_BAUD)
{
terr = CONFIG_UART_BAUD - actualbaud;
}
else
{
terr = actualbaud - CONFIG_UART_BAUD;
}
/* Is this the smallest error we have encountered? */
if (terr < err)
{
/* Yes, save these settings as the new, candidate optimal settings */
mulval = tmulval ;
divaddval = tdivaddval;
div = tdiv;
err = terr;
}
}
}
}
/* Set the Divisor Latch Access Bit (DLAB) to enable DLL/DLM access */
regval = getreg32(LPC313X_UART_LCR);
regval |= UART_LCR_DLAB;
putreg32(regval, LPC313X_UART_LCR);
/* Configure the MS and LS DLAB registers */
putreg32(div & UART_DLL_MASK, LPC313X_UART_DLL);
putreg32((div >> 8) & UART_DLL_MASK, LPC313X_UART_DLM);
regval &= ~UART_LCR_DLAB;
putreg32(regval, LPC313X_UART_LCR);
/* Configure the Fractional Divider Register (FDR) */
putreg32((mulval << UART_FDR_MULVAL_SHIFT) |
(divaddval << UART_FDR_DIVADDVAL_SHIFT),
LPC313X_UART_FDR);
#else
/* Set the Divisor Latch Access Bit (DLAB) to enable DLL/DLM access */
regval = getreg32(LPC313X_UART_LCR);
regval |= UART_LCR_DLAB;
putreg32(regval, LPC313X_UART_LCR);
/* Configure the MS and LS DLAB registers */
putreg32(CONFIG_LPC313X_UART_DIVISOR & UART_DLL_MASK, LPC313X_UART_DLL);
putreg32((CONFIG_LPC313X_UART_DIVISOR >> 8) & UART_DLL_MASK, LPC313X_UART_DLM);
regval &= ~UART_LCR_DLAB;
putreg32(regval, LPC313X_UART_LCR);
/* Configure the Fractional Divider Register (FDR) */
putreg32((CONFIG_LPC313X_UART_MULVAL << UART_FDR_MULVAL_SHIFT) |
(CONFIG_LPC313X_UART_DIVADDVAL << UART_FDR_DIVADDVAL_SHIFT),
LPC313X_UART_FDR);
#endif
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: lpc313x_lowsetup
*
* Description:
* Called early in up_boot. Performs chip-common low level initialization.
*
****************************************************************************/
void lpc313x_lowsetup(void)
{
#ifdef NEED_LOWSETUP
uint32_t regval;
/* Enable UART system clock */
lpc313x_enableclock(CLKID_UARTAPBCLK);
lpc313x_enableclock(CLKID_UARTUCLK);
/* Clear fifos */
putreg32((UART_FCR_RXFIFORST|UART_FCR_TXFIFORST), LPC313X_UART_FCR);
/* Set trigger */
putreg32((UART_FCR_FIFOENABLE|UART_FCR_RXTRIGLEVEL_16), LPC313X_UART_FCR);
/* Set up the LCR */
regval = 0;
#if CONFIG_UART_BITS == 5
regval |= UART_LCR_WDLENSEL_5BITS;
#elif CONFIG_UART_BITS == 6
regval |= UART_LCR_WDLENSEL_6BITS;
#elif CONFIG_UART_BITS == 7
regval |= UART_LCR_WDLENSEL_7BITS;
#else
regval |= UART_LCR_WDLENSEL_8BITS;
#endif
#if CONFIG_UART_2STOP > 0
regval |= UART_LCR_NSTOPBITS;
#endif
#if CONFIG_UART_PARITY == 1
regval |= UART_LCR_PAREN;
#elif CONFIG_UART_PARITY == 2
regval |= (UART_LCR_PAREVEN|UART_LCR_PAREN);
#endif
putreg32(regval, LPC313X_UART_LCR);
/* Set the BAUD divisor */
up_configbaud();
/* Configure the FIFOs */
putreg32((UART_FCR_RXTRIGLEVEL_16|UART_FCR_TXFIFORST|
UART_FCR_RXFIFORST|UART_FCR_FIFOENABLE),
LPC313X_UART_FCR);
/* The NuttX serial driver waits for the first THRE interrrupt before
* sending serial data... However, it appears that the lpc313x hardware
* does not generate that interrupt until a transition from not-empty
* to empty. So, the current kludge here is to send one NULL at
* startup to kick things off.
*/
putreg32('\0', LPC313X_UART_THR);
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_lowputc
*
* Description:
* Provide priority, low-level access to support OS debug writes
*
****************************************************************************/
void up_lowputc(char ch)
{
#ifdef HAVE_CONSOLE
up_waittxready();
putreg32((uint32_t)ch, LPC313X_UART_THR);
#endif
}
|