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