static inline void process_tx(void) { int bytes; if (!tx.buf) { tx.buf = net_buf_get(&tx.fifo, K_NO_WAIT); if (!tx.buf) { BT_ERR("TX interrupt but no pending buffer!"); uart_irq_tx_disable(h4_dev); return; } } if (!tx.type) { switch (bt_buf_get_type(tx.buf)) { case BT_BUF_ACL_OUT: tx.type = H4_ACL; break; case BT_BUF_CMD: tx.type = H4_CMD; break; default: BT_ERR("Unknown buffer type"); goto done; } bytes = uart_fifo_fill(h4_dev, &tx.type, 1); if (bytes != 1) { BT_WARN("Unable to send H:4 type"); tx.type = H4_NONE; return; } } bytes = uart_fifo_fill(h4_dev, tx.buf->data, tx.buf->len); net_buf_pull(tx.buf, bytes); if (tx.buf->len) { return; } done: tx.type = H4_NONE; net_buf_unref(tx.buf); tx.buf = net_buf_get(&tx.fifo, K_NO_WAIT); if (!tx.buf) { uart_irq_tx_disable(h4_dev); } }
void UARTClass::IrqHandler( void ) { uint8_t uc_data; int ret; ret = uart_poll_in(CONFIG_UART_CONSOLE_INDEX, &uc_data); while ( ret != -1 ) { _rx_buffer->store_char(uc_data); ret = uart_poll_in(CONFIG_UART_CONSOLE_INDEX, &uc_data); } // Do we need to keep sending data? if (!uart_irq_tx_ready(CONFIG_UART_CONSOLE_INDEX)) { if (_tx_buffer->_iTail != _tx_buffer->_iHead) { uart_poll_out(CONFIG_UART_CONSOLE_INDEX, _tx_buffer->_aucBuffer[_tx_buffer->_iTail]); _tx_buffer->_iTail = (unsigned int)(_tx_buffer->_iTail + 1) % SERIAL_BUFFER_SIZE; } else { // Mask off transmit interrupt so we don't get it anymore uart_irq_tx_disable(CONFIG_UART_CONSOLE_INDEX); } } }
int send_data(struct device *dev, const void* pBuffer, uint32_t lengthInBytes) { printf("sending %d bytes ... \n", lengthInBytes); const uint8_t *buffer = (const uint8_t*)(pBuffer); uint32_t bytesSent = 0; uint32_t remaining = lengthInBytes; uint32_t len; while (bytesSent < lengthInBytes) { if (remaining >= UART_FIFO_SIZE) len = UART_FIFO_SIZE; else len = remaining; uart_irq_tx_enable(dev); data_transmitted = false; len = uart_fifo_fill(dev, &buffer[bytesSent], len); while (data_transmitted == false) ; uart_irq_tx_disable(dev); remaining -= len; bytesSent += len; } printf("done %d bytes\n", bytesSent); return 0; }
static int h4_open(void) { BT_DBG(""); uart_irq_rx_disable(h4_dev); uart_irq_tx_disable(h4_dev); #if defined(CONFIG_BT_NRF51_PM) if (nrf51_init(h4_dev) < 0) { return -EIO; } #else h4_discard(h4_dev, 32); #endif uart_irq_callback_set(h4_dev, bt_uart_isr); k_thread_create(&rx_thread_data, rx_thread_stack, K_THREAD_STACK_SIZEOF(rx_thread_stack), rx_thread, NULL, NULL, NULL, K_PRIO_COOP(CONFIG_BT_RX_PRIO), 0, K_NO_WAIT); return 0; }
static void write_buf_irq(struct device *dev, const char *buf, int len) { int i; uart_irq_tx_enable(dev); for (i = 0; i < len; i++) { data_transmitted = false; uart_fifo_fill(dev, &buf[i], 1); while (data_transmitted == false) ; } uart_irq_tx_disable(dev); }
void UARTClass::end( void ) { int ret=0; uint8_t uc_data; // Wait for any outstanding data to be sent flush(); uart_irq_rx_disable(CONFIG_UART_CONSOLE_INDEX); uart_irq_tx_disable(CONFIG_UART_CONSOLE_INDEX); while ( ret != -1 ) { ret = uart_poll_in(CONFIG_UART_CONSOLE_INDEX, &uc_data); } opened = false; // Clear any received data _rx_buffer->_iHead = _rx_buffer->_iTail; }
static void console_input_init(void) { uint8_t c; uart_irq_rx_disable(uart_console_dev); uart_irq_tx_disable(uart_console_dev); uart_irq_callback_set(uart_console_dev, uart_console_isr); /* Drain the fifo */ while (uart_irq_rx_ready(uart_console_dev)) { uart_fifo_read(uart_console_dev, &c, 1); } uart_irq_rx_enable(uart_console_dev); }
static void uart_pipe_setup(struct device *uart) { uint8_t c; uart_irq_rx_disable(uart); uart_irq_tx_disable(uart); /* Drain the fifo */ while (uart_fifo_read(uart, &c, 1)) { continue; } uart_irq_callback_set(uart, uart_pipe_isr); uart_irq_rx_enable(uart); }
static int h5_open(void) { BT_DBG(""); uart_irq_rx_disable(h5_dev); uart_irq_tx_disable(h5_dev); bt_uart_drain(h5_dev); uart_irq_callback_set(h5_dev, bt_uart_isr); h5_init(); uart_irq_rx_enable(h5_dev); return 0; }
static void uart_simple_setup(int uart, struct uart_init_info *info) { uart_init(uart, info); uart_irq_rx_disable(uart); uart_irq_tx_disable(uart); IRQ_CONFIG(uart_simple, uart_irq_get(uart)); irq_enable(uart_irq_get(uart)); /* Drain the fifo */ while (uart_irq_rx_ready(uart)) { unsigned char c; uart_fifo_read(uart, &c, 1); } uart_irq_rx_enable(uart); }
void UARTClass::end( void ) { int ret=0; uint8_t uc_data; // Wait for any outstanding data to be sent flush(); uart_irq_rx_disable(CONFIG_UART_CONSOLE_INDEX); uart_irq_tx_disable(CONFIG_UART_CONSOLE_INDEX); while ( ret != -1 ) { ret = uart_poll_in(CONFIG_UART_CONSOLE_INDEX, &uc_data); } opened = false; // Clear any received data _rx_buffer->_iHead = _rx_buffer->_iTail; SET_PIN_MODE(17, GPIO_MUX_MODE); // Rdx SOC PIN (Arduino header pin 0) SET_PIN_MODE(16, GPIO_MUX_MODE); // Txd SOC PIN (Arduino header pin 1) }
static int bt_monitor_init(struct device *d) { ARG_UNUSED(d); monitor_dev = device_get_binding(CONFIG_BLUETOOTH_MONITOR_ON_DEV_NAME); #if defined(CONFIG_UART_INTERRUPT_DRIVEN) uart_irq_rx_disable(monitor_dev); uart_irq_tx_disable(monitor_dev); #endif #if !defined(CONFIG_UART_CONSOLE) __printk_hook_install(monitor_console_out); __stdout_hook_install(monitor_console_out); #endif return 0; }
static void bt_uart_setup(int uart, struct uart_init_info *info) { BT_DBG("\n"); uart_init(uart, info); uart_irq_rx_disable(uart); uart_irq_tx_disable(uart); IRQ_CONFIG(bluetooth, uart_irq_get(uart)); irq_enable(uart_irq_get(uart)); /* Drain the fifo */ while (uart_irq_rx_ready(uart)) { unsigned char c; uart_fifo_read(uart, &c, 1); } uart_irq_rx_enable(uart); }
static int h4_open(void) { BT_DBG(""); uart_irq_rx_disable(h4_dev); uart_irq_tx_disable(h4_dev); /* Drain the fifo */ while (uart_irq_rx_ready(h4_dev)) { unsigned char c; uart_fifo_read(h4_dev, &c, 1); } uart_irq_callback_set(h4_dev, bt_uart_isr); uart_irq_rx_enable(h4_dev); return 0; }
static int h4_open(void) { BT_DBG(""); uart_irq_rx_disable(h4_dev); uart_irq_tx_disable(h4_dev); #if defined(CONFIG_BLUETOOTH_NRF51_PM) if (nrf51_init(h4_dev) < 0) { return -EIO; } #else bt_uart_drain(h4_dev); #endif uart_irq_callback_set(h4_dev, bt_uart_isr); uart_irq_rx_enable(h4_dev); return 0; }
static int h4_open(void) { BT_DBG(""); uart_irq_rx_disable(h4_dev); uart_irq_tx_disable(h4_dev); IRQ_CONNECT(CONFIG_BLUETOOTH_UART_IRQ, CONFIG_BLUETOOTH_UART_IRQ_PRI, bt_uart_isr, 0, UART_IRQ_FLAGS); irq_enable(CONFIG_BLUETOOTH_UART_IRQ); /* Drain the fifo */ while (uart_irq_rx_ready(h4_dev)) { unsigned char c; uart_fifo_read(h4_dev, &c, 1); } uart_irq_rx_enable(h4_dev); return 0; }
void UARTClass::init(const uint32_t dwBaudRate, const uint8_t modeReg) { uint8_t c; // Make sure both ring buffers are initialized back to empty. _rx_buffer->_iHead = _rx_buffer->_iTail = 0; _tx_buffer->_iHead = _tx_buffer->_iTail = 0; SET_PIN_MODE(17, UART_MUX_MODE); // Rdx SOC PIN (Arduino header pin 0) SET_PIN_MODE(16, UART_MUX_MODE); // Txd SOC PIN (Arduino header pin 1) info->options = 0; info->sys_clk_freq = SYSCLK_DEFAULT_IOSC_HZ; info->baud_rate = dwBaudRate; info->regs = CONFIG_UART_CONSOLE_REGS; info->irq = CONFIG_UART_CONSOLE_IRQ; info->int_pri = CONFIG_UART_CONSOLE_INT_PRI; info->async_format = modeReg; uart_init(CONFIG_UART_CONSOLE_INDEX, info); uart_irq_rx_disable(CONFIG_UART_CONSOLE_INDEX); uart_irq_tx_disable(CONFIG_UART_CONSOLE_INDEX); uart_int_connect(CONFIG_UART_CONSOLE_INDEX, /* UART to which to connect */ UART_Handler, /* interrupt handler */ NULL, /* argument to pass to handler */ NULL /* ptr to interrupt stub code */ ); while (uart_irq_rx_ready(CONFIG_UART_CONSOLE_INDEX)) uart_fifo_read(CONFIG_UART_CONSOLE_INDEX, &c, 1); uart_irq_rx_enable(CONFIG_UART_CONSOLE_INDEX); }
int main(int argc, char const *argv[]) { int ret; printf("start buffers: %d, size=%d total size=%d\n", sizeof(buf_pool)/sizeof(uart_buf_t), sizeof(uart_buf_t), sizeof(buf_pool)); uart_dev = device_get_binding(CONFIG_CDC_ACM_PORT_NAME); if (uart_dev == NULL) { printf("%s: error\n", __FUNCTION__); return 0; } ret = uart_line_ctrl_set(uart_dev, LINE_CTRL_BAUD_RATE, 115200); if (ret) printf("Baudrate set failed %d\n", ret); k_sleep(1000); uart_irq_rx_disable(uart_dev); uart_irq_tx_disable(uart_dev); /* drain the fifo */ uint8_t c; while(uart_fifo_read(uart_dev, &c, 1)) { continue; } uart_irq_callback_set(uart_dev, interrupt_handler); uart_irq_rx_enable(uart_dev); read_and_echo_data(uart_dev); return 0; }