1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // math.h required for floating point operations for baud rate calculation
18 #include "mbed_assert.h"
23 #include "serial_api.h"
29 /******************************************************************************
31 ******************************************************************************/
36 #define UART_EN (0x01<<0)
39 #define TXBRKEN (0x01<<1)
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)
48 static const PinMap PinMap_UART_TX[] = {
60 static const PinMap PinMap_UART_RX[] = {
72 static uint32_t serial_irq_ids[UART_NUM] = {0};
73 static uart_irq_handler irq_handler;
75 int stdio_uart_inited = 0;
78 void serial_init(serial_t *obj, PinName tx, PinName rx) {
79 int is_stdio_uart = 0;
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);
90 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 12);
94 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 20);
95 LPC_SYSCON->PRESETCTRL |= (1 << 5);
99 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 21);
100 LPC_SYSCON->PRESETCTRL |= (1 << 6);
104 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 22);
105 LPC_SYSCON->PRESETCTRL |= (1 << 7);
109 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 22);
110 LPC_SYSCON->PRESETCTRL |= (1 << 8);
115 obj->uart = (LPC_USART0_Type *)uart;
117 obj->mini_uart = (LPC_USART4_Type *)uart;
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
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
131 // Clear all status bits
132 obj->mini_uart->STAT = (DELTACTS | DELTARXBRK);
134 obj->mini_uart->CFG |= UART_EN;
136 // set default baud rate and format
137 serial_baud (obj, 9600);
138 serial_format(obj, 8, ParityNone, 1);
140 // pinout the chosen uart
141 pinmap_pinout(tx, PinMap_UART_TX);
142 pinmap_pinout(rx, PinMap_UART_RX);
144 // set rx/tx pins in PullUp mode
146 pin_mode(tx, PullUp);
149 pin_mode(rx, PullUp);
152 is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
154 if (is_stdio_uart && (obj->index == 0)) {
155 stdio_uart_inited = 1;
156 memcpy(&stdio_uart, obj, sizeof(serial_t));
160 void serial_free(serial_t *obj) {
161 serial_irq_ids[obj->index] = 0;
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;
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);
180 uint8_t DivAddVal = 0;
185 if ((PCLK % (16 * baudrate)) != 0) { // Checking for zero remainder
186 int err_best = baudrate, b;
187 for (mv = 1; mv < 16 && !hit; mv++)
189 for (dav = 0; dav < mv; dav++)
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
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;
202 // datasheet says if DLL==DLM==0, then 1 is used instead since divide by zero is ungood
206 // datasheet says if dav > 0 then DL must be >= 2
207 if ((dav > 0) && (dlv < 2))
210 // integer rearrangement of the baudrate equation (with rounding)
211 b = ((PCLK * mv / (dlv * (dav + mv) * 8)) + 1) / 2;
213 // check to see how we went
214 b = abs(b - baudrate);
233 // set LCR[DLAB] to enable writing to divider registers
234 obj->uart->LCR |= (1 << 7);
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;
243 obj->uart->LCR &= ~(1 << 7);
246 uint32_t UARTSysClk = SystemCoreClock / LPC_SYSCON->FRGCLKDIV;
247 obj->mini_uart->BRG = UARTSysClk / 16 / baudrate - 1;
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);
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
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));
267 int parity_enable, parity_select;
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;
278 obj->uart->LCR = data_bits << 0
281 | parity_select << 4;
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));
291 case ParityNone: paritysel = 0; break;
292 case ParityEven: paritysel = 2; break;
293 case ParityOdd : paritysel = 3; break;
297 obj->mini_uart->CFG = (data_bits << 2)
304 /******************************************************************************
305 * INTERRUPTS HANDLING
306 ******************************************************************************/
307 static inline void uart_irq(uint32_t iir, uint32_t index) {
310 case 1: irq_type = TxIrq; break;
311 case 2: irq_type = RxIrq; break;
315 if (serial_irq_ids[index] != 0)
316 irq_handler(serial_irq_ids[index], irq_type);
321 uart_irq((LPC_USART0->IIR >> 1) & 0x7, 0);
326 uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 1);
331 uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 2);
336 uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 3);
341 uart_irq((LPC_USART1->STAT & (1 << 2)) ? 2 : 1, 4);
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;
349 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
350 IRQn_Type irq_n = (IRQn_Type)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;
361 if (obj->index == 0) {
362 obj->uart->IER |= (1 << irq);
365 obj->mini_uart->INTENSET = (1 << ((irq == RxIrq) ? 0 : 2));
367 NVIC_SetVector(irq_n, vector);
368 NVIC_EnableIRQ(irq_n);
370 int all_disabled = 0;
371 SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq);
373 if (obj->index == 0) {
374 obj->uart->IER &= ~(1 << irq);
375 all_disabled = (obj->uart->IER & (1 << other_irq)) == 0;
378 obj->mini_uart->INTENSET &= ~(1 << ((irq == RxIrq) ? 0 : 2));
379 all_disabled = (obj->mini_uart->INTENSET & (1 << ((other_irq == RxIrq) ? 0 : 2))) == 0;
383 NVIC_DisableIRQ(irq_n);
387 /******************************************************************************
389 ******************************************************************************/
390 int serial_getc(serial_t *obj) {
391 while (!serial_readable(obj));
392 if (obj->index == 0) {
393 return obj->uart->RBR;
396 return obj->mini_uart->RXDAT;
400 void serial_putc(serial_t *obj, int c) {
401 while (!serial_writable(obj));
402 if (obj->index == 0) {
406 obj->mini_uart->TXDAT = c;
410 int serial_readable(serial_t *obj) {
411 if (obj->index == 0) {
412 return obj->uart->LSR & 0x01;
415 return obj->mini_uart->STAT & RXRDY;
419 int serial_writable(serial_t *obj) {
420 if (obj->index == 0) {
421 return obj->uart->LSR & 0x20;
424 return obj->mini_uart->STAT & TXRDY;
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
435 obj->mini_uart->STAT = 0;
439 void serial_pinout_tx(PinName tx) {
440 pinmap_pinout(tx, PinMap_UART_TX);
443 void serial_break_set(serial_t *obj) {
444 if (obj->index == 0) {
445 obj->uart->LCR |= (1 << 6);
448 obj->mini_uart->CTL |= TXBRKEN;
452 void serial_break_clear(serial_t *obj) {
453 if (obj->index == 0) {
454 obj->uart->LCR &= ~(1 << 6);
457 obj->mini_uart->CTL &= ~TXBRKEN;