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