mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Apr 01 10:00:07 2014 +0100
Revision:
140:ca60b7a31055
Parent:
51:7838415c99e7
Child:
227:7bd0639b8911
Synchronized with git revision fdcc1e387426ce216bc17175b73bcd5f4aececff

Full URL: https://github.com/mbedmicro/mbed/commit/fdcc1e387426ce216bc17175b73bcd5f4aececff/

Changed error(...) to a weak function

Who changed what in which revision?

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