mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c
Dependents: Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/serial_api.c@176:447f873cad2f, 2017-10-25 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Oct 25 14:53:38 2017 +0100
- Revision:
- 176:447f873cad2f
- Parent:
- 172:7d866c31b3c5
This updates the lib to the mbed lib v 154
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) 2015-2016 Nuvoton |
<> | 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 | |
<> | 144:ef7eb2e8f9f7 | 17 | #include "serial_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | |
<> | 144:ef7eb2e8f9f7 | 19 | #if DEVICE_SERIAL |
<> | 144:ef7eb2e8f9f7 | 20 | |
<> | 144:ef7eb2e8f9f7 | 21 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 22 | #include "mbed_error.h" |
<> | 144:ef7eb2e8f9f7 | 23 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 24 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 25 | #include "nu_modutil.h" |
<> | 144:ef7eb2e8f9f7 | 26 | #include "nu_bitutil.h" |
AnnaBridge | 165:e614a9f1c9e2 | 27 | #include <string.h> |
<> | 144:ef7eb2e8f9f7 | 28 | |
<> | 144:ef7eb2e8f9f7 | 29 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 30 | #include "dma_api.h" |
<> | 144:ef7eb2e8f9f7 | 31 | #include "dma.h" |
<> | 144:ef7eb2e8f9f7 | 32 | #endif |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | struct nu_uart_var { |
<> | 151:5eaa88a5bcc7 | 35 | uint32_t ref_cnt; // Reference count of the H/W module |
<> | 144:ef7eb2e8f9f7 | 36 | serial_t * obj; |
<> | 144:ef7eb2e8f9f7 | 37 | uint32_t fifo_size_tx; |
<> | 144:ef7eb2e8f9f7 | 38 | uint32_t fifo_size_rx; |
<> | 144:ef7eb2e8f9f7 | 39 | void (*vec)(void); |
<> | 144:ef7eb2e8f9f7 | 40 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 41 | void (*vec_async)(void); |
<> | 144:ef7eb2e8f9f7 | 42 | uint8_t pdma_perp_tx; |
<> | 144:ef7eb2e8f9f7 | 43 | uint8_t pdma_perp_rx; |
<> | 144:ef7eb2e8f9f7 | 44 | #endif |
<> | 144:ef7eb2e8f9f7 | 45 | }; |
<> | 144:ef7eb2e8f9f7 | 46 | |
<> | 144:ef7eb2e8f9f7 | 47 | static void uart0_vec(void); |
<> | 144:ef7eb2e8f9f7 | 48 | static void uart1_vec(void); |
<> | 144:ef7eb2e8f9f7 | 49 | static void uart2_vec(void); |
<> | 144:ef7eb2e8f9f7 | 50 | static void uart3_vec(void); |
<> | 144:ef7eb2e8f9f7 | 51 | static void uart4_vec(void); |
<> | 144:ef7eb2e8f9f7 | 52 | static void uart5_vec(void); |
<> | 144:ef7eb2e8f9f7 | 53 | static void uart_irq(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 54 | |
<> | 144:ef7eb2e8f9f7 | 55 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 56 | static void uart0_vec_async(void); |
<> | 144:ef7eb2e8f9f7 | 57 | static void uart1_vec_async(void); |
<> | 144:ef7eb2e8f9f7 | 58 | static void uart2_vec_async(void); |
<> | 144:ef7eb2e8f9f7 | 59 | static void uart3_vec_async(void); |
<> | 144:ef7eb2e8f9f7 | 60 | static void uart4_vec_async(void); |
<> | 144:ef7eb2e8f9f7 | 61 | static void uart5_vec_async(void); |
<> | 144:ef7eb2e8f9f7 | 62 | static void uart_irq_async(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 63 | |
<> | 144:ef7eb2e8f9f7 | 64 | static void uart_dma_handler_tx(uint32_t id, uint32_t event); |
<> | 144:ef7eb2e8f9f7 | 65 | static void uart_dma_handler_rx(uint32_t id, uint32_t event); |
<> | 144:ef7eb2e8f9f7 | 66 | |
<> | 144:ef7eb2e8f9f7 | 67 | static void serial_tx_enable_interrupt(serial_t *obj, uint32_t address, uint8_t enable); |
<> | 144:ef7eb2e8f9f7 | 68 | static void serial_rx_enable_interrupt(serial_t *obj, uint32_t address, uint8_t enable); |
AnnaBridge | 165:e614a9f1c9e2 | 69 | static void serial_enable_interrupt(serial_t *obj, SerialIrq irq, uint32_t enable); |
AnnaBridge | 165:e614a9f1c9e2 | 70 | static void serial_rollback_interrupt(serial_t *obj, SerialIrq irq); |
<> | 144:ef7eb2e8f9f7 | 71 | static int serial_write_async(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 72 | static int serial_read_async(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 73 | |
<> | 144:ef7eb2e8f9f7 | 74 | static uint32_t serial_rx_event_check(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 75 | static uint32_t serial_tx_event_check(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 76 | |
<> | 144:ef7eb2e8f9f7 | 77 | static int serial_is_tx_complete(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 78 | static void serial_tx_enable_event(serial_t *obj, int event, uint8_t enable); |
<> | 144:ef7eb2e8f9f7 | 79 | |
<> | 144:ef7eb2e8f9f7 | 80 | static void serial_tx_buffer_set(serial_t *obj, const void *tx, size_t length, uint8_t width); |
<> | 144:ef7eb2e8f9f7 | 81 | static void serial_rx_buffer_set(serial_t *obj, void *rx, size_t length, uint8_t width); |
<> | 144:ef7eb2e8f9f7 | 82 | static void serial_rx_set_char_match(serial_t *obj, uint8_t char_match); |
<> | 144:ef7eb2e8f9f7 | 83 | static void serial_rx_enable_event(serial_t *obj, int event, uint8_t enable); |
<> | 144:ef7eb2e8f9f7 | 84 | static int serial_is_rx_complete(serial_t *obj); |
<> | 144:ef7eb2e8f9f7 | 85 | |
<> | 144:ef7eb2e8f9f7 | 86 | static void serial_check_dma_usage(DMAUsage *dma_usage, int *dma_ch); |
<> | 144:ef7eb2e8f9f7 | 87 | static int serial_is_irq_en(serial_t *obj, SerialIrq irq); |
<> | 144:ef7eb2e8f9f7 | 88 | #endif |
<> | 144:ef7eb2e8f9f7 | 89 | |
<> | 144:ef7eb2e8f9f7 | 90 | static struct nu_uart_var uart0_var = { |
<> | 151:5eaa88a5bcc7 | 91 | .ref_cnt = 0, |
<> | 144:ef7eb2e8f9f7 | 92 | .obj = NULL, |
<> | 144:ef7eb2e8f9f7 | 93 | .fifo_size_tx = 64, |
<> | 144:ef7eb2e8f9f7 | 94 | .fifo_size_rx = 64, |
<> | 144:ef7eb2e8f9f7 | 95 | .vec = uart0_vec, |
<> | 144:ef7eb2e8f9f7 | 96 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 97 | .vec_async = uart0_vec_async, |
<> | 144:ef7eb2e8f9f7 | 98 | .pdma_perp_tx = PDMA_UART0_TX, |
<> | 144:ef7eb2e8f9f7 | 99 | .pdma_perp_rx = PDMA_UART0_RX |
<> | 144:ef7eb2e8f9f7 | 100 | #endif |
<> | 144:ef7eb2e8f9f7 | 101 | }; |
<> | 144:ef7eb2e8f9f7 | 102 | static struct nu_uart_var uart1_var = { |
<> | 151:5eaa88a5bcc7 | 103 | .ref_cnt = 0, |
<> | 144:ef7eb2e8f9f7 | 104 | .obj = NULL, |
<> | 144:ef7eb2e8f9f7 | 105 | .fifo_size_tx = 16, |
<> | 144:ef7eb2e8f9f7 | 106 | .fifo_size_rx = 16, |
<> | 144:ef7eb2e8f9f7 | 107 | .vec = uart1_vec, |
<> | 144:ef7eb2e8f9f7 | 108 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 109 | .vec_async = uart1_vec_async, |
<> | 144:ef7eb2e8f9f7 | 110 | .pdma_perp_tx = PDMA_UART1_TX, |
<> | 144:ef7eb2e8f9f7 | 111 | .pdma_perp_rx = PDMA_UART1_RX |
<> | 144:ef7eb2e8f9f7 | 112 | #endif |
<> | 144:ef7eb2e8f9f7 | 113 | }; |
<> | 144:ef7eb2e8f9f7 | 114 | static struct nu_uart_var uart2_var = { |
<> | 151:5eaa88a5bcc7 | 115 | .ref_cnt = 0, |
<> | 144:ef7eb2e8f9f7 | 116 | .obj = NULL, |
<> | 144:ef7eb2e8f9f7 | 117 | .fifo_size_tx = 16, |
<> | 144:ef7eb2e8f9f7 | 118 | .fifo_size_rx = 16, |
<> | 144:ef7eb2e8f9f7 | 119 | .vec = uart2_vec, |
<> | 144:ef7eb2e8f9f7 | 120 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 121 | .vec_async = uart2_vec_async, |
<> | 144:ef7eb2e8f9f7 | 122 | .pdma_perp_tx = PDMA_UART2_TX, |
<> | 144:ef7eb2e8f9f7 | 123 | .pdma_perp_rx = PDMA_UART2_RX |
<> | 144:ef7eb2e8f9f7 | 124 | #endif |
<> | 144:ef7eb2e8f9f7 | 125 | }; |
<> | 144:ef7eb2e8f9f7 | 126 | static struct nu_uart_var uart3_var = { |
<> | 151:5eaa88a5bcc7 | 127 | .ref_cnt = 0, |
<> | 144:ef7eb2e8f9f7 | 128 | .obj = NULL, |
<> | 144:ef7eb2e8f9f7 | 129 | .fifo_size_tx = 16, |
<> | 144:ef7eb2e8f9f7 | 130 | .fifo_size_rx = 16, |
<> | 144:ef7eb2e8f9f7 | 131 | .vec = uart3_vec, |
<> | 144:ef7eb2e8f9f7 | 132 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 133 | .vec_async = uart3_vec_async, |
<> | 144:ef7eb2e8f9f7 | 134 | .pdma_perp_tx = PDMA_UART3_TX, |
<> | 144:ef7eb2e8f9f7 | 135 | .pdma_perp_rx = PDMA_UART3_RX |
<> | 144:ef7eb2e8f9f7 | 136 | #endif |
<> | 144:ef7eb2e8f9f7 | 137 | }; |
<> | 144:ef7eb2e8f9f7 | 138 | static struct nu_uart_var uart4_var = { |
<> | 151:5eaa88a5bcc7 | 139 | .ref_cnt = 0, |
<> | 144:ef7eb2e8f9f7 | 140 | .obj = NULL, |
<> | 144:ef7eb2e8f9f7 | 141 | .fifo_size_tx = 16, |
<> | 144:ef7eb2e8f9f7 | 142 | .fifo_size_rx = 16, |
<> | 144:ef7eb2e8f9f7 | 143 | .vec = uart4_vec, |
<> | 144:ef7eb2e8f9f7 | 144 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 145 | .vec_async = uart4_vec_async, |
<> | 144:ef7eb2e8f9f7 | 146 | .pdma_perp_tx = PDMA_UART4_TX, |
<> | 144:ef7eb2e8f9f7 | 147 | .pdma_perp_rx = PDMA_UART4_RX |
<> | 144:ef7eb2e8f9f7 | 148 | #endif |
<> | 144:ef7eb2e8f9f7 | 149 | }; |
<> | 144:ef7eb2e8f9f7 | 150 | static struct nu_uart_var uart5_var = { |
<> | 151:5eaa88a5bcc7 | 151 | .ref_cnt = 0, |
<> | 144:ef7eb2e8f9f7 | 152 | .obj = NULL, |
<> | 144:ef7eb2e8f9f7 | 153 | .fifo_size_tx = 16, |
<> | 144:ef7eb2e8f9f7 | 154 | .fifo_size_rx = 16, |
<> | 144:ef7eb2e8f9f7 | 155 | .vec = uart5_vec, |
<> | 144:ef7eb2e8f9f7 | 156 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 157 | .vec_async = uart5_vec_async, |
<> | 144:ef7eb2e8f9f7 | 158 | .pdma_perp_tx = PDMA_UART5_TX, |
<> | 144:ef7eb2e8f9f7 | 159 | .pdma_perp_rx = PDMA_UART5_RX |
<> | 144:ef7eb2e8f9f7 | 160 | #endif |
<> | 144:ef7eb2e8f9f7 | 161 | }; |
<> | 144:ef7eb2e8f9f7 | 162 | |
<> | 144:ef7eb2e8f9f7 | 163 | |
<> | 144:ef7eb2e8f9f7 | 164 | int stdio_uart_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 165 | serial_t stdio_uart; |
<> | 144:ef7eb2e8f9f7 | 166 | static uint32_t uart_modinit_mask = 0; |
<> | 144:ef7eb2e8f9f7 | 167 | |
<> | 144:ef7eb2e8f9f7 | 168 | static const struct nu_modinit_s uart_modinit_tab[] = { |
<> | 144:ef7eb2e8f9f7 | 169 | {UART_0, UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1), UART0_RST, UART0_IRQn, &uart0_var}, |
<> | 144:ef7eb2e8f9f7 | 170 | {UART_1, UART1_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1), UART1_RST, UART1_IRQn, &uart1_var}, |
<> | 144:ef7eb2e8f9f7 | 171 | {UART_2, UART2_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1), UART2_RST, UART2_IRQn, &uart2_var}, |
<> | 144:ef7eb2e8f9f7 | 172 | {UART_3, UART3_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1), UART3_RST, UART3_IRQn, &uart3_var}, |
<> | 144:ef7eb2e8f9f7 | 173 | {UART_4, UART4_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1), UART4_RST, UART4_IRQn, &uart4_var}, |
<> | 144:ef7eb2e8f9f7 | 174 | {UART_5, UART5_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1), UART5_RST, UART5_IRQn, &uart5_var}, |
<> | 144:ef7eb2e8f9f7 | 175 | |
<> | 144:ef7eb2e8f9f7 | 176 | {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL} |
<> | 144:ef7eb2e8f9f7 | 177 | }; |
<> | 144:ef7eb2e8f9f7 | 178 | |
<> | 144:ef7eb2e8f9f7 | 179 | extern void mbed_sdk_init(void); |
<> | 144:ef7eb2e8f9f7 | 180 | |
<> | 144:ef7eb2e8f9f7 | 181 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
<> | 144:ef7eb2e8f9f7 | 182 | { |
<> | 151:5eaa88a5bcc7 | 183 | // NOTE: With armcc, serial_init() gets called from _sys_open() timing of which is before main()/mbed_sdk_init(). |
<> | 144:ef7eb2e8f9f7 | 184 | mbed_sdk_init(); |
<> | 144:ef7eb2e8f9f7 | 185 | |
<> | 144:ef7eb2e8f9f7 | 186 | // Determine which UART_x the pins are used for |
<> | 144:ef7eb2e8f9f7 | 187 | uint32_t uart_tx = pinmap_peripheral(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 188 | uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX); |
<> | 144:ef7eb2e8f9f7 | 189 | // Get the peripheral name (UART_x) from the pins and assign it to the object |
<> | 144:ef7eb2e8f9f7 | 190 | obj->serial.uart = (UARTName) pinmap_merge(uart_tx, uart_rx); |
<> | 144:ef7eb2e8f9f7 | 191 | MBED_ASSERT((int)obj->serial.uart != NC); |
<> | 144:ef7eb2e8f9f7 | 192 | |
<> | 144:ef7eb2e8f9f7 | 193 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 194 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 195 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 196 | |
<> | 151:5eaa88a5bcc7 | 197 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
<> | 144:ef7eb2e8f9f7 | 198 | |
<> | 151:5eaa88a5bcc7 | 199 | if (! var->ref_cnt) { |
<> | 151:5eaa88a5bcc7 | 200 | // Reset this module |
<> | 151:5eaa88a5bcc7 | 201 | SYS_ResetModule(modinit->rsetidx); |
<> | 151:5eaa88a5bcc7 | 202 | |
<> | 151:5eaa88a5bcc7 | 203 | // Select IP clock source |
<> | 151:5eaa88a5bcc7 | 204 | CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); |
<> | 151:5eaa88a5bcc7 | 205 | // Enable IP clock |
<> | 151:5eaa88a5bcc7 | 206 | CLK_EnableModuleClock(modinit->clkidx); |
<> | 144:ef7eb2e8f9f7 | 207 | |
<> | 151:5eaa88a5bcc7 | 208 | pinmap_pinout(tx, PinMap_UART_TX); |
<> | 151:5eaa88a5bcc7 | 209 | pinmap_pinout(rx, PinMap_UART_RX); |
<> | 151:5eaa88a5bcc7 | 210 | |
<> | 151:5eaa88a5bcc7 | 211 | obj->serial.pin_tx = tx; |
<> | 151:5eaa88a5bcc7 | 212 | obj->serial.pin_rx = rx; |
<> | 151:5eaa88a5bcc7 | 213 | } |
<> | 151:5eaa88a5bcc7 | 214 | var->ref_cnt ++; |
<> | 144:ef7eb2e8f9f7 | 215 | |
<> | 144:ef7eb2e8f9f7 | 216 | // Configure the UART module and set its baudrate |
<> | 144:ef7eb2e8f9f7 | 217 | serial_baud(obj, 9600); |
<> | 144:ef7eb2e8f9f7 | 218 | // Configure data bits, parity, and stop bits |
<> | 144:ef7eb2e8f9f7 | 219 | serial_format(obj, 8, ParityNone, 1); |
<> | 144:ef7eb2e8f9f7 | 220 | |
<> | 151:5eaa88a5bcc7 | 221 | obj->serial.vec = var->vec; |
AnnaBridge | 165:e614a9f1c9e2 | 222 | obj->serial.irq_en = 0; |
<> | 144:ef7eb2e8f9f7 | 223 | |
<> | 144:ef7eb2e8f9f7 | 224 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 225 | obj->serial.dma_usage_tx = DMA_USAGE_NEVER; |
<> | 144:ef7eb2e8f9f7 | 226 | obj->serial.dma_usage_rx = DMA_USAGE_NEVER; |
<> | 144:ef7eb2e8f9f7 | 227 | obj->serial.event = 0; |
<> | 144:ef7eb2e8f9f7 | 228 | obj->serial.dma_chn_id_tx = DMA_ERROR_OUT_OF_CHANNELS; |
<> | 144:ef7eb2e8f9f7 | 229 | obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; |
<> | 144:ef7eb2e8f9f7 | 230 | #endif |
<> | 144:ef7eb2e8f9f7 | 231 | |
<> | 144:ef7eb2e8f9f7 | 232 | // For stdio management |
<> | 151:5eaa88a5bcc7 | 233 | if (obj->serial.uart == STDIO_UART) { |
<> | 144:ef7eb2e8f9f7 | 234 | stdio_uart_inited = 1; |
<> | 151:5eaa88a5bcc7 | 235 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
<> | 144:ef7eb2e8f9f7 | 236 | } |
<> | 144:ef7eb2e8f9f7 | 237 | |
<> | 151:5eaa88a5bcc7 | 238 | if (var->ref_cnt) { |
<> | 151:5eaa88a5bcc7 | 239 | // Mark this module to be inited. |
<> | 151:5eaa88a5bcc7 | 240 | int i = modinit - uart_modinit_tab; |
<> | 151:5eaa88a5bcc7 | 241 | uart_modinit_mask |= 1 << i; |
<> | 151:5eaa88a5bcc7 | 242 | } |
<> | 144:ef7eb2e8f9f7 | 243 | } |
<> | 144:ef7eb2e8f9f7 | 244 | |
<> | 144:ef7eb2e8f9f7 | 245 | void serial_free(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 246 | { |
<> | 144:ef7eb2e8f9f7 | 247 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 248 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 249 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 250 | |
<> | 151:5eaa88a5bcc7 | 251 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
<> | 144:ef7eb2e8f9f7 | 252 | |
<> | 151:5eaa88a5bcc7 | 253 | var->ref_cnt --; |
<> | 151:5eaa88a5bcc7 | 254 | if (! var->ref_cnt) { |
<> | 151:5eaa88a5bcc7 | 255 | #if DEVICE_SERIAL_ASYNCH |
<> | 151:5eaa88a5bcc7 | 256 | if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 151:5eaa88a5bcc7 | 257 | dma_channel_free(obj->serial.dma_chn_id_tx); |
<> | 151:5eaa88a5bcc7 | 258 | obj->serial.dma_chn_id_tx = DMA_ERROR_OUT_OF_CHANNELS; |
<> | 151:5eaa88a5bcc7 | 259 | } |
<> | 151:5eaa88a5bcc7 | 260 | if (obj->serial.dma_chn_id_rx != DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 151:5eaa88a5bcc7 | 261 | dma_channel_free(obj->serial.dma_chn_id_rx); |
<> | 151:5eaa88a5bcc7 | 262 | obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; |
<> | 151:5eaa88a5bcc7 | 263 | } |
<> | 151:5eaa88a5bcc7 | 264 | #endif |
<> | 151:5eaa88a5bcc7 | 265 | |
<> | 151:5eaa88a5bcc7 | 266 | UART_Close((UART_T *) NU_MODBASE(obj->serial.uart)); |
<> | 144:ef7eb2e8f9f7 | 267 | |
<> | 151:5eaa88a5bcc7 | 268 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk | UART_INTEN_RXTOIEN_Msk)); |
<> | 151:5eaa88a5bcc7 | 269 | NVIC_DisableIRQ(modinit->irq_n); |
<> | 144:ef7eb2e8f9f7 | 270 | |
<> | 151:5eaa88a5bcc7 | 271 | // Disable IP clock |
<> | 151:5eaa88a5bcc7 | 272 | CLK_DisableModuleClock(modinit->clkidx); |
<> | 151:5eaa88a5bcc7 | 273 | } |
<> | 151:5eaa88a5bcc7 | 274 | |
<> | 151:5eaa88a5bcc7 | 275 | if (var->obj == obj) { |
<> | 151:5eaa88a5bcc7 | 276 | var->obj = NULL; |
<> | 151:5eaa88a5bcc7 | 277 | } |
<> | 151:5eaa88a5bcc7 | 278 | |
<> | 151:5eaa88a5bcc7 | 279 | if (obj->serial.uart == STDIO_UART) { |
<> | 144:ef7eb2e8f9f7 | 280 | stdio_uart_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 281 | } |
<> | 144:ef7eb2e8f9f7 | 282 | |
<> | 151:5eaa88a5bcc7 | 283 | if (! var->ref_cnt) { |
<> | 151:5eaa88a5bcc7 | 284 | // Mark this module to be deinited. |
<> | 151:5eaa88a5bcc7 | 285 | int i = modinit - uart_modinit_tab; |
<> | 151:5eaa88a5bcc7 | 286 | uart_modinit_mask &= ~(1 << i); |
<> | 151:5eaa88a5bcc7 | 287 | } |
<> | 144:ef7eb2e8f9f7 | 288 | } |
<> | 144:ef7eb2e8f9f7 | 289 | |
<> | 144:ef7eb2e8f9f7 | 290 | void serial_baud(serial_t *obj, int baudrate) { |
<> | 144:ef7eb2e8f9f7 | 291 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
<> | 161:2cc1468da177 | 292 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
<> | 144:ef7eb2e8f9f7 | 293 | |
<> | 144:ef7eb2e8f9f7 | 294 | obj->serial.baudrate = baudrate; |
<> | 144:ef7eb2e8f9f7 | 295 | UART_Open((UART_T *) NU_MODBASE(obj->serial.uart), baudrate); |
<> | 144:ef7eb2e8f9f7 | 296 | } |
<> | 144:ef7eb2e8f9f7 | 297 | |
<> | 144:ef7eb2e8f9f7 | 298 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { |
<> | 144:ef7eb2e8f9f7 | 299 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
<> | 161:2cc1468da177 | 300 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
<> | 144:ef7eb2e8f9f7 | 301 | |
AnnaBridge | 172:7d866c31b3c5 | 302 | // Sanity check arguments |
AnnaBridge | 172:7d866c31b3c5 | 303 | MBED_ASSERT((data_bits == 5) || (data_bits == 6) || (data_bits == 7) || (data_bits == 8)); |
AnnaBridge | 172:7d866c31b3c5 | 304 | MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || (parity == ParityForced1) || (parity == ParityForced0)); |
AnnaBridge | 172:7d866c31b3c5 | 305 | MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); |
AnnaBridge | 172:7d866c31b3c5 | 306 | |
<> | 144:ef7eb2e8f9f7 | 307 | obj->serial.databits = data_bits; |
<> | 144:ef7eb2e8f9f7 | 308 | obj->serial.parity = parity; |
<> | 144:ef7eb2e8f9f7 | 309 | obj->serial.stopbits = stop_bits; |
<> | 144:ef7eb2e8f9f7 | 310 | |
<> | 144:ef7eb2e8f9f7 | 311 | uint32_t databits_intern = (data_bits == 5) ? UART_WORD_LEN_5 : |
<> | 144:ef7eb2e8f9f7 | 312 | (data_bits == 6) ? UART_WORD_LEN_6 : |
<> | 144:ef7eb2e8f9f7 | 313 | (data_bits == 7) ? UART_WORD_LEN_7 : |
<> | 144:ef7eb2e8f9f7 | 314 | UART_WORD_LEN_8; |
<> | 144:ef7eb2e8f9f7 | 315 | uint32_t parity_intern = (parity == ParityOdd || parity == ParityForced1) ? UART_PARITY_ODD : |
<> | 144:ef7eb2e8f9f7 | 316 | (parity == ParityEven || parity == ParityForced0) ? UART_PARITY_EVEN : |
<> | 144:ef7eb2e8f9f7 | 317 | UART_PARITY_NONE; |
<> | 144:ef7eb2e8f9f7 | 318 | uint32_t stopbits_intern = (stop_bits == 2) ? UART_STOP_BIT_2 : UART_STOP_BIT_1; |
<> | 144:ef7eb2e8f9f7 | 319 | UART_SetLine_Config((UART_T *) NU_MODBASE(obj->serial.uart), |
<> | 144:ef7eb2e8f9f7 | 320 | 0, // Don't change baudrate |
<> | 144:ef7eb2e8f9f7 | 321 | databits_intern, |
<> | 144:ef7eb2e8f9f7 | 322 | parity_intern, |
<> | 144:ef7eb2e8f9f7 | 323 | stopbits_intern); |
<> | 144:ef7eb2e8f9f7 | 324 | } |
<> | 144:ef7eb2e8f9f7 | 325 | |
<> | 144:ef7eb2e8f9f7 | 326 | #if DEVICE_SERIAL_FC |
<> | 144:ef7eb2e8f9f7 | 327 | |
<> | 144:ef7eb2e8f9f7 | 328 | void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) |
<> | 144:ef7eb2e8f9f7 | 329 | { |
<> | 144:ef7eb2e8f9f7 | 330 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 331 | |
<> | 144:ef7eb2e8f9f7 | 332 | // First, disable flow control completely. |
<> | 144:ef7eb2e8f9f7 | 333 | uart_base->INTEN &= ~(UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 334 | |
<> | 144:ef7eb2e8f9f7 | 335 | if ((type == FlowControlRTS || type == FlowControlRTSCTS) && rxflow != NC) { |
<> | 144:ef7eb2e8f9f7 | 336 | // Check if RTS pin matches. |
<> | 144:ef7eb2e8f9f7 | 337 | uint32_t uart_rts = pinmap_peripheral(rxflow, PinMap_UART_RTS); |
<> | 144:ef7eb2e8f9f7 | 338 | MBED_ASSERT(uart_rts == obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 339 | // Enable the pin for RTS function |
<> | 144:ef7eb2e8f9f7 | 340 | pinmap_pinout(rxflow, PinMap_UART_RTS); |
<> | 153:fa9ff456f731 | 341 | // nRTS pin output is low level active |
<> | 153:fa9ff456f731 | 342 | uart_base->MODEM |= UART_MODEM_RTSACTLV_Msk; |
<> | 153:fa9ff456f731 | 343 | uart_base->MODEM &= ~UART_MODEM_RTS_Msk; |
<> | 153:fa9ff456f731 | 344 | |
<> | 144:ef7eb2e8f9f7 | 345 | uart_base->FIFO = (uart_base->FIFO & ~UART_FIFO_RTSTRGLV_Msk) | UART_FIFO_RTSTRGLV_8BYTES; |
<> | 144:ef7eb2e8f9f7 | 346 | // Enable RTS |
<> | 144:ef7eb2e8f9f7 | 347 | uart_base->INTEN |= UART_INTEN_ATORTSEN_Msk; |
<> | 144:ef7eb2e8f9f7 | 348 | } |
<> | 144:ef7eb2e8f9f7 | 349 | |
<> | 144:ef7eb2e8f9f7 | 350 | if ((type == FlowControlCTS || type == FlowControlRTSCTS) && txflow != NC) { |
<> | 144:ef7eb2e8f9f7 | 351 | // Check if CTS pin matches. |
<> | 144:ef7eb2e8f9f7 | 352 | uint32_t uart_cts = pinmap_peripheral(txflow, PinMap_UART_CTS); |
<> | 144:ef7eb2e8f9f7 | 353 | MBED_ASSERT(uart_cts == obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 354 | // Enable the pin for CTS function |
<> | 144:ef7eb2e8f9f7 | 355 | pinmap_pinout(txflow, PinMap_UART_CTS); |
<> | 153:fa9ff456f731 | 356 | // nCTS pin input is low level active |
<> | 153:fa9ff456f731 | 357 | uart_base->MODEMSTS |= UART_MODEMSTS_CTSACTLV_Msk; |
<> | 144:ef7eb2e8f9f7 | 358 | // Enable CTS |
<> | 144:ef7eb2e8f9f7 | 359 | uart_base->INTEN |= UART_INTEN_ATOCTSEN_Msk; |
<> | 144:ef7eb2e8f9f7 | 360 | } |
<> | 144:ef7eb2e8f9f7 | 361 | } |
<> | 144:ef7eb2e8f9f7 | 362 | |
<> | 144:ef7eb2e8f9f7 | 363 | #endif //DEVICE_SERIAL_FC |
<> | 144:ef7eb2e8f9f7 | 364 | |
<> | 144:ef7eb2e8f9f7 | 365 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
<> | 144:ef7eb2e8f9f7 | 366 | { |
<> | 144:ef7eb2e8f9f7 | 367 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
<> | 161:2cc1468da177 | 368 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
<> | 144:ef7eb2e8f9f7 | 369 | |
<> | 144:ef7eb2e8f9f7 | 370 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 371 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 372 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 373 | |
<> | 144:ef7eb2e8f9f7 | 374 | obj->serial.irq_handler = (uint32_t) handler; |
<> | 144:ef7eb2e8f9f7 | 375 | obj->serial.irq_id = id; |
<> | 144:ef7eb2e8f9f7 | 376 | |
<> | 144:ef7eb2e8f9f7 | 377 | // Restore sync-mode vector |
<> | 144:ef7eb2e8f9f7 | 378 | obj->serial.vec = ((struct nu_uart_var *) modinit->var)->vec; |
<> | 144:ef7eb2e8f9f7 | 379 | } |
<> | 144:ef7eb2e8f9f7 | 380 | |
<> | 144:ef7eb2e8f9f7 | 381 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
<> | 144:ef7eb2e8f9f7 | 382 | { |
AnnaBridge | 165:e614a9f1c9e2 | 383 | obj->serial.irq_en = enable; |
AnnaBridge | 165:e614a9f1c9e2 | 384 | serial_enable_interrupt(obj, irq, enable); |
<> | 144:ef7eb2e8f9f7 | 385 | } |
<> | 144:ef7eb2e8f9f7 | 386 | |
<> | 144:ef7eb2e8f9f7 | 387 | int serial_getc(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 388 | { |
AnnaBridge | 165:e614a9f1c9e2 | 389 | // NOTE: Every byte access requires accompaniment of one interrupt. This has side effect of performance degradation. |
<> | 144:ef7eb2e8f9f7 | 390 | while (! serial_readable(obj)); |
<> | 144:ef7eb2e8f9f7 | 391 | int c = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 392 | |
AnnaBridge | 165:e614a9f1c9e2 | 393 | // NOTE: On Nuvoton targets, no H/W IRQ to match TxIrq/RxIrq. |
AnnaBridge | 165:e614a9f1c9e2 | 394 | // Simulation of TxIrq/RxIrq requires the call to Serial::putc()/Serial::getc() respectively. |
<> | 144:ef7eb2e8f9f7 | 395 | if (obj->serial.inten_msk & (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)) { |
<> | 144:ef7eb2e8f9f7 | 396 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); |
<> | 144:ef7eb2e8f9f7 | 397 | } |
<> | 144:ef7eb2e8f9f7 | 398 | |
<> | 144:ef7eb2e8f9f7 | 399 | return c; |
<> | 144:ef7eb2e8f9f7 | 400 | } |
<> | 144:ef7eb2e8f9f7 | 401 | |
<> | 144:ef7eb2e8f9f7 | 402 | void serial_putc(serial_t *obj, int c) |
<> | 144:ef7eb2e8f9f7 | 403 | { |
AnnaBridge | 165:e614a9f1c9e2 | 404 | // NOTE: Every byte access requires accompaniment of one interrupt. This has side effect of performance degradation. |
<> | 144:ef7eb2e8f9f7 | 405 | while (! serial_writable(obj)); |
<> | 144:ef7eb2e8f9f7 | 406 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), c); |
<> | 144:ef7eb2e8f9f7 | 407 | |
AnnaBridge | 165:e614a9f1c9e2 | 408 | // NOTE: On Nuvoton targets, no H/W IRQ to match TxIrq/RxIrq. |
AnnaBridge | 165:e614a9f1c9e2 | 409 | // Simulation of TxIrq/RxIrq requires the call to Serial::putc()/Serial::getc() respectively. |
<> | 144:ef7eb2e8f9f7 | 410 | if (obj->serial.inten_msk & UART_INTEN_THREIEN_Msk) { |
<> | 144:ef7eb2e8f9f7 | 411 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_THREIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 412 | } |
<> | 144:ef7eb2e8f9f7 | 413 | } |
<> | 144:ef7eb2e8f9f7 | 414 | |
<> | 144:ef7eb2e8f9f7 | 415 | int serial_readable(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 416 | { |
<> | 144:ef7eb2e8f9f7 | 417 | //return UART_IS_RX_READY(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 418 | return ! (((UART_T *) NU_MODBASE(obj->serial.uart))->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk); |
<> | 144:ef7eb2e8f9f7 | 419 | } |
<> | 144:ef7eb2e8f9f7 | 420 | |
<> | 144:ef7eb2e8f9f7 | 421 | int serial_writable(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 422 | { |
<> | 144:ef7eb2e8f9f7 | 423 | return ! UART_IS_TX_FULL(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 424 | } |
<> | 144:ef7eb2e8f9f7 | 425 | |
<> | 144:ef7eb2e8f9f7 | 426 | void serial_pinout_tx(PinName tx) |
<> | 144:ef7eb2e8f9f7 | 427 | { |
<> | 144:ef7eb2e8f9f7 | 428 | pinmap_pinout(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 429 | } |
<> | 144:ef7eb2e8f9f7 | 430 | |
<> | 144:ef7eb2e8f9f7 | 431 | void serial_break_set(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 432 | { |
<> | 144:ef7eb2e8f9f7 | 433 | ((UART_T *) NU_MODBASE(obj->serial.uart))->LINE |= UART_LINE_BCB_Msk; |
<> | 144:ef7eb2e8f9f7 | 434 | } |
<> | 144:ef7eb2e8f9f7 | 435 | |
<> | 144:ef7eb2e8f9f7 | 436 | void serial_break_clear(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 437 | { |
<> | 144:ef7eb2e8f9f7 | 438 | ((UART_T *) NU_MODBASE(obj->serial.uart))->LINE &= ~UART_LINE_BCB_Msk; |
<> | 144:ef7eb2e8f9f7 | 439 | } |
<> | 144:ef7eb2e8f9f7 | 440 | |
<> | 144:ef7eb2e8f9f7 | 441 | static void uart0_vec(void) |
<> | 144:ef7eb2e8f9f7 | 442 | { |
<> | 144:ef7eb2e8f9f7 | 443 | uart_irq(uart0_var.obj); |
<> | 144:ef7eb2e8f9f7 | 444 | } |
<> | 144:ef7eb2e8f9f7 | 445 | |
<> | 144:ef7eb2e8f9f7 | 446 | static void uart1_vec(void) |
<> | 144:ef7eb2e8f9f7 | 447 | { |
<> | 144:ef7eb2e8f9f7 | 448 | uart_irq(uart1_var.obj); |
<> | 144:ef7eb2e8f9f7 | 449 | } |
<> | 144:ef7eb2e8f9f7 | 450 | |
<> | 144:ef7eb2e8f9f7 | 451 | static void uart2_vec(void) |
<> | 144:ef7eb2e8f9f7 | 452 | { |
<> | 144:ef7eb2e8f9f7 | 453 | uart_irq(uart2_var.obj); |
<> | 144:ef7eb2e8f9f7 | 454 | } |
<> | 144:ef7eb2e8f9f7 | 455 | |
<> | 144:ef7eb2e8f9f7 | 456 | static void uart3_vec(void) |
<> | 144:ef7eb2e8f9f7 | 457 | { |
<> | 144:ef7eb2e8f9f7 | 458 | uart_irq(uart3_var.obj); |
<> | 144:ef7eb2e8f9f7 | 459 | } |
<> | 144:ef7eb2e8f9f7 | 460 | |
<> | 144:ef7eb2e8f9f7 | 461 | static void uart4_vec(void) |
<> | 144:ef7eb2e8f9f7 | 462 | { |
<> | 144:ef7eb2e8f9f7 | 463 | uart_irq(uart4_var.obj); |
<> | 144:ef7eb2e8f9f7 | 464 | } |
<> | 144:ef7eb2e8f9f7 | 465 | |
<> | 144:ef7eb2e8f9f7 | 466 | static void uart5_vec(void) |
<> | 144:ef7eb2e8f9f7 | 467 | { |
<> | 144:ef7eb2e8f9f7 | 468 | uart_irq(uart5_var.obj); |
<> | 144:ef7eb2e8f9f7 | 469 | } |
<> | 144:ef7eb2e8f9f7 | 470 | |
<> | 144:ef7eb2e8f9f7 | 471 | static void uart_irq(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 472 | { |
<> | 144:ef7eb2e8f9f7 | 473 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 474 | |
<> | 144:ef7eb2e8f9f7 | 475 | if (uart_base->INTSTS & (UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk)) { |
<> | 144:ef7eb2e8f9f7 | 476 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next read. |
<> | 144:ef7eb2e8f9f7 | 477 | UART_DISABLE_INT(uart_base, (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); |
<> | 144:ef7eb2e8f9f7 | 478 | if (obj->serial.irq_handler) { |
<> | 144:ef7eb2e8f9f7 | 479 | ((uart_irq_handler) obj->serial.irq_handler)(obj->serial.irq_id, RxIrq); |
<> | 144:ef7eb2e8f9f7 | 480 | } |
<> | 144:ef7eb2e8f9f7 | 481 | } |
<> | 144:ef7eb2e8f9f7 | 482 | |
<> | 144:ef7eb2e8f9f7 | 483 | if (uart_base->INTSTS & UART_INTSTS_THREINT_Msk) { |
<> | 144:ef7eb2e8f9f7 | 484 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next write. |
<> | 144:ef7eb2e8f9f7 | 485 | UART_DISABLE_INT(uart_base, UART_INTEN_THREIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 486 | if (obj->serial.irq_handler) { |
<> | 144:ef7eb2e8f9f7 | 487 | ((uart_irq_handler) obj->serial.irq_handler)(obj->serial.irq_id, TxIrq); |
<> | 144:ef7eb2e8f9f7 | 488 | } |
<> | 144:ef7eb2e8f9f7 | 489 | } |
<> | 144:ef7eb2e8f9f7 | 490 | |
<> | 144:ef7eb2e8f9f7 | 491 | // FIXME: Ignore all other interrupt flags. Clear them. Otherwise, program will get stuck in interrupt. |
<> | 144:ef7eb2e8f9f7 | 492 | uart_base->INTSTS = uart_base->INTSTS; |
<> | 144:ef7eb2e8f9f7 | 493 | uart_base->FIFOSTS = uart_base->FIFOSTS; |
<> | 144:ef7eb2e8f9f7 | 494 | } |
<> | 144:ef7eb2e8f9f7 | 495 | |
<> | 144:ef7eb2e8f9f7 | 496 | |
<> | 144:ef7eb2e8f9f7 | 497 | #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 498 | 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) |
<> | 144:ef7eb2e8f9f7 | 499 | { |
<> | 144:ef7eb2e8f9f7 | 500 | MBED_ASSERT(tx_width == 8 || tx_width == 16 || tx_width == 32); |
<> | 144:ef7eb2e8f9f7 | 501 | |
<> | 144:ef7eb2e8f9f7 | 502 | obj->serial.dma_usage_tx = hint; |
<> | 144:ef7eb2e8f9f7 | 503 | serial_check_dma_usage(&obj->serial.dma_usage_tx, &obj->serial.dma_chn_id_tx); |
<> | 144:ef7eb2e8f9f7 | 504 | |
<> | 144:ef7eb2e8f9f7 | 505 | // UART IRQ is necessary for both interrupt way and DMA way |
<> | 144:ef7eb2e8f9f7 | 506 | serial_tx_enable_event(obj, event, 1); |
<> | 144:ef7eb2e8f9f7 | 507 | serial_tx_buffer_set(obj, tx, tx_length, tx_width); |
<> | 144:ef7eb2e8f9f7 | 508 | //UART_HAL_DisableTransmitter(obj->serial.address); |
<> | 144:ef7eb2e8f9f7 | 509 | //UART_HAL_FlushTxFifo(obj->serial.address); |
<> | 144:ef7eb2e8f9f7 | 510 | //UART_HAL_EnableTransmitter(obj->serial.address); |
<> | 144:ef7eb2e8f9f7 | 511 | |
<> | 144:ef7eb2e8f9f7 | 512 | int n_word = 0; |
<> | 144:ef7eb2e8f9f7 | 513 | if (obj->serial.dma_usage_tx == DMA_USAGE_NEVER) { |
<> | 144:ef7eb2e8f9f7 | 514 | // Interrupt way |
<> | 144:ef7eb2e8f9f7 | 515 | n_word = serial_write_async(obj); |
<> | 144:ef7eb2e8f9f7 | 516 | serial_tx_enable_interrupt(obj, handler, 1); |
<> | 144:ef7eb2e8f9f7 | 517 | } else { |
<> | 144:ef7eb2e8f9f7 | 518 | // DMA way |
<> | 144:ef7eb2e8f9f7 | 519 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 520 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 521 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 522 | |
<> | 161:2cc1468da177 | 523 | PDMA_T *pdma_base = dma_modbase(); |
<> | 161:2cc1468da177 | 524 | |
<> | 161:2cc1468da177 | 525 | pdma_base->CHCTL |= 1 << obj->serial.dma_chn_id_tx; // Enable this DMA channel |
<> | 144:ef7eb2e8f9f7 | 526 | PDMA_SetTransferMode(obj->serial.dma_chn_id_tx, |
<> | 144:ef7eb2e8f9f7 | 527 | ((struct nu_uart_var *) modinit->var)->pdma_perp_tx, // Peripheral connected to this PDMA |
<> | 144:ef7eb2e8f9f7 | 528 | 0, // Scatter-gather disabled |
<> | 144:ef7eb2e8f9f7 | 529 | 0); // Scatter-gather descriptor address |
<> | 144:ef7eb2e8f9f7 | 530 | PDMA_SetTransferCnt(obj->serial.dma_chn_id_tx, |
<> | 144:ef7eb2e8f9f7 | 531 | (tx_width == 8) ? PDMA_WIDTH_8 : (tx_width == 16) ? PDMA_WIDTH_16 : PDMA_WIDTH_32, |
<> | 144:ef7eb2e8f9f7 | 532 | tx_length); |
<> | 144:ef7eb2e8f9f7 | 533 | PDMA_SetTransferAddr(obj->serial.dma_chn_id_tx, |
<> | 144:ef7eb2e8f9f7 | 534 | ((uint32_t) tx) + (tx_width / 8) * tx_length, // NOTE: End of source address |
<> | 144:ef7eb2e8f9f7 | 535 | PDMA_SAR_INC, // Source address incremental |
<> | 161:2cc1468da177 | 536 | (uint32_t) NU_MODBASE(obj->serial.uart), // Destination address |
<> | 144:ef7eb2e8f9f7 | 537 | PDMA_DAR_FIX); // Destination address fixed |
<> | 144:ef7eb2e8f9f7 | 538 | PDMA_SetBurstType(obj->serial.dma_chn_id_tx, |
<> | 144:ef7eb2e8f9f7 | 539 | PDMA_REQ_SINGLE, // Single mode |
<> | 144:ef7eb2e8f9f7 | 540 | 0); // Burst size |
<> | 144:ef7eb2e8f9f7 | 541 | PDMA_EnableInt(obj->serial.dma_chn_id_tx, |
<> | 144:ef7eb2e8f9f7 | 542 | 0); // Interrupt type. No use here |
<> | 144:ef7eb2e8f9f7 | 543 | // Register DMA event handler |
<> | 144:ef7eb2e8f9f7 | 544 | dma_set_handler(obj->serial.dma_chn_id_tx, (uint32_t) uart_dma_handler_tx, (uint32_t) obj, DMA_EVENT_ALL); |
<> | 144:ef7eb2e8f9f7 | 545 | serial_tx_enable_interrupt(obj, handler, 1); |
<> | 144:ef7eb2e8f9f7 | 546 | ((UART_T *) NU_MODBASE(obj->serial.uart))->INTEN |= UART_INTEN_TXPDMAEN_Msk; // Start DMA transfer |
<> | 144:ef7eb2e8f9f7 | 547 | } |
<> | 144:ef7eb2e8f9f7 | 548 | |
<> | 144:ef7eb2e8f9f7 | 549 | return n_word; |
<> | 144:ef7eb2e8f9f7 | 550 | } |
<> | 144:ef7eb2e8f9f7 | 551 | |
<> | 144:ef7eb2e8f9f7 | 552 | 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) |
<> | 144:ef7eb2e8f9f7 | 553 | { |
<> | 144:ef7eb2e8f9f7 | 554 | MBED_ASSERT(rx_width == 8 || rx_width == 16 || rx_width == 32); |
<> | 144:ef7eb2e8f9f7 | 555 | |
<> | 144:ef7eb2e8f9f7 | 556 | obj->serial.dma_usage_rx = hint; |
<> | 144:ef7eb2e8f9f7 | 557 | serial_check_dma_usage(&obj->serial.dma_usage_rx, &obj->serial.dma_chn_id_rx); |
<> | 144:ef7eb2e8f9f7 | 558 | // DMA doesn't support char match, so fall back to IRQ if it is requested. |
<> | 144:ef7eb2e8f9f7 | 559 | if (obj->serial.dma_usage_rx != DMA_USAGE_NEVER && |
<> | 144:ef7eb2e8f9f7 | 560 | (event & SERIAL_EVENT_RX_CHARACTER_MATCH) && |
<> | 144:ef7eb2e8f9f7 | 561 | char_match != SERIAL_RESERVED_CHAR_MATCH) { |
<> | 144:ef7eb2e8f9f7 | 562 | obj->serial.dma_usage_rx = DMA_USAGE_NEVER; |
<> | 144:ef7eb2e8f9f7 | 563 | dma_channel_free(obj->serial.dma_chn_id_rx); |
<> | 144:ef7eb2e8f9f7 | 564 | obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS; |
<> | 144:ef7eb2e8f9f7 | 565 | } |
<> | 144:ef7eb2e8f9f7 | 566 | |
<> | 144:ef7eb2e8f9f7 | 567 | // UART IRQ is necessary for both interrupt way and DMA way |
<> | 144:ef7eb2e8f9f7 | 568 | serial_rx_enable_event(obj, event, 1); |
<> | 144:ef7eb2e8f9f7 | 569 | serial_rx_buffer_set(obj, rx, rx_length, rx_width); |
<> | 144:ef7eb2e8f9f7 | 570 | serial_rx_set_char_match(obj, char_match); |
<> | 144:ef7eb2e8f9f7 | 571 | //UART_HAL_DisableReceiver(obj->serial.address); |
<> | 144:ef7eb2e8f9f7 | 572 | //UART_HAL_FlushRxFifo(obj->serial.address); |
<> | 144:ef7eb2e8f9f7 | 573 | //UART_HAL_EnableReceiver(obj->serial.address); |
<> | 144:ef7eb2e8f9f7 | 574 | |
<> | 144:ef7eb2e8f9f7 | 575 | if (obj->serial.dma_usage_rx == DMA_USAGE_NEVER) { |
<> | 144:ef7eb2e8f9f7 | 576 | // Interrupt way |
<> | 144:ef7eb2e8f9f7 | 577 | serial_rx_enable_interrupt(obj, handler, 1); |
<> | 144:ef7eb2e8f9f7 | 578 | } else { |
<> | 144:ef7eb2e8f9f7 | 579 | // DMA way |
<> | 144:ef7eb2e8f9f7 | 580 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 581 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 582 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 583 | |
<> | 161:2cc1468da177 | 584 | PDMA_T *pdma_base = dma_modbase(); |
<> | 161:2cc1468da177 | 585 | |
<> | 161:2cc1468da177 | 586 | pdma_base->CHCTL |= 1 << obj->serial.dma_chn_id_rx; // Enable this DMA channel |
<> | 144:ef7eb2e8f9f7 | 587 | PDMA_SetTransferMode(obj->serial.dma_chn_id_rx, |
<> | 144:ef7eb2e8f9f7 | 588 | ((struct nu_uart_var *) modinit->var)->pdma_perp_rx, // Peripheral connected to this PDMA |
<> | 144:ef7eb2e8f9f7 | 589 | 0, // Scatter-gather disabled |
<> | 144:ef7eb2e8f9f7 | 590 | 0); // Scatter-gather descriptor address |
<> | 144:ef7eb2e8f9f7 | 591 | PDMA_SetTransferCnt(obj->serial.dma_chn_id_rx, |
<> | 144:ef7eb2e8f9f7 | 592 | (rx_width == 8) ? PDMA_WIDTH_8 : (rx_width == 16) ? PDMA_WIDTH_16 : PDMA_WIDTH_32, |
<> | 144:ef7eb2e8f9f7 | 593 | rx_length); |
<> | 144:ef7eb2e8f9f7 | 594 | PDMA_SetTransferAddr(obj->serial.dma_chn_id_rx, |
<> | 161:2cc1468da177 | 595 | (uint32_t) NU_MODBASE(obj->serial.uart), // Source address |
<> | 144:ef7eb2e8f9f7 | 596 | PDMA_SAR_FIX, // Source address fixed |
<> | 144:ef7eb2e8f9f7 | 597 | ((uint32_t) rx) + (rx_width / 8) * rx_length, // NOTE: End of destination address |
<> | 144:ef7eb2e8f9f7 | 598 | PDMA_DAR_INC); // Destination address incremental |
<> | 144:ef7eb2e8f9f7 | 599 | PDMA_SetBurstType(obj->serial.dma_chn_id_rx, |
<> | 144:ef7eb2e8f9f7 | 600 | PDMA_REQ_SINGLE, // Single mode |
<> | 144:ef7eb2e8f9f7 | 601 | 0); // Burst size |
<> | 144:ef7eb2e8f9f7 | 602 | PDMA_EnableInt(obj->serial.dma_chn_id_rx, |
<> | 144:ef7eb2e8f9f7 | 603 | 0); // Interrupt type. No use here |
<> | 144:ef7eb2e8f9f7 | 604 | // Register DMA event handler |
<> | 144:ef7eb2e8f9f7 | 605 | dma_set_handler(obj->serial.dma_chn_id_rx, (uint32_t) uart_dma_handler_rx, (uint32_t) obj, DMA_EVENT_ALL); |
<> | 144:ef7eb2e8f9f7 | 606 | serial_rx_enable_interrupt(obj, handler, 1); |
<> | 144:ef7eb2e8f9f7 | 607 | ((UART_T *) NU_MODBASE(obj->serial.uart))->INTEN |= UART_INTEN_RXPDMAEN_Msk; // Start DMA transfer |
<> | 144:ef7eb2e8f9f7 | 608 | } |
<> | 144:ef7eb2e8f9f7 | 609 | } |
<> | 144:ef7eb2e8f9f7 | 610 | |
<> | 144:ef7eb2e8f9f7 | 611 | void serial_tx_abort_asynch(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 612 | { |
<> | 144:ef7eb2e8f9f7 | 613 | // Flush Tx FIFO. Otherwise, output data may get lost on this change. |
<> | 161:2cc1468da177 | 614 | while (! UART_IS_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart)))); |
<> | 144:ef7eb2e8f9f7 | 615 | |
<> | 144:ef7eb2e8f9f7 | 616 | if (obj->serial.dma_usage_tx != DMA_USAGE_NEVER) { |
<> | 161:2cc1468da177 | 617 | PDMA_T *pdma_base = dma_modbase(); |
<> | 161:2cc1468da177 | 618 | |
<> | 144:ef7eb2e8f9f7 | 619 | if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 144:ef7eb2e8f9f7 | 620 | PDMA_DisableInt(obj->serial.dma_chn_id_tx, 0); |
<> | 144:ef7eb2e8f9f7 | 621 | // FIXME: Next PDMA transfer will fail with PDMA_STOP() called. Cause is unknown. |
<> | 144:ef7eb2e8f9f7 | 622 | //PDMA_STOP(obj->serial.dma_chn_id_tx); |
<> | 161:2cc1468da177 | 623 | pdma_base->CHCTL &= ~(1 << obj->serial.dma_chn_id_tx); |
<> | 144:ef7eb2e8f9f7 | 624 | } |
<> | 144:ef7eb2e8f9f7 | 625 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_TXPDMAEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 626 | } |
<> | 144:ef7eb2e8f9f7 | 627 | |
<> | 144:ef7eb2e8f9f7 | 628 | // Necessary for both interrupt way and DMA way |
AnnaBridge | 165:e614a9f1c9e2 | 629 | serial_enable_interrupt(obj, TxIrq, 0); |
AnnaBridge | 165:e614a9f1c9e2 | 630 | serial_rollback_interrupt(obj, TxIrq); |
<> | 144:ef7eb2e8f9f7 | 631 | } |
<> | 144:ef7eb2e8f9f7 | 632 | |
<> | 144:ef7eb2e8f9f7 | 633 | void serial_rx_abort_asynch(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 634 | { |
<> | 144:ef7eb2e8f9f7 | 635 | if (obj->serial.dma_usage_rx != DMA_USAGE_NEVER) { |
<> | 161:2cc1468da177 | 636 | PDMA_T *pdma_base = dma_modbase(); |
<> | 161:2cc1468da177 | 637 | |
<> | 144:ef7eb2e8f9f7 | 638 | if (obj->serial.dma_chn_id_rx != DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 144:ef7eb2e8f9f7 | 639 | PDMA_DisableInt(obj->serial.dma_chn_id_rx, 0); |
<> | 144:ef7eb2e8f9f7 | 640 | // FIXME: Next PDMA transfer will fail with PDMA_STOP() called. Cause is unknown. |
<> | 144:ef7eb2e8f9f7 | 641 | //PDMA_STOP(obj->serial.dma_chn_id_rx); |
<> | 161:2cc1468da177 | 642 | pdma_base->CHCTL &= ~(1 << obj->serial.dma_chn_id_rx); |
<> | 144:ef7eb2e8f9f7 | 643 | } |
<> | 144:ef7eb2e8f9f7 | 644 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_RXPDMAEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 645 | } |
<> | 144:ef7eb2e8f9f7 | 646 | |
<> | 144:ef7eb2e8f9f7 | 647 | // Necessary for both interrupt way and DMA way |
AnnaBridge | 165:e614a9f1c9e2 | 648 | serial_enable_interrupt(obj, RxIrq, 0); |
AnnaBridge | 165:e614a9f1c9e2 | 649 | serial_rollback_interrupt(obj, RxIrq); |
<> | 144:ef7eb2e8f9f7 | 650 | } |
<> | 144:ef7eb2e8f9f7 | 651 | |
<> | 144:ef7eb2e8f9f7 | 652 | uint8_t serial_tx_active(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 653 | { |
AnnaBridge | 165:e614a9f1c9e2 | 654 | // NOTE: Judge by serial_is_irq_en(obj, TxIrq) doesn't work with sync/async modes interleaved. Change with TX FIFO empty flag. |
AnnaBridge | 165:e614a9f1c9e2 | 655 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 165:e614a9f1c9e2 | 656 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 657 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 165:e614a9f1c9e2 | 658 | |
AnnaBridge | 165:e614a9f1c9e2 | 659 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
AnnaBridge | 165:e614a9f1c9e2 | 660 | return (obj->serial.vec == var->vec_async); |
<> | 144:ef7eb2e8f9f7 | 661 | } |
<> | 144:ef7eb2e8f9f7 | 662 | |
<> | 144:ef7eb2e8f9f7 | 663 | uint8_t serial_rx_active(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 664 | { |
AnnaBridge | 165:e614a9f1c9e2 | 665 | // NOTE: Judge by serial_is_irq_en(obj, RxIrq) doesn't work with sync/async modes interleaved. Change with RX FIFO empty flag. |
AnnaBridge | 165:e614a9f1c9e2 | 666 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 165:e614a9f1c9e2 | 667 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 668 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 165:e614a9f1c9e2 | 669 | |
AnnaBridge | 165:e614a9f1c9e2 | 670 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
AnnaBridge | 165:e614a9f1c9e2 | 671 | return (obj->serial.vec == var->vec_async); |
<> | 144:ef7eb2e8f9f7 | 672 | } |
<> | 144:ef7eb2e8f9f7 | 673 | |
<> | 144:ef7eb2e8f9f7 | 674 | int serial_irq_handler_asynch(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 675 | { |
<> | 144:ef7eb2e8f9f7 | 676 | int event_rx = 0; |
<> | 144:ef7eb2e8f9f7 | 677 | int event_tx = 0; |
<> | 144:ef7eb2e8f9f7 | 678 | |
<> | 151:5eaa88a5bcc7 | 679 | // Necessary for both interrupt way and DMA way |
<> | 144:ef7eb2e8f9f7 | 680 | if (serial_is_irq_en(obj, RxIrq)) { |
<> | 144:ef7eb2e8f9f7 | 681 | event_rx = serial_rx_event_check(obj); |
<> | 144:ef7eb2e8f9f7 | 682 | if (event_rx) { |
<> | 144:ef7eb2e8f9f7 | 683 | serial_rx_abort_asynch(obj); |
<> | 144:ef7eb2e8f9f7 | 684 | } |
<> | 144:ef7eb2e8f9f7 | 685 | } |
<> | 144:ef7eb2e8f9f7 | 686 | |
<> | 144:ef7eb2e8f9f7 | 687 | if (serial_is_irq_en(obj, TxIrq)) { |
<> | 144:ef7eb2e8f9f7 | 688 | event_tx = serial_tx_event_check(obj); |
<> | 144:ef7eb2e8f9f7 | 689 | if (event_tx) { |
<> | 144:ef7eb2e8f9f7 | 690 | serial_tx_abort_asynch(obj); |
<> | 144:ef7eb2e8f9f7 | 691 | } |
<> | 144:ef7eb2e8f9f7 | 692 | } |
<> | 144:ef7eb2e8f9f7 | 693 | |
<> | 144:ef7eb2e8f9f7 | 694 | return (obj->serial.event & (event_rx | event_tx)); |
<> | 144:ef7eb2e8f9f7 | 695 | } |
<> | 144:ef7eb2e8f9f7 | 696 | |
<> | 144:ef7eb2e8f9f7 | 697 | static void uart0_vec_async(void) |
<> | 144:ef7eb2e8f9f7 | 698 | { |
<> | 144:ef7eb2e8f9f7 | 699 | uart_irq_async(uart0_var.obj); |
<> | 144:ef7eb2e8f9f7 | 700 | } |
<> | 144:ef7eb2e8f9f7 | 701 | |
<> | 144:ef7eb2e8f9f7 | 702 | static void uart1_vec_async(void) |
<> | 144:ef7eb2e8f9f7 | 703 | { |
<> | 144:ef7eb2e8f9f7 | 704 | uart_irq_async(uart1_var.obj); |
<> | 144:ef7eb2e8f9f7 | 705 | } |
<> | 144:ef7eb2e8f9f7 | 706 | |
<> | 144:ef7eb2e8f9f7 | 707 | static void uart2_vec_async(void) |
<> | 144:ef7eb2e8f9f7 | 708 | { |
<> | 144:ef7eb2e8f9f7 | 709 | uart_irq_async(uart2_var.obj); |
<> | 144:ef7eb2e8f9f7 | 710 | } |
<> | 144:ef7eb2e8f9f7 | 711 | |
<> | 144:ef7eb2e8f9f7 | 712 | static void uart3_vec_async(void) |
<> | 144:ef7eb2e8f9f7 | 713 | { |
<> | 144:ef7eb2e8f9f7 | 714 | uart_irq_async(uart3_var.obj); |
<> | 144:ef7eb2e8f9f7 | 715 | } |
<> | 144:ef7eb2e8f9f7 | 716 | |
<> | 144:ef7eb2e8f9f7 | 717 | static void uart4_vec_async(void) |
<> | 144:ef7eb2e8f9f7 | 718 | { |
<> | 144:ef7eb2e8f9f7 | 719 | uart_irq_async(uart4_var.obj); |
<> | 144:ef7eb2e8f9f7 | 720 | } |
<> | 144:ef7eb2e8f9f7 | 721 | |
<> | 144:ef7eb2e8f9f7 | 722 | static void uart5_vec_async(void) |
<> | 144:ef7eb2e8f9f7 | 723 | { |
<> | 144:ef7eb2e8f9f7 | 724 | uart_irq_async(uart5_var.obj); |
<> | 144:ef7eb2e8f9f7 | 725 | } |
<> | 144:ef7eb2e8f9f7 | 726 | |
<> | 144:ef7eb2e8f9f7 | 727 | static void uart_irq_async(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 728 | { |
<> | 144:ef7eb2e8f9f7 | 729 | if (serial_is_irq_en(obj, RxIrq)) { |
<> | 144:ef7eb2e8f9f7 | 730 | (*obj->serial.irq_handler_rx_async)(); |
<> | 144:ef7eb2e8f9f7 | 731 | } |
<> | 144:ef7eb2e8f9f7 | 732 | if (serial_is_irq_en(obj, TxIrq)) { |
<> | 144:ef7eb2e8f9f7 | 733 | (*obj->serial.irq_handler_tx_async)(); |
<> | 144:ef7eb2e8f9f7 | 734 | } |
<> | 144:ef7eb2e8f9f7 | 735 | } |
<> | 144:ef7eb2e8f9f7 | 736 | |
<> | 144:ef7eb2e8f9f7 | 737 | static void serial_rx_set_char_match(serial_t *obj, uint8_t char_match) |
<> | 144:ef7eb2e8f9f7 | 738 | { |
<> | 144:ef7eb2e8f9f7 | 739 | obj->char_match = char_match; |
<> | 144:ef7eb2e8f9f7 | 740 | obj->char_found = 0; |
<> | 144:ef7eb2e8f9f7 | 741 | } |
<> | 144:ef7eb2e8f9f7 | 742 | |
<> | 144:ef7eb2e8f9f7 | 743 | static void serial_tx_enable_event(serial_t *obj, int event, uint8_t enable) |
<> | 144:ef7eb2e8f9f7 | 744 | { |
<> | 144:ef7eb2e8f9f7 | 745 | obj->serial.event &= ~SERIAL_EVENT_TX_MASK; |
<> | 144:ef7eb2e8f9f7 | 746 | obj->serial.event |= (event & SERIAL_EVENT_TX_MASK); |
<> | 144:ef7eb2e8f9f7 | 747 | |
<> | 144:ef7eb2e8f9f7 | 748 | //if (event & SERIAL_EVENT_TX_COMPLETE) { |
<> | 144:ef7eb2e8f9f7 | 749 | //} |
<> | 144:ef7eb2e8f9f7 | 750 | } |
<> | 144:ef7eb2e8f9f7 | 751 | |
<> | 144:ef7eb2e8f9f7 | 752 | static void serial_rx_enable_event(serial_t *obj, int event, uint8_t enable) |
<> | 144:ef7eb2e8f9f7 | 753 | { |
<> | 144:ef7eb2e8f9f7 | 754 | obj->serial.event &= ~SERIAL_EVENT_RX_MASK; |
<> | 144:ef7eb2e8f9f7 | 755 | obj->serial.event |= (event & SERIAL_EVENT_RX_MASK); |
<> | 144:ef7eb2e8f9f7 | 756 | |
<> | 144:ef7eb2e8f9f7 | 757 | //if (event & SERIAL_EVENT_RX_COMPLETE) { |
<> | 144:ef7eb2e8f9f7 | 758 | //} |
<> | 144:ef7eb2e8f9f7 | 759 | //if (event & SERIAL_EVENT_RX_OVERRUN_ERROR) { |
<> | 144:ef7eb2e8f9f7 | 760 | //} |
<> | 144:ef7eb2e8f9f7 | 761 | if (event & SERIAL_EVENT_RX_FRAMING_ERROR) { |
<> | 144:ef7eb2e8f9f7 | 762 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_RLSIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 763 | } |
<> | 144:ef7eb2e8f9f7 | 764 | if (event & SERIAL_EVENT_RX_PARITY_ERROR) { |
<> | 144:ef7eb2e8f9f7 | 765 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_RLSIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 766 | } |
<> | 144:ef7eb2e8f9f7 | 767 | if (event & SERIAL_EVENT_RX_OVERFLOW) { |
<> | 144:ef7eb2e8f9f7 | 768 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_BUFERRIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 769 | } |
<> | 144:ef7eb2e8f9f7 | 770 | //if (event & SERIAL_EVENT_RX_CHARACTER_MATCH) { |
<> | 144:ef7eb2e8f9f7 | 771 | //} |
<> | 144:ef7eb2e8f9f7 | 772 | } |
<> | 144:ef7eb2e8f9f7 | 773 | |
<> | 144:ef7eb2e8f9f7 | 774 | static int serial_is_tx_complete(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 775 | { |
<> | 144:ef7eb2e8f9f7 | 776 | // NOTE: Exclude tx fifo empty check due to no such interrupt on DMA way |
<> | 144:ef7eb2e8f9f7 | 777 | //return (obj->tx_buff.pos == obj->tx_buff.length) && UART_GET_TX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 778 | // FIXME: Premature abort??? |
<> | 144:ef7eb2e8f9f7 | 779 | return (obj->tx_buff.pos == obj->tx_buff.length); |
<> | 144:ef7eb2e8f9f7 | 780 | } |
<> | 144:ef7eb2e8f9f7 | 781 | |
<> | 144:ef7eb2e8f9f7 | 782 | static int serial_is_rx_complete(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 783 | { |
<> | 144:ef7eb2e8f9f7 | 784 | //return (obj->rx_buff.pos == obj->rx_buff.length) && UART_GET_RX_EMPTY(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 785 | return (obj->rx_buff.pos == obj->rx_buff.length); |
<> | 144:ef7eb2e8f9f7 | 786 | } |
<> | 144:ef7eb2e8f9f7 | 787 | |
<> | 144:ef7eb2e8f9f7 | 788 | static uint32_t serial_tx_event_check(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 789 | { |
<> | 144:ef7eb2e8f9f7 | 790 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 791 | |
<> | 144:ef7eb2e8f9f7 | 792 | if (uart_base->INTSTS & UART_INTSTS_THREINT_Msk) { |
<> | 144:ef7eb2e8f9f7 | 793 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next write. |
<> | 144:ef7eb2e8f9f7 | 794 | UART_DISABLE_INT(uart_base, UART_INTEN_THREIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 795 | } |
<> | 144:ef7eb2e8f9f7 | 796 | |
<> | 144:ef7eb2e8f9f7 | 797 | uint32_t event = 0; |
<> | 144:ef7eb2e8f9f7 | 798 | |
<> | 144:ef7eb2e8f9f7 | 799 | if (obj->serial.dma_usage_tx == DMA_USAGE_NEVER) { |
<> | 144:ef7eb2e8f9f7 | 800 | serial_write_async(obj); |
<> | 144:ef7eb2e8f9f7 | 801 | } |
<> | 144:ef7eb2e8f9f7 | 802 | |
<> | 144:ef7eb2e8f9f7 | 803 | if (serial_is_tx_complete(obj)) { |
<> | 144:ef7eb2e8f9f7 | 804 | event |= SERIAL_EVENT_TX_COMPLETE; |
<> | 144:ef7eb2e8f9f7 | 805 | } |
<> | 144:ef7eb2e8f9f7 | 806 | |
<> | 144:ef7eb2e8f9f7 | 807 | return event; |
<> | 144:ef7eb2e8f9f7 | 808 | } |
<> | 144:ef7eb2e8f9f7 | 809 | |
<> | 144:ef7eb2e8f9f7 | 810 | static uint32_t serial_rx_event_check(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 811 | { |
<> | 144:ef7eb2e8f9f7 | 812 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 813 | |
<> | 144:ef7eb2e8f9f7 | 814 | if (uart_base->INTSTS & (UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk)) { |
<> | 144:ef7eb2e8f9f7 | 815 | // Simulate clear of the interrupt flag. Temporarily disable the interrupt here and to be recovered on next read. |
<> | 144:ef7eb2e8f9f7 | 816 | UART_DISABLE_INT(uart_base, (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); |
<> | 144:ef7eb2e8f9f7 | 817 | } |
<> | 144:ef7eb2e8f9f7 | 818 | |
<> | 144:ef7eb2e8f9f7 | 819 | uint32_t event = 0; |
<> | 144:ef7eb2e8f9f7 | 820 | |
<> | 144:ef7eb2e8f9f7 | 821 | if (uart_base->FIFOSTS & UART_FIFOSTS_BIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 822 | uart_base->FIFOSTS = UART_FIFOSTS_BIF_Msk; |
<> | 144:ef7eb2e8f9f7 | 823 | } |
<> | 144:ef7eb2e8f9f7 | 824 | if (uart_base->FIFOSTS & UART_FIFOSTS_FEF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 825 | uart_base->FIFOSTS = UART_FIFOSTS_FEF_Msk; |
<> | 144:ef7eb2e8f9f7 | 826 | event |= SERIAL_EVENT_RX_FRAMING_ERROR; |
<> | 144:ef7eb2e8f9f7 | 827 | } |
<> | 144:ef7eb2e8f9f7 | 828 | if (uart_base->FIFOSTS & UART_FIFOSTS_PEF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 829 | uart_base->FIFOSTS = UART_FIFOSTS_PEF_Msk; |
<> | 144:ef7eb2e8f9f7 | 830 | event |= SERIAL_EVENT_RX_PARITY_ERROR; |
<> | 144:ef7eb2e8f9f7 | 831 | } |
<> | 144:ef7eb2e8f9f7 | 832 | |
<> | 144:ef7eb2e8f9f7 | 833 | if (uart_base->FIFOSTS & UART_FIFOSTS_RXOVIF_Msk) { |
<> | 144:ef7eb2e8f9f7 | 834 | uart_base->FIFOSTS = UART_FIFOSTS_RXOVIF_Msk; |
<> | 144:ef7eb2e8f9f7 | 835 | event |= SERIAL_EVENT_RX_OVERFLOW; |
<> | 144:ef7eb2e8f9f7 | 836 | } |
<> | 144:ef7eb2e8f9f7 | 837 | |
<> | 144:ef7eb2e8f9f7 | 838 | if (obj->serial.dma_usage_rx == DMA_USAGE_NEVER) { |
<> | 144:ef7eb2e8f9f7 | 839 | serial_read_async(obj); |
<> | 144:ef7eb2e8f9f7 | 840 | } |
<> | 144:ef7eb2e8f9f7 | 841 | |
<> | 144:ef7eb2e8f9f7 | 842 | if (serial_is_rx_complete(obj)) { |
<> | 144:ef7eb2e8f9f7 | 843 | event |= SERIAL_EVENT_RX_COMPLETE; |
<> | 144:ef7eb2e8f9f7 | 844 | } |
<> | 144:ef7eb2e8f9f7 | 845 | if ((obj->char_match != SERIAL_RESERVED_CHAR_MATCH) && obj->char_found) { |
<> | 144:ef7eb2e8f9f7 | 846 | event |= SERIAL_EVENT_RX_CHARACTER_MATCH; |
<> | 144:ef7eb2e8f9f7 | 847 | // FIXME: Timing to reset char_found? |
<> | 144:ef7eb2e8f9f7 | 848 | //obj->char_found = 0; |
<> | 144:ef7eb2e8f9f7 | 849 | } |
<> | 144:ef7eb2e8f9f7 | 850 | |
<> | 144:ef7eb2e8f9f7 | 851 | return event; |
<> | 144:ef7eb2e8f9f7 | 852 | } |
<> | 144:ef7eb2e8f9f7 | 853 | |
<> | 144:ef7eb2e8f9f7 | 854 | static void uart_dma_handler_tx(uint32_t id, uint32_t event_dma) |
<> | 144:ef7eb2e8f9f7 | 855 | { |
<> | 144:ef7eb2e8f9f7 | 856 | serial_t *obj = (serial_t *) id; |
<> | 144:ef7eb2e8f9f7 | 857 | |
<> | 144:ef7eb2e8f9f7 | 858 | // FIXME: Pass this error to caller |
<> | 144:ef7eb2e8f9f7 | 859 | if (event_dma & DMA_EVENT_ABORT) { |
<> | 144:ef7eb2e8f9f7 | 860 | } |
<> | 144:ef7eb2e8f9f7 | 861 | // Expect UART IRQ will catch this transfer done event |
<> | 144:ef7eb2e8f9f7 | 862 | if (event_dma & DMA_EVENT_TRANSFER_DONE) { |
<> | 144:ef7eb2e8f9f7 | 863 | obj->tx_buff.pos = obj->tx_buff.length; |
<> | 144:ef7eb2e8f9f7 | 864 | } |
<> | 144:ef7eb2e8f9f7 | 865 | // FIXME: Pass this error to caller |
<> | 144:ef7eb2e8f9f7 | 866 | if (event_dma & DMA_EVENT_TIMEOUT) { |
<> | 144:ef7eb2e8f9f7 | 867 | } |
<> | 144:ef7eb2e8f9f7 | 868 | |
<> | 144:ef7eb2e8f9f7 | 869 | uart_irq_async(obj); |
<> | 144:ef7eb2e8f9f7 | 870 | } |
<> | 144:ef7eb2e8f9f7 | 871 | |
<> | 144:ef7eb2e8f9f7 | 872 | static void uart_dma_handler_rx(uint32_t id, uint32_t event_dma) |
<> | 144:ef7eb2e8f9f7 | 873 | { |
<> | 144:ef7eb2e8f9f7 | 874 | serial_t *obj = (serial_t *) id; |
<> | 144:ef7eb2e8f9f7 | 875 | |
<> | 144:ef7eb2e8f9f7 | 876 | // FIXME: Pass this error to caller |
<> | 144:ef7eb2e8f9f7 | 877 | if (event_dma & DMA_EVENT_ABORT) { |
<> | 144:ef7eb2e8f9f7 | 878 | } |
<> | 144:ef7eb2e8f9f7 | 879 | // Expect UART IRQ will catch this transfer done event |
<> | 144:ef7eb2e8f9f7 | 880 | if (event_dma & DMA_EVENT_TRANSFER_DONE) { |
<> | 144:ef7eb2e8f9f7 | 881 | obj->rx_buff.pos = obj->rx_buff.length; |
<> | 144:ef7eb2e8f9f7 | 882 | } |
<> | 144:ef7eb2e8f9f7 | 883 | // FIXME: Pass this error to caller |
<> | 144:ef7eb2e8f9f7 | 884 | if (event_dma & DMA_EVENT_TIMEOUT) { |
<> | 144:ef7eb2e8f9f7 | 885 | } |
<> | 144:ef7eb2e8f9f7 | 886 | |
<> | 144:ef7eb2e8f9f7 | 887 | uart_irq_async(obj); |
<> | 144:ef7eb2e8f9f7 | 888 | } |
<> | 144:ef7eb2e8f9f7 | 889 | |
<> | 144:ef7eb2e8f9f7 | 890 | static int serial_write_async(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 891 | { |
<> | 144:ef7eb2e8f9f7 | 892 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 893 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 894 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 895 | |
<> | 144:ef7eb2e8f9f7 | 896 | UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 897 | |
<> | 144:ef7eb2e8f9f7 | 898 | uint32_t tx_fifo_max = ((struct nu_uart_var *) modinit->var)->fifo_size_tx; |
<> | 144:ef7eb2e8f9f7 | 899 | uint32_t tx_fifo_busy = (uart_base->FIFOSTS & UART_FIFOSTS_TXPTR_Msk) >> UART_FIFOSTS_TXPTR_Pos; |
<> | 144:ef7eb2e8f9f7 | 900 | if (uart_base->FIFOSTS & UART_FIFOSTS_TXFULL_Msk) { |
<> | 144:ef7eb2e8f9f7 | 901 | tx_fifo_busy = tx_fifo_max; |
<> | 144:ef7eb2e8f9f7 | 902 | } |
<> | 144:ef7eb2e8f9f7 | 903 | uint32_t tx_fifo_free = tx_fifo_max - tx_fifo_busy; |
<> | 144:ef7eb2e8f9f7 | 904 | if (tx_fifo_free == 0) { |
<> | 144:ef7eb2e8f9f7 | 905 | // Simulate clear of the interrupt flag |
<> | 144:ef7eb2e8f9f7 | 906 | if (obj->serial.inten_msk & UART_INTEN_THREIEN_Msk) { |
<> | 144:ef7eb2e8f9f7 | 907 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_THREIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 908 | } |
<> | 144:ef7eb2e8f9f7 | 909 | return 0; |
<> | 144:ef7eb2e8f9f7 | 910 | } |
<> | 144:ef7eb2e8f9f7 | 911 | |
<> | 144:ef7eb2e8f9f7 | 912 | uint32_t bytes_per_word = obj->tx_buff.width / 8; |
<> | 144:ef7eb2e8f9f7 | 913 | |
<> | 144:ef7eb2e8f9f7 | 914 | uint8_t *tx = (uint8_t *)(obj->tx_buff.buffer) + bytes_per_word * obj->tx_buff.pos; |
<> | 144:ef7eb2e8f9f7 | 915 | int n_words = 0; |
<> | 144:ef7eb2e8f9f7 | 916 | while (obj->tx_buff.pos < obj->tx_buff.length && tx_fifo_free >= bytes_per_word) { |
<> | 144:ef7eb2e8f9f7 | 917 | switch (bytes_per_word) { |
<> | 144:ef7eb2e8f9f7 | 918 | case 4: |
<> | 144:ef7eb2e8f9f7 | 919 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
<> | 144:ef7eb2e8f9f7 | 920 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
<> | 144:ef7eb2e8f9f7 | 921 | case 2: |
<> | 144:ef7eb2e8f9f7 | 922 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
<> | 144:ef7eb2e8f9f7 | 923 | case 1: |
<> | 144:ef7eb2e8f9f7 | 924 | UART_WRITE(((UART_T *) NU_MODBASE(obj->serial.uart)), *tx ++); |
<> | 144:ef7eb2e8f9f7 | 925 | } |
<> | 144:ef7eb2e8f9f7 | 926 | |
<> | 144:ef7eb2e8f9f7 | 927 | n_words ++; |
<> | 144:ef7eb2e8f9f7 | 928 | tx_fifo_free -= bytes_per_word; |
<> | 144:ef7eb2e8f9f7 | 929 | obj->tx_buff.pos ++; |
<> | 144:ef7eb2e8f9f7 | 930 | } |
<> | 144:ef7eb2e8f9f7 | 931 | |
<> | 144:ef7eb2e8f9f7 | 932 | if (n_words) { |
<> | 144:ef7eb2e8f9f7 | 933 | // Simulate clear of the interrupt flag |
<> | 144:ef7eb2e8f9f7 | 934 | if (obj->serial.inten_msk & UART_INTEN_THREIEN_Msk) { |
<> | 144:ef7eb2e8f9f7 | 935 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_THREIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 936 | } |
<> | 144:ef7eb2e8f9f7 | 937 | } |
<> | 144:ef7eb2e8f9f7 | 938 | |
<> | 144:ef7eb2e8f9f7 | 939 | return n_words; |
<> | 144:ef7eb2e8f9f7 | 940 | } |
<> | 144:ef7eb2e8f9f7 | 941 | |
<> | 144:ef7eb2e8f9f7 | 942 | static int serial_read_async(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 943 | { |
<> | 144:ef7eb2e8f9f7 | 944 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 945 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 946 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 947 | |
<> | 144:ef7eb2e8f9f7 | 948 | uint32_t rx_fifo_busy = (((UART_T *) NU_MODBASE(obj->serial.uart))->FIFOSTS & UART_FIFOSTS_RXPTR_Msk) >> UART_FIFOSTS_RXPTR_Pos; |
<> | 144:ef7eb2e8f9f7 | 949 | //uint32_t rx_fifo_free = ((struct nu_uart_var *) modinit->var)->fifo_size_rx - rx_fifo_busy; |
<> | 144:ef7eb2e8f9f7 | 950 | //if (rx_fifo_free == 0) { |
<> | 144:ef7eb2e8f9f7 | 951 | // return 0; |
<> | 144:ef7eb2e8f9f7 | 952 | //} |
<> | 144:ef7eb2e8f9f7 | 953 | |
<> | 144:ef7eb2e8f9f7 | 954 | uint32_t bytes_per_word = obj->rx_buff.width / 8; |
<> | 144:ef7eb2e8f9f7 | 955 | |
<> | 144:ef7eb2e8f9f7 | 956 | uint8_t *rx = (uint8_t *)(obj->rx_buff.buffer) + bytes_per_word * obj->rx_buff.pos; |
<> | 144:ef7eb2e8f9f7 | 957 | int n_words = 0; |
<> | 144:ef7eb2e8f9f7 | 958 | while (obj->rx_buff.pos < obj->rx_buff.length && rx_fifo_busy >= bytes_per_word) { |
<> | 144:ef7eb2e8f9f7 | 959 | switch (bytes_per_word) { |
<> | 144:ef7eb2e8f9f7 | 960 | case 4: |
<> | 144:ef7eb2e8f9f7 | 961 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 962 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 963 | case 2: |
<> | 144:ef7eb2e8f9f7 | 964 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 965 | case 1: |
<> | 144:ef7eb2e8f9f7 | 966 | *rx ++ = UART_READ(((UART_T *) NU_MODBASE(obj->serial.uart))); |
<> | 144:ef7eb2e8f9f7 | 967 | } |
<> | 144:ef7eb2e8f9f7 | 968 | |
<> | 144:ef7eb2e8f9f7 | 969 | n_words ++; |
<> | 144:ef7eb2e8f9f7 | 970 | rx_fifo_busy -= bytes_per_word; |
<> | 144:ef7eb2e8f9f7 | 971 | obj->rx_buff.pos ++; |
<> | 144:ef7eb2e8f9f7 | 972 | |
<> | 144:ef7eb2e8f9f7 | 973 | if ((obj->serial.event & SERIAL_EVENT_RX_CHARACTER_MATCH) && |
<> | 144:ef7eb2e8f9f7 | 974 | obj->char_match != SERIAL_RESERVED_CHAR_MATCH) { |
<> | 144:ef7eb2e8f9f7 | 975 | uint8_t *rx_cmp = rx; |
<> | 144:ef7eb2e8f9f7 | 976 | switch (bytes_per_word) { |
<> | 144:ef7eb2e8f9f7 | 977 | case 4: |
<> | 144:ef7eb2e8f9f7 | 978 | rx_cmp -= 2; |
<> | 144:ef7eb2e8f9f7 | 979 | case 2: |
<> | 144:ef7eb2e8f9f7 | 980 | rx_cmp --; |
<> | 144:ef7eb2e8f9f7 | 981 | case 1: |
<> | 144:ef7eb2e8f9f7 | 982 | rx_cmp --; |
<> | 144:ef7eb2e8f9f7 | 983 | } |
<> | 144:ef7eb2e8f9f7 | 984 | if (*rx_cmp == obj->char_match) { |
<> | 144:ef7eb2e8f9f7 | 985 | obj->char_found = 1; |
<> | 144:ef7eb2e8f9f7 | 986 | break; |
<> | 144:ef7eb2e8f9f7 | 987 | } |
<> | 144:ef7eb2e8f9f7 | 988 | } |
<> | 144:ef7eb2e8f9f7 | 989 | } |
<> | 144:ef7eb2e8f9f7 | 990 | |
<> | 144:ef7eb2e8f9f7 | 991 | if (n_words) { |
<> | 144:ef7eb2e8f9f7 | 992 | // Simulate clear of the interrupt flag |
<> | 144:ef7eb2e8f9f7 | 993 | if (obj->serial.inten_msk & (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)) { |
<> | 144:ef7eb2e8f9f7 | 994 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); |
<> | 144:ef7eb2e8f9f7 | 995 | } |
<> | 144:ef7eb2e8f9f7 | 996 | } |
<> | 144:ef7eb2e8f9f7 | 997 | |
<> | 144:ef7eb2e8f9f7 | 998 | return n_words; |
<> | 144:ef7eb2e8f9f7 | 999 | } |
<> | 144:ef7eb2e8f9f7 | 1000 | |
<> | 144:ef7eb2e8f9f7 | 1001 | static void serial_tx_buffer_set(serial_t *obj, const void *tx, size_t length, uint8_t width) |
<> | 144:ef7eb2e8f9f7 | 1002 | { |
<> | 144:ef7eb2e8f9f7 | 1003 | obj->tx_buff.buffer = (void *) tx; |
<> | 144:ef7eb2e8f9f7 | 1004 | obj->tx_buff.length = length; |
<> | 144:ef7eb2e8f9f7 | 1005 | obj->tx_buff.pos = 0; |
<> | 144:ef7eb2e8f9f7 | 1006 | obj->tx_buff.width = width; |
<> | 144:ef7eb2e8f9f7 | 1007 | } |
<> | 144:ef7eb2e8f9f7 | 1008 | |
<> | 144:ef7eb2e8f9f7 | 1009 | static void serial_rx_buffer_set(serial_t *obj, void *rx, size_t length, uint8_t width) |
<> | 144:ef7eb2e8f9f7 | 1010 | { |
<> | 144:ef7eb2e8f9f7 | 1011 | obj->rx_buff.buffer = rx; |
<> | 144:ef7eb2e8f9f7 | 1012 | obj->rx_buff.length = length; |
<> | 144:ef7eb2e8f9f7 | 1013 | obj->rx_buff.pos = 0; |
<> | 144:ef7eb2e8f9f7 | 1014 | obj->rx_buff.width = width; |
<> | 144:ef7eb2e8f9f7 | 1015 | } |
<> | 144:ef7eb2e8f9f7 | 1016 | |
<> | 144:ef7eb2e8f9f7 | 1017 | static void serial_tx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t enable) |
<> | 144:ef7eb2e8f9f7 | 1018 | { |
<> | 144:ef7eb2e8f9f7 | 1019 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 1020 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 1021 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 1022 | |
<> | 144:ef7eb2e8f9f7 | 1023 | // Necessary for both interrupt way and DMA way |
<> | 151:5eaa88a5bcc7 | 1024 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
<> | 144:ef7eb2e8f9f7 | 1025 | // With our own async vector, tx/rx handlers can be different. |
<> | 151:5eaa88a5bcc7 | 1026 | obj->serial.vec = var->vec_async; |
<> | 144:ef7eb2e8f9f7 | 1027 | obj->serial.irq_handler_tx_async = (void (*)(void)) handler; |
AnnaBridge | 165:e614a9f1c9e2 | 1028 | serial_enable_interrupt(obj, TxIrq, enable); |
<> | 144:ef7eb2e8f9f7 | 1029 | } |
<> | 144:ef7eb2e8f9f7 | 1030 | |
<> | 144:ef7eb2e8f9f7 | 1031 | static void serial_rx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t enable) |
<> | 144:ef7eb2e8f9f7 | 1032 | { |
<> | 144:ef7eb2e8f9f7 | 1033 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
<> | 144:ef7eb2e8f9f7 | 1034 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 1035 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
<> | 144:ef7eb2e8f9f7 | 1036 | |
<> | 144:ef7eb2e8f9f7 | 1037 | // Necessary for both interrupt way and DMA way |
<> | 151:5eaa88a5bcc7 | 1038 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
<> | 144:ef7eb2e8f9f7 | 1039 | // With our own async vector, tx/rx handlers can be different. |
<> | 151:5eaa88a5bcc7 | 1040 | obj->serial.vec = var->vec_async; |
<> | 144:ef7eb2e8f9f7 | 1041 | obj->serial.irq_handler_rx_async = (void (*) (void)) handler; |
AnnaBridge | 165:e614a9f1c9e2 | 1042 | serial_enable_interrupt(obj, RxIrq, enable); |
AnnaBridge | 165:e614a9f1c9e2 | 1043 | } |
AnnaBridge | 165:e614a9f1c9e2 | 1044 | |
AnnaBridge | 165:e614a9f1c9e2 | 1045 | static void serial_enable_interrupt(serial_t *obj, SerialIrq irq, uint32_t enable) |
AnnaBridge | 165:e614a9f1c9e2 | 1046 | { |
AnnaBridge | 165:e614a9f1c9e2 | 1047 | if (enable) { |
AnnaBridge | 165:e614a9f1c9e2 | 1048 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 165:e614a9f1c9e2 | 1049 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 1050 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 165:e614a9f1c9e2 | 1051 | |
AnnaBridge | 165:e614a9f1c9e2 | 1052 | NVIC_SetVector(modinit->irq_n, (uint32_t) obj->serial.vec); |
AnnaBridge | 165:e614a9f1c9e2 | 1053 | NVIC_EnableIRQ(modinit->irq_n); |
AnnaBridge | 165:e614a9f1c9e2 | 1054 | |
AnnaBridge | 165:e614a9f1c9e2 | 1055 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
AnnaBridge | 165:e614a9f1c9e2 | 1056 | // Multiple serial S/W objects for single UART H/W module possibly. |
AnnaBridge | 165:e614a9f1c9e2 | 1057 | // Bind serial S/W object to UART H/W module as interrupt is enabled. |
AnnaBridge | 165:e614a9f1c9e2 | 1058 | var->obj = obj; |
AnnaBridge | 165:e614a9f1c9e2 | 1059 | |
AnnaBridge | 165:e614a9f1c9e2 | 1060 | switch (irq) { |
AnnaBridge | 165:e614a9f1c9e2 | 1061 | // NOTE: Setting inten_msk first to avoid race condition |
AnnaBridge | 165:e614a9f1c9e2 | 1062 | case RxIrq: |
AnnaBridge | 165:e614a9f1c9e2 | 1063 | obj->serial.inten_msk = obj->serial.inten_msk | (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk); |
AnnaBridge | 165:e614a9f1c9e2 | 1064 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); |
AnnaBridge | 165:e614a9f1c9e2 | 1065 | break; |
AnnaBridge | 165:e614a9f1c9e2 | 1066 | case TxIrq: |
AnnaBridge | 165:e614a9f1c9e2 | 1067 | obj->serial.inten_msk = obj->serial.inten_msk | UART_INTEN_THREIEN_Msk; |
AnnaBridge | 165:e614a9f1c9e2 | 1068 | UART_ENABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_THREIEN_Msk); |
AnnaBridge | 165:e614a9f1c9e2 | 1069 | break; |
AnnaBridge | 165:e614a9f1c9e2 | 1070 | } |
AnnaBridge | 165:e614a9f1c9e2 | 1071 | } |
AnnaBridge | 165:e614a9f1c9e2 | 1072 | else { // disable |
AnnaBridge | 165:e614a9f1c9e2 | 1073 | switch (irq) { |
AnnaBridge | 165:e614a9f1c9e2 | 1074 | case RxIrq: |
AnnaBridge | 165:e614a9f1c9e2 | 1075 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk)); |
AnnaBridge | 165:e614a9f1c9e2 | 1076 | obj->serial.inten_msk = obj->serial.inten_msk & ~(UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk); |
AnnaBridge | 165:e614a9f1c9e2 | 1077 | break; |
AnnaBridge | 165:e614a9f1c9e2 | 1078 | case TxIrq: |
AnnaBridge | 165:e614a9f1c9e2 | 1079 | UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), UART_INTEN_THREIEN_Msk); |
AnnaBridge | 165:e614a9f1c9e2 | 1080 | obj->serial.inten_msk = obj->serial.inten_msk & ~UART_INTEN_THREIEN_Msk; |
AnnaBridge | 165:e614a9f1c9e2 | 1081 | break; |
AnnaBridge | 165:e614a9f1c9e2 | 1082 | } |
AnnaBridge | 165:e614a9f1c9e2 | 1083 | } |
AnnaBridge | 165:e614a9f1c9e2 | 1084 | } |
AnnaBridge | 165:e614a9f1c9e2 | 1085 | |
AnnaBridge | 165:e614a9f1c9e2 | 1086 | static void serial_rollback_interrupt(serial_t *obj, SerialIrq irq) |
AnnaBridge | 165:e614a9f1c9e2 | 1087 | { |
AnnaBridge | 165:e614a9f1c9e2 | 1088 | const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab); |
AnnaBridge | 165:e614a9f1c9e2 | 1089 | MBED_ASSERT(modinit != NULL); |
AnnaBridge | 165:e614a9f1c9e2 | 1090 | MBED_ASSERT(modinit->modname == (int) obj->serial.uart); |
AnnaBridge | 165:e614a9f1c9e2 | 1091 | |
AnnaBridge | 165:e614a9f1c9e2 | 1092 | struct nu_uart_var *var = (struct nu_uart_var *) modinit->var; |
AnnaBridge | 165:e614a9f1c9e2 | 1093 | |
AnnaBridge | 165:e614a9f1c9e2 | 1094 | obj->serial.vec = var->vec; |
AnnaBridge | 165:e614a9f1c9e2 | 1095 | serial_enable_interrupt(obj, irq, obj->serial.irq_en); |
<> | 144:ef7eb2e8f9f7 | 1096 | } |
<> | 144:ef7eb2e8f9f7 | 1097 | |
<> | 144:ef7eb2e8f9f7 | 1098 | static void serial_check_dma_usage(DMAUsage *dma_usage, int *dma_ch) |
<> | 144:ef7eb2e8f9f7 | 1099 | { |
<> | 144:ef7eb2e8f9f7 | 1100 | if (*dma_usage != DMA_USAGE_NEVER) { |
<> | 144:ef7eb2e8f9f7 | 1101 | if (*dma_ch == DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 144:ef7eb2e8f9f7 | 1102 | *dma_ch = dma_channel_allocate(DMA_CAP_NONE); |
<> | 144:ef7eb2e8f9f7 | 1103 | } |
<> | 144:ef7eb2e8f9f7 | 1104 | if (*dma_ch == DMA_ERROR_OUT_OF_CHANNELS) { |
<> | 144:ef7eb2e8f9f7 | 1105 | *dma_usage = DMA_USAGE_NEVER; |
<> | 144:ef7eb2e8f9f7 | 1106 | } |
<> | 144:ef7eb2e8f9f7 | 1107 | } |
<> | 144:ef7eb2e8f9f7 | 1108 | else { |
<> | 144:ef7eb2e8f9f7 | 1109 | dma_channel_free(*dma_ch); |
<> | 144:ef7eb2e8f9f7 | 1110 | *dma_ch = DMA_ERROR_OUT_OF_CHANNELS; |
<> | 144:ef7eb2e8f9f7 | 1111 | } |
<> | 144:ef7eb2e8f9f7 | 1112 | } |
<> | 144:ef7eb2e8f9f7 | 1113 | |
<> | 144:ef7eb2e8f9f7 | 1114 | static int serial_is_irq_en(serial_t *obj, SerialIrq irq) |
<> | 144:ef7eb2e8f9f7 | 1115 | { |
<> | 144:ef7eb2e8f9f7 | 1116 | int inten_msk = 0; |
<> | 144:ef7eb2e8f9f7 | 1117 | |
<> | 144:ef7eb2e8f9f7 | 1118 | switch (irq) { |
<> | 144:ef7eb2e8f9f7 | 1119 | case RxIrq: |
<> | 144:ef7eb2e8f9f7 | 1120 | inten_msk = obj->serial.inten_msk & (UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk); |
<> | 144:ef7eb2e8f9f7 | 1121 | break; |
<> | 144:ef7eb2e8f9f7 | 1122 | case TxIrq: |
<> | 144:ef7eb2e8f9f7 | 1123 | inten_msk = obj->serial.inten_msk & UART_INTEN_THREIEN_Msk; |
<> | 144:ef7eb2e8f9f7 | 1124 | break; |
<> | 144:ef7eb2e8f9f7 | 1125 | } |
<> | 144:ef7eb2e8f9f7 | 1126 | |
<> | 144:ef7eb2e8f9f7 | 1127 | return !! inten_msk; |
<> | 144:ef7eb2e8f9f7 | 1128 | } |
<> | 144:ef7eb2e8f9f7 | 1129 | |
<> | 144:ef7eb2e8f9f7 | 1130 | #endif // #if DEVICE_SERIAL_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 1131 | #endif // #if DEVICE_SERIAL |