static void uart_irq_handler(int port) { struct hal_uart *u; uint32_t status; uint8_t data; u = &uarts[port]; if (u->u_configured && u->u_open) { status = UART_GetStatusFlags(u->u_base); /* Check for RX data */ if (status & (kUART_RxDataRegFullFlag | kUART_RxOverrunFlag)) { data = UART_ReadByte(u->u_base); if (u->u_rx_stall || u->u_rx_func(u->u_func_arg, data) < 0) { /* * RX queue full. */ u->u_rx_stall = 1; ur_queue(&u->ur_rx, data); } } /* Check for TX complete */ if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(u->u_base)) { if (u->u_tx_started) { u->u_tx_started = 0; if (u->u_tx_done) u->u_tx_done(u->u_func_arg); } } } }
static int uart_mcux_irq_rx_full(struct device *dev) { const struct uart_mcux_config *config = dev->config->config_info; u32_t flags = UART_GetStatusFlags(config->base); return (flags & kUART_RxDataRegFullFlag) != 0; }
static int uart_mcux_irq_tx_complete(struct device *dev) { const struct uart_mcux_config *config = dev->config->config_info; u32_t flags = UART_GetStatusFlags(config->base); return (flags & kUART_TxDataRegEmptyFlag) != 0; }
int serial_writable(serial_t *obj) { uint32_t status_flags = UART_GetStatusFlags(uart_addrs[obj->index]); if (status_flags & kUART_RxOverrunFlag) UART_ClearStatusFlags(uart_addrs[obj->index], kUART_RxOverrunFlag); return (status_flags & kUART_TxDataRegEmptyFlag); }
static unsigned char uart_mcux_poll_out(struct device *dev, unsigned char c) { const struct uart_mcux_config *config = dev->config->config_info; while (!(UART_GetStatusFlags(config->base) & kUART_TxDataRegEmptyFlag)) ; UART_WriteByte(config->base, c); return c; }
static int uart_mcux_poll_in(struct device *dev, unsigned char *c) { const struct uart_mcux_config *config = dev->config->config_info; u32_t flags = UART_GetStatusFlags(config->base); int ret = -1; if (flags & kUART_RxDataRegFullFlag) { *c = UART_ReadByte(config->base); ret = 0; } return ret; }
static int uart_mcux_fifo_read(struct device *dev, u8_t *rx_data, const int len) { const struct uart_mcux_config *config = dev->config->config_info; u8_t num_rx = 0; while ((len - num_rx > 0) && (UART_GetStatusFlags(config->base) & kUART_RxDataRegFullFlag)) { rx_data[num_rx++] = UART_ReadByte(config->base); } return num_rx; }
static int uart_mcux_fifo_fill(struct device *dev, const u8_t *tx_data, int len) { const struct uart_mcux_config *config = dev->config->config_info; u8_t num_tx = 0; while ((len - num_tx > 0) && (UART_GetStatusFlags(config->base) & kUART_TxDataRegEmptyFlag)) { UART_WriteByte(config->base, tx_data[num_tx++]); } return num_tx; }
void DEMO_UART_IRQHandler(void) { uint8_t data; /* If new data arrived. */ if ((kUART_RxDataRegFullFlag | kUART_RxOverrunFlag) & UART_GetStatusFlags(DEMO_UART)) { data = UART_ReadByte(DEMO_UART); /* If ring buffer is not full, add data to ring buffer. */ if (((rxIndex + 1) % DEMO_RING_BUFFER_SIZE) != txIndex) { demoRingBuffer[rxIndex] = data; rxIndex++; rxIndex %= DEMO_RING_BUFFER_SIZE; } } }
void hal_uart_start_tx(int port) { struct hal_uart *u; int data = -1; int rc; if (port >= FSL_FEATURE_SOC_UART_COUNT) { return; } u = &uarts[port]; if (!u->u_configured || !u->u_open) { return; } /* main loop */ while (true) { /* add data to TX ring buffer */ if (u->u_tx_started == 0) { rc = hal_uart_tx_fill_buf(u); if (rc > 0) { u->u_tx_started = 1; } } /* Send data only when UART TX register is empty and TX ring buffer has data to send out. */ while (!ur_is_empty(&u->ur_tx) && (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(u->u_base))) { data = ur_read(&u->ur_tx); UART_WriteByte(u->u_base, data); ur_bump(&u->ur_tx); } if (ur_is_empty(&u->ur_tx)) { if (u->u_tx_done) { u->u_tx_done(u->u_func_arg); } u->u_tx_started = 0; break; } } }
/*! * @brief Main function */ int main(void) { uart_config_t config; BOARD_InitPins(); BOARD_BootClockRUN(); /* * config.baudRate_Bps = 115200U; * config.parityMode = kUART_ParityDisabled; * config.stopBitCount = kUART_OneStopBit; * config.txFifoWatermark = 0; * config.rxFifoWatermark = 1; * config.enableTx = false; * config.enableRx = false; */ UART_GetDefaultConfig(&config); config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE; config.enableTx = true; config.enableRx = true; UART_Init(DEMO_UART, &config, CLOCK_GetFreq(DEMO_UART_CLKSRC)); /* Send g_tipString out. */ UART_WriteBlocking(DEMO_UART, g_tipString, sizeof(g_tipString) / sizeof(g_tipString[0])); /* Enable RX interrupt. */ UART_EnableInterrupts(DEMO_UART, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable); EnableIRQ(DEMO_UART_IRQn); while (1) { /* Send data only when UART TX register is empty and ring buffer has data to send out. */ while ((kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(DEMO_UART)) && (rxIndex != txIndex)) { UART_WriteByte(DEMO_UART, demoRingBuffer[txIndex]); txIndex++; txIndex %= DEMO_RING_BUFFER_SIZE; } } }
static int uart_mcux_err_check(struct device *dev) { const struct uart_mcux_config *config = dev->config->config_info; u32_t flags = UART_GetStatusFlags(config->base); int err = 0; if (flags & kUART_RxOverrunFlag) { err |= UART_ERROR_OVERRUN; } if (flags & kUART_ParityErrorFlag) { err |= UART_ERROR_PARITY; } if (flags & kUART_FramingErrorFlag) { err |= UART_ERROR_FRAMING; } UART_ClearStatusFlags(config->base, kUART_RxOverrunFlag | kUART_ParityErrorFlag | kUART_FramingErrorFlag); return err; }