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