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