mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Thu Mar 30 13:45:57 2017 +0100
Revision:
161:2cc1468da177
Parent:
153:fa9ff456f731
Child:
165:e614a9f1c9e2
This updates the lib to the mbed lib v139

Who changed what in which revision?

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