mbed
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 177:447f873cad2f
mbed-dev library. Release version 159.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 175:b96e65c34a4d | 1 | /* mbed Microcontroller Library |
AnnaBridge | 175:b96e65c34a4d | 2 | * Copyright (c) 2015-2017 Nuvoton |
AnnaBridge | 175:b96e65c34a4d | 3 | * |
AnnaBridge | 175:b96e65c34a4d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 175:b96e65c34a4d | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 175:b96e65c34a4d | 6 | * You may obtain a copy of the License at |
AnnaBridge | 175:b96e65c34a4d | 7 | * |
AnnaBridge | 175:b96e65c34a4d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 175:b96e65c34a4d | 9 | * |
AnnaBridge | 175:b96e65c34a4d | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 175:b96e65c34a4d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 175:b96e65c34a4d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 175:b96e65c34a4d | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 175:b96e65c34a4d | 14 | * limitations under the License. |
AnnaBridge | 175:b96e65c34a4d | 15 | */ |
AnnaBridge | 175:b96e65c34a4d | 16 | |
AnnaBridge | 175:b96e65c34a4d | 17 | #include "serial_api.h" |
AnnaBridge | 175:b96e65c34a4d | 18 | |
AnnaBridge | 175:b96e65c34a4d | 19 | #if DEVICE_SERIAL |
AnnaBridge | 175:b96e65c34a4d | 20 | |
AnnaBridge | 175:b96e65c34a4d | 21 | #include <string.h> |
AnnaBridge | 175:b96e65c34a4d | 22 | #include "cmsis.h" |
AnnaBridge | 175:b96e65c34a4d | 23 | #include "mbed_error.h" |
AnnaBridge | 175:b96e65c34a4d | 24 | #include "mbed_assert.h" |
AnnaBridge | 175:b96e65c34a4d | 25 | #include "PeripheralPins.h" |
AnnaBridge | 175:b96e65c34a4d | 26 | #include "nu_modutil.h" |
AnnaBridge | 175:b96e65c34a4d | 27 | #include "nu_bitutil.h" |
AnnaBridge | 175:b96e65c34a4d | 28 | |
AnnaBridge | 175:b96e65c34a4d | 29 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 30 | #include "dma_api.h" |
AnnaBridge | 175:b96e65c34a4d | 31 | #include "dma.h" |
AnnaBridge | 175:b96e65c34a4d | 32 | #endif |
AnnaBridge | 175:b96e65c34a4d | 33 | |
AnnaBridge | 175:b96e65c34a4d | 34 | struct nu_uart_var { |
AnnaBridge | 175:b96e65c34a4d | 35 | uint32_t ref_cnt; // Reference count of the H/W module |
AnnaBridge | 175:b96e65c34a4d | 36 | serial_t * obj; |
AnnaBridge | 175:b96e65c34a4d | 37 | uint32_t fifo_size_tx; |
AnnaBridge | 175:b96e65c34a4d | 38 | uint32_t fifo_size_rx; |
AnnaBridge | 175:b96e65c34a4d | 39 | void (*vec)(void); |
AnnaBridge | 175:b96e65c34a4d | 40 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 41 | uint8_t pdma_perp_tx; |
AnnaBridge | 175:b96e65c34a4d | 42 | uint8_t pdma_perp_rx; |
AnnaBridge | 175:b96e65c34a4d | 43 | #endif |
AnnaBridge | 175:b96e65c34a4d | 44 | }; |
AnnaBridge | 175:b96e65c34a4d | 45 | |
AnnaBridge | 175:b96e65c34a4d | 46 | void UART0_IRQHandler(void); |
AnnaBridge | 175:b96e65c34a4d | 47 | void UART1_IRQHandler(void); |
AnnaBridge | 175:b96e65c34a4d | 48 | static void uart_irq(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 49 | |
AnnaBridge | 175:b96e65c34a4d | 50 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 51 | static void uart_irq_async(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 52 | |
AnnaBridge | 175:b96e65c34a4d | 53 | static void uart_dma_handler_tx(uint32_t id, uint32_t event); |
AnnaBridge | 175:b96e65c34a4d | 54 | static void uart_dma_handler_rx(uint32_t id, uint32_t event); |
AnnaBridge | 175:b96e65c34a4d | 55 | |
AnnaBridge | 175:b96e65c34a4d | 56 | static void serial_tx_enable_interrupt(serial_t *obj, uint32_t address, uint8_t enable); |
AnnaBridge | 175:b96e65c34a4d | 57 | static void serial_rx_enable_interrupt(serial_t *obj, uint32_t address, uint8_t enable); |
AnnaBridge | 175:b96e65c34a4d | 58 | static void serial_enable_interrupt(serial_t *obj, SerialIrq irq, uint32_t enable); |
AnnaBridge | 175:b96e65c34a4d | 59 | static void serial_rollback_interrupt(serial_t *obj, SerialIrq irq); |
AnnaBridge | 175:b96e65c34a4d | 60 | static int serial_write_async(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 61 | static int serial_read_async(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 62 | |
AnnaBridge | 175:b96e65c34a4d | 63 | static uint32_t serial_rx_event_check(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 64 | static uint32_t serial_tx_event_check(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 65 | |
AnnaBridge | 175:b96e65c34a4d | 66 | static int serial_is_tx_complete(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 67 | static void serial_tx_enable_event(serial_t *obj, int event, uint8_t enable); |
AnnaBridge | 175:b96e65c34a4d | 68 | |
AnnaBridge | 175:b96e65c34a4d | 69 | static void serial_tx_buffer_set(serial_t *obj, const void *tx, size_t length, uint8_t width); |
AnnaBridge | 175:b96e65c34a4d | 70 | static void serial_rx_buffer_set(serial_t *obj, void *rx, size_t length, uint8_t width); |
AnnaBridge | 175:b96e65c34a4d | 71 | static void serial_rx_set_char_match(serial_t *obj, uint8_t char_match); |
AnnaBridge | 175:b96e65c34a4d | 72 | static void serial_rx_enable_event(serial_t *obj, int event, uint8_t enable); |
AnnaBridge | 175:b96e65c34a4d | 73 | static int serial_is_rx_complete(serial_t *obj); |
AnnaBridge | 175:b96e65c34a4d | 74 | |
AnnaBridge | 175:b96e65c34a4d | 75 | static void serial_check_dma_usage(DMAUsage *dma_usage, int *dma_ch); |
AnnaBridge | 175:b96e65c34a4d | 76 | static int serial_is_irq_en(serial_t *obj, SerialIrq irq); |
AnnaBridge | 175:b96e65c34a4d | 77 | #endif |
AnnaBridge | 175:b96e65c34a4d | 78 | |
AnnaBridge | 175:b96e65c34a4d | 79 | static struct nu_uart_var uart0_var = { |
AnnaBridge | 175:b96e65c34a4d | 80 | .ref_cnt = 0, |
AnnaBridge | 175:b96e65c34a4d | 81 | .obj = NULL, |
AnnaBridge | 175:b96e65c34a4d | 82 | .fifo_size_tx = 16, |
AnnaBridge | 175:b96e65c34a4d | 83 | .fifo_size_rx = 16, |
AnnaBridge | 175:b96e65c34a4d | 84 | .vec = UART0_IRQHandler, |
AnnaBridge | 175:b96e65c34a4d | 85 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 86 | .pdma_perp_tx = PDMA_UART0_TX, |
AnnaBridge | 175:b96e65c34a4d | 87 | .pdma_perp_rx = PDMA_UART0_RX |
AnnaBridge | 175:b96e65c34a4d | 88 | #endif |
AnnaBridge | 175:b96e65c34a4d | 89 | }; |
AnnaBridge | 175:b96e65c34a4d | 90 | static struct nu_uart_var uart1_var = { |
AnnaBridge | 175:b96e65c34a4d | 91 | .ref_cnt = 0, |
AnnaBridge | 175:b96e65c34a4d | 92 | .obj = NULL, |
AnnaBridge | 175:b96e65c34a4d | 93 | .fifo_size_tx = 16, |
AnnaBridge | 175:b96e65c34a4d | 94 | .fifo_size_rx = 16, |
AnnaBridge | 175:b96e65c34a4d | 95 | .vec = UART1_IRQHandler, |
AnnaBridge | 175:b96e65c34a4d | 96 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 97 | .pdma_perp_tx = PDMA_UART1_TX, |
AnnaBridge | 175:b96e65c34a4d | 98 | .pdma_perp_rx = PDMA_UART1_RX |
AnnaBridge | 175:b96e65c34a4d | 99 | #endif |
AnnaBridge | 175:b96e65c34a4d | 100 | }; |
AnnaBridge | 175:b96e65c34a4d | 101 | |
AnnaBridge | 175:b96e65c34a4d | 102 | |
AnnaBridge | 175:b96e65c34a4d | 103 | int stdio_uart_inited = 0; |
AnnaBridge | 175:b96e65c34a4d | 104 | serial_t stdio_uart; |
AnnaBridge | 175:b96e65c34a4d | 105 | static uint32_t uart_modinit_mask = 0; |
AnnaBridge | 175:b96e65c34a4d | 106 | |
AnnaBridge | 175:b96e65c34a4d | 107 | static const struct nu_modinit_s uart_modinit_tab[] = { |
AnnaBridge | 175:b96e65c34a4d | 108 | {UART_0, UART0_MODULE, CLK_CLKSEL1_UART_S_HIRC, CLK_UART_CLK_DIVIDER(1), UART0_RST, UART0_IRQn, &uart0_var}, |
AnnaBridge | 175:b96e65c34a4d | 109 | {UART_1, UART1_MODULE, CLK_CLKSEL1_UART_S_HIRC, CLK_UART_CLK_DIVIDER(1), UART1_RST, UART1_IRQn, &uart1_var}, |
AnnaBridge | 175:b96e65c34a4d | 110 | |
AnnaBridge | 175:b96e65c34a4d | 111 | {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL} |
AnnaBridge | 175:b96e65c34a4d | 112 | }; |
AnnaBridge | 175:b96e65c34a4d | 113 | |
AnnaBridge | 175:b96e65c34a4d | 114 | extern void mbed_sdk_init(void); |
AnnaBridge | 175:b96e65c34a4d | 115 | |
AnnaBridge | 175:b96e65c34a4d | 116 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
AnnaBridge | 175:b96e65c34a4d | 117 | { |
AnnaBridge | 175:b96e65c34a4d | 118 | // NOTE: With armcc, serial_init() gets called from _sys_open() timing of which is before main()/mbed_sdk_init(). |
AnnaBridge | 175:b96e65c34a4d | 119 | mbed_sdk_init(); |
AnnaBridge | 175:b96e65c34a4d | 120 | |
AnnaBridge | 175:b96e65c34a4d | 121 | // Determine which UART_x the pins are used for |
AnnaBridge | 175:b96e65c34a4d | 122 | uint32_t uart_tx = pinmap_peripheral(tx, PinMap_UART_TX); |
AnnaBridge | 175:b96e65c34a4d | 123 | uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX); |
AnnaBridge | 175:b96e65c34a4d | 124 | // Get the peripheral name (UART_x) from the pins and assign it to the object |
AnnaBridge | 175:b96e65c34a4d | 125 | obj->serial.uart = (UARTName) pinmap_merge(uart_tx, uart_rx); |
AnnaBridge | 175:b96e65c34a4d | 126 | MBED_ASSERT((int)obj->serial.uart != NC); |
AnnaBridge | 175:b96e65c34a4d | 127 | |
AnnaBridge | 175:b96e65c34a4d | 128 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 129 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 130 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 131 | |
AnnaBridge | 175:b96e65c34a4d | 132 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
AnnaBridge | 175:b96e65c34a4d | 133 | |
AnnaBridge | 175:b96e65c34a4d | 134 | if (! var->ref_cnt) { |
AnnaBridge | 175:b96e65c34a4d | 135 | // Reset this module |
AnnaBridge | 175:b96e65c34a4d | 136 | SYS_ResetModule(modinit->rsetidx); |
AnnaBridge | 175:b96e65c34a4d | 137 | |
AnnaBridge | 175:b96e65c34a4d | 138 | // Select IP clock source |
AnnaBridge | 175:b96e65c34a4d | 139 | CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); |
AnnaBridge | 175:b96e65c34a4d | 140 | // Enable IP clock |
AnnaBridge | 175:b96e65c34a4d | 141 | CLK_EnableModuleClock(modinit->clkidx); |
AnnaBridge | 175:b96e65c34a4d | 142 | |
AnnaBridge | 175:b96e65c34a4d | 143 | pinmap_pinout(tx, PinMap_UART_TX); |
AnnaBridge | 175:b96e65c34a4d | 144 | pinmap_pinout(rx, PinMap_UART_RX); |
AnnaBridge | 175:b96e65c34a4d | 145 | |
AnnaBridge | 175:b96e65c34a4d | 146 | obj->serial.pin_tx = tx; |
AnnaBridge | 175:b96e65c34a4d | 147 | obj->serial.pin_rx = rx; |
AnnaBridge | 175:b96e65c34a4d | 148 | } |
AnnaBridge | 175:b96e65c34a4d | 149 | var->ref_cnt ++; |
AnnaBridge | 175:b96e65c34a4d | 150 | |
AnnaBridge | 175:b96e65c34a4d | 151 | // Configure the UART module and set its baudrate |
AnnaBridge | 175:b96e65c34a4d | 152 | serial_baud(obj, 9600); |
AnnaBridge | 175:b96e65c34a4d | 153 | // Configure data bits, parity, and stop bits |
AnnaBridge | 175:b96e65c34a4d | 154 | serial_format(obj, 8, ParityNone, 1); |
AnnaBridge | 175:b96e65c34a4d | 155 | |
AnnaBridge | 175:b96e65c34a4d | 156 | obj->serial.vec = var->vec; |
AnnaBridge | 175:b96e65c34a4d | 157 | obj->serial.irq_en = 0; |
AnnaBridge | 175:b96e65c34a4d | 158 | |
AnnaBridge | 175:b96e65c34a4d | 159 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 160 | obj->serial.dma_usage_tx = DMA_USAGE_NEVER; |
AnnaBridge | 175:b96e65c34a4d | 161 | obj->serial.dma_usage_rx = DMA_USAGE_NEVER; |
AnnaBridge | 175:b96e65c34a4d | 162 | obj->serial.event = 0; |
AnnaBridge | 175:b96e65c34a4d | 163 | obj->serial.dma_chn_id_tx = DMA_ERROR_OUT_OF_CHANNELS; |
AnnaBridge | 175:b96e65c34a4d | 164 | obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; |
AnnaBridge | 175:b96e65c34a4d | 165 | #endif |
AnnaBridge | 175:b96e65c34a4d | 166 | |
AnnaBridge | 175:b96e65c34a4d | 167 | // For stdio management |
AnnaBridge | 175:b96e65c34a4d | 168 | if (obj->serial.uart == STDIO_UART) { |
AnnaBridge | 175:b96e65c34a4d | 169 | stdio_uart_inited = 1; |
AnnaBridge | 175:b96e65c34a4d | 170 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
AnnaBridge | 175:b96e65c34a4d | 171 | } |
AnnaBridge | 175:b96e65c34a4d | 172 | |
AnnaBridge | 175:b96e65c34a4d | 173 | if (var->ref_cnt) { |
AnnaBridge | 175:b96e65c34a4d | 174 | // Mark this module to be inited. |
AnnaBridge | 175:b96e65c34a4d | 175 | int i = modinit - uart_modinit_tab; |
AnnaBridge | 175:b96e65c34a4d | 176 | uart_modinit_mask |= 1 << i; |
AnnaBridge | 175:b96e65c34a4d | 177 | } |
AnnaBridge | 175:b96e65c34a4d | 178 | } |
AnnaBridge | 175:b96e65c34a4d | 179 | |
AnnaBridge | 175:b96e65c34a4d | 180 | void serial_free(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 181 | { |
AnnaBridge | 175:b96e65c34a4d | 182 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 183 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 184 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 185 | |
AnnaBridge | 175:b96e65c34a4d | 186 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
AnnaBridge | 175:b96e65c34a4d | 187 | |
AnnaBridge | 175:b96e65c34a4d | 188 | var->ref_cnt --; |
AnnaBridge | 175:b96e65c34a4d | 189 | if (! var->ref_cnt) { |
AnnaBridge | 175:b96e65c34a4d | 190 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 191 | if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) { |
AnnaBridge | 175:b96e65c34a4d | 192 | dma_channel_free(obj->serial.dma_chn_id_tx); |
AnnaBridge | 175:b96e65c34a4d | 193 | obj->serial.dma_chn_id_tx = DMA_ERROR_OUT_OF_CHANNELS; |
AnnaBridge | 175:b96e65c34a4d | 194 | } |
AnnaBridge | 175:b96e65c34a4d | 195 | if (obj->serial.dma_chn_id_rx != DMA_ERROR_OUT_OF_CHANNELS) { |
AnnaBridge | 175:b96e65c34a4d | 196 | dma_channel_free(obj->serial.dma_chn_id_rx); |
AnnaBridge | 175:b96e65c34a4d | 197 | obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; |
AnnaBridge | 175:b96e65c34a4d | 198 | } |
AnnaBridge | 175:b96e65c34a4d | 199 | #endif |
AnnaBridge | 175:b96e65c34a4d | 200 | |
AnnaBridge | 175:b96e65c34a4d | 201 | UART_Close((UART_T *) NU_MODBASE(obj->serial.uart)); |
AnnaBridge | 175:b96e65c34a4d | 202 | |
AnnaBridge | 175:b96e65c34a4d | 203 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_IER_RDA_IE_Msk | UART_IER_THRE_IE_Msk | UART_IER_RTO_IE_Msk)); |
AnnaBridge | 175:b96e65c34a4d | 204 | NVIC_DisableIRQ(modinit->irq_n); |
AnnaBridge | 175:b96e65c34a4d | 205 | |
AnnaBridge | 175:b96e65c34a4d | 206 | // Disable IP clock |
AnnaBridge | 175:b96e65c34a4d | 207 | CLK_DisableModuleClock(modinit->clkidx); |
AnnaBridge | 175:b96e65c34a4d | 208 | } |
AnnaBridge | 175:b96e65c34a4d | 209 | |
AnnaBridge | 175:b96e65c34a4d | 210 | if (var->obj == obj) { |
AnnaBridge | 175:b96e65c34a4d | 211 | var->obj = NULL; |
AnnaBridge | 175:b96e65c34a4d | 212 | } |
AnnaBridge | 175:b96e65c34a4d | 213 | |
AnnaBridge | 175:b96e65c34a4d | 214 | if (obj->serial.uart == STDIO_UART) { |
AnnaBridge | 175:b96e65c34a4d | 215 | stdio_uart_inited = 0; |
AnnaBridge | 175:b96e65c34a4d | 216 | } |
AnnaBridge | 175:b96e65c34a4d | 217 | |
AnnaBridge | 175:b96e65c34a4d | 218 | if (! var->ref_cnt) { |
AnnaBridge | 175:b96e65c34a4d | 219 | // Mark this module to be deinited. |
AnnaBridge | 175:b96e65c34a4d | 220 | int i = modinit - uart_modinit_tab; |
AnnaBridge | 175:b96e65c34a4d | 221 | uart_modinit_mask &= ~(1 << i); |
AnnaBridge | 175:b96e65c34a4d | 222 | } |
AnnaBridge | 175:b96e65c34a4d | 223 | } |
AnnaBridge | 175:b96e65c34a4d | 224 | |
AnnaBridge | 175:b96e65c34a4d | 225 | void serial_baud(serial_t *obj, int baudrate) { |
AnnaBridge | 175:b96e65c34a4d | 226 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
AnnaBridge | 175:b96e65c34a4d | 227 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
AnnaBridge | 175:b96e65c34a4d | 228 | |
AnnaBridge | 175:b96e65c34a4d | 229 | obj->serial.baudrate = baudrate; |
AnnaBridge | 175:b96e65c34a4d | 230 | UART_Open((UART_T *) NU_MODBASE(obj->serial.uart), baudrate); |
AnnaBridge | 175:b96e65c34a4d | 231 | } |
AnnaBridge | 175:b96e65c34a4d | 232 | |
AnnaBridge | 175:b96e65c34a4d | 233 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { |
AnnaBridge | 175:b96e65c34a4d | 234 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
AnnaBridge | 175:b96e65c34a4d | 235 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
AnnaBridge | 175:b96e65c34a4d | 236 | |
AnnaBridge | 175:b96e65c34a4d | 237 | // TODO: Assert for not supported parity and data bits |
AnnaBridge | 175:b96e65c34a4d | 238 | obj->serial.databits = data_bits; |
AnnaBridge | 175:b96e65c34a4d | 239 | obj->serial.parity = parity; |
AnnaBridge | 175:b96e65c34a4d | 240 | obj->serial.stopbits = stop_bits; |
AnnaBridge | 175:b96e65c34a4d | 241 | |
AnnaBridge | 175:b96e65c34a4d | 242 | uint32_t databits_intern = (data_bits == 5) ? UART_WORD_LEN_5 : |
AnnaBridge | 175:b96e65c34a4d | 243 | (data_bits == 6) ? UART_WORD_LEN_6 : |
AnnaBridge | 175:b96e65c34a4d | 244 | (data_bits == 7) ? UART_WORD_LEN_7 : |
AnnaBridge | 175:b96e65c34a4d | 245 | UART_WORD_LEN_8; |
AnnaBridge | 175:b96e65c34a4d | 246 | uint32_t parity_intern = (parity == ParityOdd || parity == ParityForced1) ? UART_PARITY_ODD : |
AnnaBridge | 175:b96e65c34a4d | 247 | (parity == ParityEven || parity == ParityForced0) ? UART_PARITY_EVEN : |
AnnaBridge | 175:b96e65c34a4d | 248 | UART_PARITY_NONE; |
AnnaBridge | 175:b96e65c34a4d | 249 | uint32_t stopbits_intern = (stop_bits == 2) ? UART_STOP_BIT_2 : UART_STOP_BIT_1; |
AnnaBridge | 175:b96e65c34a4d | 250 | UART_SetLine_Config((UART_T *) NU_MODBASE(obj->serial.uart), |
AnnaBridge | 175:b96e65c34a4d | 251 | 0, // Don't change baudrate |
AnnaBridge | 175:b96e65c34a4d | 252 | databits_intern, |
AnnaBridge | 175:b96e65c34a4d | 253 | parity_intern, |
AnnaBridge | 175:b96e65c34a4d | 254 | stopbits_intern); |
AnnaBridge | 175:b96e65c34a4d | 255 | } |
AnnaBridge | 175:b96e65c34a4d | 256 | |
AnnaBridge | 175:b96e65c34a4d | 257 | #if DEVICE_SERIAL_FC |
AnnaBridge | 175:b96e65c34a4d | 258 | |
AnnaBridge | 175:b96e65c34a4d | 259 | void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) |
AnnaBridge | 175:b96e65c34a4d | 260 | { |
AnnaBridge | 175:b96e65c34a4d | 261 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 262 | |
AnnaBridge | 175:b96e65c34a4d | 263 | // First, disable flow control completely. |
AnnaBridge | 175:b96e65c34a4d | 264 | UART_DisableFlowCtrl(uart_base); |
AnnaBridge | 175:b96e65c34a4d | 265 | |
AnnaBridge | 175:b96e65c34a4d | 266 | if ((type == FlowControlRTS || type == FlowControlRTSCTS) && rxflow != NC) { |
AnnaBridge | 175:b96e65c34a4d | 267 | // Check if RTS pin matches. |
AnnaBridge | 175:b96e65c34a4d | 268 | uint32_t uart_rts = pinmap_peripheral(rxflow, PinMap_UART_RTS); |
AnnaBridge | 175:b96e65c34a4d | 269 | MBED_ASSERT(uart_rts == obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 270 | // Enable the pin for RTS function |
AnnaBridge | 175:b96e65c34a4d | 271 | pinmap_pinout(rxflow, PinMap_UART_RTS); |
AnnaBridge | 175:b96e65c34a4d | 272 | // nRTS pin output is low level active |
AnnaBridge | 175:b96e65c34a4d | 273 | uart_base->MCSR |= UART_MCSR_LEV_RTS_Msk; |
AnnaBridge | 175:b96e65c34a4d | 274 | // Set RTS Trigger Level as 8 bytes |
AnnaBridge | 175:b96e65c34a4d | 275 | uart_base->TLCTL = (uart_base->TLCTL & ~UART_TLCTL_RTS_TRI_LEV_Msk) | UART_TLCTL_RTS_TRI_LEV_8BYTES; |
AnnaBridge | 175:b96e65c34a4d | 276 | // Set RX Trigger Level as 8 bytes |
AnnaBridge | 175:b96e65c34a4d | 277 | uart_base->TLCTL = (uart_base->TLCTL & ~UART_TLCTL_RFITL_Msk) | UART_TLCTL_RFITL_8BYTES; |
AnnaBridge | 175:b96e65c34a4d | 278 | // Enable RTS |
AnnaBridge | 175:b96e65c34a4d | 279 | uart_base->CTL |= UART_CTL_AUTO_RTS_EN_Msk; |
AnnaBridge | 175:b96e65c34a4d | 280 | } |
AnnaBridge | 175:b96e65c34a4d | 281 | |
AnnaBridge | 175:b96e65c34a4d | 282 | if ((type == FlowControlCTS || type == FlowControlRTSCTS) && txflow != NC) { |
AnnaBridge | 175:b96e65c34a4d | 283 | // Check if CTS pin matches. |
AnnaBridge | 175:b96e65c34a4d | 284 | uint32_t uart_cts = pinmap_peripheral(txflow, PinMap_UART_CTS); |
AnnaBridge | 175:b96e65c34a4d | 285 | MBED_ASSERT(uart_cts == obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 286 | // Enable the pin for CTS function |
AnnaBridge | 175:b96e65c34a4d | 287 | pinmap_pinout(txflow, PinMap_UART_CTS); |
AnnaBridge | 175:b96e65c34a4d | 288 | // nCTS pin input is low level active |
AnnaBridge | 175:b96e65c34a4d | 289 | uart_base->MCSR |= UART_MCSR_LEV_CTS_Msk; |
AnnaBridge | 175:b96e65c34a4d | 290 | // Enable CTS |
AnnaBridge | 175:b96e65c34a4d | 291 | uart_base->CTL |= UART_CTL_AUTO_CTS_EN_Msk; |
AnnaBridge | 175:b96e65c34a4d | 292 | } |
AnnaBridge | 175:b96e65c34a4d | 293 | } |
AnnaBridge | 175:b96e65c34a4d | 294 | |
AnnaBridge | 175:b96e65c34a4d | 295 | #endif //DEVICE_SERIAL_FC |
AnnaBridge | 175:b96e65c34a4d | 296 | |
AnnaBridge | 175:b96e65c34a4d | 297 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
AnnaBridge | 175:b96e65c34a4d | 298 | { |
AnnaBridge | 175:b96e65c34a4d | 299 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
AnnaBridge | 175:b96e65c34a4d | 300 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
AnnaBridge | 175:b96e65c34a4d | 301 | |
AnnaBridge | 175:b96e65c34a4d | 302 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 303 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 304 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 305 | |
AnnaBridge | 175:b96e65c34a4d | 306 | obj->serial.irq_handler = (uint32_t) handler; |
AnnaBridge | 175:b96e65c34a4d | 307 | obj->serial.irq_id = id; |
AnnaBridge | 175:b96e65c34a4d | 308 | |
AnnaBridge | 175:b96e65c34a4d | 309 | // Restore sync-mode vector |
AnnaBridge | 175:b96e65c34a4d | 310 | obj->serial.async_en = 0; |
AnnaBridge | 175:b96e65c34a4d | 311 | } |
AnnaBridge | 175:b96e65c34a4d | 312 | |
AnnaBridge | 175:b96e65c34a4d | 313 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
AnnaBridge | 175:b96e65c34a4d | 314 | { |
AnnaBridge | 175:b96e65c34a4d | 315 | obj->serial.irq_en = enable; |
AnnaBridge | 175:b96e65c34a4d | 316 | serial_enable_interrupt(obj, irq, enable); |
AnnaBridge | 175:b96e65c34a4d | 317 | } |
AnnaBridge | 175:b96e65c34a4d | 318 | |
AnnaBridge | 175:b96e65c34a4d | 319 | int serial_getc(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 320 | { |
AnnaBridge | 175:b96e65c34a4d | 321 | // TODO: Fix every byte access requires accompaniment of one interrupt. This has side effect of performance degradation. |
AnnaBridge | 175:b96e65c34a4d | 322 | while (! serial_readable(obj)); |
AnnaBridge | 175:b96e65c34a4d | 323 | int c = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
AnnaBridge | 175:b96e65c34a4d | 324 | |
AnnaBridge | 175:b96e65c34a4d | 325 | // NOTE: On Nuvoton targets, no H/W IRQ to match TxIrq/RxIrq. |
AnnaBridge | 175:b96e65c34a4d | 326 | // Simulation of TxIrq/RxIrq requires the call to Serial::putc()/Serial::getc() respectively. |
AnnaBridge | 175:b96e65c34a4d | 327 | if (obj->serial.ier_msk & (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)) { |
AnnaBridge | 175:b96e65c34a4d | 328 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)); |
AnnaBridge | 175:b96e65c34a4d | 329 | } |
AnnaBridge | 175:b96e65c34a4d | 330 | |
AnnaBridge | 175:b96e65c34a4d | 331 | return c; |
AnnaBridge | 175:b96e65c34a4d | 332 | } |
AnnaBridge | 175:b96e65c34a4d | 333 | |
AnnaBridge | 175:b96e65c34a4d | 334 | void serial_putc(serial_t *obj, int c) |
AnnaBridge | 175:b96e65c34a4d | 335 | { |
AnnaBridge | 175:b96e65c34a4d | 336 | // TODO: Fix every byte access requires accompaniment of one interrupt. This has side effect of performance degradation. |
AnnaBridge | 175:b96e65c34a4d | 337 | while (! serial_writable(obj)); |
AnnaBridge | 175:b96e65c34a4d | 338 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), c); |
AnnaBridge | 175:b96e65c34a4d | 339 | |
AnnaBridge | 175:b96e65c34a4d | 340 | // NOTE: On Nuvoton targets, no H/W IRQ to match TxIrq/RxIrq. |
AnnaBridge | 175:b96e65c34a4d | 341 | // Simulation of TxIrq/RxIrq requires the call to Serial::putc()/Serial::getc() respectively. |
AnnaBridge | 175:b96e65c34a4d | 342 | if (obj->serial.ier_msk & UART_IER_THRE_IE_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 343 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_THRE_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 344 | } |
AnnaBridge | 175:b96e65c34a4d | 345 | } |
AnnaBridge | 175:b96e65c34a4d | 346 | |
AnnaBridge | 175:b96e65c34a4d | 347 | int serial_readable(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 348 | { |
AnnaBridge | 175:b96e65c34a4d | 349 | return ! UART_GET_RX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart))); |
AnnaBridge | 175:b96e65c34a4d | 350 | } |
AnnaBridge | 175:b96e65c34a4d | 351 | |
AnnaBridge | 175:b96e65c34a4d | 352 | int serial_writable(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 353 | { |
AnnaBridge | 175:b96e65c34a4d | 354 | return ! UART_IS_TX_FULL(((UART_T *) NU_MODBASE(obj->serial.uart))); |
AnnaBridge | 175:b96e65c34a4d | 355 | } |
AnnaBridge | 175:b96e65c34a4d | 356 | |
AnnaBridge | 175:b96e65c34a4d | 357 | void serial_pinout_tx(PinName tx) |
AnnaBridge | 175:b96e65c34a4d | 358 | { |
AnnaBridge | 175:b96e65c34a4d | 359 | pinmap_pinout(tx, PinMap_UART_TX); |
AnnaBridge | 175:b96e65c34a4d | 360 | } |
AnnaBridge | 175:b96e65c34a4d | 361 | |
AnnaBridge | 175:b96e65c34a4d | 362 | void serial_break_set(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 363 | { |
AnnaBridge | 175:b96e65c34a4d | 364 | ((UART_T *) NU_MODBASE(obj->serial.uart))->TLCTL |= UART_TLCTL_BCB_Msk; |
AnnaBridge | 175:b96e65c34a4d | 365 | } |
AnnaBridge | 175:b96e65c34a4d | 366 | |
AnnaBridge | 175:b96e65c34a4d | 367 | void serial_break_clear(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 368 | { |
AnnaBridge | 175:b96e65c34a4d | 369 | ((UART_T *) NU_MODBASE(obj->serial.uart))->TLCTL &= ~UART_TLCTL_BCB_Msk; |
AnnaBridge | 175:b96e65c34a4d | 370 | } |
AnnaBridge | 175:b96e65c34a4d | 371 | |
AnnaBridge | 175:b96e65c34a4d | 372 | void UART0_IRQHandler(void) |
AnnaBridge | 175:b96e65c34a4d | 373 | { |
AnnaBridge | 175:b96e65c34a4d | 374 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 375 | if (uart0_var.obj->serial.async_en) |
AnnaBridge | 175:b96e65c34a4d | 376 | uart_irq_async(uart0_var.obj); |
AnnaBridge | 175:b96e65c34a4d | 377 | else |
AnnaBridge | 175:b96e65c34a4d | 378 | #endif |
AnnaBridge | 175:b96e65c34a4d | 379 | uart_irq(uart0_var.obj); |
AnnaBridge | 175:b96e65c34a4d | 380 | } |
AnnaBridge | 175:b96e65c34a4d | 381 | |
AnnaBridge | 175:b96e65c34a4d | 382 | void UART1_IRQHandler(void) |
AnnaBridge | 175:b96e65c34a4d | 383 | { |
AnnaBridge | 175:b96e65c34a4d | 384 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 385 | if (uart1_var.obj->serial.async_en) |
AnnaBridge | 175:b96e65c34a4d | 386 | uart_irq_async(uart1_var.obj); |
AnnaBridge | 175:b96e65c34a4d | 387 | else |
AnnaBridge | 175:b96e65c34a4d | 388 | #endif |
AnnaBridge | 175:b96e65c34a4d | 389 | uart_irq(uart1_var.obj); |
AnnaBridge | 175:b96e65c34a4d | 390 | } |
AnnaBridge | 175:b96e65c34a4d | 391 | |
AnnaBridge | 175:b96e65c34a4d | 392 | static void uart_irq(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 393 | { |
AnnaBridge | 175:b96e65c34a4d | 394 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 395 | |
AnnaBridge | 175:b96e65c34a4d | 396 | if (uart_base->ISR & (UART_ISR_RDA_IS_Msk | UART_ISR_RTO_IS_Msk)) { |
AnnaBridge | 175:b96e65c34a4d | 397 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next read. |
AnnaBridge | 175:b96e65c34a4d | 398 | UART_DISABLE_INT(uart_base, (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)); |
AnnaBridge | 175:b96e65c34a4d | 399 | if (obj->serial.irq_handler) { |
AnnaBridge | 175:b96e65c34a4d | 400 | ((uart_irq_handler) obj->serial.irq_handler)(obj->serial.irq_id, RxIrq); |
AnnaBridge | 175:b96e65c34a4d | 401 | } |
AnnaBridge | 175:b96e65c34a4d | 402 | } |
AnnaBridge | 175:b96e65c34a4d | 403 | |
AnnaBridge | 175:b96e65c34a4d | 404 | if (uart_base->ISR & UART_ISR_THRE_IS_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 405 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next write. |
AnnaBridge | 175:b96e65c34a4d | 406 | UART_DISABLE_INT(uart_base, UART_IER_THRE_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 407 | if (obj->serial.irq_handler) { |
AnnaBridge | 175:b96e65c34a4d | 408 | ((uart_irq_handler) obj->serial.irq_handler)(obj->serial.irq_id, TxIrq); |
AnnaBridge | 175:b96e65c34a4d | 409 | } |
AnnaBridge | 175:b96e65c34a4d | 410 | } |
AnnaBridge | 175:b96e65c34a4d | 411 | |
AnnaBridge | 175:b96e65c34a4d | 412 | // NOTE: Ignore all other interrupt flags. Clear them. Otherwise, program will get stuck in interrupt. |
AnnaBridge | 175:b96e65c34a4d | 413 | uart_base->ISR = uart_base->ISR; |
AnnaBridge | 175:b96e65c34a4d | 414 | uart_base->FSR = uart_base->FSR; |
AnnaBridge | 175:b96e65c34a4d | 415 | } |
AnnaBridge | 175:b96e65c34a4d | 416 | |
AnnaBridge | 175:b96e65c34a4d | 417 | |
AnnaBridge | 175:b96e65c34a4d | 418 | #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 419 | int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint) |
AnnaBridge | 175:b96e65c34a4d | 420 | { |
AnnaBridge | 175:b96e65c34a4d | 421 | MBED_ASSERT(tx_width == 8 || tx_width == 16 || tx_width == 32); |
AnnaBridge | 175:b96e65c34a4d | 422 | |
AnnaBridge | 175:b96e65c34a4d | 423 | obj->serial.dma_usage_tx = hint; |
AnnaBridge | 175:b96e65c34a4d | 424 | serial_check_dma_usage(&obj->serial.dma_usage_tx, &obj->serial.dma_chn_id_tx); |
AnnaBridge | 175:b96e65c34a4d | 425 | |
AnnaBridge | 175:b96e65c34a4d | 426 | // UART IRQ is necessary for both interrupt way and DMA way |
AnnaBridge | 175:b96e65c34a4d | 427 | serial_tx_enable_event(obj, event, 1); |
AnnaBridge | 175:b96e65c34a4d | 428 | serial_tx_buffer_set(obj, tx, tx_length, tx_width); |
AnnaBridge | 175:b96e65c34a4d | 429 | |
AnnaBridge | 175:b96e65c34a4d | 430 | int n_word = 0; |
AnnaBridge | 175:b96e65c34a4d | 431 | if (obj->serial.dma_usage_tx == DMA_USAGE_NEVER) { |
AnnaBridge | 175:b96e65c34a4d | 432 | // Interrupt way |
AnnaBridge | 175:b96e65c34a4d | 433 | n_word = serial_write_async(obj); |
AnnaBridge | 175:b96e65c34a4d | 434 | serial_tx_enable_interrupt(obj, handler, 1); |
AnnaBridge | 175:b96e65c34a4d | 435 | } else { |
AnnaBridge | 175:b96e65c34a4d | 436 | // DMA way |
AnnaBridge | 175:b96e65c34a4d | 437 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 438 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 439 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 440 | |
AnnaBridge | 175:b96e65c34a4d | 441 | dma_enable(obj->serial.dma_chn_id_tx, 1); // Enable this DMA channel |
AnnaBridge | 175:b96e65c34a4d | 442 | PDMA_SetTransferMode(obj->serial.dma_chn_id_tx, |
AnnaBridge | 175:b96e65c34a4d | 443 | ((struct nu_uart_var *) modinit->var)->pdma_perp_tx, // Peripheral connected to this PDMA |
AnnaBridge | 175:b96e65c34a4d | 444 | 0, // Scatter-gather disabled |
AnnaBridge | 175:b96e65c34a4d | 445 | 0); // Scatter-gather descriptor address |
AnnaBridge | 175:b96e65c34a4d | 446 | PDMA_SetTransferCnt(obj->serial.dma_chn_id_tx, |
AnnaBridge | 175:b96e65c34a4d | 447 | (tx_width == 8) ? PDMA_WIDTH_8 : (tx_width == 16) ? PDMA_WIDTH_16 : PDMA_WIDTH_32, |
AnnaBridge | 175:b96e65c34a4d | 448 | tx_length); |
AnnaBridge | 175:b96e65c34a4d | 449 | PDMA_SetTransferAddr(obj->serial.dma_chn_id_tx, |
AnnaBridge | 175:b96e65c34a4d | 450 | (uint32_t) tx, // NOTE: |
AnnaBridge | 175:b96e65c34a4d | 451 | // NUC472: End of source address |
AnnaBridge | 175:b96e65c34a4d | 452 | // M451: Start of source address |
AnnaBridge | 175:b96e65c34a4d | 453 | PDMA_SAR_INC, // Source address incremental |
AnnaBridge | 175:b96e65c34a4d | 454 | (uint32_t) NU_MODBASE(obj->serial.uart), // Destination address |
AnnaBridge | 175:b96e65c34a4d | 455 | PDMA_DAR_FIX); // Destination address fixed |
AnnaBridge | 175:b96e65c34a4d | 456 | PDMA_EnableInt(obj->serial.dma_chn_id_tx, |
AnnaBridge | 175:b96e65c34a4d | 457 | PDMA_IER_TD_IE_Msk); // Interrupt type |
AnnaBridge | 175:b96e65c34a4d | 458 | // Register DMA event handler |
AnnaBridge | 175:b96e65c34a4d | 459 | dma_set_handler(obj->serial.dma_chn_id_tx, (uint32_t) uart_dma_handler_tx, (uint32_t) obj, DMA_EVENT_ALL); |
AnnaBridge | 175:b96e65c34a4d | 460 | serial_tx_enable_interrupt(obj, handler, 1); |
AnnaBridge | 175:b96e65c34a4d | 461 | PDMA_Trigger(obj->serial.dma_chn_id_tx); |
AnnaBridge | 175:b96e65c34a4d | 462 | ((UART_T *) NU_MODBASE(obj->serial.uart))->CTL |= UART_CTL_DMA_TX_EN_Msk; // Start DMA transfer |
AnnaBridge | 175:b96e65c34a4d | 463 | } |
AnnaBridge | 175:b96e65c34a4d | 464 | |
AnnaBridge | 175:b96e65c34a4d | 465 | return n_word; |
AnnaBridge | 175:b96e65c34a4d | 466 | } |
AnnaBridge | 175:b96e65c34a4d | 467 | |
AnnaBridge | 175:b96e65c34a4d | 468 | void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint) |
AnnaBridge | 175:b96e65c34a4d | 469 | { |
AnnaBridge | 175:b96e65c34a4d | 470 | MBED_ASSERT(rx_width == 8 || rx_width == 16 || rx_width == 32); |
AnnaBridge | 175:b96e65c34a4d | 471 | |
AnnaBridge | 175:b96e65c34a4d | 472 | obj->serial.dma_usage_rx = hint; |
AnnaBridge | 175:b96e65c34a4d | 473 | serial_check_dma_usage(&obj->serial.dma_usage_rx, &obj->serial.dma_chn_id_rx); |
AnnaBridge | 175:b96e65c34a4d | 474 | // DMA doesn't support char match, so fall back to IRQ if it is requested. |
AnnaBridge | 175:b96e65c34a4d | 475 | if (obj->serial.dma_usage_rx != DMA_USAGE_NEVER && |
AnnaBridge | 175:b96e65c34a4d | 476 | (event & SERIAL_EVENT_RX_CHARACTER_MATCH) && |
AnnaBridge | 175:b96e65c34a4d | 477 | char_match != SERIAL_RESERVED_CHAR_MATCH) { |
AnnaBridge | 175:b96e65c34a4d | 478 | obj->serial.dma_usage_rx = DMA_USAGE_NEVER; |
AnnaBridge | 175:b96e65c34a4d | 479 | dma_channel_free(obj->serial.dma_chn_id_rx); |
AnnaBridge | 175:b96e65c34a4d | 480 | obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; |
AnnaBridge | 175:b96e65c34a4d | 481 | } |
AnnaBridge | 175:b96e65c34a4d | 482 | |
AnnaBridge | 175:b96e65c34a4d | 483 | // UART IRQ is necessary for both interrupt way and DMA way |
AnnaBridge | 175:b96e65c34a4d | 484 | serial_rx_enable_event(obj, event, 1); |
AnnaBridge | 175:b96e65c34a4d | 485 | serial_rx_buffer_set(obj, rx, rx_length, rx_width); |
AnnaBridge | 175:b96e65c34a4d | 486 | serial_rx_set_char_match(obj, char_match); |
AnnaBridge | 175:b96e65c34a4d | 487 | |
AnnaBridge | 175:b96e65c34a4d | 488 | if (obj->serial.dma_usage_rx == DMA_USAGE_NEVER) { |
AnnaBridge | 175:b96e65c34a4d | 489 | // Interrupt way |
AnnaBridge | 175:b96e65c34a4d | 490 | serial_rx_enable_interrupt(obj, handler, 1); |
AnnaBridge | 175:b96e65c34a4d | 491 | } else { |
AnnaBridge | 175:b96e65c34a4d | 492 | // DMA way |
AnnaBridge | 175:b96e65c34a4d | 493 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 494 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 495 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 496 | |
AnnaBridge | 175:b96e65c34a4d | 497 | dma_enable(obj->serial.dma_chn_id_rx, 1); // Enable this DMA channel |
AnnaBridge | 175:b96e65c34a4d | 498 | PDMA_SetTransferMode(obj->serial.dma_chn_id_rx, |
AnnaBridge | 175:b96e65c34a4d | 499 | ((struct nu_uart_var *) modinit->var)->pdma_perp_rx, // Peripheral connected to this PDMA |
AnnaBridge | 175:b96e65c34a4d | 500 | 0, // Scatter-gather disabled |
AnnaBridge | 175:b96e65c34a4d | 501 | 0); // Scatter-gather descriptor address |
AnnaBridge | 175:b96e65c34a4d | 502 | PDMA_SetTransferCnt(obj->serial.dma_chn_id_rx, |
AnnaBridge | 175:b96e65c34a4d | 503 | (rx_width == 8) ? PDMA_WIDTH_8 : (rx_width == 16) ? PDMA_WIDTH_16 : PDMA_WIDTH_32, |
AnnaBridge | 175:b96e65c34a4d | 504 | rx_length); |
AnnaBridge | 175:b96e65c34a4d | 505 | PDMA_SetTransferAddr(obj->serial.dma_chn_id_rx, |
AnnaBridge | 175:b96e65c34a4d | 506 | (uint32_t) NU_MODBASE(obj->serial.uart), // Source address |
AnnaBridge | 175:b96e65c34a4d | 507 | PDMA_SAR_FIX, // Source address fixed |
AnnaBridge | 175:b96e65c34a4d | 508 | (uint32_t) rx, // NOTE: |
AnnaBridge | 175:b96e65c34a4d | 509 | // NUC472: End of destination address |
AnnaBridge | 175:b96e65c34a4d | 510 | // M451: Start of destination address |
AnnaBridge | 175:b96e65c34a4d | 511 | PDMA_DAR_INC); // Destination address incremental |
AnnaBridge | 175:b96e65c34a4d | 512 | PDMA_EnableInt(obj->serial.dma_chn_id_rx, |
AnnaBridge | 175:b96e65c34a4d | 513 | PDMA_IER_TD_IE_Msk); // Interrupt type |
AnnaBridge | 175:b96e65c34a4d | 514 | // Register DMA event handler |
AnnaBridge | 175:b96e65c34a4d | 515 | dma_set_handler(obj->serial.dma_chn_id_rx, (uint32_t) uart_dma_handler_rx, (uint32_t) obj, DMA_EVENT_ALL); |
AnnaBridge | 175:b96e65c34a4d | 516 | serial_rx_enable_interrupt(obj, handler, 1); |
AnnaBridge | 175:b96e65c34a4d | 517 | PDMA_Trigger(obj->serial.dma_chn_id_rx); |
AnnaBridge | 175:b96e65c34a4d | 518 | ((UART_T *) NU_MODBASE(obj->serial.uart))->CTL |= UART_CTL_DMA_RX_EN_Msk; // Start DMA transfer |
AnnaBridge | 175:b96e65c34a4d | 519 | } |
AnnaBridge | 175:b96e65c34a4d | 520 | } |
AnnaBridge | 175:b96e65c34a4d | 521 | |
AnnaBridge | 175:b96e65c34a4d | 522 | void serial_tx_abort_asynch(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 523 | { |
AnnaBridge | 175:b96e65c34a4d | 524 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
AnnaBridge | 175:b96e65c34a4d | 525 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
AnnaBridge | 175:b96e65c34a4d | 526 | |
AnnaBridge | 175:b96e65c34a4d | 527 | if (obj->serial.dma_usage_tx != DMA_USAGE_NEVER) { |
AnnaBridge | 175:b96e65c34a4d | 528 | if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) { |
AnnaBridge | 175:b96e65c34a4d | 529 | PDMA_DisableInt(obj->serial.dma_chn_id_tx, PDMA_IER_TD_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 530 | // NOTE: On NUC472, next PDMA transfer will fail with PDMA_STOP() called. |
AnnaBridge | 175:b96e65c34a4d | 531 | dma_enable(obj->serial.dma_chn_id_tx, 0); |
AnnaBridge | 175:b96e65c34a4d | 532 | } |
AnnaBridge | 175:b96e65c34a4d | 533 | ((UART_T *) NU_MODBASE(obj->serial.uart))->CTL &= ~UART_CTL_DMA_TX_EN_Msk; |
AnnaBridge | 175:b96e65c34a4d | 534 | } |
AnnaBridge | 175:b96e65c34a4d | 535 | |
AnnaBridge | 175:b96e65c34a4d | 536 | // Necessary for both interrupt way and DMA way |
AnnaBridge | 175:b96e65c34a4d | 537 | serial_enable_interrupt(obj, TxIrq, 0); |
AnnaBridge | 175:b96e65c34a4d | 538 | serial_rollback_interrupt(obj, TxIrq); |
AnnaBridge | 175:b96e65c34a4d | 539 | } |
AnnaBridge | 175:b96e65c34a4d | 540 | |
AnnaBridge | 175:b96e65c34a4d | 541 | void serial_rx_abort_asynch(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 542 | { |
AnnaBridge | 175:b96e65c34a4d | 543 | if (obj->serial.dma_usage_rx != DMA_USAGE_NEVER) { |
AnnaBridge | 175:b96e65c34a4d | 544 | if (obj->serial.dma_chn_id_rx != DMA_ERROR_OUT_OF_CHANNELS) { |
AnnaBridge | 175:b96e65c34a4d | 545 | PDMA_DisableInt(obj->serial.dma_chn_id_rx, PDMA_IER_TD_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 546 | // NOTE: On NUC472, next PDMA transfer will fail with PDMA_STOP() called. |
AnnaBridge | 175:b96e65c34a4d | 547 | dma_enable(obj->serial.dma_chn_id_rx, 0); |
AnnaBridge | 175:b96e65c34a4d | 548 | } |
AnnaBridge | 175:b96e65c34a4d | 549 | ((UART_T *) NU_MODBASE(obj->serial.uart))->CTL &= ~UART_CTL_DMA_RX_EN_Msk; |
AnnaBridge | 175:b96e65c34a4d | 550 | } |
AnnaBridge | 175:b96e65c34a4d | 551 | |
AnnaBridge | 175:b96e65c34a4d | 552 | // Necessary for both interrupt way and DMA way |
AnnaBridge | 175:b96e65c34a4d | 553 | serial_enable_interrupt(obj, RxIrq, 0); |
AnnaBridge | 175:b96e65c34a4d | 554 | serial_rollback_interrupt(obj, RxIrq); |
AnnaBridge | 175:b96e65c34a4d | 555 | } |
AnnaBridge | 175:b96e65c34a4d | 556 | |
AnnaBridge | 175:b96e65c34a4d | 557 | uint8_t serial_tx_active(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 558 | { |
AnnaBridge | 175:b96e65c34a4d | 559 | // NOTE: Judge by serial_is_irq_en(obj, TxIrq) doesn't work with sync/async modes interleaved. Change with TX FIFO empty flag. |
AnnaBridge | 175:b96e65c34a4d | 560 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 561 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 562 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 563 | |
AnnaBridge | 175:b96e65c34a4d | 564 | return (obj->serial.async_en); |
AnnaBridge | 175:b96e65c34a4d | 565 | } |
AnnaBridge | 175:b96e65c34a4d | 566 | |
AnnaBridge | 175:b96e65c34a4d | 567 | uint8_t serial_rx_active(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 568 | { |
AnnaBridge | 175:b96e65c34a4d | 569 | // NOTE: Judge by serial_is_irq_en(obj, RxIrq) doesn't work with sync/async modes interleaved. Change with RX FIFO empty flag. |
AnnaBridge | 175:b96e65c34a4d | 570 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 571 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 572 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 573 | |
AnnaBridge | 175:b96e65c34a4d | 574 | return (obj->serial.async_en); |
AnnaBridge | 175:b96e65c34a4d | 575 | } |
AnnaBridge | 175:b96e65c34a4d | 576 | |
AnnaBridge | 175:b96e65c34a4d | 577 | int serial_irq_handler_asynch(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 578 | { |
AnnaBridge | 175:b96e65c34a4d | 579 | int event_rx = 0; |
AnnaBridge | 175:b96e65c34a4d | 580 | int event_tx = 0; |
AnnaBridge | 175:b96e65c34a4d | 581 | |
AnnaBridge | 175:b96e65c34a4d | 582 | // Necessary for both interrupt way and DMA way |
AnnaBridge | 175:b96e65c34a4d | 583 | if (serial_is_irq_en(obj, RxIrq)) { |
AnnaBridge | 175:b96e65c34a4d | 584 | event_rx = serial_rx_event_check(obj); |
AnnaBridge | 175:b96e65c34a4d | 585 | if (event_rx) { |
AnnaBridge | 175:b96e65c34a4d | 586 | serial_rx_abort_asynch(obj); |
AnnaBridge | 175:b96e65c34a4d | 587 | } |
AnnaBridge | 175:b96e65c34a4d | 588 | } |
AnnaBridge | 175:b96e65c34a4d | 589 | |
AnnaBridge | 175:b96e65c34a4d | 590 | if (serial_is_irq_en(obj, TxIrq)) { |
AnnaBridge | 175:b96e65c34a4d | 591 | event_tx = serial_tx_event_check(obj); |
AnnaBridge | 175:b96e65c34a4d | 592 | if (event_tx) { |
AnnaBridge | 175:b96e65c34a4d | 593 | serial_tx_abort_asynch(obj); |
AnnaBridge | 175:b96e65c34a4d | 594 | } |
AnnaBridge | 175:b96e65c34a4d | 595 | } |
AnnaBridge | 175:b96e65c34a4d | 596 | |
AnnaBridge | 175:b96e65c34a4d | 597 | return (obj->serial.event & (event_rx | event_tx)); |
AnnaBridge | 175:b96e65c34a4d | 598 | } |
AnnaBridge | 175:b96e65c34a4d | 599 | |
AnnaBridge | 175:b96e65c34a4d | 600 | static void uart_irq_async(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 601 | { |
AnnaBridge | 175:b96e65c34a4d | 602 | if (serial_is_irq_en(obj, RxIrq)) { |
AnnaBridge | 175:b96e65c34a4d | 603 | (*obj->serial.irq_handler_rx_async)(); |
AnnaBridge | 175:b96e65c34a4d | 604 | } |
AnnaBridge | 175:b96e65c34a4d | 605 | if (serial_is_irq_en(obj, TxIrq)) { |
AnnaBridge | 175:b96e65c34a4d | 606 | (*obj->serial.irq_handler_tx_async)(); |
AnnaBridge | 175:b96e65c34a4d | 607 | } |
AnnaBridge | 175:b96e65c34a4d | 608 | } |
AnnaBridge | 175:b96e65c34a4d | 609 | |
AnnaBridge | 175:b96e65c34a4d | 610 | static void serial_rx_set_char_match(serial_t *obj, uint8_t char_match) |
AnnaBridge | 175:b96e65c34a4d | 611 | { |
AnnaBridge | 175:b96e65c34a4d | 612 | obj->char_match = char_match; |
AnnaBridge | 175:b96e65c34a4d | 613 | obj->char_found = 0; |
AnnaBridge | 175:b96e65c34a4d | 614 | } |
AnnaBridge | 175:b96e65c34a4d | 615 | |
AnnaBridge | 175:b96e65c34a4d | 616 | static void serial_tx_enable_event(serial_t *obj, int event, uint8_t enable) |
AnnaBridge | 175:b96e65c34a4d | 617 | { |
AnnaBridge | 175:b96e65c34a4d | 618 | obj->serial.event &= ~SERIAL_EVENT_TX_MASK; |
AnnaBridge | 175:b96e65c34a4d | 619 | obj->serial.event |= (event & SERIAL_EVENT_TX_MASK); |
AnnaBridge | 175:b96e65c34a4d | 620 | |
AnnaBridge | 175:b96e65c34a4d | 621 | } |
AnnaBridge | 175:b96e65c34a4d | 622 | |
AnnaBridge | 175:b96e65c34a4d | 623 | static void serial_rx_enable_event(serial_t *obj, int event, uint8_t enable) |
AnnaBridge | 175:b96e65c34a4d | 624 | { |
AnnaBridge | 175:b96e65c34a4d | 625 | obj->serial.event &= ~SERIAL_EVENT_RX_MASK; |
AnnaBridge | 175:b96e65c34a4d | 626 | obj->serial.event |= (event & SERIAL_EVENT_RX_MASK); |
AnnaBridge | 175:b96e65c34a4d | 627 | |
AnnaBridge | 175:b96e65c34a4d | 628 | if (event & SERIAL_EVENT_RX_FRAMING_ERROR) { |
AnnaBridge | 175:b96e65c34a4d | 629 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_RLS_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 630 | } |
AnnaBridge | 175:b96e65c34a4d | 631 | if (event & SERIAL_EVENT_RX_PARITY_ERROR) { |
AnnaBridge | 175:b96e65c34a4d | 632 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_RLS_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 633 | } |
AnnaBridge | 175:b96e65c34a4d | 634 | if (event & SERIAL_EVENT_RX_OVERFLOW) { |
AnnaBridge | 175:b96e65c34a4d | 635 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_BUF_ERR_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 636 | } |
AnnaBridge | 175:b96e65c34a4d | 637 | } |
AnnaBridge | 175:b96e65c34a4d | 638 | |
AnnaBridge | 175:b96e65c34a4d | 639 | static int serial_is_tx_complete(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 640 | { |
AnnaBridge | 175:b96e65c34a4d | 641 | // NOTE: Exclude tx fifo empty check due to no such interrupt on DMA way |
AnnaBridge | 175:b96e65c34a4d | 642 | return (obj->tx_buff.pos == obj->tx_buff.length); |
AnnaBridge | 175:b96e65c34a4d | 643 | } |
AnnaBridge | 175:b96e65c34a4d | 644 | |
AnnaBridge | 175:b96e65c34a4d | 645 | static int serial_is_rx_complete(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 646 | { |
AnnaBridge | 175:b96e65c34a4d | 647 | return (obj->rx_buff.pos == obj->rx_buff.length); |
AnnaBridge | 175:b96e65c34a4d | 648 | } |
AnnaBridge | 175:b96e65c34a4d | 649 | |
AnnaBridge | 175:b96e65c34a4d | 650 | static uint32_t serial_tx_event_check(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 651 | { |
AnnaBridge | 175:b96e65c34a4d | 652 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 653 | |
AnnaBridge | 175:b96e65c34a4d | 654 | if (uart_base->ISR & UART_ISR_THRE_IS_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 655 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next write. |
AnnaBridge | 175:b96e65c34a4d | 656 | UART_DISABLE_INT(uart_base, UART_IER_THRE_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 657 | } |
AnnaBridge | 175:b96e65c34a4d | 658 | |
AnnaBridge | 175:b96e65c34a4d | 659 | uint32_t event = 0; |
AnnaBridge | 175:b96e65c34a4d | 660 | |
AnnaBridge | 175:b96e65c34a4d | 661 | if (obj->serial.dma_usage_tx == DMA_USAGE_NEVER) { |
AnnaBridge | 175:b96e65c34a4d | 662 | serial_write_async(obj); |
AnnaBridge | 175:b96e65c34a4d | 663 | } |
AnnaBridge | 175:b96e65c34a4d | 664 | |
AnnaBridge | 175:b96e65c34a4d | 665 | if (serial_is_tx_complete(obj)) { |
AnnaBridge | 175:b96e65c34a4d | 666 | event |= SERIAL_EVENT_TX_COMPLETE; |
AnnaBridge | 175:b96e65c34a4d | 667 | } |
AnnaBridge | 175:b96e65c34a4d | 668 | |
AnnaBridge | 175:b96e65c34a4d | 669 | return event; |
AnnaBridge | 175:b96e65c34a4d | 670 | } |
AnnaBridge | 175:b96e65c34a4d | 671 | |
AnnaBridge | 175:b96e65c34a4d | 672 | static uint32_t serial_rx_event_check(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 673 | { |
AnnaBridge | 175:b96e65c34a4d | 674 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 675 | |
AnnaBridge | 175:b96e65c34a4d | 676 | if (uart_base->ISR & (UART_ISR_RDA_IS_Msk | UART_ISR_RTO_IS_Msk)) { |
AnnaBridge | 175:b96e65c34a4d | 677 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next read. |
AnnaBridge | 175:b96e65c34a4d | 678 | UART_DISABLE_INT(uart_base, (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)); |
AnnaBridge | 175:b96e65c34a4d | 679 | } |
AnnaBridge | 175:b96e65c34a4d | 680 | |
AnnaBridge | 175:b96e65c34a4d | 681 | uint32_t event = 0; |
AnnaBridge | 175:b96e65c34a4d | 682 | |
AnnaBridge | 175:b96e65c34a4d | 683 | if (uart_base->FSR & UART_FSR_BI_F_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 684 | uart_base->FSR = UART_FSR_BI_F_Msk; |
AnnaBridge | 175:b96e65c34a4d | 685 | } |
AnnaBridge | 175:b96e65c34a4d | 686 | if (uart_base->FSR & UART_FSR_FE_F_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 687 | uart_base->FSR = UART_FSR_FE_F_Msk; |
AnnaBridge | 175:b96e65c34a4d | 688 | event |= SERIAL_EVENT_RX_FRAMING_ERROR; |
AnnaBridge | 175:b96e65c34a4d | 689 | } |
AnnaBridge | 175:b96e65c34a4d | 690 | if (uart_base->FSR & UART_FSR_PE_F_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 691 | uart_base->FSR = UART_FSR_PE_F_Msk; |
AnnaBridge | 175:b96e65c34a4d | 692 | event |= SERIAL_EVENT_RX_PARITY_ERROR; |
AnnaBridge | 175:b96e65c34a4d | 693 | } |
AnnaBridge | 175:b96e65c34a4d | 694 | |
AnnaBridge | 175:b96e65c34a4d | 695 | if (uart_base->FSR & UART_FSR_RX_OVER_F_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 696 | uart_base->FSR = UART_FSR_RX_OVER_F_Msk; |
AnnaBridge | 175:b96e65c34a4d | 697 | event |= SERIAL_EVENT_RX_OVERFLOW; |
AnnaBridge | 175:b96e65c34a4d | 698 | } |
AnnaBridge | 175:b96e65c34a4d | 699 | |
AnnaBridge | 175:b96e65c34a4d | 700 | if (obj->serial.dma_usage_rx == DMA_USAGE_NEVER) { |
AnnaBridge | 175:b96e65c34a4d | 701 | serial_read_async(obj); |
AnnaBridge | 175:b96e65c34a4d | 702 | } |
AnnaBridge | 175:b96e65c34a4d | 703 | |
AnnaBridge | 175:b96e65c34a4d | 704 | if (serial_is_rx_complete(obj)) { |
AnnaBridge | 175:b96e65c34a4d | 705 | event |= SERIAL_EVENT_RX_COMPLETE; |
AnnaBridge | 175:b96e65c34a4d | 706 | } |
AnnaBridge | 175:b96e65c34a4d | 707 | if ((obj->char_match != SERIAL_RESERVED_CHAR_MATCH) && obj->char_found) { |
AnnaBridge | 175:b96e65c34a4d | 708 | event |= SERIAL_EVENT_RX_CHARACTER_MATCH; |
AnnaBridge | 175:b96e65c34a4d | 709 | // NOTE: Timing to reset char_found? |
AnnaBridge | 175:b96e65c34a4d | 710 | // by obj->char_found = 0; |
AnnaBridge | 175:b96e65c34a4d | 711 | } |
AnnaBridge | 175:b96e65c34a4d | 712 | |
AnnaBridge | 175:b96e65c34a4d | 713 | return event; |
AnnaBridge | 175:b96e65c34a4d | 714 | } |
AnnaBridge | 175:b96e65c34a4d | 715 | |
AnnaBridge | 175:b96e65c34a4d | 716 | static void uart_dma_handler_tx(uint32_t id, uint32_t event_dma) |
AnnaBridge | 175:b96e65c34a4d | 717 | { |
AnnaBridge | 175:b96e65c34a4d | 718 | serial_t *obj = (serial_t *) id; |
AnnaBridge | 175:b96e65c34a4d | 719 | |
AnnaBridge | 175:b96e65c34a4d | 720 | // TODO: Pass this error to caller |
AnnaBridge | 175:b96e65c34a4d | 721 | if (event_dma & DMA_EVENT_ABORT) { |
AnnaBridge | 175:b96e65c34a4d | 722 | } |
AnnaBridge | 175:b96e65c34a4d | 723 | // Expect UART IRQ will catch this transfer done event |
AnnaBridge | 175:b96e65c34a4d | 724 | if (event_dma & DMA_EVENT_TRANSFER_DONE) { |
AnnaBridge | 175:b96e65c34a4d | 725 | obj->tx_buff.pos = obj->tx_buff.length; |
AnnaBridge | 175:b96e65c34a4d | 726 | } |
AnnaBridge | 175:b96e65c34a4d | 727 | // TODO: Pass this error to caller |
AnnaBridge | 175:b96e65c34a4d | 728 | if (event_dma & DMA_EVENT_TIMEOUT) { |
AnnaBridge | 175:b96e65c34a4d | 729 | } |
AnnaBridge | 175:b96e65c34a4d | 730 | |
AnnaBridge | 175:b96e65c34a4d | 731 | uart_irq_async(obj); |
AnnaBridge | 175:b96e65c34a4d | 732 | } |
AnnaBridge | 175:b96e65c34a4d | 733 | |
AnnaBridge | 175:b96e65c34a4d | 734 | static void uart_dma_handler_rx(uint32_t id, uint32_t event_dma) |
AnnaBridge | 175:b96e65c34a4d | 735 | { |
AnnaBridge | 175:b96e65c34a4d | 736 | serial_t *obj = (serial_t *) id; |
AnnaBridge | 175:b96e65c34a4d | 737 | |
AnnaBridge | 175:b96e65c34a4d | 738 | // TODO: Pass this error to caller |
AnnaBridge | 175:b96e65c34a4d | 739 | if (event_dma & DMA_EVENT_ABORT) { |
AnnaBridge | 175:b96e65c34a4d | 740 | } |
AnnaBridge | 175:b96e65c34a4d | 741 | // Expect UART IRQ will catch this transfer done event |
AnnaBridge | 175:b96e65c34a4d | 742 | if (event_dma & DMA_EVENT_TRANSFER_DONE) { |
AnnaBridge | 175:b96e65c34a4d | 743 | obj->rx_buff.pos = obj->rx_buff.length; |
AnnaBridge | 175:b96e65c34a4d | 744 | } |
AnnaBridge | 175:b96e65c34a4d | 745 | // TODO: Pass this error to caller |
AnnaBridge | 175:b96e65c34a4d | 746 | if (event_dma & DMA_EVENT_TIMEOUT) { |
AnnaBridge | 175:b96e65c34a4d | 747 | } |
AnnaBridge | 175:b96e65c34a4d | 748 | |
AnnaBridge | 175:b96e65c34a4d | 749 | uart_irq_async(obj); |
AnnaBridge | 175:b96e65c34a4d | 750 | } |
AnnaBridge | 175:b96e65c34a4d | 751 | |
AnnaBridge | 175:b96e65c34a4d | 752 | static int serial_write_async(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 753 | { |
AnnaBridge | 175:b96e65c34a4d | 754 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 755 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 756 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 757 | |
AnnaBridge | 175:b96e65c34a4d | 758 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 759 | |
AnnaBridge | 175:b96e65c34a4d | 760 | uint32_t tx_fifo_max = ((struct nu_uart_var *) modinit->var)->fifo_size_tx; |
AnnaBridge | 175:b96e65c34a4d | 761 | uint32_t tx_fifo_busy = (uart_base->FSR & UART_FSR_TX_POINTER_F_Msk) >> UART_FSR_TX_POINTER_F_Pos; |
AnnaBridge | 175:b96e65c34a4d | 762 | if (uart_base->FSR & UART_FSR_TX_FULL_F_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 763 | tx_fifo_busy = tx_fifo_max; |
AnnaBridge | 175:b96e65c34a4d | 764 | } |
AnnaBridge | 175:b96e65c34a4d | 765 | uint32_t tx_fifo_free = tx_fifo_max - tx_fifo_busy; |
AnnaBridge | 175:b96e65c34a4d | 766 | if (tx_fifo_free == 0) { |
AnnaBridge | 175:b96e65c34a4d | 767 | // Simulate clear of the interrupt flag |
AnnaBridge | 175:b96e65c34a4d | 768 | if (obj->serial.ier_msk & UART_IER_THRE_IE_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 769 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_THRE_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 770 | } |
AnnaBridge | 175:b96e65c34a4d | 771 | return 0; |
AnnaBridge | 175:b96e65c34a4d | 772 | } |
AnnaBridge | 175:b96e65c34a4d | 773 | |
AnnaBridge | 175:b96e65c34a4d | 774 | uint32_t bytes_per_word = obj->tx_buff.width / 8; |
AnnaBridge | 175:b96e65c34a4d | 775 | |
AnnaBridge | 175:b96e65c34a4d | 776 | uint8_t *tx = (uint8_t *)(obj->tx_buff.buffer) + bytes_per_word * obj->tx_buff.pos; |
AnnaBridge | 175:b96e65c34a4d | 777 | int n_words = 0; |
AnnaBridge | 175:b96e65c34a4d | 778 | while (obj->tx_buff.pos < obj->tx_buff.length && tx_fifo_free >= bytes_per_word) { |
AnnaBridge | 175:b96e65c34a4d | 779 | switch (bytes_per_word) { |
AnnaBridge | 175:b96e65c34a4d | 780 | case 4: |
AnnaBridge | 175:b96e65c34a4d | 781 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
AnnaBridge | 175:b96e65c34a4d | 782 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
AnnaBridge | 175:b96e65c34a4d | 783 | case 2: |
AnnaBridge | 175:b96e65c34a4d | 784 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
AnnaBridge | 175:b96e65c34a4d | 785 | case 1: |
AnnaBridge | 175:b96e65c34a4d | 786 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
AnnaBridge | 175:b96e65c34a4d | 787 | } |
AnnaBridge | 175:b96e65c34a4d | 788 | |
AnnaBridge | 175:b96e65c34a4d | 789 | n_words ++; |
AnnaBridge | 175:b96e65c34a4d | 790 | tx_fifo_free -= bytes_per_word; |
AnnaBridge | 175:b96e65c34a4d | 791 | obj->tx_buff.pos ++; |
AnnaBridge | 175:b96e65c34a4d | 792 | } |
AnnaBridge | 175:b96e65c34a4d | 793 | |
AnnaBridge | 175:b96e65c34a4d | 794 | if (n_words) { |
AnnaBridge | 175:b96e65c34a4d | 795 | // Simulate clear of the interrupt flag |
AnnaBridge | 175:b96e65c34a4d | 796 | if (obj->serial.ier_msk & UART_IER_THRE_IE_Msk) { |
AnnaBridge | 175:b96e65c34a4d | 797 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_THRE_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 798 | } |
AnnaBridge | 175:b96e65c34a4d | 799 | } |
AnnaBridge | 175:b96e65c34a4d | 800 | |
AnnaBridge | 175:b96e65c34a4d | 801 | return n_words; |
AnnaBridge | 175:b96e65c34a4d | 802 | } |
AnnaBridge | 175:b96e65c34a4d | 803 | |
AnnaBridge | 175:b96e65c34a4d | 804 | static int serial_read_async(serial_t *obj) |
AnnaBridge | 175:b96e65c34a4d | 805 | { |
AnnaBridge | 175:b96e65c34a4d | 806 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 807 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 808 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 809 | |
AnnaBridge | 175:b96e65c34a4d | 810 | uint32_t rx_fifo_busy = (((UART_T *) NU_MODBASE(obj->serial.uart))->FSR & UART_FSR_RX_POINTER_F_Msk) >> UART_FSR_RX_POINTER_F_Pos; |
AnnaBridge | 175:b96e65c34a4d | 811 | |
AnnaBridge | 175:b96e65c34a4d | 812 | uint32_t bytes_per_word = obj->rx_buff.width / 8; |
AnnaBridge | 175:b96e65c34a4d | 813 | |
AnnaBridge | 175:b96e65c34a4d | 814 | uint8_t *rx = (uint8_t *)(obj->rx_buff.buffer) + bytes_per_word * obj->rx_buff.pos; |
AnnaBridge | 175:b96e65c34a4d | 815 | int n_words = 0; |
AnnaBridge | 175:b96e65c34a4d | 816 | while (obj->rx_buff.pos < obj->rx_buff.length && rx_fifo_busy >= bytes_per_word) { |
AnnaBridge | 175:b96e65c34a4d | 817 | switch (bytes_per_word) { |
AnnaBridge | 175:b96e65c34a4d | 818 | case 4: |
AnnaBridge | 175:b96e65c34a4d | 819 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
AnnaBridge | 175:b96e65c34a4d | 820 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
AnnaBridge | 175:b96e65c34a4d | 821 | case 2: |
AnnaBridge | 175:b96e65c34a4d | 822 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
AnnaBridge | 175:b96e65c34a4d | 823 | case 1: |
AnnaBridge | 175:b96e65c34a4d | 824 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
AnnaBridge | 175:b96e65c34a4d | 825 | } |
AnnaBridge | 175:b96e65c34a4d | 826 | |
AnnaBridge | 175:b96e65c34a4d | 827 | n_words ++; |
AnnaBridge | 175:b96e65c34a4d | 828 | rx_fifo_busy -= bytes_per_word; |
AnnaBridge | 175:b96e65c34a4d | 829 | obj->rx_buff.pos ++; |
AnnaBridge | 175:b96e65c34a4d | 830 | |
AnnaBridge | 175:b96e65c34a4d | 831 | if ((obj->serial.event & SERIAL_EVENT_RX_CHARACTER_MATCH) && |
AnnaBridge | 175:b96e65c34a4d | 832 | obj->char_match != SERIAL_RESERVED_CHAR_MATCH) { |
AnnaBridge | 175:b96e65c34a4d | 833 | uint8_t *rx_cmp = rx; |
AnnaBridge | 175:b96e65c34a4d | 834 | switch (bytes_per_word) { |
AnnaBridge | 175:b96e65c34a4d | 835 | case 4: |
AnnaBridge | 175:b96e65c34a4d | 836 | rx_cmp -= 2; |
AnnaBridge | 175:b96e65c34a4d | 837 | case 2: |
AnnaBridge | 175:b96e65c34a4d | 838 | rx_cmp --; |
AnnaBridge | 175:b96e65c34a4d | 839 | case 1: |
AnnaBridge | 175:b96e65c34a4d | 840 | rx_cmp --; |
AnnaBridge | 175:b96e65c34a4d | 841 | } |
AnnaBridge | 175:b96e65c34a4d | 842 | if (*rx_cmp == obj->char_match) { |
AnnaBridge | 175:b96e65c34a4d | 843 | obj->char_found = 1; |
AnnaBridge | 175:b96e65c34a4d | 844 | break; |
AnnaBridge | 175:b96e65c34a4d | 845 | } |
AnnaBridge | 175:b96e65c34a4d | 846 | } |
AnnaBridge | 175:b96e65c34a4d | 847 | } |
AnnaBridge | 175:b96e65c34a4d | 848 | |
AnnaBridge | 175:b96e65c34a4d | 849 | if (n_words) { |
AnnaBridge | 175:b96e65c34a4d | 850 | // Simulate clear of the interrupt flag |
AnnaBridge | 175:b96e65c34a4d | 851 | if (obj->serial.ier_msk & (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)) { |
AnnaBridge | 175:b96e65c34a4d | 852 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)); |
AnnaBridge | 175:b96e65c34a4d | 853 | } |
AnnaBridge | 175:b96e65c34a4d | 854 | } |
AnnaBridge | 175:b96e65c34a4d | 855 | |
AnnaBridge | 175:b96e65c34a4d | 856 | return n_words; |
AnnaBridge | 175:b96e65c34a4d | 857 | } |
AnnaBridge | 175:b96e65c34a4d | 858 | |
AnnaBridge | 175:b96e65c34a4d | 859 | static void serial_tx_buffer_set(serial_t *obj, const void *tx, size_t length, uint8_t width) |
AnnaBridge | 175:b96e65c34a4d | 860 | { |
AnnaBridge | 175:b96e65c34a4d | 861 | obj->tx_buff.buffer = (void *) tx; |
AnnaBridge | 175:b96e65c34a4d | 862 | obj->tx_buff.length = length; |
AnnaBridge | 175:b96e65c34a4d | 863 | obj->tx_buff.pos = 0; |
AnnaBridge | 175:b96e65c34a4d | 864 | obj->tx_buff.width = width; |
AnnaBridge | 175:b96e65c34a4d | 865 | } |
AnnaBridge | 175:b96e65c34a4d | 866 | |
AnnaBridge | 175:b96e65c34a4d | 867 | static void serial_rx_buffer_set(serial_t *obj, void *rx, size_t length, uint8_t width) |
AnnaBridge | 175:b96e65c34a4d | 868 | { |
AnnaBridge | 175:b96e65c34a4d | 869 | obj->rx_buff.buffer = rx; |
AnnaBridge | 175:b96e65c34a4d | 870 | obj->rx_buff.length = length; |
AnnaBridge | 175:b96e65c34a4d | 871 | obj->rx_buff.pos = 0; |
AnnaBridge | 175:b96e65c34a4d | 872 | obj->rx_buff.width = width; |
AnnaBridge | 175:b96e65c34a4d | 873 | } |
AnnaBridge | 175:b96e65c34a4d | 874 | |
AnnaBridge | 175:b96e65c34a4d | 875 | static void serial_tx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t enable) |
AnnaBridge | 175:b96e65c34a4d | 876 | { |
AnnaBridge | 175:b96e65c34a4d | 877 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 878 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 879 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 880 | |
AnnaBridge | 175:b96e65c34a4d | 881 | // Necessary for both interrupt way and DMA way |
AnnaBridge | 175:b96e65c34a4d | 882 | // A flag to indicate async mode, and tx/rx handlers can be different. |
AnnaBridge | 175:b96e65c34a4d | 883 | obj->serial.async_en = 1; |
AnnaBridge | 175:b96e65c34a4d | 884 | obj->serial.irq_handler_tx_async = (void (*)(void)) handler; |
AnnaBridge | 175:b96e65c34a4d | 885 | serial_enable_interrupt(obj, TxIrq, enable); |
AnnaBridge | 175:b96e65c34a4d | 886 | } |
AnnaBridge | 175:b96e65c34a4d | 887 | |
AnnaBridge | 175:b96e65c34a4d | 888 | static void serial_rx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t enable) |
AnnaBridge | 175:b96e65c34a4d | 889 | { |
AnnaBridge | 175:b96e65c34a4d | 890 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 891 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 892 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 893 | |
AnnaBridge | 175:b96e65c34a4d | 894 | // Necessary for both interrupt way and DMA way |
AnnaBridge | 175:b96e65c34a4d | 895 | // A flag to indicate async mode, and tx/rx handlers can be different. |
AnnaBridge | 175:b96e65c34a4d | 896 | obj->serial.async_en = 1; |
AnnaBridge | 175:b96e65c34a4d | 897 | obj->serial.irq_handler_rx_async = (void (*) (void)) handler; |
AnnaBridge | 175:b96e65c34a4d | 898 | serial_enable_interrupt(obj, RxIrq, enable); |
AnnaBridge | 175:b96e65c34a4d | 899 | } |
AnnaBridge | 175:b96e65c34a4d | 900 | |
AnnaBridge | 175:b96e65c34a4d | 901 | static void serial_enable_interrupt(serial_t *obj, SerialIrq irq, uint32_t enable) |
AnnaBridge | 175:b96e65c34a4d | 902 | { |
AnnaBridge | 175:b96e65c34a4d | 903 | if (enable) { |
AnnaBridge | 175:b96e65c34a4d | 904 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 905 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 906 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 907 | |
AnnaBridge | 175:b96e65c34a4d | 908 | NVIC_SetVector(modinit->irq_n, (uint32_t) obj->serial.vec); |
AnnaBridge | 175:b96e65c34a4d | 909 | NVIC_EnableIRQ(modinit->irq_n); |
AnnaBridge | 175:b96e65c34a4d | 910 | |
AnnaBridge | 175:b96e65c34a4d | 911 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
AnnaBridge | 175:b96e65c34a4d | 912 | // Multiple serial S/W objects for single UART H/W module possibly. |
AnnaBridge | 175:b96e65c34a4d | 913 | // Bind serial S/W object to UART H/W module as interrupt is enabled. |
AnnaBridge | 175:b96e65c34a4d | 914 | var->obj = obj; |
AnnaBridge | 175:b96e65c34a4d | 915 | |
AnnaBridge | 175:b96e65c34a4d | 916 | switch (irq) { |
AnnaBridge | 175:b96e65c34a4d | 917 | // NOTE: Setting ier_msk first to avoid race condition |
AnnaBridge | 175:b96e65c34a4d | 918 | case RxIrq: |
AnnaBridge | 175:b96e65c34a4d | 919 | obj->serial.ier_msk = obj->serial.ier_msk | (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 920 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)); |
AnnaBridge | 175:b96e65c34a4d | 921 | break; |
AnnaBridge | 175:b96e65c34a4d | 922 | case TxIrq: |
AnnaBridge | 175:b96e65c34a4d | 923 | obj->serial.ier_msk = obj->serial.ier_msk | UART_IER_THRE_IE_Msk; |
AnnaBridge | 175:b96e65c34a4d | 924 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_THRE_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 925 | break; |
AnnaBridge | 175:b96e65c34a4d | 926 | } |
AnnaBridge | 175:b96e65c34a4d | 927 | } |
AnnaBridge | 175:b96e65c34a4d | 928 | else { // disable |
AnnaBridge | 175:b96e65c34a4d | 929 | switch (irq) { |
AnnaBridge | 175:b96e65c34a4d | 930 | case RxIrq: |
AnnaBridge | 175:b96e65c34a4d | 931 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk)); |
AnnaBridge | 175:b96e65c34a4d | 932 | obj->serial.ier_msk = obj->serial.ier_msk & ~(UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 933 | break; |
AnnaBridge | 175:b96e65c34a4d | 934 | case TxIrq: |
AnnaBridge | 175:b96e65c34a4d | 935 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_IER_THRE_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 936 | obj->serial.ier_msk = obj->serial.ier_msk & ~UART_IER_THRE_IE_Msk; |
AnnaBridge | 175:b96e65c34a4d | 937 | break; |
AnnaBridge | 175:b96e65c34a4d | 938 | } |
AnnaBridge | 175:b96e65c34a4d | 939 | } |
AnnaBridge | 175:b96e65c34a4d | 940 | } |
AnnaBridge | 175:b96e65c34a4d | 941 | |
AnnaBridge | 175:b96e65c34a4d | 942 | static void serial_rollback_interrupt(serial_t *obj, SerialIrq irq) |
AnnaBridge | 175:b96e65c34a4d | 943 | { |
AnnaBridge | 175:b96e65c34a4d | 944 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 175:b96e65c34a4d | 945 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 175:b96e65c34a4d | 946 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 175:b96e65c34a4d | 947 | |
AnnaBridge | 175:b96e65c34a4d | 948 | obj->serial.async_en = 0; |
AnnaBridge | 175:b96e65c34a4d | 949 | serial_enable_interrupt(obj, irq, obj->serial.irq_en); |
AnnaBridge | 175:b96e65c34a4d | 950 | } |
AnnaBridge | 175:b96e65c34a4d | 951 | |
AnnaBridge | 175:b96e65c34a4d | 952 | static void serial_check_dma_usage(DMAUsage *dma_usage, int *dma_ch) |
AnnaBridge | 175:b96e65c34a4d | 953 | { |
AnnaBridge | 175:b96e65c34a4d | 954 | if (*dma_usage != DMA_USAGE_NEVER) { |
AnnaBridge | 175:b96e65c34a4d | 955 | if (*dma_ch == DMA_ERROR_OUT_OF_CHANNELS) { |
AnnaBridge | 175:b96e65c34a4d | 956 | *dma_ch = dma_channel_allocate(DMA_CAP_NONE); |
AnnaBridge | 175:b96e65c34a4d | 957 | } |
AnnaBridge | 175:b96e65c34a4d | 958 | if (*dma_ch == DMA_ERROR_OUT_OF_CHANNELS) { |
AnnaBridge | 175:b96e65c34a4d | 959 | *dma_usage = DMA_USAGE_NEVER; |
AnnaBridge | 175:b96e65c34a4d | 960 | } |
AnnaBridge | 175:b96e65c34a4d | 961 | } |
AnnaBridge | 175:b96e65c34a4d | 962 | else { |
AnnaBridge | 175:b96e65c34a4d | 963 | dma_channel_free(*dma_ch); |
AnnaBridge | 175:b96e65c34a4d | 964 | *dma_ch = DMA_ERROR_OUT_OF_CHANNELS; |
AnnaBridge | 175:b96e65c34a4d | 965 | } |
AnnaBridge | 175:b96e65c34a4d | 966 | } |
AnnaBridge | 175:b96e65c34a4d | 967 | |
AnnaBridge | 175:b96e65c34a4d | 968 | static int serial_is_irq_en(serial_t *obj, SerialIrq irq) |
AnnaBridge | 175:b96e65c34a4d | 969 | { |
AnnaBridge | 175:b96e65c34a4d | 970 | int ier_msk = 0; |
AnnaBridge | 175:b96e65c34a4d | 971 | |
AnnaBridge | 175:b96e65c34a4d | 972 | switch (irq) { |
AnnaBridge | 175:b96e65c34a4d | 973 | case RxIrq: |
AnnaBridge | 175:b96e65c34a4d | 974 | ier_msk = obj->serial.ier_msk & (UART_IER_RDA_IE_Msk | UART_IER_RTO_IE_Msk); |
AnnaBridge | 175:b96e65c34a4d | 975 | break; |
AnnaBridge | 175:b96e65c34a4d | 976 | case TxIrq: |
AnnaBridge | 175:b96e65c34a4d | 977 | ier_msk = obj->serial.ier_msk & UART_IER_THRE_IE_Msk; |
AnnaBridge | 175:b96e65c34a4d | 978 | break; |
AnnaBridge | 175:b96e65c34a4d | 979 | } |
AnnaBridge | 175:b96e65c34a4d | 980 | |
AnnaBridge | 175:b96e65c34a4d | 981 | return !! ier_msk; |
AnnaBridge | 175:b96e65c34a4d | 982 | } |
AnnaBridge | 175:b96e65c34a4d | 983 | |
AnnaBridge | 175:b96e65c34a4d | 984 | #endif // #if DEVICE_SERIAL_ASYNCH |
AnnaBridge | 175:b96e65c34a4d | 985 | #endif // #if DEVICE_SERIAL |