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
|
/**************************************************************************
* arch/arm/src/str71x/str71x_lowputc.c
*
* Copyright (C) 2008 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 "up_internal.h"
#include "up_arch.h"
#include "chip.h"
/**************************************************************************
* Private Definitions
**************************************************************************/
/* Configuration **********************************************************/
/* Is there a UART enabled? */
#if defined(CONFIG_STR71X_UART0) || defined(CONFIG_STR71X_UART1) || \
defined(CONFIG_STR71X_UART2) || defined(CONFIG_STR71X_UART3)
# define HAVE_UART 1
/* Is there a serial console? */
# if defined(CONFIG_UART0_SERIAL_CONSOLE) || defined(CONFIG_UART1_SERIAL_CONSOLE) ||\
defined(CONFIG_UART2_SERIAL_CONSOLE) || defined(CONFIG_UART3_SERIAL_CONSOLE)
# define HAVE_CONSOLE
# else
# undef HAVE_CONSOLE
# endif
#else
# undef HAVE_UART
# undef HAVE_CONSOLE
#endif
/* GPIO0 UART configuration bits */
#if CONFIG_STR71X_UART0
# define STR71X_UART0_GPIO0_MASK (0x0300) /* P0.8->U0.TX, B0.9->U0.RX */
# define STR71X_UART0_GPIO0_PC0BITS (0x0300)
# define STR71X_UART0_GPIO0_PC1BITS (0x0200)
# define STR71X_UART0_GPIO0_PC2BITS (0x0200)
#else
# define STR71X_UART0_GPIO0_MASK (0)
# define STR71X_UART0_GPIO0_PC0BITS (0)
# define STR71X_UART0_GPIO0_PC1BITS (0)
# define STR71X_UART0_GPIO0_PC2BITS (0)
#endif
#if CONFIG_STR71X_UART0
# define STR71X_UART1_GPIO0_MASK (0x0c00) /* P0,10->U1.RX, P0.11->U1.TX */
# define STR71X_UART1_GPIO0_PC0BITS (0x0c00)
# define STR71X_UART1_GPIO0_PC1BITS (0x0800)
# define STR71X_UART1_GPIO0_PC2BITS (0x0800)
#else
# define STR71X_UART1_GPIO0_MASK (0)
# define STR71X_UART1_GPIO0_PC0BITS (0)
# define STR71X_UART1_GPIO0_PC1BITS (0)
# define STR71X_UART1_GPIO0_PC2BITS (0)
#endif
#if CONFIG_STR71X_UART0
# define STR71X_UART2_GPIO0_MASK (0x6000) /* P0.13->U2.RX, P0.14>U2.TX */
# define STR71X_UART2_GPIO0_PC0BITS (0x6000)
# define STR71X_UART2_GPIO0_PC1BITS (0x4000)
# define STR71X_UART2_GPIO0_PC2BITS (0x4000)
#else
# define STR71X_UART2_GPIO0_MASK (0)
# define STR71X_UART2_GPIO0_PC0BITS (0)
# define STR71X_UART2_GPIO0_PC1BITS (0)
# define STR71X_UART2_GPIO0_PC2BITS (0)
#endif
#if CONFIG_STR71X_UART0
# define STR71X_UART3_GPIO0_MASK (0x0003) /* P0.0->U3.TX, P0.1->U3.RX */
# define STR71X_UART3_GPIO0_PC0BITS (0x0003)
# define STR71X_UART3_GPIO0_PC1BITS (0x0001)
# define STR71X_UART3_GPIO0_PC2BITS (0x0001)
#else
# define STR71X_UART3_GPIO0_MASK (0)
# define STR71X_UART3_GPIO0_PC0BITS (0)
# define STR71X_UART3_GPIO0_PC1BITS (0)
# define STR71X_UART3_GPIO0_PC2BITS (0)
#endif
#define STR71X_UART_GPIO0_MASK (STR71X_UART0_GPIO0_MASK |STR71X_UART1_GPIO0_MASK|\
STR71X_UART2_GPIO0_MASK |STR71X_UART3_GPIO0_MASK)
#define STR71X_UART_GPIO0_PC0BITS (STR71X_UART0_GPIO0_PC0BITS|STR71X_UART1_GPIO0_PC0BITS|\
STR71X_UART2_GPIO0_PC0BITS|STR71X_UART3_GPIO0_PC0BITS)
#define STR71X_UART_GPIO0_PC1BITS (STR71X_UART0_GPIO0_PC0BITS|STR71X_UART1_GPIO0_PC0BITS|\
STR71X_UART2_GPIO0_PC0BITS|STR71X_UART3_GPIO0_PC0BITS)
#define STR71X_UART_GPIO0_PC2BITS (STR71X_UART0_GPIO0_PC0BITS|STR71X_UART1_GPIO0_PC0BITS|\
STR71X_UART2_GPIO0_PC0BITS|STR71X_UART3_GPIO0_PC0BITS)
/* Select UART parameters for the selected console */
#if defined(CONFIG_UART0_SERIAL_CONSOLE)
# define STR71X_UART_BASE STR71X_UART0_BASE
# define STR71X_UART_BAUD CONFIG_UART0_BAUD
# define STR71X_UART_BITS CONFIG_UART0_BITS
# define STR71X_UART_PARITY CONFIG_UART0_PARITY
# define STR71X_UART_2STOP CONFIG_UART0_2STOP
#elif defined(CONFIG_UART1_SERIAL_CONSOLE)
# define STR71X_UART_BASE STR71X_UART1_BASE
# define STR71X_UART_BAUD CONFIG_UART1_BAUD
# define STR71X_UART_BITS CONFIG_UART1_BITS
# define STR71X_UART_PARITY CONFIG_UART1_PARITY
# define STR71X_UART_2STOP CONFIG_UART1_2STOP
#elif defined(CONFIG_UART2_SERIAL_CONSOLE)
# define STR71X_UART_BASE STR71X_UART2_BASE
# define STR71X_UART_BAUD CONFIG_UART2_BAUD
# define STR71X_UART_BITS CONFIG_UART2_BITS
# define STR71X_UART_PARITY CONFIG_UART2_PARITY
# define STR71X_UART_2STOP CONFIG_UART2_2STOP
#elif defined(CONFIG_UART3_SERIAL_CONSOLE)
# define STR71X_UART_BASE STR71X_UART3_BASE
# define STR71X_UART_BAUD CONFIG_UART3_BAUD
# define STR71X_UART_BITS CONFIG_UART3_BITS
# define STR71X_UART_PARITY CONFIG_UART3_PARITY
# define STR71X_UART_2STOP CONFIG_UART3_2STOP
#else
# error "No CONFIG_UARTn_SERIAL_CONSOLE Setting"
#endif
/* Get mode setting */
#if STR71X_UART_BITS == 7
# if STR71X_UART_PARITY == 0
# error "7-bits, no parity mode not supported"
# else
# define STR71X_UARTCR_MODE STR71X_UARTCR_MODE7BITP
# endif
#elif STR71X_UART_BITS == 8
# if STR71X_UART_PARITY == 0
# define STR71X_UARTCR_MODE STR71X_UARTCR_MODE8BIT
# else
# define STR71X_UARTCR_MODE STR71X_UARTCR_MODE8BITP
# endif
#elif STR71X_UART_BITS == 9
# if STR71X_UART_PARITY == 0
# define STR71X_UARTCR_MODE STR71X_UARTCR_MODE9BIT
# else
# error "9-bits with parity not supported"
# endif
#else
# error "Number of bits not supported"
#endif
#if STR71X_UART_PARITY == 0 || STR71X_UART_PARITY == 2
# define STR71X_UARTCR_PARITY (0)
#elif STR71X_UART_PARITY == 1
# define STR71X_UARTCR_PARITY STR71X_UARTCR_PARITYODD
#else
# error "Invalid parity selection"
#endif
#if STR71X_UART_2STOP != 0
# define STR71X_UARTCR_STOP STR71X_UARTCR_STOPBIT20
#else
# define STR71X_UARTCR_STOP STR71X_UARTCR_STOPBIT05
#endif
#define STR71X_UARTCR_VALUE (STR71X_UARTCR_MODE|STR71X_UARTCR_PARITY|STR71X_UARTCR_STOP|\
STR71X_UARTCR_RUN|STR71X_UARTCR_RXENABLE|STR71X_UARTCR_FIFOENABLE)
/* Calculate the value of PCLK1 from settings in board.h.
*
* Example:
* STR71X_RCCU_MAIN_OSC = 4MHz (not divided by 2)
* CLK2 = 4MHz
* PLL1OUT = 16 * CLK2 / 2 = 32MHz
* CLK3 = 32MHz
* RCLK = 32MHz
* PCLK1 = 32MHz / 1 = 32MHz
*/
#ifdef STR71X_PLL1IN_DIV2 /* Input may be divided by 2 */
# define CLK2 (STR71X_RCCU_MAIN_OSC/2) /* CLK2 is input to PLL1 */
#else
# define CLK2 STR71X_RCCU_MAIN_OSC /* CLK2 is input to PLL1 */
#endif
/* PLL1OUT derives from CLK2 */
#define PLL1OUT (STR71X_PLL1OUT_MUL * CLK2 / STR71X_PLL1OUT_DIV)
#define CLK3 PLL1OUT /* CLK3 hard coded to be PLL1OUT */
#define RCLK CLK3 /* RCLK hard coded to be CLK3 */
#define PCLK1 (RCLK / STR71X_APB1_DIV) /* PCLK1 derives from RCLK */
/* Calculate BAUD rate from PCLK1:
*
* Example:
* PCLK1 = 32MHz
* STR71X_UART_BAUD = 38,400
* UART_BAUDRATE_DIVISOR = 614,400
* UART_BAUDRATE = 52.583 (exact) or 52 truncated (1.1% error)
*/
#define UART_BAUDDIVISOR (16 * STR71X_UART_BAUD)
#define UART_BAUDRATE ((PCLK1 + (UART_BAUDDIVISOR/2) / UART_BAUDDIVISOR))
/**************************************************************************
* Private Types
**************************************************************************/
/**************************************************************************
* Private Function Prototypes
**************************************************************************/
/**************************************************************************
* Global Variables
**************************************************************************/
/**************************************************************************
* Private Variables
**************************************************************************/
/**************************************************************************
* Private Functions
**************************************************************************/
/**************************************************************************
* Public Functions
**************************************************************************/
/**************************************************************************
* Name: up_lowputc
*
* Description:
* Output one byte on the serial console
*
**************************************************************************/
void up_lowputc(char ch)
{
#ifdef HAVE_CONSOLE
/* Wait until the TX FIFO is not full */
while ((getreg16(STR71X_UART_SR(STR71X_UART_BASE)) & STR71X_UARTSR_TF) != 0);
/* Then send the character */
putreg16((uint16)ch, STR71X_UART_TXBUFR(STR71X_UART_BASE));
#endif
}
/**************************************************************************
* Name: up_lowsetup
*
* Description:
* This performs basic initialization of the UART used for the serial
* console. Its purpose is to get the console output availabe as soon
* as possible.
*
**************************************************************************/
void up_lowsetup(void)
{
#ifdef HAVE_CONSOLE
uint16 reg16;
/* Enable the selected console device */
/* Set the UART baud rate */
putreg16((uint16)UART_BAUDRATE, STR71X_UART_BR(STR71X_UART_BASE));
/* Configure the UART control registers */
putreg16(STR71X_UARTCR_VALUE, STR71X_UART_CR(STR71X_UART_BASE));
/* Clear FIFOs */
putreg16(0, STR71X_UART_TXRSTR(STR71X_UART_BASE));
putreg16(0, STR71X_UART_RXRSTR(STR71X_UART_BASE));
#endif
/* Configure GPIO0 pins to enable all UARTs in the configuration
* (the serial driver later depends on this configuration)
*/
#if HAVE_UART
reg16 = getreg16(STR71X_GPIO0_PC0);
reg16 &= STR71X_UART_GPIO0_MASK;
reg16 |= STR71X_UART_GPIO0_PC0BITS;
putreg16(reg16, STR71X_GPIO0_PC0);
reg16 = getreg16(STR71X_GPIO0_PC1);
reg16 &= STR71X_UART_GPIO0_MASK;
reg16 |= STR71X_UART_GPIO0_PC1BITS;
putreg16(reg16, STR71X_GPIO0_PC1);
reg16 = getreg16(STR71X_GPIO0_PC2);
reg16 &= STR71X_UART_GPIO0_MASK;
reg16 |= STR71X_UART_GPIO0_PC2BITS;
putreg16(reg16, STR71X_GPIO0_PC2);
#endif
}
|