]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC23XX/serial_api.c
allow overriding of TARGET
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC23XX / serial_api.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // math.h required for floating point operations for baud rate calculation
17 #include "mbed_assert.h"
18 #include <math.h>
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "serial_api.h"
23 #include "cmsis.h"
24 #include "pinmap.h"
25
26 /******************************************************************************
27  * INITIALIZATION
28  ******************************************************************************/
29 #define UART_NUM    4
30
31 static const PinMap PinMap_UART_TX[] = {
32     {P0_0,  UART_3, 2},
33     {P0_2,  UART_0, 1},
34     {P0_10, UART_2, 1},
35     {P0_15, UART_1, 1},
36     {P0_25, UART_3, 3},
37     {P2_0 , UART_1, 2},
38     {P2_8 , UART_2, 2},
39     {P4_28, UART_3, 3},
40     {NC   , NC    , 0}
41 };
42
43 static const PinMap PinMap_UART_RX[] = {
44     {P0_1 , UART_3, 2},
45     {P0_3 , UART_0, 1},
46     {P0_11, UART_2, 1},
47     {P0_16, UART_1, 1},
48     {P0_26, UART_3, 3},
49     {P2_1 , UART_1, 2},
50     {P2_9 , UART_2, 2},
51     {P4_29, UART_3, 3},
52     {NC   , NC    , 0}
53 };
54
55 static uint32_t serial_irq_ids[UART_NUM] = {0};
56 static uart_irq_handler irq_handler;
57
58 int stdio_uart_inited = 0;
59 serial_t stdio_uart;
60
61 void serial_init(serial_t *obj, PinName tx, PinName rx) {
62     int is_stdio_uart = 0;
63     
64     // determine the UART to use
65     UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
66     UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
67     UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
68     MBED_ASSERT((int)uart != NC);
69     
70     obj->uart = (LPC_UART_TypeDef *)uart;
71     // enable power
72     switch (uart) {
73         case UART_0: LPC_SC->PCONP |= 1 <<  3; break;
74         case UART_1: LPC_SC->PCONP |= 1 <<  4; break;
75         case UART_2: LPC_SC->PCONP |= 1 << 24; break;
76         case UART_3: LPC_SC->PCONP |= 1 << 25; break;
77     }
78     
79     // enable fifos and default rx trigger level
80     obj->uart->FCR = 1 << 0  // FIFO Enable - 0 = Disables, 1 = Enabled
81                    | 0 << 1  // Rx Fifo Reset
82                    | 0 << 2  // Tx Fifo Reset
83                    | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars
84
85     // disable irqs
86     obj->uart->IER = 0 << 0  // Rx Data available irq enable
87                    | 0 << 1  // Tx Fifo empty irq enable
88                    | 0 << 2; // Rx Line Status irq enable
89     
90     // set default baud rate and format
91     serial_baud  (obj, 9600);
92     serial_format(obj, 8, ParityNone, 1);
93     
94     // pinout the chosen uart
95     pinmap_pinout(tx, PinMap_UART_TX);
96     pinmap_pinout(rx, PinMap_UART_RX);
97     
98     // set rx/tx pins in PullUp mode
99     if (tx != NC) {
100         pin_mode(tx, PullUp);
101     }
102     if (rx != NC) {
103         pin_mode(rx, PullUp);
104     }
105     
106     switch (uart) {
107         case UART_0: obj->index = 0; break;
108         case UART_1: obj->index = 1; break;
109         case UART_2: obj->index = 2; break;
110         case UART_3: obj->index = 3; break;
111     }
112     
113     is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
114     
115     if (is_stdio_uart) {
116         stdio_uart_inited = 1;
117         memcpy(&stdio_uart, obj, sizeof(serial_t));
118     }
119 }
120
121 void serial_free(serial_t *obj) {
122     serial_irq_ids[obj->index] = 0;
123 }
124
125 // serial_baud
126 // set the baud rate, taking in to account the current SystemFrequency
127 void serial_baud(serial_t *obj, int baudrate) {
128     MBED_ASSERT((int)obj->uart <= UART_3);
129     // The LPC2300 and LPC1700 have a divider and a fractional divider to control the
130     // baud rate. The formula is:
131     //
132     // Baudrate = (1 / PCLK) * 16 * DL * (1 + DivAddVal / MulVal)
133     //   where:
134     //     1 < MulVal <= 15
135     //     0 <= DivAddVal < 14
136     //     DivAddVal < MulVal
137     //
138     // set pclk to /1
139     switch ((int)obj->uart) {
140         case UART_0: LPC_SC->PCLKSEL0 &= ~(0x3 <<  6); LPC_SC->PCLKSEL0 |= (0x1 <<  6); break;
141         case UART_1: LPC_SC->PCLKSEL0 &= ~(0x3 <<  8); LPC_SC->PCLKSEL0 |= (0x1 <<  8); break;
142         case UART_2: LPC_SC->PCLKSEL1 &= ~(0x3 << 16); LPC_SC->PCLKSEL1 |= (0x1 << 16); break;
143         case UART_3: LPC_SC->PCLKSEL1 &= ~(0x3 << 18); LPC_SC->PCLKSEL1 |= (0x1 << 18); break;
144         default: break;
145     }
146     
147     uint32_t PCLK = SystemCoreClock;
148     
149     // First we check to see if the basic divide with no DivAddVal/MulVal
150     // ratio gives us an integer result. If it does, we set DivAddVal = 0,
151     // MulVal = 1. Otherwise, we search the valid ratio value range to find
152     // the closest match. This could be more elegant, using search methods
153     // and/or lookup tables, but the brute force method is not that much
154     // slower, and is more maintainable.
155     uint16_t DL = PCLK / (16 * baudrate);
156
157     uint8_t DivAddVal = 0;
158     uint8_t MulVal = 1;
159     int hit = 0;
160     uint16_t dlv;
161     uint8_t mv, dav;
162     if ((PCLK % (16 * baudrate)) != 0) {     // Checking for zero remainder
163         int err_best = baudrate, b;
164         for (mv = 1; mv < 16 && !hit; mv++)
165         {
166             for (dav = 0; dav < mv; dav++)
167             {
168                 // baudrate = PCLK / (16 * dlv * (1 + (DivAdd / Mul))
169                 // solving for dlv, we get dlv = mul * PCLK / (16 * baudrate * (divadd + mul))
170                 // mul has 4 bits, PCLK has 27 so we have 1 bit headroom which can be used for rounding
171                 // for many values of mul and PCLK we have 2 or more bits of headroom which can be used to improve precision
172                 // note: X / 32 doesn't round correctly. Instead, we use ((X / 16) + 1) / 2 for correct rounding
173
174                 if ((mv * PCLK * 2) & 0x80000000) // 1 bit headroom
175                     dlv = ((((2 * mv * PCLK) / (baudrate * (dav + mv))) / 16) + 1) / 2;
176                 else // 2 bits headroom, use more precision
177                     dlv = ((((4 * mv * PCLK) / (baudrate * (dav + mv))) / 32) + 1) / 2;
178
179                 // datasheet says if DLL==DLM==0, then 1 is used instead since divide by zero is ungood
180                 if (dlv == 0)
181                     dlv = 1;
182
183                 // datasheet says if dav > 0 then DL must be >= 2
184                 if ((dav > 0) && (dlv < 2))
185                     dlv = 2;
186
187                 // integer rearrangement of the baudrate equation (with rounding)
188                 b = ((PCLK * mv / (dlv * (dav + mv) * 8)) + 1) / 2;
189
190                 // check to see how we went
191                 b = abs(b - baudrate);
192                 if (b < err_best)
193                 {
194                     err_best  = b;
195
196                     DL        = dlv;
197                     MulVal    = mv;
198                     DivAddVal = dav;
199
200                     if (b == baudrate)
201                     {
202                         hit = 1;
203                         break;
204                     }
205                 }
206             }
207         }
208     }
209     
210     // set LCR[DLAB] to enable writing to divider registers
211     obj->uart->LCR |= (1 << 7);
212     
213     // set divider values
214     obj->uart->DLM = (DL >> 8) & 0xFF;
215     obj->uart->DLL = (DL >> 0) & 0xFF;
216     obj->uart->FDR = (uint32_t) DivAddVal << 0
217                    | (uint32_t) MulVal    << 4;
218     
219     // clear LCR[DLAB]
220     obj->uart->LCR &= ~(1 << 7);
221 }
222
223 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
224     MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
225     MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
226     MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) ||
227            (parity == ParityForced1) || (parity == ParityForced0));
228
229     stop_bits -= 1;
230     data_bits -= 5;
231
232     int parity_enable = 0, parity_select = 0;
233     switch (parity) {
234         case ParityNone: parity_enable = 0; parity_select = 0; break;
235         case ParityOdd : parity_enable = 1; parity_select = 0; break;
236         case ParityEven: parity_enable = 1; parity_select = 1; break;
237         case ParityForced1: parity_enable = 1; parity_select = 2; break;
238         case ParityForced0: parity_enable = 1; parity_select = 3; break;
239         default:
240             break;
241     }
242     
243     obj->uart->LCR = data_bits            << 0
244                    | stop_bits            << 2
245                    | parity_enable        << 3
246                    | parity_select        << 4;
247 }
248
249 /******************************************************************************
250  * INTERRUPTS HANDLING
251  ******************************************************************************/
252 static inline void uart_irq(uint32_t iir, uint32_t index) {
253     // [Chapter 14] LPC17xx UART0/2/3: UARTn Interrupt Handling
254     SerialIrq irq_type;
255     switch (iir) {
256         case 1: irq_type = TxIrq; break;
257         case 2: irq_type = RxIrq; break;
258         default: return;
259     }
260     
261     if (serial_irq_ids[index] != 0)
262         irq_handler(serial_irq_ids[index], irq_type);
263 }
264
265 void uart0_irq() {uart_irq((LPC_UART0->IIR >> 1) & 0x7, 0);}
266 void uart1_irq() {uart_irq((LPC_UART1->IIR >> 1) & 0x7, 1);}
267 void uart2_irq() {uart_irq((LPC_UART2->IIR >> 1) & 0x7, 2);}
268 void uart3_irq() {uart_irq((LPC_UART3->IIR >> 1) & 0x7, 3);}
269
270 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
271     irq_handler = handler;
272     serial_irq_ids[obj->index] = id;
273 }
274
275 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
276     IRQn_Type irq_n = (IRQn_Type)0;
277     uint32_t vector = 0;
278     switch ((int)obj->uart) {
279         case UART_0: irq_n=UART0_IRQn; vector = (uint32_t)&uart0_irq; break;
280         case UART_1: irq_n=UART1_IRQn; vector = (uint32_t)&uart1_irq; break;
281         case UART_2: irq_n=UART2_IRQn; vector = (uint32_t)&uart2_irq; break;
282         case UART_3: irq_n=UART3_IRQn; vector = (uint32_t)&uart3_irq; break;
283     }
284     
285     if (enable) {
286         obj->uart->IER |= 1 << irq;
287         NVIC_SetVector(irq_n, vector);
288         NVIC_EnableIRQ(irq_n);
289     } else { // disable
290         int all_disabled = 0;
291         SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq);
292         obj->uart->IER &= ~(1 << irq);
293         all_disabled = (obj->uart->IER & (1 << other_irq)) == 0;
294         if (all_disabled)
295             NVIC_DisableIRQ(irq_n);
296     }
297 }
298
299 /******************************************************************************
300  * READ/WRITE
301  ******************************************************************************/
302 int serial_getc(serial_t *obj) {
303     while (!serial_readable(obj));
304     return obj->uart->RBR;
305 }
306
307 void serial_putc(serial_t *obj, int c) {
308     while (!serial_writable(obj));
309     obj->uart->THR = c;
310 }
311
312 int serial_readable(serial_t *obj) {
313     return obj->uart->LSR & 0x01;
314 }
315
316 int serial_writable(serial_t *obj) {
317     return obj->uart->LSR & 0x20;
318 }
319
320 void serial_clear(serial_t *obj) {
321     obj->uart->FCR = 1 << 1  // rx FIFO reset
322                    | 1 << 2  // tx FIFO reset
323                    | 0 << 6; // interrupt depth
324 }
325
326 void serial_pinout_tx(PinName tx) {
327     pinmap_pinout(tx, PinMap_UART_TX);
328 }
329
330 void serial_break_set(serial_t *obj) {
331     obj->uart->LCR |= (1 << 6);
332 }
333
334 void serial_break_clear(serial_t *obj) {
335     obj->uart->LCR &= ~(1 << 6);
336 }
337