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