mbed
Fork of mbed-dev by
targets/TARGET_Freescale/TARGET_K20XX/serial_api.c@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 149:156823d33999
mbed-dev library. Release version 159.
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-2015 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 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 17 | #include "serial_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | |
<> | 144:ef7eb2e8f9f7 | 19 | #include <string.h> |
<> | 144:ef7eb2e8f9f7 | 20 | |
<> | 144:ef7eb2e8f9f7 | 21 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 22 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 23 | #include "clk_freqs.h" |
<> | 144:ef7eb2e8f9f7 | 24 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 25 | |
<> | 144:ef7eb2e8f9f7 | 26 | #define UART_NUM 3 |
<> | 144:ef7eb2e8f9f7 | 27 | |
<> | 144:ef7eb2e8f9f7 | 28 | static uint32_t serial_irq_ids[UART_NUM] = {0}; |
<> | 144:ef7eb2e8f9f7 | 29 | static uart_irq_handler irq_handler; |
<> | 144:ef7eb2e8f9f7 | 30 | |
<> | 144:ef7eb2e8f9f7 | 31 | int stdio_uart_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 32 | serial_t stdio_uart; |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | void serial_init(serial_t *obj, PinName tx, PinName rx) { |
<> | 144:ef7eb2e8f9f7 | 35 | // determine the UART to use |
<> | 144:ef7eb2e8f9f7 | 36 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 37 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
<> | 144:ef7eb2e8f9f7 | 38 | UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
<> | 144:ef7eb2e8f9f7 | 39 | MBED_ASSERT((int)uart != NC); |
<> | 144:ef7eb2e8f9f7 | 40 | |
<> | 144:ef7eb2e8f9f7 | 41 | obj->uart = (UART_Type *)uart; |
<> | 144:ef7eb2e8f9f7 | 42 | // enable clk |
<> | 144:ef7eb2e8f9f7 | 43 | switch (uart) { |
<> | 144:ef7eb2e8f9f7 | 44 | case UART_0: |
<> | 144:ef7eb2e8f9f7 | 45 | mcgpllfll_frequency(); |
<> | 144:ef7eb2e8f9f7 | 46 | SIM->SCGC4 |= SIM_SCGC4_UART0_MASK; |
<> | 144:ef7eb2e8f9f7 | 47 | break; |
<> | 144:ef7eb2e8f9f7 | 48 | case UART_1: |
<> | 144:ef7eb2e8f9f7 | 49 | mcgpllfll_frequency(); |
<> | 144:ef7eb2e8f9f7 | 50 | SIM->SCGC4 |= SIM_SCGC4_UART1_MASK; |
<> | 144:ef7eb2e8f9f7 | 51 | break; |
<> | 144:ef7eb2e8f9f7 | 52 | case UART_2: |
<> | 144:ef7eb2e8f9f7 | 53 | SIM->SCGC4 |= SIM_SCGC4_UART2_MASK; |
<> | 144:ef7eb2e8f9f7 | 54 | break; |
<> | 144:ef7eb2e8f9f7 | 55 | } |
<> | 144:ef7eb2e8f9f7 | 56 | // Disable UART before changing registers |
<> | 144:ef7eb2e8f9f7 | 57 | obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK); |
<> | 144:ef7eb2e8f9f7 | 58 | |
<> | 144:ef7eb2e8f9f7 | 59 | switch (uart) { |
<> | 144:ef7eb2e8f9f7 | 60 | case UART_0: |
<> | 144:ef7eb2e8f9f7 | 61 | obj->index = 0; |
<> | 144:ef7eb2e8f9f7 | 62 | break; |
<> | 144:ef7eb2e8f9f7 | 63 | case UART_1: |
<> | 144:ef7eb2e8f9f7 | 64 | obj->index = 1; |
<> | 144:ef7eb2e8f9f7 | 65 | break; |
<> | 144:ef7eb2e8f9f7 | 66 | case UART_2: |
<> | 144:ef7eb2e8f9f7 | 67 | obj->index = 2; |
<> | 144:ef7eb2e8f9f7 | 68 | break; |
<> | 144:ef7eb2e8f9f7 | 69 | } |
<> | 144:ef7eb2e8f9f7 | 70 | |
<> | 144:ef7eb2e8f9f7 | 71 | // set default baud rate and format |
<> | 144:ef7eb2e8f9f7 | 72 | serial_baud (obj, 9600); |
<> | 144:ef7eb2e8f9f7 | 73 | serial_format(obj, 8, ParityNone, 1); |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | // pinout the chosen uart |
<> | 144:ef7eb2e8f9f7 | 76 | pinmap_pinout(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 77 | pinmap_pinout(rx, PinMap_UART_RX); |
<> | 144:ef7eb2e8f9f7 | 78 | |
<> | 144:ef7eb2e8f9f7 | 79 | // set rx/tx pins in PullUp mode |
<> | 144:ef7eb2e8f9f7 | 80 | if (tx != NC) { |
<> | 144:ef7eb2e8f9f7 | 81 | pin_mode(tx, PullUp); |
<> | 144:ef7eb2e8f9f7 | 82 | } |
<> | 144:ef7eb2e8f9f7 | 83 | if (rx != NC) { |
<> | 144:ef7eb2e8f9f7 | 84 | pin_mode(rx, PullUp); |
<> | 144:ef7eb2e8f9f7 | 85 | } |
<> | 144:ef7eb2e8f9f7 | 86 | |
<> | 144:ef7eb2e8f9f7 | 87 | obj->uart->C2 |= (UART_C2_RE_MASK | UART_C2_TE_MASK); |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | if (uart == STDIO_UART) { |
<> | 144:ef7eb2e8f9f7 | 90 | stdio_uart_inited = 1; |
<> | 144:ef7eb2e8f9f7 | 91 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
<> | 144:ef7eb2e8f9f7 | 92 | } |
<> | 144:ef7eb2e8f9f7 | 93 | } |
<> | 144:ef7eb2e8f9f7 | 94 | |
<> | 144:ef7eb2e8f9f7 | 95 | void serial_free(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 96 | serial_irq_ids[obj->index] = 0; |
<> | 144:ef7eb2e8f9f7 | 97 | } |
<> | 144:ef7eb2e8f9f7 | 98 | |
<> | 144:ef7eb2e8f9f7 | 99 | void serial_baud(serial_t *obj, int baudrate) { |
<> | 144:ef7eb2e8f9f7 | 100 | // save C2 state |
<> | 144:ef7eb2e8f9f7 | 101 | uint8_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK)); |
<> | 144:ef7eb2e8f9f7 | 102 | |
<> | 144:ef7eb2e8f9f7 | 103 | // Disable UART before changing registers |
<> | 144:ef7eb2e8f9f7 | 104 | obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK); |
<> | 144:ef7eb2e8f9f7 | 105 | |
<> | 144:ef7eb2e8f9f7 | 106 | uint32_t PCLK; |
<> | 144:ef7eb2e8f9f7 | 107 | if (obj->uart != UART2) { |
<> | 144:ef7eb2e8f9f7 | 108 | PCLK = mcgpllfll_frequency(); |
<> | 144:ef7eb2e8f9f7 | 109 | } |
<> | 144:ef7eb2e8f9f7 | 110 | else { |
<> | 144:ef7eb2e8f9f7 | 111 | PCLK = bus_frequency(); |
<> | 144:ef7eb2e8f9f7 | 112 | } |
<> | 144:ef7eb2e8f9f7 | 113 | |
<> | 144:ef7eb2e8f9f7 | 114 | uint16_t DL = PCLK / (16 * baudrate); |
<> | 144:ef7eb2e8f9f7 | 115 | uint32_t BRFA = (2 * PCLK) / baudrate - 32 * DL; |
<> | 144:ef7eb2e8f9f7 | 116 | |
<> | 144:ef7eb2e8f9f7 | 117 | // set BDH and BDL |
<> | 144:ef7eb2e8f9f7 | 118 | obj->uart->BDH = (obj->uart->BDH & ~(0x1f)) | ((DL >> 8) & 0x1f); |
<> | 144:ef7eb2e8f9f7 | 119 | obj->uart->BDL = (obj->uart->BDL & ~(0xff)) | ((DL >> 0) & 0xff); |
<> | 144:ef7eb2e8f9f7 | 120 | |
<> | 144:ef7eb2e8f9f7 | 121 | obj->uart->C4 &= ~0x1F; |
<> | 144:ef7eb2e8f9f7 | 122 | obj->uart->C4 |= BRFA & 0x1F; |
<> | 144:ef7eb2e8f9f7 | 123 | |
<> | 144:ef7eb2e8f9f7 | 124 | // restore C2 state |
<> | 144:ef7eb2e8f9f7 | 125 | obj->uart->C2 |= c2_state; |
<> | 144:ef7eb2e8f9f7 | 126 | } |
<> | 144:ef7eb2e8f9f7 | 127 | |
<> | 144:ef7eb2e8f9f7 | 128 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { |
<> | 144:ef7eb2e8f9f7 | 129 | MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); |
<> | 144:ef7eb2e8f9f7 | 130 | MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven)); |
<> | 144:ef7eb2e8f9f7 | 131 | MBED_ASSERT((data_bits == 8) || (data_bits == 9)); |
<> | 144:ef7eb2e8f9f7 | 132 | |
<> | 144:ef7eb2e8f9f7 | 133 | // save C2 state |
<> | 144:ef7eb2e8f9f7 | 134 | uint32_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK)); |
<> | 144:ef7eb2e8f9f7 | 135 | |
<> | 144:ef7eb2e8f9f7 | 136 | // Disable UART before changing registers |
<> | 144:ef7eb2e8f9f7 | 137 | obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK); |
<> | 144:ef7eb2e8f9f7 | 138 | |
<> | 144:ef7eb2e8f9f7 | 139 | // 8 data bits = 0 ... 9 data bits = 1 |
<> | 144:ef7eb2e8f9f7 | 140 | data_bits -= 8; |
<> | 144:ef7eb2e8f9f7 | 141 | |
<> | 144:ef7eb2e8f9f7 | 142 | uint32_t parity_enable, parity_select; |
<> | 144:ef7eb2e8f9f7 | 143 | switch (parity) { |
<> | 144:ef7eb2e8f9f7 | 144 | case ParityNone: |
<> | 144:ef7eb2e8f9f7 | 145 | parity_enable = 0; |
<> | 144:ef7eb2e8f9f7 | 146 | parity_select = 0; |
<> | 144:ef7eb2e8f9f7 | 147 | break; |
<> | 144:ef7eb2e8f9f7 | 148 | case ParityOdd : |
<> | 144:ef7eb2e8f9f7 | 149 | parity_enable = 1; |
<> | 144:ef7eb2e8f9f7 | 150 | parity_select = 1; |
<> | 144:ef7eb2e8f9f7 | 151 | data_bits++; |
<> | 144:ef7eb2e8f9f7 | 152 | break; |
<> | 144:ef7eb2e8f9f7 | 153 | case ParityEven: |
<> | 144:ef7eb2e8f9f7 | 154 | parity_enable = 1; |
<> | 144:ef7eb2e8f9f7 | 155 | parity_select = 0; |
<> | 144:ef7eb2e8f9f7 | 156 | data_bits++; |
<> | 144:ef7eb2e8f9f7 | 157 | break; |
<> | 144:ef7eb2e8f9f7 | 158 | default: |
<> | 144:ef7eb2e8f9f7 | 159 | break; |
<> | 144:ef7eb2e8f9f7 | 160 | } |
<> | 144:ef7eb2e8f9f7 | 161 | |
<> | 144:ef7eb2e8f9f7 | 162 | stop_bits -= 1; |
<> | 144:ef7eb2e8f9f7 | 163 | |
<> | 144:ef7eb2e8f9f7 | 164 | uint32_t m10 = 0; |
<> | 144:ef7eb2e8f9f7 | 165 | |
<> | 144:ef7eb2e8f9f7 | 166 | // 9 data bits + parity - only uart0 support |
<> | 144:ef7eb2e8f9f7 | 167 | if (data_bits == 2) { |
<> | 144:ef7eb2e8f9f7 | 168 | MBED_ASSERT(obj->index == 0); |
<> | 144:ef7eb2e8f9f7 | 169 | data_bits = 0; |
<> | 144:ef7eb2e8f9f7 | 170 | m10 = 1; |
<> | 144:ef7eb2e8f9f7 | 171 | } |
<> | 144:ef7eb2e8f9f7 | 172 | |
<> | 144:ef7eb2e8f9f7 | 173 | // data bits, parity and parity mode |
<> | 144:ef7eb2e8f9f7 | 174 | obj->uart->C1 = ((data_bits << 4) |
<> | 144:ef7eb2e8f9f7 | 175 | | (parity_enable << 1) |
<> | 144:ef7eb2e8f9f7 | 176 | | (parity_select << 0)); |
<> | 144:ef7eb2e8f9f7 | 177 | |
<> | 144:ef7eb2e8f9f7 | 178 | //enable 10bit mode if needed |
<> | 144:ef7eb2e8f9f7 | 179 | if (obj->index == 0) { |
<> | 144:ef7eb2e8f9f7 | 180 | obj->uart->C4 &= ~UART_C4_M10_MASK; |
<> | 144:ef7eb2e8f9f7 | 181 | obj->uart->C4 |= (m10 << UART_C4_M10_SHIFT); |
<> | 144:ef7eb2e8f9f7 | 182 | } |
<> | 144:ef7eb2e8f9f7 | 183 | |
<> | 144:ef7eb2e8f9f7 | 184 | // stop bits |
<> | 144:ef7eb2e8f9f7 | 185 | obj->uart->BDH &= ~UART_BDH_SBR_MASK; |
<> | 144:ef7eb2e8f9f7 | 186 | obj->uart->BDH |= (stop_bits << UART_BDH_SBR_SHIFT); |
<> | 144:ef7eb2e8f9f7 | 187 | |
<> | 144:ef7eb2e8f9f7 | 188 | // restore C2 state |
<> | 144:ef7eb2e8f9f7 | 189 | obj->uart->C2 |= c2_state; |
<> | 144:ef7eb2e8f9f7 | 190 | } |
<> | 144:ef7eb2e8f9f7 | 191 | |
<> | 144:ef7eb2e8f9f7 | 192 | /****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 193 | * INTERRUPTS HANDLING |
<> | 144:ef7eb2e8f9f7 | 194 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 195 | static inline void uart_irq(uint8_t status, uint32_t index) { |
<> | 144:ef7eb2e8f9f7 | 196 | if (serial_irq_ids[index] != 0) { |
<> | 144:ef7eb2e8f9f7 | 197 | if (status & UART_S1_TDRE_MASK) |
<> | 144:ef7eb2e8f9f7 | 198 | irq_handler(serial_irq_ids[index], TxIrq); |
<> | 144:ef7eb2e8f9f7 | 199 | |
<> | 144:ef7eb2e8f9f7 | 200 | if (status & UART_S1_RDRF_MASK) |
<> | 144:ef7eb2e8f9f7 | 201 | irq_handler(serial_irq_ids[index], RxIrq); |
<> | 144:ef7eb2e8f9f7 | 202 | } |
<> | 144:ef7eb2e8f9f7 | 203 | } |
<> | 144:ef7eb2e8f9f7 | 204 | |
<> | 144:ef7eb2e8f9f7 | 205 | void uart0_irq() {uart_irq(UART0->S1, 0);} |
<> | 144:ef7eb2e8f9f7 | 206 | void uart1_irq() {uart_irq(UART1->S1, 1);} |
<> | 144:ef7eb2e8f9f7 | 207 | void uart2_irq() {uart_irq(UART2->S1, 2);} |
<> | 144:ef7eb2e8f9f7 | 208 | |
<> | 144:ef7eb2e8f9f7 | 209 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { |
<> | 144:ef7eb2e8f9f7 | 210 | irq_handler = handler; |
<> | 144:ef7eb2e8f9f7 | 211 | serial_irq_ids[obj->index] = id; |
<> | 144:ef7eb2e8f9f7 | 212 | } |
<> | 144:ef7eb2e8f9f7 | 213 | |
<> | 144:ef7eb2e8f9f7 | 214 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) { |
<> | 144:ef7eb2e8f9f7 | 215 | IRQn_Type irq_n = (IRQn_Type)0; |
<> | 144:ef7eb2e8f9f7 | 216 | uint32_t vector = 0; |
<> | 144:ef7eb2e8f9f7 | 217 | switch ((int)obj->uart) { |
<> | 144:ef7eb2e8f9f7 | 218 | case UART_0: |
<> | 144:ef7eb2e8f9f7 | 219 | irq_n=UART0_RX_TX_IRQn; |
<> | 144:ef7eb2e8f9f7 | 220 | vector = (uint32_t)&uart0_irq; |
<> | 144:ef7eb2e8f9f7 | 221 | break; |
<> | 144:ef7eb2e8f9f7 | 222 | case UART_1: |
<> | 144:ef7eb2e8f9f7 | 223 | irq_n=UART1_RX_TX_IRQn; |
<> | 144:ef7eb2e8f9f7 | 224 | vector = (uint32_t)&uart1_irq; |
<> | 144:ef7eb2e8f9f7 | 225 | break; |
<> | 144:ef7eb2e8f9f7 | 226 | case UART_2: |
<> | 144:ef7eb2e8f9f7 | 227 | irq_n=UART2_RX_TX_IRQn; |
<> | 144:ef7eb2e8f9f7 | 228 | vector = (uint32_t)&uart2_irq; |
<> | 144:ef7eb2e8f9f7 | 229 | break; |
<> | 144:ef7eb2e8f9f7 | 230 | } |
<> | 144:ef7eb2e8f9f7 | 231 | |
<> | 144:ef7eb2e8f9f7 | 232 | if (enable) { |
<> | 144:ef7eb2e8f9f7 | 233 | switch (irq) { |
<> | 144:ef7eb2e8f9f7 | 234 | case RxIrq: |
<> | 144:ef7eb2e8f9f7 | 235 | obj->uart->C2 |= (UART_C2_RIE_MASK); |
<> | 144:ef7eb2e8f9f7 | 236 | break; |
<> | 144:ef7eb2e8f9f7 | 237 | case TxIrq: |
<> | 144:ef7eb2e8f9f7 | 238 | obj->uart->C2 |= (UART_C2_TIE_MASK); |
<> | 144:ef7eb2e8f9f7 | 239 | break; |
<> | 144:ef7eb2e8f9f7 | 240 | } |
<> | 144:ef7eb2e8f9f7 | 241 | NVIC_SetVector(irq_n, vector); |
<> | 144:ef7eb2e8f9f7 | 242 | NVIC_EnableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 243 | |
<> | 144:ef7eb2e8f9f7 | 244 | } else { // disable |
<> | 144:ef7eb2e8f9f7 | 245 | int all_disabled = 0; |
<> | 144:ef7eb2e8f9f7 | 246 | SerialIrq other_irq = (irq == RxIrq) ? (TxIrq) : (RxIrq); |
<> | 144:ef7eb2e8f9f7 | 247 | switch (irq) { |
<> | 144:ef7eb2e8f9f7 | 248 | case RxIrq: |
<> | 144:ef7eb2e8f9f7 | 249 | obj->uart->C2 &= ~(UART_C2_RIE_MASK); |
<> | 144:ef7eb2e8f9f7 | 250 | break; |
<> | 144:ef7eb2e8f9f7 | 251 | case TxIrq: |
<> | 144:ef7eb2e8f9f7 | 252 | obj->uart->C2 &= ~(UART_C2_TIE_MASK); |
<> | 144:ef7eb2e8f9f7 | 253 | break; |
<> | 144:ef7eb2e8f9f7 | 254 | } |
<> | 144:ef7eb2e8f9f7 | 255 | switch (other_irq) { |
<> | 144:ef7eb2e8f9f7 | 256 | case RxIrq: |
<> | 144:ef7eb2e8f9f7 | 257 | all_disabled = (obj->uart->C2 & (UART_C2_RIE_MASK)) == 0; |
<> | 144:ef7eb2e8f9f7 | 258 | break; |
<> | 144:ef7eb2e8f9f7 | 259 | case TxIrq: |
<> | 144:ef7eb2e8f9f7 | 260 | all_disabled = (obj->uart->C2 & (UART_C2_TIE_MASK)) == 0; |
<> | 144:ef7eb2e8f9f7 | 261 | break; |
<> | 144:ef7eb2e8f9f7 | 262 | } |
<> | 144:ef7eb2e8f9f7 | 263 | if (all_disabled) |
<> | 144:ef7eb2e8f9f7 | 264 | NVIC_DisableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 265 | } |
<> | 144:ef7eb2e8f9f7 | 266 | } |
<> | 144:ef7eb2e8f9f7 | 267 | |
<> | 144:ef7eb2e8f9f7 | 268 | int serial_getc(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 269 | while (!serial_readable(obj)); |
<> | 144:ef7eb2e8f9f7 | 270 | return obj->uart->D; |
<> | 144:ef7eb2e8f9f7 | 271 | } |
<> | 144:ef7eb2e8f9f7 | 272 | |
<> | 144:ef7eb2e8f9f7 | 273 | void serial_putc(serial_t *obj, int c) { |
<> | 144:ef7eb2e8f9f7 | 274 | while (!serial_writable(obj)); |
<> | 144:ef7eb2e8f9f7 | 275 | obj->uart->D = c; |
<> | 144:ef7eb2e8f9f7 | 276 | } |
<> | 144:ef7eb2e8f9f7 | 277 | |
<> | 144:ef7eb2e8f9f7 | 278 | int serial_readable(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 279 | |
<> | 144:ef7eb2e8f9f7 | 280 | return (obj->uart->S1 & UART_S1_RDRF_MASK); |
<> | 144:ef7eb2e8f9f7 | 281 | } |
<> | 144:ef7eb2e8f9f7 | 282 | |
<> | 144:ef7eb2e8f9f7 | 283 | int serial_writable(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 284 | |
<> | 144:ef7eb2e8f9f7 | 285 | return (obj->uart->S1 & UART_S1_TDRE_MASK); |
<> | 144:ef7eb2e8f9f7 | 286 | } |
<> | 144:ef7eb2e8f9f7 | 287 | |
<> | 144:ef7eb2e8f9f7 | 288 | void serial_clear(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 289 | } |
<> | 144:ef7eb2e8f9f7 | 290 | |
<> | 144:ef7eb2e8f9f7 | 291 | void serial_pinout_tx(PinName tx) { |
<> | 144:ef7eb2e8f9f7 | 292 | pinmap_pinout(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 293 | } |
<> | 144:ef7eb2e8f9f7 | 294 | |
<> | 144:ef7eb2e8f9f7 | 295 | void serial_break_set(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 296 | obj->uart->C2 |= UART_C2_SBK_MASK; |
<> | 144:ef7eb2e8f9f7 | 297 | } |
<> | 144:ef7eb2e8f9f7 | 298 | |
<> | 144:ef7eb2e8f9f7 | 299 | void serial_break_clear(serial_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 300 | obj->uart->C2 &= ~UART_C2_SBK_MASK; |
<> | 144:ef7eb2e8f9f7 | 301 | } |