Forked.
Fork of mbed-dev by
targets/TARGET_TOSHIBA/TARGET_TMPM066/serial_api.c@174:ed647f63e28d, 2017-12-19 (annotated)
- Committer:
- Dollyparton
- Date:
- Tue Dec 19 12:50:13 2017 +0000
- Revision:
- 174:ed647f63e28d
- Parent:
- 172:7d866c31b3c5
Added RAW socket.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 172:7d866c31b3c5 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 172:7d866c31b3c5 | 2 | * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved |
AnnaBridge | 172:7d866c31b3c5 | 3 | * |
AnnaBridge | 172:7d866c31b3c5 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 172:7d866c31b3c5 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 172:7d866c31b3c5 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 172:7d866c31b3c5 | 7 | * |
AnnaBridge | 172:7d866c31b3c5 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 172:7d866c31b3c5 | 9 | * |
AnnaBridge | 172:7d866c31b3c5 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 172:7d866c31b3c5 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 172:7d866c31b3c5 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 172:7d866c31b3c5 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 172:7d866c31b3c5 | 14 | * limitations under the License. |
AnnaBridge | 172:7d866c31b3c5 | 15 | */ |
AnnaBridge | 172:7d866c31b3c5 | 16 | #include <string.h> |
AnnaBridge | 172:7d866c31b3c5 | 17 | #include "serial_api.h" |
AnnaBridge | 172:7d866c31b3c5 | 18 | #include "PeripheralNames.h" |
AnnaBridge | 172:7d866c31b3c5 | 19 | #include "pinmap.h" |
AnnaBridge | 172:7d866c31b3c5 | 20 | |
AnnaBridge | 172:7d866c31b3c5 | 21 | #define UART_NUM 2 |
AnnaBridge | 172:7d866c31b3c5 | 22 | |
AnnaBridge | 172:7d866c31b3c5 | 23 | static const PinMap PinMap_UART_TX[] = { |
AnnaBridge | 172:7d866c31b3c5 | 24 | {PC2, SERIAL_0, PIN_DATA(1, 1)}, |
AnnaBridge | 172:7d866c31b3c5 | 25 | {PE2, SERIAL_1, PIN_DATA(1, 1)}, |
AnnaBridge | 172:7d866c31b3c5 | 26 | {NC, NC, 0} |
AnnaBridge | 172:7d866c31b3c5 | 27 | }; |
AnnaBridge | 172:7d866c31b3c5 | 28 | |
AnnaBridge | 172:7d866c31b3c5 | 29 | static const PinMap PinMap_UART_RX[] = { |
AnnaBridge | 172:7d866c31b3c5 | 30 | {PC3, SERIAL_0, PIN_DATA(1, 0)}, |
AnnaBridge | 172:7d866c31b3c5 | 31 | {PE1, SERIAL_1, PIN_DATA(1, 0)}, |
AnnaBridge | 172:7d866c31b3c5 | 32 | {NC, NC, 0} |
AnnaBridge | 172:7d866c31b3c5 | 33 | }; |
AnnaBridge | 172:7d866c31b3c5 | 34 | |
AnnaBridge | 172:7d866c31b3c5 | 35 | static int serial_irq_ids[UART_NUM] = {0}; |
AnnaBridge | 172:7d866c31b3c5 | 36 | static uart_irq_handler irq_handler; |
AnnaBridge | 172:7d866c31b3c5 | 37 | |
AnnaBridge | 172:7d866c31b3c5 | 38 | int stdio_uart_inited = 0; |
AnnaBridge | 172:7d866c31b3c5 | 39 | serial_t stdio_uart; |
AnnaBridge | 172:7d866c31b3c5 | 40 | |
AnnaBridge | 172:7d866c31b3c5 | 41 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
AnnaBridge | 172:7d866c31b3c5 | 42 | { |
AnnaBridge | 172:7d866c31b3c5 | 43 | int is_stdio_uart = 0; |
AnnaBridge | 172:7d866c31b3c5 | 44 | |
AnnaBridge | 172:7d866c31b3c5 | 45 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
AnnaBridge | 172:7d866c31b3c5 | 46 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
AnnaBridge | 172:7d866c31b3c5 | 47 | UARTName uart_name = (UARTName)pinmap_merge(uart_tx, uart_rx); |
AnnaBridge | 172:7d866c31b3c5 | 48 | MBED_ASSERT((int)uart_name != NC); |
AnnaBridge | 172:7d866c31b3c5 | 49 | |
AnnaBridge | 172:7d866c31b3c5 | 50 | obj->index = uart_name; |
AnnaBridge | 172:7d866c31b3c5 | 51 | // Initialize UART instance |
AnnaBridge | 172:7d866c31b3c5 | 52 | switch (uart_name) { |
AnnaBridge | 172:7d866c31b3c5 | 53 | case SERIAL_0: |
AnnaBridge | 172:7d866c31b3c5 | 54 | CG_SetFcPeriphA(CG_FC_PERIPH_SIO0, ENABLE); |
AnnaBridge | 172:7d866c31b3c5 | 55 | obj->UARTx = UART0; |
AnnaBridge | 172:7d866c31b3c5 | 56 | break; |
AnnaBridge | 172:7d866c31b3c5 | 57 | case SERIAL_1: |
AnnaBridge | 172:7d866c31b3c5 | 58 | CG_SetFcPeriphB(CG_FC_PERIPH_SIO1, ENABLE); |
AnnaBridge | 172:7d866c31b3c5 | 59 | obj->UARTx = UART1; |
AnnaBridge | 172:7d866c31b3c5 | 60 | break; |
AnnaBridge | 172:7d866c31b3c5 | 61 | default: |
AnnaBridge | 172:7d866c31b3c5 | 62 | break; |
AnnaBridge | 172:7d866c31b3c5 | 63 | } |
AnnaBridge | 172:7d866c31b3c5 | 64 | |
AnnaBridge | 172:7d866c31b3c5 | 65 | obj->uart_config.BaudRate = 9600; |
AnnaBridge | 172:7d866c31b3c5 | 66 | obj->uart_config.DataBits = UART_DATA_BITS_8; |
AnnaBridge | 172:7d866c31b3c5 | 67 | obj->uart_config.StopBits = UART_STOP_BITS_1; |
AnnaBridge | 172:7d866c31b3c5 | 68 | obj->uart_config.Parity = UART_NO_PARITY; |
AnnaBridge | 172:7d866c31b3c5 | 69 | obj->uart_config.FlowCtrl = UART_NONE_FLOW_CTRL; |
AnnaBridge | 172:7d866c31b3c5 | 70 | |
AnnaBridge | 172:7d866c31b3c5 | 71 | if (tx != NC && rx != NC) { |
AnnaBridge | 172:7d866c31b3c5 | 72 | obj->uart_config.Mode = UART_ENABLE_RX | UART_ENABLE_TX; |
AnnaBridge | 172:7d866c31b3c5 | 73 | } else { |
AnnaBridge | 172:7d866c31b3c5 | 74 | if (tx != NC) { |
AnnaBridge | 172:7d866c31b3c5 | 75 | obj->uart_config.Mode = UART_ENABLE_TX; |
AnnaBridge | 172:7d866c31b3c5 | 76 | } else { |
AnnaBridge | 172:7d866c31b3c5 | 77 | if (rx != NC) { |
AnnaBridge | 172:7d866c31b3c5 | 78 | obj->uart_config.Mode = UART_ENABLE_RX; |
AnnaBridge | 172:7d866c31b3c5 | 79 | } |
AnnaBridge | 172:7d866c31b3c5 | 80 | } |
AnnaBridge | 172:7d866c31b3c5 | 81 | } |
AnnaBridge | 172:7d866c31b3c5 | 82 | |
AnnaBridge | 172:7d866c31b3c5 | 83 | // Pinout the chosen uart |
AnnaBridge | 172:7d866c31b3c5 | 84 | pinmap_pinout(tx, PinMap_UART_TX); |
AnnaBridge | 172:7d866c31b3c5 | 85 | pinmap_pinout(rx, PinMap_UART_RX); |
AnnaBridge | 172:7d866c31b3c5 | 86 | // Initialize UART configure |
AnnaBridge | 172:7d866c31b3c5 | 87 | UART_Enable(obj->UARTx); |
AnnaBridge | 172:7d866c31b3c5 | 88 | UART_SetIdleMode(obj->UARTx, ENABLE); |
AnnaBridge | 172:7d866c31b3c5 | 89 | UART_Init(obj->UARTx, &obj->uart_config); |
AnnaBridge | 172:7d866c31b3c5 | 90 | is_stdio_uart = (uart_name == STDIO_UART) ? (1) : (0); |
AnnaBridge | 172:7d866c31b3c5 | 91 | if (is_stdio_uart) { |
AnnaBridge | 172:7d866c31b3c5 | 92 | stdio_uart_inited = 1; |
AnnaBridge | 172:7d866c31b3c5 | 93 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
AnnaBridge | 172:7d866c31b3c5 | 94 | } |
AnnaBridge | 172:7d866c31b3c5 | 95 | } |
AnnaBridge | 172:7d866c31b3c5 | 96 | |
AnnaBridge | 172:7d866c31b3c5 | 97 | void serial_free(serial_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 98 | { |
AnnaBridge | 172:7d866c31b3c5 | 99 | // Disable UART |
AnnaBridge | 172:7d866c31b3c5 | 100 | UART_Disable(obj->UARTx); |
AnnaBridge | 172:7d866c31b3c5 | 101 | UART_SWReset(obj->UARTx); |
AnnaBridge | 172:7d866c31b3c5 | 102 | // Set information of object to invalid |
AnnaBridge | 172:7d866c31b3c5 | 103 | obj->uart_config.BaudRate = 0; |
AnnaBridge | 172:7d866c31b3c5 | 104 | obj->uart_config.DataBits = 0; |
AnnaBridge | 172:7d866c31b3c5 | 105 | obj->uart_config.StopBits = 0; |
AnnaBridge | 172:7d866c31b3c5 | 106 | obj->uart_config.Parity = 0; |
AnnaBridge | 172:7d866c31b3c5 | 107 | obj->uart_config.Mode = 0; |
AnnaBridge | 172:7d866c31b3c5 | 108 | obj->uart_config.FlowCtrl = 0; |
AnnaBridge | 172:7d866c31b3c5 | 109 | } |
AnnaBridge | 172:7d866c31b3c5 | 110 | |
AnnaBridge | 172:7d866c31b3c5 | 111 | void serial_baud(serial_t *obj, int baudrate) |
AnnaBridge | 172:7d866c31b3c5 | 112 | { |
AnnaBridge | 172:7d866c31b3c5 | 113 | obj->uart_config.BaudRate = baudrate; |
AnnaBridge | 172:7d866c31b3c5 | 114 | UART_Init(obj->UARTx, &obj->uart_config); |
AnnaBridge | 172:7d866c31b3c5 | 115 | } |
AnnaBridge | 172:7d866c31b3c5 | 116 | |
AnnaBridge | 172:7d866c31b3c5 | 117 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
AnnaBridge | 172:7d866c31b3c5 | 118 | { |
AnnaBridge | 172:7d866c31b3c5 | 119 | // 0: 1 stop bits, 1: 2 stop bits |
AnnaBridge | 172:7d866c31b3c5 | 120 | MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); |
AnnaBridge | 172:7d866c31b3c5 | 121 | MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || |
AnnaBridge | 172:7d866c31b3c5 | 122 | (parity == ParityForced1) || (parity == ParityForced0)); |
AnnaBridge | 172:7d866c31b3c5 | 123 | // 0: 7 data bits ... 2: 9 data bits |
AnnaBridge | 172:7d866c31b3c5 | 124 | MBED_ASSERT((data_bits > 6) && (data_bits < 10)); |
AnnaBridge | 172:7d866c31b3c5 | 125 | |
AnnaBridge | 172:7d866c31b3c5 | 126 | obj->uart_config.DataBits = data_bits; |
AnnaBridge | 172:7d866c31b3c5 | 127 | obj->uart_config.StopBits = stop_bits; |
AnnaBridge | 172:7d866c31b3c5 | 128 | obj->uart_config.Parity = parity; |
AnnaBridge | 172:7d866c31b3c5 | 129 | UART_Init(obj->UARTx, &obj->uart_config); |
AnnaBridge | 172:7d866c31b3c5 | 130 | } |
AnnaBridge | 172:7d866c31b3c5 | 131 | |
AnnaBridge | 172:7d866c31b3c5 | 132 | // INTERRUPTS HANDLING |
AnnaBridge | 172:7d866c31b3c5 | 133 | void INTTX0_IRQHandler(void) |
AnnaBridge | 172:7d866c31b3c5 | 134 | { |
AnnaBridge | 172:7d866c31b3c5 | 135 | irq_handler(serial_irq_ids[SERIAL_0], TxIrq); |
AnnaBridge | 172:7d866c31b3c5 | 136 | } |
AnnaBridge | 172:7d866c31b3c5 | 137 | |
AnnaBridge | 172:7d866c31b3c5 | 138 | void INTRX0_IRQHandler(void) |
AnnaBridge | 172:7d866c31b3c5 | 139 | { |
AnnaBridge | 172:7d866c31b3c5 | 140 | irq_handler(serial_irq_ids[SERIAL_0], RxIrq); |
AnnaBridge | 172:7d866c31b3c5 | 141 | } |
AnnaBridge | 172:7d866c31b3c5 | 142 | |
AnnaBridge | 172:7d866c31b3c5 | 143 | void INTTX1_IRQHandler(void) |
AnnaBridge | 172:7d866c31b3c5 | 144 | { |
AnnaBridge | 172:7d866c31b3c5 | 145 | irq_handler(serial_irq_ids[SERIAL_1], TxIrq); |
AnnaBridge | 172:7d866c31b3c5 | 146 | } |
AnnaBridge | 172:7d866c31b3c5 | 147 | |
AnnaBridge | 172:7d866c31b3c5 | 148 | void INTRX1_IRQHandler(void) |
AnnaBridge | 172:7d866c31b3c5 | 149 | { |
AnnaBridge | 172:7d866c31b3c5 | 150 | irq_handler(serial_irq_ids[SERIAL_1], RxIrq); |
AnnaBridge | 172:7d866c31b3c5 | 151 | } |
AnnaBridge | 172:7d866c31b3c5 | 152 | |
AnnaBridge | 172:7d866c31b3c5 | 153 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
AnnaBridge | 172:7d866c31b3c5 | 154 | { |
AnnaBridge | 172:7d866c31b3c5 | 155 | irq_handler = handler; |
AnnaBridge | 172:7d866c31b3c5 | 156 | serial_irq_ids[obj->index] = id; |
AnnaBridge | 172:7d866c31b3c5 | 157 | } |
AnnaBridge | 172:7d866c31b3c5 | 158 | |
AnnaBridge | 172:7d866c31b3c5 | 159 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
AnnaBridge | 172:7d866c31b3c5 | 160 | { |
AnnaBridge | 172:7d866c31b3c5 | 161 | IRQn_Type irq_n = (IRQn_Type)0; |
AnnaBridge | 172:7d866c31b3c5 | 162 | |
AnnaBridge | 172:7d866c31b3c5 | 163 | switch (obj->index) { |
AnnaBridge | 172:7d866c31b3c5 | 164 | case SERIAL_0: |
AnnaBridge | 172:7d866c31b3c5 | 165 | if (irq == RxIrq) { |
AnnaBridge | 172:7d866c31b3c5 | 166 | irq_n = INTRX0_IRQn; |
AnnaBridge | 172:7d866c31b3c5 | 167 | } else { |
AnnaBridge | 172:7d866c31b3c5 | 168 | irq_n = INTTX0_IRQn; |
AnnaBridge | 172:7d866c31b3c5 | 169 | } |
AnnaBridge | 172:7d866c31b3c5 | 170 | break; |
AnnaBridge | 172:7d866c31b3c5 | 171 | case SERIAL_1: |
AnnaBridge | 172:7d866c31b3c5 | 172 | if (irq == RxIrq) { |
AnnaBridge | 172:7d866c31b3c5 | 173 | irq_n = INTRX1_IRQn; |
AnnaBridge | 172:7d866c31b3c5 | 174 | } else { |
AnnaBridge | 172:7d866c31b3c5 | 175 | irq_n = INTTX1_IRQn; |
AnnaBridge | 172:7d866c31b3c5 | 176 | } |
AnnaBridge | 172:7d866c31b3c5 | 177 | break; |
AnnaBridge | 172:7d866c31b3c5 | 178 | default: |
AnnaBridge | 172:7d866c31b3c5 | 179 | break; |
AnnaBridge | 172:7d866c31b3c5 | 180 | } |
AnnaBridge | 172:7d866c31b3c5 | 181 | |
AnnaBridge | 172:7d866c31b3c5 | 182 | if (enable) { |
AnnaBridge | 172:7d866c31b3c5 | 183 | NVIC_EnableIRQ(irq_n); |
AnnaBridge | 172:7d866c31b3c5 | 184 | } else { |
AnnaBridge | 172:7d866c31b3c5 | 185 | NVIC_DisableIRQ(irq_n); |
AnnaBridge | 172:7d866c31b3c5 | 186 | } |
AnnaBridge | 172:7d866c31b3c5 | 187 | } |
AnnaBridge | 172:7d866c31b3c5 | 188 | |
AnnaBridge | 172:7d866c31b3c5 | 189 | int serial_getc(serial_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 190 | { |
AnnaBridge | 172:7d866c31b3c5 | 191 | // Wait until Rx buffer is full |
AnnaBridge | 172:7d866c31b3c5 | 192 | while (!serial_readable(obj)) { |
AnnaBridge | 172:7d866c31b3c5 | 193 | // Do nothing |
AnnaBridge | 172:7d866c31b3c5 | 194 | } |
AnnaBridge | 172:7d866c31b3c5 | 195 | |
AnnaBridge | 172:7d866c31b3c5 | 196 | return UART_GetRxData(obj->UARTx); |
AnnaBridge | 172:7d866c31b3c5 | 197 | } |
AnnaBridge | 172:7d866c31b3c5 | 198 | |
AnnaBridge | 172:7d866c31b3c5 | 199 | void serial_putc(serial_t *obj, int c) |
AnnaBridge | 172:7d866c31b3c5 | 200 | { |
AnnaBridge | 172:7d866c31b3c5 | 201 | // Wait until Tx buffer is empty |
AnnaBridge | 172:7d866c31b3c5 | 202 | while (!serial_writable(obj)) { |
AnnaBridge | 172:7d866c31b3c5 | 203 | // Do nothing |
AnnaBridge | 172:7d866c31b3c5 | 204 | } |
AnnaBridge | 172:7d866c31b3c5 | 205 | |
AnnaBridge | 172:7d866c31b3c5 | 206 | UART_SetTxData(obj->UARTx, (uint32_t)c); |
AnnaBridge | 172:7d866c31b3c5 | 207 | } |
AnnaBridge | 172:7d866c31b3c5 | 208 | |
AnnaBridge | 172:7d866c31b3c5 | 209 | int serial_readable(serial_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 210 | { |
AnnaBridge | 172:7d866c31b3c5 | 211 | int ret = 0; |
AnnaBridge | 172:7d866c31b3c5 | 212 | |
AnnaBridge | 172:7d866c31b3c5 | 213 | if (UART_GetBufState(obj->UARTx, UART_RX) == DONE) { |
AnnaBridge | 172:7d866c31b3c5 | 214 | ret = 1; |
AnnaBridge | 172:7d866c31b3c5 | 215 | } |
AnnaBridge | 172:7d866c31b3c5 | 216 | |
AnnaBridge | 172:7d866c31b3c5 | 217 | return ret; |
AnnaBridge | 172:7d866c31b3c5 | 218 | } |
AnnaBridge | 172:7d866c31b3c5 | 219 | |
AnnaBridge | 172:7d866c31b3c5 | 220 | int serial_writable(serial_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 221 | { |
AnnaBridge | 172:7d866c31b3c5 | 222 | int ret = 0; |
AnnaBridge | 172:7d866c31b3c5 | 223 | |
AnnaBridge | 172:7d866c31b3c5 | 224 | if (UART_GetBufState(obj->UARTx, UART_TX) == DONE) { |
AnnaBridge | 172:7d866c31b3c5 | 225 | ret = 1; |
AnnaBridge | 172:7d866c31b3c5 | 226 | } |
AnnaBridge | 172:7d866c31b3c5 | 227 | |
AnnaBridge | 172:7d866c31b3c5 | 228 | return ret; |
AnnaBridge | 172:7d866c31b3c5 | 229 | } |
AnnaBridge | 172:7d866c31b3c5 | 230 | |
AnnaBridge | 172:7d866c31b3c5 | 231 | void serial_clear(serial_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 232 | { |
AnnaBridge | 172:7d866c31b3c5 | 233 | UART_TxFIFOClear(obj->UARTx); |
AnnaBridge | 172:7d866c31b3c5 | 234 | UART_RxFIFOClear(obj->UARTx); |
AnnaBridge | 172:7d866c31b3c5 | 235 | UART_TxBufferClear(obj->UARTx); |
AnnaBridge | 172:7d866c31b3c5 | 236 | } |
AnnaBridge | 172:7d866c31b3c5 | 237 | |
AnnaBridge | 172:7d866c31b3c5 | 238 | void serial_pinout_tx(PinName tx) |
AnnaBridge | 172:7d866c31b3c5 | 239 | { |
AnnaBridge | 172:7d866c31b3c5 | 240 | pinmap_pinout(tx, PinMap_UART_TX); |
AnnaBridge | 172:7d866c31b3c5 | 241 | } |
AnnaBridge | 172:7d866c31b3c5 | 242 | |
AnnaBridge | 172:7d866c31b3c5 | 243 | void serial_break_set(serial_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 244 | { |
AnnaBridge | 172:7d866c31b3c5 | 245 | } |
AnnaBridge | 172:7d866c31b3c5 | 246 | |
AnnaBridge | 172:7d866c31b3c5 | 247 | void serial_break_clear(serial_t *obj) |
AnnaBridge | 172:7d866c31b3c5 | 248 | { |
AnnaBridge | 172:7d866c31b3c5 | 249 | } |