]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/serial_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11U6X / 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
17 // math.h required for floating point operations for baud rate calculation
18 #include "mbed_assert.h"
19 #include <math.h>
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include "serial_api.h"
24 #include "cmsis.h"
25 #include "pinmap.h"
26
27 #if DEVICE_SERIAL
28
29 /******************************************************************************
30  * INITIALIZATION
31  ******************************************************************************/
32
33 #define UART_NUM      5
34
35 // CFG
36 #define UART_EN       (0x01<<0)
37
38 // CTL
39 #define TXBRKEN       (0x01<<1)
40
41 // STAT
42 #define RXRDY         (0x01<<0)
43 #define TXRDY         (0x01<<2)
44 #define DELTACTS      (0x01<<5)
45 #define RXBRK         (0x01<<10)
46 #define DELTARXBRK    (0x01<<11)
47
48 static const PinMap PinMap_UART_TX[] = {
49     {P0_19, UART_0, 1},
50     {P1_18, UART_0, 2},
51     {P1_27, UART_0, 2},
52     {P1_8 , UART_1, 2},
53     {P1_0 , UART_2, 3},
54     {P1_23, UART_2, 3},
55     {P2_4 , UART_3, 1},
56     {P2_12, UART_4, 1},
57     { NC  , NC    , 0}
58 };
59
60 static const PinMap PinMap_UART_RX[] = {
61     {P0_18, UART_0, 1},
62     {P1_17, UART_0, 2},
63     {P1_26, UART_0, 2},
64     {P1_2 , UART_1, 3},
65     {P0_20, UART_2, 2},
66     {P1_6 , UART_2, 2},
67     {P2_3 , UART_3, 1},
68     {P2_11, UART_4, 1},
69     {NC   , NC    , 0}
70 };
71
72 static uint32_t serial_irq_ids[UART_NUM] = {0};
73 static uart_irq_handler irq_handler;
74
75 int stdio_uart_inited = 0;
76 serial_t stdio_uart;
77
78 void serial_init(serial_t *obj, PinName tx, PinName rx) {
79     int is_stdio_uart = 0;
80     
81     // determine the UART to use
82     UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
83     UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
84     UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
85     MBED_ASSERT((int)uart != NC);
86     
87     switch (uart) {
88         case UART_0:
89             obj->index = 0;
90             LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 12);
91             break;
92         case UART_1:
93             obj->index = 1;
94             LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 20);
95             LPC_SYSCON->PRESETCTRL |= (1 << 5);
96             break;
97         case UART_2:
98             obj->index = 2;
99             LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 21);
100             LPC_SYSCON->PRESETCTRL |= (1 << 6);
101             break;
102         case UART_3:
103             obj->index = 3;
104             LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 22);
105             LPC_SYSCON->PRESETCTRL |= (1 << 7);
106             break;
107         case UART_4:
108             obj->index = 4;
109             LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 22);
110             LPC_SYSCON->PRESETCTRL |= (1 << 8);
111             break;
112     }
113
114     if (obj->index == 0)
115         obj->uart = (LPC_USART0_Type *)uart;
116     else
117         obj->mini_uart = (LPC_USART4_Type *)uart;
118     
119     if (obj->index == 0) {
120         // enable fifos and default rx trigger level
121         obj->uart->FCR = 1 << 0  // FIFO Enable - 0 = Disables, 1 = Enabled
122                        | 0 << 1  // Rx Fifo Clear
123                        | 0 << 2  // Tx Fifo Clear
124                        | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars
125         // disable irqs
126         obj->uart->IER = 0 << 0  // Rx Data available irq enable
127                        | 0 << 1  // Tx Fifo empty irq enable
128                        | 0 << 2; // Rx Line Status irq enable
129     }
130     else {
131         // Clear all status bits
132         obj->mini_uart->STAT = (DELTACTS | DELTARXBRK);
133         // Enable UART
134         obj->mini_uart->CFG |= UART_EN;
135     }
136     // set default baud rate and format
137     serial_baud  (obj, 9600);
138     serial_format(obj, 8, ParityNone, 1);
139     
140     // pinout the chosen uart
141     pinmap_pinout(tx, PinMap_UART_TX);
142     pinmap_pinout(rx, PinMap_UART_RX);
143     
144     // set rx/tx pins in PullUp mode
145     if (tx != NC) {
146         pin_mode(tx, PullUp);
147     }
148     if (rx != NC) {
149         pin_mode(rx, PullUp);
150     }
151     
152     is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
153     
154     if (is_stdio_uart && (obj->index == 0)) {
155         stdio_uart_inited = 1;
156         memcpy(&stdio_uart, obj, sizeof(serial_t));
157     }
158 }
159
160 void serial_free(serial_t *obj) {
161     serial_irq_ids[obj->index] = 0;
162 }
163
164 // serial_baud
165 // set the baud rate, taking in to account the current SystemFrequency
166 void serial_baud(serial_t *obj, int baudrate) {
167     LPC_SYSCON->USART0CLKDIV = 1;
168     LPC_SYSCON->FRGCLKDIV = 1;
169
170     if (obj->index == 0) {
171         uint32_t PCLK = SystemCoreClock;
172         // First we check to see if the basic divide with no DivAddVal/MulVal
173         // ratio gives us an integer result. If it does, we set DivAddVal = 0,
174         // MulVal = 1. Otherwise, we search the valid ratio value range to find
175         // the closest match. This could be more elegant, using search methods
176         // and/or lookup tables, but the brute force method is not that much
177         // slower, and is more maintainable.
178         uint16_t DL = PCLK / (16 * baudrate);
179         
180         uint8_t DivAddVal = 0;
181         uint8_t MulVal = 1;
182         int hit = 0;
183         uint16_t dlv;
184         uint8_t mv, dav;
185         if ((PCLK % (16 * baudrate)) != 0) {     // Checking for zero remainder
186             int err_best = baudrate, b;
187             for (mv = 1; mv < 16 && !hit; mv++)
188             {
189                 for (dav = 0; dav < mv; dav++)
190                 {
191                     // baudrate = PCLK / (16 * dlv * (1 + (DivAdd / Mul))
192                     // solving for dlv, we get dlv = mul * PCLK / (16 * baudrate * (divadd + mul))
193                     // mul has 4 bits, PCLK has 27 so we have 1 bit headroom which can be used for rounding
194                     // for many values of mul and PCLK we have 2 or more bits of headroom which can be used to improve precision
195                     // note: X / 32 doesn't round correctly. Instead, we use ((X / 16) + 1) / 2 for correct rounding
196
197                     if ((mv * PCLK * 2) & 0x80000000) // 1 bit headroom
198                         dlv = ((((2 * mv * PCLK) / (baudrate * (dav + mv))) / 16) + 1) / 2;
199                     else // 2 bits headroom, use more precision
200                         dlv = ((((4 * mv * PCLK) / (baudrate * (dav + mv))) / 32) + 1) / 2;
201
202                     // datasheet says if DLL==DLM==0, then 1 is used instead since divide by zero is ungood
203                     if (dlv == 0)
204                         dlv = 1;
205
206                     // datasheet says if dav > 0 then DL must be >= 2
207                     if ((dav > 0) && (dlv < 2))
208                         dlv = 2;
209
210                     // integer rearrangement of the baudrate equation (with rounding)
211                     b = ((PCLK * mv / (dlv * (dav + mv) * 8)) + 1) / 2;
212
213                     // check to see how we went
214                     b = abs(b - baudrate);
215                     if (b < err_best)
216                     {
217                         err_best  = b;
218
219                         DL        = dlv;
220                         MulVal    = mv;
221                         DivAddVal = dav;
222
223                         if (b == baudrate)
224                         {
225                             hit = 1;
226                             break;
227                         }
228                     }
229                 }
230             }
231         }
232         
233         // set LCR[DLAB] to enable writing to divider registers
234         obj->uart->LCR |= (1 << 7);
235         
236         // set divider values
237         obj->uart->DLM = (DL >> 8) & 0xFF;
238         obj->uart->DLL = (DL >> 0) & 0xFF;
239         obj->uart->FDR = (uint32_t) DivAddVal << 0
240                        | (uint32_t) MulVal    << 4;
241         
242         // clear LCR[DLAB]
243         obj->uart->LCR &= ~(1 << 7);
244     }
245     else {
246         uint32_t UARTSysClk = SystemCoreClock / LPC_SYSCON->FRGCLKDIV;
247         obj->mini_uart->BRG = UARTSysClk / 16 / baudrate - 1;
248         
249         LPC_SYSCON->UARTFRGDIV = 0xFF;
250         LPC_SYSCON->UARTFRGMULT = ( ((UARTSysClk / 16) * (LPC_SYSCON->UARTFRGDIV + 1)) /
251                                     (baudrate * (obj->mini_uart->BRG + 1))
252                                   ) - (LPC_SYSCON->UARTFRGDIV + 1);
253     }
254 }
255
256 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
257     MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
258
259     stop_bits -= 1;
260
261     if (obj->index == 0) {
262         MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
263         MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) ||
264                     (parity == ParityForced1) || (parity == ParityForced0));
265         data_bits -= 5;
266     
267         int parity_enable, parity_select;
268         switch (parity) {
269             case ParityNone: parity_enable = 0; parity_select = 0; break;
270             case ParityOdd : parity_enable = 1; parity_select = 0; break;
271             case ParityEven: parity_enable = 1; parity_select = 1; break;
272             case ParityForced1: parity_enable = 1; parity_select = 2; break;
273             case ParityForced0: parity_enable = 1; parity_select = 3; break;
274             default:
275                 return;
276         }
277         
278         obj->uart->LCR = data_bits       << 0
279                        | stop_bits       << 2
280                        | parity_enable   << 3
281                        | parity_select   << 4;
282     }
283     else {
284         // 0: 7 data bits ... 2: 9 data bits
285         MBED_ASSERT((data_bits > 6) && (data_bits < 10));
286         MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven));
287         data_bits -= 7;
288
289         int paritysel;
290         switch (parity) {
291             case ParityNone: paritysel = 0; break;
292             case ParityEven: paritysel = 2; break;
293             case ParityOdd : paritysel = 3; break;
294             default:
295                 return;
296         }
297         obj->mini_uart->CFG = (data_bits << 2)
298                             | (paritysel << 4)
299                             | (stop_bits << 6)
300                             | UART_EN;
301     }
302 }
303
304 /******************************************************************************
305  * INTERRUPTS HANDLING
306  ******************************************************************************/
307 static inline void uart_irq(uint32_t iir, uint32_t index) {
308     SerialIrq irq_type;
309     switch (iir) {
310         case 1: irq_type = TxIrq; break;
311         case 2: irq_type = RxIrq; break;
312         default: return;
313     }
314     
315     if (serial_irq_ids[index] != 0)
316         irq_handler(serial_irq_ids[index], irq_type);
317 }
318
319 void uart0_irq()
320 {
321     uart_irq((LPC_USART0->IIR >> 1) & 0x7, 0);
322 }
323
324 void uart1_irq()
325 {
326     uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 1);
327 }
328
329 void uart2_irq()
330 {
331     uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 2);
332 }
333
334 void uart3_irq()
335 {
336     uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 3);
337 }
338
339 void uart4_irq()
340 {
341     uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 4);
342 }
343
344 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
345     irq_handler = handler;
346     serial_irq_ids[obj->index] = id;
347 }
348
349 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
350     IRQn_Type irq_n = (IRQn_Type)0;
351     uint32_t vector = 0;
352     switch ((int)obj->uart) {
353         case UART_0: irq_n = USART0_IRQn;   vector = (uint32_t)&uart0_irq; break;
354         case UART_1: irq_n = USART1_4_IRQn; vector = (uint32_t)&uart1_irq; break;
355         case UART_2: irq_n = USART2_3_IRQn; vector = (uint32_t)&uart2_irq; break;
356         case UART_3: irq_n = USART2_3_IRQn; vector = (uint32_t)&uart3_irq; break;
357         case UART_4: irq_n = USART1_4_IRQn; vector = (uint32_t)&uart4_irq; break;
358     }
359     
360     if (enable) {
361         if (obj->index == 0) {
362             obj->uart->IER |= (1 << irq);
363         }
364         else {
365             obj->mini_uart->INTENSET = (1 << ((irq == RxIrq) ? 0 : 2));
366         }
367         NVIC_SetVector(irq_n, vector);
368         NVIC_EnableIRQ(irq_n);
369     } else { // disable
370         int all_disabled = 0;
371         SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq);
372
373         if (obj->index == 0) {
374             obj->uart->IER &= ~(1 << irq);
375             all_disabled = (obj->uart->IER & (1 << other_irq)) == 0;
376         }
377         else {
378             obj->mini_uart->INTENSET &= ~(1 << ((irq == RxIrq) ? 0 : 2));
379             all_disabled = (obj->mini_uart->INTENSET & (1 << ((other_irq == RxIrq) ? 0 : 2))) == 0;
380          }
381
382         if (all_disabled)
383             NVIC_DisableIRQ(irq_n);
384     }
385 }
386
387 /******************************************************************************
388  * READ/WRITE
389  ******************************************************************************/
390 int serial_getc(serial_t *obj) {
391     while (!serial_readable(obj));
392     if (obj->index == 0) {
393         return obj->uart->RBR;
394     }
395     else {
396         return obj->mini_uart->RXDAT;
397     }
398 }
399
400 void serial_putc(serial_t *obj, int c) {
401     while (!serial_writable(obj));
402     if (obj->index == 0) {
403         obj->uart->THR = c;
404     }
405     else {
406         obj->mini_uart->TXDAT = c;
407     }
408 }
409
410 int serial_readable(serial_t *obj) {
411     if (obj->index == 0) {
412         return obj->uart->LSR & 0x01;
413     }
414     else {
415         return obj->mini_uart->STAT & RXRDY;
416     }
417 }
418
419 int serial_writable(serial_t *obj) {
420     if (obj->index == 0) {
421         return obj->uart->LSR & 0x20;
422     }
423     else {
424         return obj->mini_uart->STAT & TXRDY;
425     }
426 }
427
428 void serial_clear(serial_t *obj) {
429     if (obj->index == 0) {
430         obj->uart->FCR = 1 << 1  // rx FIFO reset
431                        | 1 << 2  // tx FIFO reset
432                        | 0 << 6; // interrupt depth
433     }
434     else {
435         obj->mini_uart->STAT = 0;
436     }
437 }
438
439 void serial_pinout_tx(PinName tx) {
440     pinmap_pinout(tx, PinMap_UART_TX);
441 }
442
443 void serial_break_set(serial_t *obj) {
444     if (obj->index == 0) {
445         obj->uart->LCR |= (1 << 6);
446     }
447     else {
448         obj->mini_uart->CTL |= TXBRKEN;
449     }
450 }
451
452 void serial_break_clear(serial_t *obj) {
453     if (obj->index == 0) {
454         obj->uart->LCR &= ~(1 << 6);
455     }
456     else {
457         obj->mini_uart->CTL &= ~TXBRKEN;
458     }
459 }
460
461
462 #endif