mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NXP/TARGET_LPC11U6X/serial_api.c@162:16168a1438f3, 2017-04-08 (annotated)
- Committer:
- funshine
- Date:
- Sat Apr 08 17:03:55 2017 +0000
- Revision:
- 162:16168a1438f3
- Parent:
- 149:156823d33999
add code to handle serial port rx error in uart_irq()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | |
<> | 144:ef7eb2e8f9f7 | 17 | // math.h required for floating point operations for baud rate calculation |
<> | 144:ef7eb2e8f9f7 | 18 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 19 | #include <math.h> |
<> | 144:ef7eb2e8f9f7 | 20 | #include <string.h> |
<> | 144:ef7eb2e8f9f7 | 21 | #include <stdlib.h> |
<> | 144:ef7eb2e8f9f7 | 22 | |
<> | 144:ef7eb2e8f9f7 | 23 | #include "serial_api.h" |
<> | 144:ef7eb2e8f9f7 | 24 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 25 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 26 | |
<> | 144:ef7eb2e8f9f7 | 27 | #if DEVICE_SERIAL |
<> | 144:ef7eb2e8f9f7 | 28 | |
<> | 144:ef7eb2e8f9f7 | 29 | /****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 30 | * INITIALIZATION |
<> | 144:ef7eb2e8f9f7 | 31 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 32 | |
<> | 144:ef7eb2e8f9f7 | 33 | #define UART_NUM 5 |
<> | 144:ef7eb2e8f9f7 | 34 | |
<> | 144:ef7eb2e8f9f7 | 35 | // CFG |
<> | 144:ef7eb2e8f9f7 | 36 | #define UART_EN (0x01<<0) |
<> | 144:ef7eb2e8f9f7 | 37 | |
<> | 144:ef7eb2e8f9f7 | 38 | // CTL |
<> | 144:ef7eb2e8f9f7 | 39 | #define TXBRKEN (0x01<<1) |
<> | 144:ef7eb2e8f9f7 | 40 | |
<> | 144:ef7eb2e8f9f7 | 41 | // STAT |
<> | 144:ef7eb2e8f9f7 | 42 | #define RXRDY (0x01<<0) |
<> | 144:ef7eb2e8f9f7 | 43 | #define TXRDY (0x01<<2) |
<> | 144:ef7eb2e8f9f7 | 44 | #define DELTACTS (0x01<<5) |
<> | 144:ef7eb2e8f9f7 | 45 | #define RXBRK (0x01<<10) |
<> | 144:ef7eb2e8f9f7 | 46 | #define DELTARXBRK (0x01<<11) |
<> | 144:ef7eb2e8f9f7 | 47 | |
<> | 144:ef7eb2e8f9f7 | 48 | static const PinMap PinMap_UART_TX[] = { |
<> | 144:ef7eb2e8f9f7 | 49 | {P0_19, UART_0, 1}, |
<> | 144:ef7eb2e8f9f7 | 50 | {P1_18, UART_0, 2}, |
<> | 144:ef7eb2e8f9f7 | 51 | {P1_27, UART_0, 2}, |
<> | 144:ef7eb2e8f9f7 | 52 | {P1_8 , UART_1, 2}, |
<> | 144:ef7eb2e8f9f7 | 53 | {P0_14, UART_1, 4}, |
<> | 144:ef7eb2e8f9f7 | 54 | {P1_0 , UART_2, 3}, |
<> | 144:ef7eb2e8f9f7 | 55 | {P1_23, UART_2, 3}, |
<> | 144:ef7eb2e8f9f7 | 56 | {P2_4 , UART_3, 1}, |
<> | 144:ef7eb2e8f9f7 | 57 | {P2_12, UART_4, 1}, |
<> | 144:ef7eb2e8f9f7 | 58 | { NC , NC , 0} |
<> | 144:ef7eb2e8f9f7 | 59 | }; |
<> | 144:ef7eb2e8f9f7 | 60 | |
<> | 144:ef7eb2e8f9f7 | 61 | static const PinMap PinMap_UART_RX[] = { |
<> | 144:ef7eb2e8f9f7 | 62 | {P0_18, UART_0, 1}, |
<> | 144:ef7eb2e8f9f7 | 63 | {P1_17, UART_0, 2}, |
<> | 144:ef7eb2e8f9f7 | 64 | {P1_26, UART_0, 2}, |
<> | 144:ef7eb2e8f9f7 | 65 | {P1_2 , UART_1, 3}, |
<> | 144:ef7eb2e8f9f7 | 66 | {P0_13, UART_1, 4}, |
<> | 144:ef7eb2e8f9f7 | 67 | {P0_20, UART_2, 2}, |
<> | 144:ef7eb2e8f9f7 | 68 | {P1_6 , UART_2, 2}, |
<> | 144:ef7eb2e8f9f7 | 69 | {P2_3 , UART_3, 1}, |
<> | 144:ef7eb2e8f9f7 | 70 | {P2_11, UART_4, 1}, |
<> | 144:ef7eb2e8f9f7 | 71 | {NC , NC , 0} |
<> | 144:ef7eb2e8f9f7 | 72 | }; |
<> | 144:ef7eb2e8f9f7 | 73 | |
<> | 144:ef7eb2e8f9f7 | 74 | static uint32_t serial_irq_ids[UART_NUM] = {0}; |
<> | 144:ef7eb2e8f9f7 | 75 | static uart_irq_handler irq_handler; |
<> | 144:ef7eb2e8f9f7 | 76 | |
<> | 144:ef7eb2e8f9f7 | 77 | int stdio_uart_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 78 | serial_t stdio_uart; |
<> | 144:ef7eb2e8f9f7 | 79 | |
<> | 144:ef7eb2e8f9f7 | 80 | void serial_init(serial_t *obj, PinName tx, PinName rx) { |
<> | 144:ef7eb2e8f9f7 | 81 | int is_stdio_uart = 0; |
<> | 144:ef7eb2e8f9f7 | 82 | |
<> | 144:ef7eb2e8f9f7 | 83 | // determine the UART to use |
<> | 144:ef7eb2e8f9f7 | 84 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 85 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
<> | 144:ef7eb2e8f9f7 | 86 | UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
<> | 144:ef7eb2e8f9f7 | 87 | MBED_ASSERT((int)uart != NC); |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | switch (uart) { |
<> | 144:ef7eb2e8f9f7 | 90 | case UART_0: |
<> | 144:ef7eb2e8f9f7 | 91 | obj->index = 0; |
<> | 144:ef7eb2e8f9f7 | 92 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 12); |
<> | 144:ef7eb2e8f9f7 | 93 | break; |
<> | 144:ef7eb2e8f9f7 | 94 | case UART_1: |
<> | 144:ef7eb2e8f9f7 | 95 | obj->index = 1; |
<> | 144:ef7eb2e8f9f7 | 96 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 20); |
<> | 144:ef7eb2e8f9f7 | 97 | LPC_SYSCON->PRESETCTRL |= (1 << 5); |
<> | 144:ef7eb2e8f9f7 | 98 | break; |
<> | 144:ef7eb2e8f9f7 | 99 | case UART_2: |
<> | 144:ef7eb2e8f9f7 | 100 | obj->index = 2; |
<> | 144:ef7eb2e8f9f7 | 101 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 21); |
<> | 144:ef7eb2e8f9f7 | 102 | LPC_SYSCON->PRESETCTRL |= (1 << 6); |
<> | 144:ef7eb2e8f9f7 | 103 | break; |
<> | 144:ef7eb2e8f9f7 | 104 | case UART_3: |
<> | 144:ef7eb2e8f9f7 | 105 | obj->index = 3; |
<> | 144:ef7eb2e8f9f7 | 106 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 22); |
<> | 144:ef7eb2e8f9f7 | 107 | LPC_SYSCON->PRESETCTRL |= (1 << 7); |
<> | 144:ef7eb2e8f9f7 | 108 | break; |
<> | 144:ef7eb2e8f9f7 | 109 | case UART_4: |
<> | 144:ef7eb2e8f9f7 | 110 | obj->index = 4; |
<> | 144:ef7eb2e8f9f7 | 111 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 22); |
<> | 144:ef7eb2e8f9f7 | 112 | LPC_SYSCON->PRESETCTRL |= (1 << 8); |
<> | 144:ef7eb2e8f9f7 | 113 | break; |
<> | 144:ef7eb2e8f9f7 | 114 | } |
<> | 144:ef7eb2e8f9f7 | 115 | |
<> | 144:ef7eb2e8f9f7 | 116 | if (obj->index == 0) |
<> | 144:ef7eb2e8f9f7 | 117 | obj->uart = (LPC_USART0_Type *)uart; |
<> | 144:ef7eb2e8f9f7 | 118 | else |
<> | 144:ef7eb2e8f9f7 | 119 | obj->mini_uart = (LPC_USART4_Type *)uart; |
<> | 144:ef7eb2e8f9f7 | 120 | |
<> | 144:ef7eb2e8f9f7 | 121 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 122 | // enable fifos and default rx trigger level |
<> | 144:ef7eb2e8f9f7 | 123 | obj->uart->FCR = 1 << 0 // FIFO Enable - 0 = Disables, 1 = Enabled |
<> | 144:ef7eb2e8f9f7 | 124 | | 0 << 1 // Rx Fifo Clear |
<> | 144:ef7eb2e8f9f7 | 125 | | 0 << 2 // Tx Fifo Clear |
<> | 144:ef7eb2e8f9f7 | 126 | | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars |
<> | 144:ef7eb2e8f9f7 | 127 | // disable irqs |
<> | 144:ef7eb2e8f9f7 | 128 | obj->uart->IER = 0 << 0 // Rx Data available irq enable |
<> | 144:ef7eb2e8f9f7 | 129 | | 0 << 1 // Tx Fifo empty irq enable |
<> | 144:ef7eb2e8f9f7 | 130 | | 0 << 2; // Rx Line Status irq enable |
<> | 144:ef7eb2e8f9f7 | 131 | } |
<> | 144:ef7eb2e8f9f7 | 132 | else { |
<> | 144:ef7eb2e8f9f7 | 133 | // Clear all status bits |
<> | 144:ef7eb2e8f9f7 | 134 | obj->mini_uart->STAT = (DELTACTS | DELTARXBRK); |
<> | 144:ef7eb2e8f9f7 | 135 | // Enable UART |
<> | 144:ef7eb2e8f9f7 | 136 | obj->mini_uart->CFG |= UART_EN; |
<> | 144:ef7eb2e8f9f7 | 137 | } |
<> | 144:ef7eb2e8f9f7 | 138 | // set default baud rate and format |
<> | 144:ef7eb2e8f9f7 | 139 | serial_baud (obj, 9600); |
<> | 144:ef7eb2e8f9f7 | 140 | serial_format(obj, 8, ParityNone, 1); |
<> | 144:ef7eb2e8f9f7 | 141 | |
<> | 144:ef7eb2e8f9f7 | 142 | // pinout the chosen uart |
<> | 144:ef7eb2e8f9f7 | 143 | pinmap_pinout(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 144 | pinmap_pinout(rx, PinMap_UART_RX); |
<> | 144:ef7eb2e8f9f7 | 145 | |
<> | 144:ef7eb2e8f9f7 | 146 | // set rx/tx pins in PullUp mode |
<> | 144:ef7eb2e8f9f7 | 147 | if (tx != NC) { |
<> | 144:ef7eb2e8f9f7 | 148 | pin_mode(tx, PullUp); |
<> | 144:ef7eb2e8f9f7 | 149 | } |
<> | 144:ef7eb2e8f9f7 | 150 | if (rx != NC) { |
<> | 144:ef7eb2e8f9f7 | 151 | pin_mode(rx, PullUp); |
<> | 144:ef7eb2e8f9f7 | 152 | } |
<> | 144:ef7eb2e8f9f7 | 153 | |
<> | 144:ef7eb2e8f9f7 | 154 | is_stdio_uart = (uart == STDIO_UART) ? (1) : (0); |
<> | 144:ef7eb2e8f9f7 | 155 | |
<> | 144:ef7eb2e8f9f7 | 156 | if (is_stdio_uart && (obj->index == 0)) { |
<> | 144:ef7eb2e8f9f7 | 157 | stdio_uart_inited = 1; |
<> | 144:ef7eb2e8f9f7 | 158 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
<> | 144:ef7eb2e8f9f7 | 159 | } |
<> | 144:ef7eb2e8f9f7 | 160 | } |
<> | 144:ef7eb2e8f9f7 | 161 | |
<> | 144:ef7eb2e8f9f7 | 162 | void serial_free(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 163 | serial_irq_ids[obj->index] = 0; |
<> | 144:ef7eb2e8f9f7 | 164 | } |
<> | 144:ef7eb2e8f9f7 | 165 | |
<> | 144:ef7eb2e8f9f7 | 166 | // serial_baud |
<> | 144:ef7eb2e8f9f7 | 167 | // set the baud rate, taking in to account the current SystemFrequency |
<> | 144:ef7eb2e8f9f7 | 168 | void serial_baud(serial_t *obj, int baudrate) { |
<> | 144:ef7eb2e8f9f7 | 169 | LPC_SYSCON->USART0CLKDIV = 1; |
<> | 144:ef7eb2e8f9f7 | 170 | LPC_SYSCON->FRGCLKDIV = 1; |
<> | 144:ef7eb2e8f9f7 | 171 | |
<> | 144:ef7eb2e8f9f7 | 172 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 173 | uint32_t PCLK = SystemCoreClock; |
<> | 144:ef7eb2e8f9f7 | 174 | // First we check to see if the basic divide with no DivAddVal/MulVal |
<> | 144:ef7eb2e8f9f7 | 175 | // ratio gives us an integer result. If it does, we set DivAddVal = 0, |
<> | 144:ef7eb2e8f9f7 | 176 | // MulVal = 1. Otherwise, we search the valid ratio value range to find |
<> | 144:ef7eb2e8f9f7 | 177 | // the closest match. This could be more elegant, using search methods |
<> | 144:ef7eb2e8f9f7 | 178 | // and/or lookup tables, but the brute force method is not that much |
<> | 144:ef7eb2e8f9f7 | 179 | // slower, and is more maintainable. |
<> | 144:ef7eb2e8f9f7 | 180 | uint16_t DL = PCLK / (16 * baudrate); |
<> | 144:ef7eb2e8f9f7 | 181 | |
<> | 144:ef7eb2e8f9f7 | 182 | uint8_t DivAddVal = 0; |
<> | 144:ef7eb2e8f9f7 | 183 | uint8_t MulVal = 1; |
<> | 144:ef7eb2e8f9f7 | 184 | int hit = 0; |
<> | 144:ef7eb2e8f9f7 | 185 | uint16_t dlv; |
<> | 144:ef7eb2e8f9f7 | 186 | uint8_t mv, dav; |
<> | 144:ef7eb2e8f9f7 | 187 | if ((PCLK % (16 * baudrate)) != 0) { // Checking for zero remainder |
<> | 144:ef7eb2e8f9f7 | 188 | int err_best = baudrate, b; |
<> | 144:ef7eb2e8f9f7 | 189 | for (mv = 1; mv < 16 && !hit; mv++) |
<> | 144:ef7eb2e8f9f7 | 190 | { |
<> | 144:ef7eb2e8f9f7 | 191 | for (dav = 0; dav < mv; dav++) |
<> | 144:ef7eb2e8f9f7 | 192 | { |
<> | 144:ef7eb2e8f9f7 | 193 | // baudrate = PCLK / (16 * dlv * (1 + (DivAdd / Mul)) |
<> | 144:ef7eb2e8f9f7 | 194 | // solving for dlv, we get dlv = mul * PCLK / (16 * baudrate * (divadd + mul)) |
<> | 144:ef7eb2e8f9f7 | 195 | // mul has 4 bits, PCLK has 27 so we have 1 bit headroom which can be used for rounding |
<> | 144:ef7eb2e8f9f7 | 196 | // for many values of mul and PCLK we have 2 or more bits of headroom which can be used to improve precision |
<> | 144:ef7eb2e8f9f7 | 197 | // note: X / 32 doesn't round correctly. Instead, we use ((X / 16) + 1) / 2 for correct rounding |
<> | 144:ef7eb2e8f9f7 | 198 | |
<> | 144:ef7eb2e8f9f7 | 199 | if ((mv * PCLK * 2) & 0x80000000) // 1 bit headroom |
<> | 144:ef7eb2e8f9f7 | 200 | dlv = ((((2 * mv * PCLK) / (baudrate * (dav + mv))) / 16) + 1) / 2; |
<> | 144:ef7eb2e8f9f7 | 201 | else // 2 bits headroom, use more precision |
<> | 144:ef7eb2e8f9f7 | 202 | dlv = ((((4 * mv * PCLK) / (baudrate * (dav + mv))) / 32) + 1) / 2; |
<> | 144:ef7eb2e8f9f7 | 203 | |
<> | 144:ef7eb2e8f9f7 | 204 | // datasheet says if DLL==DLM==0, then 1 is used instead since divide by zero is ungood |
<> | 144:ef7eb2e8f9f7 | 205 | if (dlv == 0) |
<> | 144:ef7eb2e8f9f7 | 206 | dlv = 1; |
<> | 144:ef7eb2e8f9f7 | 207 | |
<> | 144:ef7eb2e8f9f7 | 208 | // datasheet says if dav > 0 then DL must be >= 2 |
<> | 144:ef7eb2e8f9f7 | 209 | if ((dav > 0) && (dlv < 2)) |
<> | 144:ef7eb2e8f9f7 | 210 | dlv = 2; |
<> | 144:ef7eb2e8f9f7 | 211 | |
<> | 144:ef7eb2e8f9f7 | 212 | // integer rearrangement of the baudrate equation (with rounding) |
<> | 144:ef7eb2e8f9f7 | 213 | b = ((PCLK * mv / (dlv * (dav + mv) * 8)) + 1) / 2; |
<> | 144:ef7eb2e8f9f7 | 214 | |
<> | 144:ef7eb2e8f9f7 | 215 | // check to see how we went |
<> | 144:ef7eb2e8f9f7 | 216 | b = abs(b - baudrate); |
<> | 144:ef7eb2e8f9f7 | 217 | if (b < err_best) |
<> | 144:ef7eb2e8f9f7 | 218 | { |
<> | 144:ef7eb2e8f9f7 | 219 | err_best = b; |
<> | 144:ef7eb2e8f9f7 | 220 | |
<> | 144:ef7eb2e8f9f7 | 221 | DL = dlv; |
<> | 144:ef7eb2e8f9f7 | 222 | MulVal = mv; |
<> | 144:ef7eb2e8f9f7 | 223 | DivAddVal = dav; |
<> | 144:ef7eb2e8f9f7 | 224 | |
<> | 144:ef7eb2e8f9f7 | 225 | if (b == baudrate) |
<> | 144:ef7eb2e8f9f7 | 226 | { |
<> | 144:ef7eb2e8f9f7 | 227 | hit = 1; |
<> | 144:ef7eb2e8f9f7 | 228 | break; |
<> | 144:ef7eb2e8f9f7 | 229 | } |
<> | 144:ef7eb2e8f9f7 | 230 | } |
<> | 144:ef7eb2e8f9f7 | 231 | } |
<> | 144:ef7eb2e8f9f7 | 232 | } |
<> | 144:ef7eb2e8f9f7 | 233 | } |
<> | 144:ef7eb2e8f9f7 | 234 | |
<> | 144:ef7eb2e8f9f7 | 235 | // set LCR[DLAB] to enable writing to divider registers |
<> | 144:ef7eb2e8f9f7 | 236 | obj->uart->LCR |= (1 << 7); |
<> | 144:ef7eb2e8f9f7 | 237 | |
<> | 144:ef7eb2e8f9f7 | 238 | // set divider values |
<> | 144:ef7eb2e8f9f7 | 239 | obj->uart->DLM = (DL >> 8) & 0xFF; |
<> | 144:ef7eb2e8f9f7 | 240 | obj->uart->DLL = (DL >> 0) & 0xFF; |
<> | 144:ef7eb2e8f9f7 | 241 | obj->uart->FDR = (uint32_t) DivAddVal << 0 |
<> | 144:ef7eb2e8f9f7 | 242 | | (uint32_t) MulVal << 4; |
<> | 144:ef7eb2e8f9f7 | 243 | |
<> | 144:ef7eb2e8f9f7 | 244 | // clear LCR[DLAB] |
<> | 144:ef7eb2e8f9f7 | 245 | obj->uart->LCR &= ~(1 << 7); |
<> | 144:ef7eb2e8f9f7 | 246 | } |
<> | 144:ef7eb2e8f9f7 | 247 | else { |
<> | 144:ef7eb2e8f9f7 | 248 | uint32_t UARTSysClk = SystemCoreClock / LPC_SYSCON->FRGCLKDIV; |
<> | 144:ef7eb2e8f9f7 | 249 | obj->mini_uart->BRG = UARTSysClk / 16 / baudrate - 1; |
<> | 144:ef7eb2e8f9f7 | 250 | |
<> | 144:ef7eb2e8f9f7 | 251 | LPC_SYSCON->UARTFRGDIV = 0xFF; |
<> | 144:ef7eb2e8f9f7 | 252 | LPC_SYSCON->UARTFRGMULT = ( ((UARTSysClk / 16) * (LPC_SYSCON->UARTFRGDIV + 1)) / |
<> | 144:ef7eb2e8f9f7 | 253 | (baudrate * (obj->mini_uart->BRG + 1)) |
<> | 144:ef7eb2e8f9f7 | 254 | ) - (LPC_SYSCON->UARTFRGDIV + 1); |
<> | 144:ef7eb2e8f9f7 | 255 | } |
<> | 144:ef7eb2e8f9f7 | 256 | } |
<> | 144:ef7eb2e8f9f7 | 257 | |
<> | 144:ef7eb2e8f9f7 | 258 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { |
<> | 144:ef7eb2e8f9f7 | 259 | MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits |
<> | 144:ef7eb2e8f9f7 | 260 | |
<> | 144:ef7eb2e8f9f7 | 261 | stop_bits -= 1; |
<> | 144:ef7eb2e8f9f7 | 262 | |
<> | 144:ef7eb2e8f9f7 | 263 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 264 | MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits |
<> | 144:ef7eb2e8f9f7 | 265 | MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || |
<> | 144:ef7eb2e8f9f7 | 266 | (parity == ParityForced1) || (parity == ParityForced0)); |
<> | 144:ef7eb2e8f9f7 | 267 | data_bits -= 5; |
<> | 144:ef7eb2e8f9f7 | 268 | |
<> | 144:ef7eb2e8f9f7 | 269 | int parity_enable, parity_select; |
<> | 144:ef7eb2e8f9f7 | 270 | switch (parity) { |
<> | 144:ef7eb2e8f9f7 | 271 | case ParityNone: parity_enable = 0; parity_select = 0; break; |
<> | 144:ef7eb2e8f9f7 | 272 | case ParityOdd : parity_enable = 1; parity_select = 0; break; |
<> | 144:ef7eb2e8f9f7 | 273 | case ParityEven: parity_enable = 1; parity_select = 1; break; |
<> | 144:ef7eb2e8f9f7 | 274 | case ParityForced1: parity_enable = 1; parity_select = 2; break; |
<> | 144:ef7eb2e8f9f7 | 275 | case ParityForced0: parity_enable = 1; parity_select = 3; break; |
<> | 144:ef7eb2e8f9f7 | 276 | default: |
<> | 144:ef7eb2e8f9f7 | 277 | return; |
<> | 144:ef7eb2e8f9f7 | 278 | } |
<> | 144:ef7eb2e8f9f7 | 279 | |
<> | 144:ef7eb2e8f9f7 | 280 | obj->uart->LCR = data_bits << 0 |
<> | 144:ef7eb2e8f9f7 | 281 | | stop_bits << 2 |
<> | 144:ef7eb2e8f9f7 | 282 | | parity_enable << 3 |
<> | 144:ef7eb2e8f9f7 | 283 | | parity_select << 4; |
<> | 144:ef7eb2e8f9f7 | 284 | } |
<> | 144:ef7eb2e8f9f7 | 285 | else { |
<> | 144:ef7eb2e8f9f7 | 286 | // 0: 7 data bits ... 2: 9 data bits |
<> | 144:ef7eb2e8f9f7 | 287 | MBED_ASSERT((data_bits > 6) && (data_bits < 10)); |
<> | 144:ef7eb2e8f9f7 | 288 | MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven)); |
<> | 144:ef7eb2e8f9f7 | 289 | data_bits -= 7; |
<> | 144:ef7eb2e8f9f7 | 290 | |
<> | 144:ef7eb2e8f9f7 | 291 | int paritysel; |
<> | 144:ef7eb2e8f9f7 | 292 | switch (parity) { |
<> | 144:ef7eb2e8f9f7 | 293 | case ParityNone: paritysel = 0; break; |
<> | 144:ef7eb2e8f9f7 | 294 | case ParityEven: paritysel = 2; break; |
<> | 144:ef7eb2e8f9f7 | 295 | case ParityOdd : paritysel = 3; break; |
<> | 144:ef7eb2e8f9f7 | 296 | default: |
<> | 144:ef7eb2e8f9f7 | 297 | return; |
<> | 144:ef7eb2e8f9f7 | 298 | } |
<> | 144:ef7eb2e8f9f7 | 299 | obj->mini_uart->CFG = (data_bits << 2) |
<> | 144:ef7eb2e8f9f7 | 300 | | (paritysel << 4) |
<> | 144:ef7eb2e8f9f7 | 301 | | (stop_bits << 6) |
<> | 144:ef7eb2e8f9f7 | 302 | | UART_EN; |
<> | 144:ef7eb2e8f9f7 | 303 | } |
<> | 144:ef7eb2e8f9f7 | 304 | } |
<> | 144:ef7eb2e8f9f7 | 305 | |
<> | 144:ef7eb2e8f9f7 | 306 | /****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 307 | * INTERRUPTS HANDLING |
<> | 144:ef7eb2e8f9f7 | 308 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 309 | static inline void uart_irq(uint32_t iir, uint32_t index) { |
<> | 144:ef7eb2e8f9f7 | 310 | SerialIrq irq_type; |
<> | 144:ef7eb2e8f9f7 | 311 | switch (iir) { |
<> | 144:ef7eb2e8f9f7 | 312 | case 1: irq_type = TxIrq; break; |
<> | 144:ef7eb2e8f9f7 | 313 | case 2: irq_type = RxIrq; break; |
<> | 144:ef7eb2e8f9f7 | 314 | default: return; |
<> | 144:ef7eb2e8f9f7 | 315 | } |
<> | 144:ef7eb2e8f9f7 | 316 | |
<> | 144:ef7eb2e8f9f7 | 317 | if (serial_irq_ids[index] != 0) |
<> | 144:ef7eb2e8f9f7 | 318 | irq_handler(serial_irq_ids[index], irq_type); |
<> | 144:ef7eb2e8f9f7 | 319 | } |
<> | 144:ef7eb2e8f9f7 | 320 | |
<> | 144:ef7eb2e8f9f7 | 321 | void uart0_irq() |
<> | 144:ef7eb2e8f9f7 | 322 | { |
<> | 144:ef7eb2e8f9f7 | 323 | uart_irq((LPC_USART0->IIR >> 1) & 0x7, 0); |
<> | 144:ef7eb2e8f9f7 | 324 | } |
<> | 144:ef7eb2e8f9f7 | 325 | |
<> | 144:ef7eb2e8f9f7 | 326 | void uart1_irq() |
<> | 144:ef7eb2e8f9f7 | 327 | { |
<> | 144:ef7eb2e8f9f7 | 328 | if(LPC_USART1->STAT & (1 << 2)){ |
<> | 144:ef7eb2e8f9f7 | 329 | uart_irq(1, 1); |
<> | 144:ef7eb2e8f9f7 | 330 | } |
<> | 144:ef7eb2e8f9f7 | 331 | if(LPC_USART1->STAT & (1 << 0)){ |
<> | 144:ef7eb2e8f9f7 | 332 | uart_irq(2, 1); |
<> | 144:ef7eb2e8f9f7 | 333 | } |
<> | 144:ef7eb2e8f9f7 | 334 | } |
<> | 144:ef7eb2e8f9f7 | 335 | |
<> | 144:ef7eb2e8f9f7 | 336 | void uart2_irq() |
<> | 144:ef7eb2e8f9f7 | 337 | { |
<> | 144:ef7eb2e8f9f7 | 338 | if(LPC_USART2->STAT & (1 << 2)){ |
<> | 144:ef7eb2e8f9f7 | 339 | uart_irq(1, 2); |
<> | 144:ef7eb2e8f9f7 | 340 | } |
<> | 144:ef7eb2e8f9f7 | 341 | if(LPC_USART2->STAT & (1 << 0)){ |
<> | 144:ef7eb2e8f9f7 | 342 | uart_irq(2, 2); |
<> | 144:ef7eb2e8f9f7 | 343 | } |
<> | 144:ef7eb2e8f9f7 | 344 | } |
<> | 144:ef7eb2e8f9f7 | 345 | |
<> | 144:ef7eb2e8f9f7 | 346 | void uart3_irq() |
<> | 144:ef7eb2e8f9f7 | 347 | { |
<> | 144:ef7eb2e8f9f7 | 348 | if(LPC_USART3->STAT & (1 << 2)){ |
<> | 144:ef7eb2e8f9f7 | 349 | uart_irq(1, 3); |
<> | 144:ef7eb2e8f9f7 | 350 | } |
<> | 144:ef7eb2e8f9f7 | 351 | if(LPC_USART3->STAT & (1 << 0)){ |
<> | 144:ef7eb2e8f9f7 | 352 | uart_irq(2, 3); |
<> | 144:ef7eb2e8f9f7 | 353 | } |
<> | 144:ef7eb2e8f9f7 | 354 | } |
<> | 144:ef7eb2e8f9f7 | 355 | |
<> | 144:ef7eb2e8f9f7 | 356 | void uart4_irq() |
<> | 144:ef7eb2e8f9f7 | 357 | { |
<> | 144:ef7eb2e8f9f7 | 358 | if(LPC_USART4->STAT & (1 << 2)){ |
<> | 144:ef7eb2e8f9f7 | 359 | uart_irq(1, 4); |
<> | 144:ef7eb2e8f9f7 | 360 | } |
<> | 144:ef7eb2e8f9f7 | 361 | if(LPC_USART4->STAT & (1 << 0)){ |
<> | 144:ef7eb2e8f9f7 | 362 | uart_irq(2, 4); |
<> | 144:ef7eb2e8f9f7 | 363 | } |
<> | 144:ef7eb2e8f9f7 | 364 | } |
<> | 144:ef7eb2e8f9f7 | 365 | |
<> | 144:ef7eb2e8f9f7 | 366 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { |
<> | 144:ef7eb2e8f9f7 | 367 | irq_handler = handler; |
<> | 144:ef7eb2e8f9f7 | 368 | serial_irq_ids[obj->index] = id; |
<> | 144:ef7eb2e8f9f7 | 369 | } |
<> | 144:ef7eb2e8f9f7 | 370 | |
<> | 144:ef7eb2e8f9f7 | 371 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) { |
<> | 144:ef7eb2e8f9f7 | 372 | IRQn_Type irq_n = (IRQn_Type)0; |
<> | 144:ef7eb2e8f9f7 | 373 | uint32_t vector = 0; |
<> | 144:ef7eb2e8f9f7 | 374 | if(obj->index == 0){ |
<> | 144:ef7eb2e8f9f7 | 375 | irq_n = USART0_IRQn; vector = (uint32_t)&uart0_irq; |
<> | 144:ef7eb2e8f9f7 | 376 | } |
<> | 144:ef7eb2e8f9f7 | 377 | else{ |
<> | 144:ef7eb2e8f9f7 | 378 | switch ((int)obj->mini_uart) { |
<> | 144:ef7eb2e8f9f7 | 379 | case UART_0: irq_n = USART0_IRQn; vector = (uint32_t)&uart0_irq; break; |
<> | 144:ef7eb2e8f9f7 | 380 | case UART_1: irq_n = USART1_4_IRQn; vector = (uint32_t)&uart1_irq; break; |
<> | 144:ef7eb2e8f9f7 | 381 | case UART_2: irq_n = USART2_3_IRQn; vector = (uint32_t)&uart2_irq; break; |
<> | 144:ef7eb2e8f9f7 | 382 | case UART_3: irq_n = USART2_3_IRQn; vector = (uint32_t)&uart3_irq; break; |
<> | 144:ef7eb2e8f9f7 | 383 | case UART_4: irq_n = USART1_4_IRQn; vector = (uint32_t)&uart4_irq; break; |
<> | 144:ef7eb2e8f9f7 | 384 | } |
<> | 144:ef7eb2e8f9f7 | 385 | } |
<> | 144:ef7eb2e8f9f7 | 386 | |
<> | 144:ef7eb2e8f9f7 | 387 | if (enable) { |
<> | 144:ef7eb2e8f9f7 | 388 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 389 | obj->uart->IER |= (1 << irq); |
<> | 144:ef7eb2e8f9f7 | 390 | } |
<> | 144:ef7eb2e8f9f7 | 391 | else { |
<> | 144:ef7eb2e8f9f7 | 392 | obj->mini_uart->INTENSET = (1 << ((irq == RxIrq) ? 0 : 2)); |
<> | 144:ef7eb2e8f9f7 | 393 | } |
<> | 144:ef7eb2e8f9f7 | 394 | NVIC_SetVector(irq_n, vector); |
<> | 144:ef7eb2e8f9f7 | 395 | NVIC_EnableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 396 | } else { // disable |
<> | 144:ef7eb2e8f9f7 | 397 | int all_disabled = 0; |
<> | 144:ef7eb2e8f9f7 | 398 | SerialIrq other_irq = (irq == RxIrq) ? (RxIrq) : (TxIrq); |
<> | 144:ef7eb2e8f9f7 | 399 | |
<> | 144:ef7eb2e8f9f7 | 400 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 401 | obj->uart->IER &= ~(1 << irq); |
<> | 144:ef7eb2e8f9f7 | 402 | all_disabled = (obj->uart->IER & (1 << other_irq)) == 0; |
<> | 144:ef7eb2e8f9f7 | 403 | } |
<> | 144:ef7eb2e8f9f7 | 404 | else { |
<> | 144:ef7eb2e8f9f7 | 405 | obj->mini_uart->INTENCLR = (1 << ((irq == RxIrq) ? 0 : 2)); |
<> | 144:ef7eb2e8f9f7 | 406 | all_disabled = (obj->mini_uart->INTENSET) == 0; |
<> | 144:ef7eb2e8f9f7 | 407 | } |
<> | 144:ef7eb2e8f9f7 | 408 | |
<> | 144:ef7eb2e8f9f7 | 409 | if (all_disabled) |
<> | 144:ef7eb2e8f9f7 | 410 | NVIC_DisableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 411 | } |
<> | 144:ef7eb2e8f9f7 | 412 | } |
<> | 144:ef7eb2e8f9f7 | 413 | |
<> | 144:ef7eb2e8f9f7 | 414 | /****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 415 | * READ/WRITE |
<> | 144:ef7eb2e8f9f7 | 416 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 417 | int serial_getc(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 418 | while (!serial_readable(obj)); |
<> | 144:ef7eb2e8f9f7 | 419 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 420 | return obj->uart->RBR; |
<> | 144:ef7eb2e8f9f7 | 421 | } |
<> | 144:ef7eb2e8f9f7 | 422 | else { |
<> | 144:ef7eb2e8f9f7 | 423 | return obj->mini_uart->RXDAT; |
<> | 144:ef7eb2e8f9f7 | 424 | } |
<> | 144:ef7eb2e8f9f7 | 425 | } |
<> | 144:ef7eb2e8f9f7 | 426 | |
<> | 144:ef7eb2e8f9f7 | 427 | void serial_putc(serial_t *obj, int c) { |
<> | 144:ef7eb2e8f9f7 | 428 | while (!serial_writable(obj)); |
<> | 144:ef7eb2e8f9f7 | 429 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 430 | obj->uart->THR = c; |
<> | 144:ef7eb2e8f9f7 | 431 | } |
<> | 144:ef7eb2e8f9f7 | 432 | else { |
<> | 144:ef7eb2e8f9f7 | 433 | obj->mini_uart->TXDAT = c; |
<> | 144:ef7eb2e8f9f7 | 434 | } |
<> | 144:ef7eb2e8f9f7 | 435 | } |
<> | 144:ef7eb2e8f9f7 | 436 | |
<> | 144:ef7eb2e8f9f7 | 437 | int serial_readable(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 438 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 439 | return obj->uart->LSR & 0x01; |
<> | 144:ef7eb2e8f9f7 | 440 | } |
<> | 144:ef7eb2e8f9f7 | 441 | else { |
<> | 144:ef7eb2e8f9f7 | 442 | return obj->mini_uart->STAT & RXRDY; |
<> | 144:ef7eb2e8f9f7 | 443 | } |
<> | 144:ef7eb2e8f9f7 | 444 | } |
<> | 144:ef7eb2e8f9f7 | 445 | |
<> | 144:ef7eb2e8f9f7 | 446 | int serial_writable(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 447 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 448 | return obj->uart->LSR & 0x20; |
<> | 144:ef7eb2e8f9f7 | 449 | } |
<> | 144:ef7eb2e8f9f7 | 450 | else { |
<> | 144:ef7eb2e8f9f7 | 451 | return obj->mini_uart->STAT & TXRDY; |
<> | 144:ef7eb2e8f9f7 | 452 | } |
<> | 144:ef7eb2e8f9f7 | 453 | } |
<> | 144:ef7eb2e8f9f7 | 454 | |
<> | 144:ef7eb2e8f9f7 | 455 | void serial_clear(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 456 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 457 | obj->uart->FCR = 1 << 1 // rx FIFO reset |
<> | 144:ef7eb2e8f9f7 | 458 | | 1 << 2 // tx FIFO reset |
<> | 144:ef7eb2e8f9f7 | 459 | | 0 << 6; // interrupt depth |
<> | 144:ef7eb2e8f9f7 | 460 | } |
<> | 144:ef7eb2e8f9f7 | 461 | else { |
<> | 144:ef7eb2e8f9f7 | 462 | obj->mini_uart->STAT = 0; |
<> | 144:ef7eb2e8f9f7 | 463 | } |
<> | 144:ef7eb2e8f9f7 | 464 | } |
<> | 144:ef7eb2e8f9f7 | 465 | |
<> | 144:ef7eb2e8f9f7 | 466 | void serial_pinout_tx(PinName tx) { |
<> | 144:ef7eb2e8f9f7 | 467 | pinmap_pinout(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 468 | } |
<> | 144:ef7eb2e8f9f7 | 469 | |
<> | 144:ef7eb2e8f9f7 | 470 | void serial_break_set(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 471 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 472 | obj->uart->LCR |= (1 << 6); |
<> | 144:ef7eb2e8f9f7 | 473 | } |
<> | 144:ef7eb2e8f9f7 | 474 | else { |
<> | 144:ef7eb2e8f9f7 | 475 | obj->mini_uart->CTL |= TXBRKEN; |
<> | 144:ef7eb2e8f9f7 | 476 | } |
<> | 144:ef7eb2e8f9f7 | 477 | } |
<> | 144:ef7eb2e8f9f7 | 478 | |
<> | 144:ef7eb2e8f9f7 | 479 | void serial_break_clear(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 480 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 481 | obj->uart->LCR &= ~(1 << 6); |
<> | 144:ef7eb2e8f9f7 | 482 | } |
<> | 144:ef7eb2e8f9f7 | 483 | else { |
<> | 144:ef7eb2e8f9f7 | 484 | obj->mini_uart->CTL &= ~TXBRKEN; |
<> | 144:ef7eb2e8f9f7 | 485 | } |
<> | 144:ef7eb2e8f9f7 | 486 | } |
<> | 144:ef7eb2e8f9f7 | 487 | |
<> | 144:ef7eb2e8f9f7 | 488 | |
<> | 144:ef7eb2e8f9f7 | 489 | #endif |