mbed library sources

Dependents:   Nucleo_blink_led

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Fri Jun 14 17:49:17 2013 +0100
Revision:
10:3bc89ef62ce7
Unify mbed library sources

Who changed what in which revision?

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