mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32620C/serial_api.c@188:3f10722804f9, 2018-07-30 (annotated)
- Committer:
- ua1arn
- Date:
- Mon Jul 30 12:31:10 2018 +0000
- Revision:
- 188:3f10722804f9
- Parent:
- 186:707f6e361f3e
before add multi-configuration USB descriptors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anna Bridge |
186:707f6e361f3e | 1 | /******************************************************************************* |
Anna Bridge |
186:707f6e361f3e | 2 | * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. |
Anna Bridge |
186:707f6e361f3e | 3 | * |
Anna Bridge |
186:707f6e361f3e | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
Anna Bridge |
186:707f6e361f3e | 5 | * copy of this software and associated documentation files (the "Software"), |
Anna Bridge |
186:707f6e361f3e | 6 | * to deal in the Software without restriction, including without limitation |
Anna Bridge |
186:707f6e361f3e | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Anna Bridge |
186:707f6e361f3e | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
Anna Bridge |
186:707f6e361f3e | 9 | * Software is furnished to do so, subject to the following conditions: |
Anna Bridge |
186:707f6e361f3e | 10 | * |
Anna Bridge |
186:707f6e361f3e | 11 | * The above copyright notice and this permission notice shall be included |
Anna Bridge |
186:707f6e361f3e | 12 | * in all copies or substantial portions of the Software. |
Anna Bridge |
186:707f6e361f3e | 13 | * |
Anna Bridge |
186:707f6e361f3e | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
Anna Bridge |
186:707f6e361f3e | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Anna Bridge |
186:707f6e361f3e | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Anna Bridge |
186:707f6e361f3e | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
Anna Bridge |
186:707f6e361f3e | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
Anna Bridge |
186:707f6e361f3e | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
Anna Bridge |
186:707f6e361f3e | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
Anna Bridge |
186:707f6e361f3e | 21 | * |
Anna Bridge |
186:707f6e361f3e | 22 | * Except as contained in this notice, the name of Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 24 | * Products, Inc. Branding Policy. |
Anna Bridge |
186:707f6e361f3e | 25 | * |
Anna Bridge |
186:707f6e361f3e | 26 | * The mere transfer of this software does not imply any licenses |
Anna Bridge |
186:707f6e361f3e | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
Anna Bridge |
186:707f6e361f3e | 28 | * trademarks, maskwork rights, or any other form of intellectual |
Anna Bridge |
186:707f6e361f3e | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
Anna Bridge |
186:707f6e361f3e | 30 | * ownership rights. |
Anna Bridge |
186:707f6e361f3e | 31 | ******************************************************************************* |
Anna Bridge |
186:707f6e361f3e | 32 | */ |
Anna Bridge |
186:707f6e361f3e | 33 | |
Anna Bridge |
186:707f6e361f3e | 34 | #include <string.h> |
Anna Bridge |
186:707f6e361f3e | 35 | #include "mbed_assert.h" |
Anna Bridge |
186:707f6e361f3e | 36 | #include "cmsis.h" |
Anna Bridge |
186:707f6e361f3e | 37 | #include "serial_api.h" |
Anna Bridge |
186:707f6e361f3e | 38 | #include "gpio_api.h" |
Anna Bridge |
186:707f6e361f3e | 39 | #include "uart.h" |
Anna Bridge |
186:707f6e361f3e | 40 | #include "uart_regs.h" |
Anna Bridge |
186:707f6e361f3e | 41 | #include "ioman_regs.h" |
Anna Bridge |
186:707f6e361f3e | 42 | #include "PeripheralPins.h" |
Anna Bridge |
186:707f6e361f3e | 43 | |
Anna Bridge |
186:707f6e361f3e | 44 | #define DEFAULT_BAUD 9600 |
Anna Bridge |
186:707f6e361f3e | 45 | |
Anna Bridge |
186:707f6e361f3e | 46 | #define UART_ERRORS (MXC_F_UART_INTFL_RX_FRAMING_ERR | \ |
Anna Bridge |
186:707f6e361f3e | 47 | MXC_F_UART_INTFL_RX_PARITY_ERR | \ |
Anna Bridge |
186:707f6e361f3e | 48 | MXC_F_UART_INTFL_RX_FIFO_OVERFLOW) |
Anna Bridge |
186:707f6e361f3e | 49 | |
Anna Bridge |
186:707f6e361f3e | 50 | // Variables for managing the stdio UART |
Anna Bridge |
186:707f6e361f3e | 51 | int stdio_uart_inited = 0; |
Anna Bridge |
186:707f6e361f3e | 52 | serial_t stdio_uart = {0}; |
Anna Bridge |
186:707f6e361f3e | 53 | |
Anna Bridge |
186:707f6e361f3e | 54 | // Variables for interrupt driven |
Anna Bridge |
186:707f6e361f3e | 55 | static uart_irq_handler irq_handler; |
Anna Bridge |
186:707f6e361f3e | 56 | static serial_t *objs[MXC_CFG_UART_INSTANCES]; |
Anna Bridge |
186:707f6e361f3e | 57 | |
Anna Bridge |
186:707f6e361f3e | 58 | static void usurp_pin(PinName, int); |
Anna Bridge |
186:707f6e361f3e | 59 | |
Anna Bridge |
186:707f6e361f3e | 60 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 61 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
Anna Bridge |
186:707f6e361f3e | 62 | { |
Anna Bridge |
186:707f6e361f3e | 63 | // Determine which uart is associated with each pin |
Anna Bridge |
186:707f6e361f3e | 64 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
Anna Bridge |
186:707f6e361f3e | 65 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
Anna Bridge |
186:707f6e361f3e | 66 | UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
Anna Bridge |
186:707f6e361f3e | 67 | |
Anna Bridge |
186:707f6e361f3e | 68 | // Make sure that both pins are pointing to the same uart |
Anna Bridge |
186:707f6e361f3e | 69 | MBED_ASSERT(uart != (UARTName)NC); |
Anna Bridge |
186:707f6e361f3e | 70 | |
Anna Bridge |
186:707f6e361f3e | 71 | // Set the obj pointer to the proper uart |
Anna Bridge |
186:707f6e361f3e | 72 | obj->uart = (mxc_uart_regs_t*)uart; |
Anna Bridge |
186:707f6e361f3e | 73 | |
Anna Bridge |
186:707f6e361f3e | 74 | // Set the uart index |
Anna Bridge |
186:707f6e361f3e | 75 | obj->index = MXC_UART_GET_IDX(obj->uart); |
Anna Bridge |
186:707f6e361f3e | 76 | obj->fifo = (mxc_uart_fifo_regs_t*)MXC_UART_GET_BASE_FIFO(obj->index); |
Anna Bridge |
186:707f6e361f3e | 77 | |
Anna Bridge |
186:707f6e361f3e | 78 | // Record the pins requested |
Anna Bridge |
186:707f6e361f3e | 79 | obj->tx = tx; |
Anna Bridge |
186:707f6e361f3e | 80 | obj->rx = rx; |
Anna Bridge |
186:707f6e361f3e | 81 | |
Anna Bridge |
186:707f6e361f3e | 82 | // Merge pin function requests for use with CMSIS init func |
Anna Bridge |
186:707f6e361f3e | 83 | ioman_req_t io_req = {0}; |
Anna Bridge |
186:707f6e361f3e | 84 | pin_function_t *pin_func = NULL; |
Anna Bridge |
186:707f6e361f3e | 85 | if (tx != NC) { |
Anna Bridge |
186:707f6e361f3e | 86 | pin_func = (pin_function_t *)pinmap_find_function(tx, PinMap_UART_TX); |
Anna Bridge |
186:707f6e361f3e | 87 | io_req.value = pin_func->req_val; |
Anna Bridge |
186:707f6e361f3e | 88 | } |
Anna Bridge |
186:707f6e361f3e | 89 | if (rx != NC) { |
Anna Bridge |
186:707f6e361f3e | 90 | pin_func = (pin_function_t *)pinmap_find_function(rx, PinMap_UART_RX); |
Anna Bridge |
186:707f6e361f3e | 91 | io_req.value |= pin_func->req_val; |
Anna Bridge |
186:707f6e361f3e | 92 | } |
Anna Bridge |
186:707f6e361f3e | 93 | |
Anna Bridge |
186:707f6e361f3e | 94 | // Using req and ack pointers of last pin function lookup |
Anna Bridge |
186:707f6e361f3e | 95 | obj->sys_cfg.io_cfg.req_reg = pin_func->reg_req; |
Anna Bridge |
186:707f6e361f3e | 96 | obj->sys_cfg.io_cfg.ack_reg = pin_func->reg_ack; |
Anna Bridge |
186:707f6e361f3e | 97 | obj->sys_cfg.io_cfg.req_val = io_req; |
Anna Bridge |
186:707f6e361f3e | 98 | obj->sys_cfg.clk_scale = CLKMAN_SCALE_DIV_8; |
Anna Bridge |
186:707f6e361f3e | 99 | |
Anna Bridge |
186:707f6e361f3e | 100 | // Configure the UART with default parameters |
Anna Bridge |
186:707f6e361f3e | 101 | obj->cfg.extra_stop = 0; |
Anna Bridge |
186:707f6e361f3e | 102 | obj->cfg.cts = 0; |
Anna Bridge |
186:707f6e361f3e | 103 | obj->cfg.rts = 0; |
Anna Bridge |
186:707f6e361f3e | 104 | obj->cfg.baud = DEFAULT_BAUD; |
Anna Bridge |
186:707f6e361f3e | 105 | obj->cfg.size = UART_DATA_SIZE_8_BITS; |
Anna Bridge |
186:707f6e361f3e | 106 | obj->cfg.parity = UART_PARITY_DISABLE; |
Anna Bridge |
186:707f6e361f3e | 107 | |
Anna Bridge |
186:707f6e361f3e | 108 | // Manage stdio UART |
Anna Bridge |
186:707f6e361f3e | 109 | if (uart == STDIO_UART) { |
Anna Bridge |
186:707f6e361f3e | 110 | stdio_uart_inited = 1; |
Anna Bridge |
186:707f6e361f3e | 111 | stdio_uart = *obj; |
Anna Bridge |
186:707f6e361f3e | 112 | } |
Anna Bridge |
186:707f6e361f3e | 113 | |
Anna Bridge |
186:707f6e361f3e | 114 | int retval = UART_Init(obj->uart, &obj->cfg, &obj->sys_cfg); |
Anna Bridge |
186:707f6e361f3e | 115 | MBED_ASSERT(retval == E_NO_ERROR); |
Anna Bridge |
186:707f6e361f3e | 116 | |
Anna Bridge |
186:707f6e361f3e | 117 | objs[obj->index] = obj; |
Anna Bridge |
186:707f6e361f3e | 118 | } |
Anna Bridge |
186:707f6e361f3e | 119 | |
Anna Bridge |
186:707f6e361f3e | 120 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 121 | void serial_baud(serial_t *obj, int baudrate) |
Anna Bridge |
186:707f6e361f3e | 122 | { |
Anna Bridge |
186:707f6e361f3e | 123 | obj->cfg.baud = baudrate; |
Anna Bridge |
186:707f6e361f3e | 124 | int retval = UART_Init(obj->uart, &obj->cfg, NULL); |
Anna Bridge |
186:707f6e361f3e | 125 | MBED_ASSERT(retval == E_NO_ERROR); |
Anna Bridge |
186:707f6e361f3e | 126 | } |
Anna Bridge |
186:707f6e361f3e | 127 | |
Anna Bridge |
186:707f6e361f3e | 128 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 129 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
Anna Bridge |
186:707f6e361f3e | 130 | { |
Anna Bridge |
186:707f6e361f3e | 131 | switch (data_bits) { |
Anna Bridge |
186:707f6e361f3e | 132 | case 5: |
Anna Bridge |
186:707f6e361f3e | 133 | obj->cfg.size = UART_DATA_SIZE_5_BITS; |
Anna Bridge |
186:707f6e361f3e | 134 | break; |
Anna Bridge |
186:707f6e361f3e | 135 | case 6: |
Anna Bridge |
186:707f6e361f3e | 136 | obj->cfg.size = UART_DATA_SIZE_6_BITS; |
Anna Bridge |
186:707f6e361f3e | 137 | break; |
Anna Bridge |
186:707f6e361f3e | 138 | case 7: |
Anna Bridge |
186:707f6e361f3e | 139 | obj->cfg.size = UART_DATA_SIZE_7_BITS; |
Anna Bridge |
186:707f6e361f3e | 140 | break; |
Anna Bridge |
186:707f6e361f3e | 141 | case 8: |
Anna Bridge |
186:707f6e361f3e | 142 | obj->cfg.size = UART_DATA_SIZE_8_BITS; |
Anna Bridge |
186:707f6e361f3e | 143 | break; |
Anna Bridge |
186:707f6e361f3e | 144 | default: |
Anna Bridge |
186:707f6e361f3e | 145 | MBED_ASSERT(0); |
Anna Bridge |
186:707f6e361f3e | 146 | break; |
Anna Bridge |
186:707f6e361f3e | 147 | } |
Anna Bridge |
186:707f6e361f3e | 148 | |
Anna Bridge |
186:707f6e361f3e | 149 | switch (parity) { |
Anna Bridge |
186:707f6e361f3e | 150 | case ParityNone: |
Anna Bridge |
186:707f6e361f3e | 151 | obj->cfg.parity = UART_PARITY_DISABLE; |
Anna Bridge |
186:707f6e361f3e | 152 | break; |
Anna Bridge |
186:707f6e361f3e | 153 | case ParityOdd : |
Anna Bridge |
186:707f6e361f3e | 154 | obj->cfg.parity = UART_PARITY_ODD; |
Anna Bridge |
186:707f6e361f3e | 155 | break; |
Anna Bridge |
186:707f6e361f3e | 156 | case ParityEven: |
Anna Bridge |
186:707f6e361f3e | 157 | obj->cfg.parity = UART_PARITY_EVEN; |
Anna Bridge |
186:707f6e361f3e | 158 | break; |
Anna Bridge |
186:707f6e361f3e | 159 | case ParityForced1: |
Anna Bridge |
186:707f6e361f3e | 160 | case ParityForced0: |
Anna Bridge |
186:707f6e361f3e | 161 | default: |
Anna Bridge |
186:707f6e361f3e | 162 | MBED_ASSERT(0); |
Anna Bridge |
186:707f6e361f3e | 163 | break; |
Anna Bridge |
186:707f6e361f3e | 164 | } |
Anna Bridge |
186:707f6e361f3e | 165 | |
Anna Bridge |
186:707f6e361f3e | 166 | switch (stop_bits) { |
Anna Bridge |
186:707f6e361f3e | 167 | case 1: |
Anna Bridge |
186:707f6e361f3e | 168 | obj->cfg.extra_stop = 0; |
Anna Bridge |
186:707f6e361f3e | 169 | break; |
Anna Bridge |
186:707f6e361f3e | 170 | case 2: |
Anna Bridge |
186:707f6e361f3e | 171 | obj->cfg.extra_stop = 1; |
Anna Bridge |
186:707f6e361f3e | 172 | break; |
Anna Bridge |
186:707f6e361f3e | 173 | default: |
Anna Bridge |
186:707f6e361f3e | 174 | MBED_ASSERT(0); |
Anna Bridge |
186:707f6e361f3e | 175 | break; |
Anna Bridge |
186:707f6e361f3e | 176 | } |
Anna Bridge |
186:707f6e361f3e | 177 | |
Anna Bridge |
186:707f6e361f3e | 178 | int retval = UART_Init(obj->uart, &obj->cfg, NULL); |
Anna Bridge |
186:707f6e361f3e | 179 | MBED_ASSERT(retval == E_NO_ERROR); |
Anna Bridge |
186:707f6e361f3e | 180 | } |
Anna Bridge |
186:707f6e361f3e | 181 | |
Anna Bridge |
186:707f6e361f3e | 182 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 183 | void uart_handler(serial_t *obj) |
Anna Bridge |
186:707f6e361f3e | 184 | { |
Anna Bridge |
186:707f6e361f3e | 185 | if (obj && obj->id) { |
Anna Bridge |
186:707f6e361f3e | 186 | if ((obj->uart->inten & (MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY | UART_ERRORS)) && |
Anna Bridge |
186:707f6e361f3e | 187 | (obj->uart->intfl & (MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY | UART_ERRORS))) { |
Anna Bridge |
186:707f6e361f3e | 188 | irq_handler(obj->id, RxIrq); |
Anna Bridge |
186:707f6e361f3e | 189 | obj->uart->intfl = (MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY | UART_ERRORS); |
Anna Bridge |
186:707f6e361f3e | 190 | } |
Anna Bridge |
186:707f6e361f3e | 191 | |
Anna Bridge |
186:707f6e361f3e | 192 | if ((obj->uart->inten & MXC_F_UART_INTFL_TX_FIFO_AE) && |
Anna Bridge |
186:707f6e361f3e | 193 | (obj->uart->intfl & MXC_F_UART_INTFL_TX_FIFO_AE)) { |
Anna Bridge |
186:707f6e361f3e | 194 | irq_handler(obj->id, TxIrq); |
Anna Bridge |
186:707f6e361f3e | 195 | obj->uart->intfl = MXC_F_UART_INTFL_TX_FIFO_AE; |
Anna Bridge |
186:707f6e361f3e | 196 | } |
Anna Bridge |
186:707f6e361f3e | 197 | } |
Anna Bridge |
186:707f6e361f3e | 198 | } |
Anna Bridge |
186:707f6e361f3e | 199 | |
Anna Bridge |
186:707f6e361f3e | 200 | void uart0_handler(void) { uart_handler(objs[0]); } |
Anna Bridge |
186:707f6e361f3e | 201 | void uart1_handler(void) { uart_handler(objs[1]); } |
Anna Bridge |
186:707f6e361f3e | 202 | void uart2_handler(void) { uart_handler(objs[2]); } |
Anna Bridge |
186:707f6e361f3e | 203 | void uart3_handler(void) { uart_handler(objs[3]); } |
Anna Bridge |
186:707f6e361f3e | 204 | |
Anna Bridge |
186:707f6e361f3e | 205 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 206 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
Anna Bridge |
186:707f6e361f3e | 207 | { |
Anna Bridge |
186:707f6e361f3e | 208 | irq_handler = handler; |
Anna Bridge |
186:707f6e361f3e | 209 | obj->id = id; |
Anna Bridge |
186:707f6e361f3e | 210 | } |
Anna Bridge |
186:707f6e361f3e | 211 | |
Anna Bridge |
186:707f6e361f3e | 212 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 213 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
Anna Bridge |
186:707f6e361f3e | 214 | { |
Anna Bridge |
186:707f6e361f3e | 215 | MBED_ASSERT(obj->index < MXC_CFG_UART_INSTANCES); |
Anna Bridge |
186:707f6e361f3e | 216 | // objs[obj->index] = obj; |
Anna Bridge |
186:707f6e361f3e | 217 | |
Anna Bridge |
186:707f6e361f3e | 218 | switch (obj->index) { |
Anna Bridge |
186:707f6e361f3e | 219 | case 0: |
Anna Bridge |
186:707f6e361f3e | 220 | NVIC_SetVector(UART0_IRQn, (uint32_t)uart0_handler); |
Anna Bridge |
186:707f6e361f3e | 221 | NVIC_EnableIRQ(UART0_IRQn); |
Anna Bridge |
186:707f6e361f3e | 222 | break; |
Anna Bridge |
186:707f6e361f3e | 223 | case 1: |
Anna Bridge |
186:707f6e361f3e | 224 | NVIC_SetVector(UART1_IRQn, (uint32_t)uart1_handler); |
Anna Bridge |
186:707f6e361f3e | 225 | NVIC_EnableIRQ(UART1_IRQn); |
Anna Bridge |
186:707f6e361f3e | 226 | break; |
Anna Bridge |
186:707f6e361f3e | 227 | case 2: |
Anna Bridge |
186:707f6e361f3e | 228 | NVIC_SetVector(UART2_IRQn, (uint32_t)uart2_handler); |
Anna Bridge |
186:707f6e361f3e | 229 | NVIC_EnableIRQ(UART2_IRQn); |
Anna Bridge |
186:707f6e361f3e | 230 | break; |
Anna Bridge |
186:707f6e361f3e | 231 | case 3: |
Anna Bridge |
186:707f6e361f3e | 232 | NVIC_SetVector(UART3_IRQn, (uint32_t)uart3_handler); |
Anna Bridge |
186:707f6e361f3e | 233 | NVIC_EnableIRQ(UART3_IRQn); |
Anna Bridge |
186:707f6e361f3e | 234 | break; |
Anna Bridge |
186:707f6e361f3e | 235 | default: |
Anna Bridge |
186:707f6e361f3e | 236 | MBED_ASSERT(0); |
Anna Bridge |
186:707f6e361f3e | 237 | } |
Anna Bridge |
186:707f6e361f3e | 238 | |
Anna Bridge |
186:707f6e361f3e | 239 | if (irq == RxIrq) { |
Anna Bridge |
186:707f6e361f3e | 240 | // Enable RX FIFO Threshold Interrupt |
Anna Bridge |
186:707f6e361f3e | 241 | if (enable) { |
Anna Bridge |
186:707f6e361f3e | 242 | // Clear pending interrupts |
Anna Bridge |
186:707f6e361f3e | 243 | obj->uart->intfl = obj->uart->intfl; |
Anna Bridge |
186:707f6e361f3e | 244 | obj->uart->inten |= (MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY | UART_ERRORS); |
Anna Bridge |
186:707f6e361f3e | 245 | } else { |
Anna Bridge |
186:707f6e361f3e | 246 | // Clear pending interrupts |
Anna Bridge |
186:707f6e361f3e | 247 | obj->uart->intfl = obj->uart->intfl; |
Anna Bridge |
186:707f6e361f3e | 248 | obj->uart->inten &= ~(MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY | UART_ERRORS); |
Anna Bridge |
186:707f6e361f3e | 249 | } |
Anna Bridge |
186:707f6e361f3e | 250 | } else if (irq == TxIrq) { |
Anna Bridge |
186:707f6e361f3e | 251 | // Set TX Almost Empty level to interrupt when empty |
Anna Bridge |
186:707f6e361f3e | 252 | MXC_SET_FIELD(&obj->uart->tx_fifo_ctrl, |
Anna Bridge |
186:707f6e361f3e | 253 | MXC_F_UART_TX_FIFO_CTRL_FIFO_AE_LVL, |
Anna Bridge |
186:707f6e361f3e | 254 | (MXC_UART_FIFO_DEPTH - 1) << MXC_F_UART_TX_FIFO_CTRL_FIFO_AE_LVL_POS); |
Anna Bridge |
186:707f6e361f3e | 255 | |
Anna Bridge |
186:707f6e361f3e | 256 | // Enable TX Almost Empty Interrupt |
Anna Bridge |
186:707f6e361f3e | 257 | if (enable) { |
Anna Bridge |
186:707f6e361f3e | 258 | // Clear pending interrupts |
Anna Bridge |
186:707f6e361f3e | 259 | obj->uart->intfl = obj->uart->intfl; |
Anna Bridge |
186:707f6e361f3e | 260 | obj->uart->inten |= MXC_F_UART_INTFL_TX_FIFO_AE; |
Anna Bridge |
186:707f6e361f3e | 261 | } else { |
Anna Bridge |
186:707f6e361f3e | 262 | // Clear pending interrupts |
Anna Bridge |
186:707f6e361f3e | 263 | obj->uart->intfl = obj->uart->intfl; |
Anna Bridge |
186:707f6e361f3e | 264 | obj->uart->inten &= ~MXC_F_UART_INTFL_TX_FIFO_AE; |
Anna Bridge |
186:707f6e361f3e | 265 | } |
Anna Bridge |
186:707f6e361f3e | 266 | } else { |
Anna Bridge |
186:707f6e361f3e | 267 | MBED_ASSERT(0); |
Anna Bridge |
186:707f6e361f3e | 268 | } |
Anna Bridge |
186:707f6e361f3e | 269 | } |
Anna Bridge |
186:707f6e361f3e | 270 | |
Anna Bridge |
186:707f6e361f3e | 271 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 272 | int serial_getc(serial_t *obj) |
Anna Bridge |
186:707f6e361f3e | 273 | { |
Anna Bridge |
186:707f6e361f3e | 274 | int c = -1; |
Anna Bridge |
186:707f6e361f3e | 275 | |
Anna Bridge |
186:707f6e361f3e | 276 | if (obj->rx != NC) { |
Anna Bridge |
186:707f6e361f3e | 277 | // Wait for data to be available |
Anna Bridge |
186:707f6e361f3e | 278 | while ((obj->uart->rx_fifo_ctrl & MXC_F_UART_RX_FIFO_CTRL_FIFO_ENTRY) == 0); |
Anna Bridge |
186:707f6e361f3e | 279 | |
Anna Bridge |
186:707f6e361f3e | 280 | c = obj->fifo->rx; |
Anna Bridge |
186:707f6e361f3e | 281 | |
Anna Bridge |
186:707f6e361f3e | 282 | // Clear receive interrupt sources |
Anna Bridge |
186:707f6e361f3e | 283 | obj->uart->intfl = (MXC_F_UART_INTFL_RX_FIFO_NOT_EMPTY | UART_ERRORS); |
Anna Bridge |
186:707f6e361f3e | 284 | } |
Anna Bridge |
186:707f6e361f3e | 285 | |
Anna Bridge |
186:707f6e361f3e | 286 | return c; |
Anna Bridge |
186:707f6e361f3e | 287 | } |
Anna Bridge |
186:707f6e361f3e | 288 | |
Anna Bridge |
186:707f6e361f3e | 289 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 290 | void serial_putc(serial_t *obj, int c) |
Anna Bridge |
186:707f6e361f3e | 291 | { |
Anna Bridge |
186:707f6e361f3e | 292 | if (obj->tx != NC) { |
Anna Bridge |
186:707f6e361f3e | 293 | // Wait for room in the FIFO without blocking interrupts. |
Anna Bridge |
186:707f6e361f3e | 294 | while (UART_NumWriteAvail(obj->uart) == 0); |
Anna Bridge |
186:707f6e361f3e | 295 | |
Anna Bridge |
186:707f6e361f3e | 296 | // Must clear before every write to the buffer to know that the FIFO |
Anna Bridge |
186:707f6e361f3e | 297 | // is empty when the TX DONE bit is set |
Anna Bridge |
186:707f6e361f3e | 298 | obj->uart->intfl = MXC_F_UART_INTFL_TX_DONE; |
Anna Bridge |
186:707f6e361f3e | 299 | obj->fifo->tx = (uint8_t)c; |
Anna Bridge |
186:707f6e361f3e | 300 | } |
Anna Bridge |
186:707f6e361f3e | 301 | } |
Anna Bridge |
186:707f6e361f3e | 302 | |
Anna Bridge |
186:707f6e361f3e | 303 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 304 | int serial_readable(serial_t *obj) |
Anna Bridge |
186:707f6e361f3e | 305 | { |
Anna Bridge |
186:707f6e361f3e | 306 | return UART_NumReadAvail(obj->uart); |
Anna Bridge |
186:707f6e361f3e | 307 | } |
Anna Bridge |
186:707f6e361f3e | 308 | |
Anna Bridge |
186:707f6e361f3e | 309 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 310 | int serial_writable(serial_t *obj) |
Anna Bridge |
186:707f6e361f3e | 311 | { |
Anna Bridge |
186:707f6e361f3e | 312 | return UART_NumWriteAvail(obj->uart); |
Anna Bridge |
186:707f6e361f3e | 313 | } |
Anna Bridge |
186:707f6e361f3e | 314 | |
Anna Bridge |
186:707f6e361f3e | 315 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 316 | void serial_clear(serial_t *obj) |
Anna Bridge |
186:707f6e361f3e | 317 | { |
Anna Bridge |
186:707f6e361f3e | 318 | // Clear the RX and TX FIFOs |
Anna Bridge |
186:707f6e361f3e | 319 | UART_DrainRX(obj->uart); |
Anna Bridge |
186:707f6e361f3e | 320 | UART_DrainTX(obj->uart); |
Anna Bridge |
186:707f6e361f3e | 321 | } |
Anna Bridge |
186:707f6e361f3e | 322 | |
Anna Bridge |
186:707f6e361f3e | 323 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 324 | void serial_break_set(serial_t *obj) |
Anna Bridge |
186:707f6e361f3e | 325 | { |
Anna Bridge |
186:707f6e361f3e | 326 | // Make sure that nothing is being sent |
Anna Bridge |
186:707f6e361f3e | 327 | while (((obj->uart->tx_fifo_ctrl & MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY) |
Anna Bridge |
186:707f6e361f3e | 328 | >> MXC_F_UART_TX_FIFO_CTRL_FIFO_ENTRY_POS) > 0); |
Anna Bridge |
186:707f6e361f3e | 329 | while (!(obj->uart->intfl & MXC_F_UART_INTFL_TX_DONE)); |
Anna Bridge |
186:707f6e361f3e | 330 | |
Anna Bridge |
186:707f6e361f3e | 331 | // Configure TX to output 0 |
Anna Bridge |
186:707f6e361f3e | 332 | usurp_pin(obj->tx, 0); |
Anna Bridge |
186:707f6e361f3e | 333 | |
Anna Bridge |
186:707f6e361f3e | 334 | // GPIO is setup now, but we need to unmap UART from the pin |
Anna Bridge |
186:707f6e361f3e | 335 | pin_function_t *pin_func = (pin_function_t *)pinmap_find_function(obj->tx, PinMap_UART_TX); |
Anna Bridge |
186:707f6e361f3e | 336 | *pin_func->reg_req &= ~MXC_F_IOMAN_UART_REQ_IO_REQ; |
Anna Bridge |
186:707f6e361f3e | 337 | MBED_ASSERT((*pin_func->reg_ack & MXC_F_IOMAN_UART_ACK_IO_ACK) == 0); |
Anna Bridge |
186:707f6e361f3e | 338 | } |
Anna Bridge |
186:707f6e361f3e | 339 | |
Anna Bridge |
186:707f6e361f3e | 340 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 341 | void serial_break_clear(serial_t *obj) |
Anna Bridge |
186:707f6e361f3e | 342 | { |
Anna Bridge |
186:707f6e361f3e | 343 | // Configure TX to output 1 |
Anna Bridge |
186:707f6e361f3e | 344 | usurp_pin(obj->tx, 1); |
Anna Bridge |
186:707f6e361f3e | 345 | // Return TX to UART control |
Anna Bridge |
186:707f6e361f3e | 346 | serial_pinout_tx(obj->tx); |
Anna Bridge |
186:707f6e361f3e | 347 | } |
Anna Bridge |
186:707f6e361f3e | 348 | |
Anna Bridge |
186:707f6e361f3e | 349 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 350 | void serial_pinout_tx(PinName tx) |
Anna Bridge |
186:707f6e361f3e | 351 | { |
Anna Bridge |
186:707f6e361f3e | 352 | pinmap_pinout(tx, PinMap_UART_TX); |
Anna Bridge |
186:707f6e361f3e | 353 | } |
Anna Bridge |
186:707f6e361f3e | 354 | |
Anna Bridge |
186:707f6e361f3e | 355 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 356 | void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) |
Anna Bridge |
186:707f6e361f3e | 357 | { |
Anna Bridge |
186:707f6e361f3e | 358 | pin_function_t rtscts_pin_func = {0}; |
Anna Bridge |
186:707f6e361f3e | 359 | |
Anna Bridge |
186:707f6e361f3e | 360 | obj->cfg.cts = 0; |
Anna Bridge |
186:707f6e361f3e | 361 | obj->cfg.rts = 0; |
Anna Bridge |
186:707f6e361f3e | 362 | |
Anna Bridge |
186:707f6e361f3e | 363 | if ((FlowControlCTS == type) || (FlowControlRTSCTS == type)) { |
Anna Bridge |
186:707f6e361f3e | 364 | UARTName uart_cts = (UARTName)pinmap_peripheral(txflow, PinMap_UART_CTS); |
Anna Bridge |
186:707f6e361f3e | 365 | UARTName uart = (UARTName)pinmap_merge(uart_cts, (UARTName)obj->uart); |
Anna Bridge |
186:707f6e361f3e | 366 | // Assert pin is usable with existing uart |
Anna Bridge |
186:707f6e361f3e | 367 | MBED_ASSERT(uart != (UARTName)NC); |
Anna Bridge |
186:707f6e361f3e | 368 | |
Anna Bridge |
186:707f6e361f3e | 369 | pin_function_t *pin_func; |
Anna Bridge |
186:707f6e361f3e | 370 | pin_func = (pin_function_t *)pinmap_find_function(txflow, PinMap_UART_CTS); |
Anna Bridge |
186:707f6e361f3e | 371 | rtscts_pin_func.req_val |= pin_func->req_val; |
Anna Bridge |
186:707f6e361f3e | 372 | |
Anna Bridge |
186:707f6e361f3e | 373 | obj->cfg.cts = 1; |
Anna Bridge |
186:707f6e361f3e | 374 | } |
Anna Bridge |
186:707f6e361f3e | 375 | |
Anna Bridge |
186:707f6e361f3e | 376 | if ((FlowControlRTS == type) || (FlowControlRTSCTS == type)) { |
Anna Bridge |
186:707f6e361f3e | 377 | UARTName uart_rts = (UARTName)pinmap_peripheral(rxflow, PinMap_UART_RTS); |
Anna Bridge |
186:707f6e361f3e | 378 | UARTName uart = (UARTName)pinmap_merge(uart_rts, (UARTName)obj->uart); |
Anna Bridge |
186:707f6e361f3e | 379 | MBED_ASSERT(uart != (UARTName)NC); |
Anna Bridge |
186:707f6e361f3e | 380 | |
Anna Bridge |
186:707f6e361f3e | 381 | pin_function_t *pin_func; |
Anna Bridge |
186:707f6e361f3e | 382 | pin_func = (pin_function_t *)pinmap_find_function(rxflow, PinMap_UART_RTS); |
Anna Bridge |
186:707f6e361f3e | 383 | rtscts_pin_func.req_val |= pin_func->req_val; |
Anna Bridge |
186:707f6e361f3e | 384 | |
Anna Bridge |
186:707f6e361f3e | 385 | obj->cfg.rts = 1; |
Anna Bridge |
186:707f6e361f3e | 386 | } |
Anna Bridge |
186:707f6e361f3e | 387 | |
Anna Bridge |
186:707f6e361f3e | 388 | obj->sys_cfg.io_cfg.req_val.value |= rtscts_pin_func.req_val; |
Anna Bridge |
186:707f6e361f3e | 389 | |
Anna Bridge |
186:707f6e361f3e | 390 | int retval = UART_Init(obj->uart, &obj->cfg, &obj->sys_cfg); |
Anna Bridge |
186:707f6e361f3e | 391 | MBED_ASSERT(retval == E_NO_ERROR); |
Anna Bridge |
186:707f6e361f3e | 392 | } |
Anna Bridge |
186:707f6e361f3e | 393 | |
Anna Bridge |
186:707f6e361f3e | 394 | //****************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 395 | static void usurp_pin(PinName pin, int state) |
Anna Bridge |
186:707f6e361f3e | 396 | { |
Anna Bridge |
186:707f6e361f3e | 397 | gpio_t gpio; |
Anna Bridge |
186:707f6e361f3e | 398 | gpio_init_out(&gpio, pin); |
Anna Bridge |
186:707f6e361f3e | 399 | gpio_write(&gpio, state); |
Anna Bridge |
186:707f6e361f3e | 400 | } |