uint32_t app_uart_put(uint8_t byte) { uint32_t err_code; err_code = app_fifo_put(&m_tx_fifo, byte); if (err_code == NRF_SUCCESS) { // The new byte has been added to FIFO. It will be picked up from there // (in 'uart_event_handler') when all preceding bytes are transmitted. // But if UART is not transmitting anything at the moment, we must start // a new transmission here. if (!nrf_drv_uart_tx_in_progress()) { // This operation should be almost always successful, since we've // just added a byte to FIFO, but if some bigger delay occurred // (some heavy interrupt handler routine has been executed) since // that time, FIFO might be empty already. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS) { err_code = nrf_drv_uart_tx(tx_buffer, 1); } } } return err_code; }
/**@brief Function for the UART Interrupt handler. * * @details UART interrupt handler to process TX Ready when TXD is available, RX Ready when a byte * is received, or in case of error when receiving a byte. */ void UART0_IRQHandler(void) { // Handle reception if (NRF_UART0->EVENTS_RXDRDY != 0) { uint32_t err_code; // Clear UART RX event flag NRF_UART0->EVENTS_RXDRDY = 0; // Write received byte to FIFO err_code = app_fifo_put(&m_rx_fifo, (uint8_t)NRF_UART0->RXD); if (err_code != NRF_SUCCESS) { app_uart_evt_t app_uart_event; app_uart_event.evt_type = APP_UART_FIFO_ERROR; app_uart_event.data.error_code = err_code; m_event_handler(&app_uart_event); } // Notify that new data is available if this was first byte put in the buffer. else if (FIFO_LENGTH(m_rx_fifo) == 1) { app_uart_evt_t app_uart_event; app_uart_event.evt_type = APP_UART_DATA_READY; m_event_handler(&app_uart_event); } else { // Do nothing, only send event if first byte was added or overflow in FIFO occurred. } } // Handle transmission. if (NRF_UART0->EVENTS_TXDRDY != 0) { // Clear UART TX event flag. NRF_UART0->EVENTS_TXDRDY = 0; on_uart_event(ON_TX_READY); } // Handle errors. if (NRF_UART0->EVENTS_ERROR != 0) { uint32_t error_source; app_uart_evt_t app_uart_event; // Clear UART ERROR event flag. NRF_UART0->EVENTS_ERROR = 0; // Clear error source. error_source = NRF_UART0->ERRORSRC; NRF_UART0->ERRORSRC = error_source; app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR; app_uart_event.data.error_communication = error_source; m_event_handler(&app_uart_event); } }
uint32_t app_uart_put(uint8_t byte) { uint32_t err_code; err_code = app_fifo_put(&m_tx_fifo, byte); on_uart_event(ON_UART_PUT); return err_code; }
uint32_t app_uart_get(uint8_t * p_byte) { NVIC_DisableIRQ(UART0_IRQn); uint32_t retval = app_fifo_get(&m_rx_fifo, p_byte); if(rx_fifo_overflow) { app_fifo_put(&m_rx_fifo, (uint8_t)NRF_UART0->RXD); rx_fifo_overflow = false; } NVIC_EnableIRQ(UART0_IRQn); return retval; }
static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context) { app_uart_evt_t app_uart_event; if (p_event->type == NRF_DRV_UART_EVT_RX_DONE) { // Write received byte to FIFO uint32_t err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]); if (err_code != NRF_SUCCESS) { app_uart_event.evt_type = APP_UART_FIFO_ERROR; app_uart_event.data.error_code = err_code; m_event_handler(&app_uart_event); } // Notify that new data is available if this was first byte put in the buffer. else if (FIFO_LENGTH(m_rx_fifo) == 1) { app_uart_event.evt_type = APP_UART_DATA_READY; m_event_handler(&app_uart_event); } else { // Do nothing, only send event if first byte was added or overflow in FIFO occurred. } if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask) { (void)nrf_drv_uart_rx(rx_buffer,1); } } else if (p_event->type == NRF_DRV_UART_EVT_ERROR) { app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR; app_uart_event.data.error_communication = p_event->data.error.error_mask; (void)nrf_drv_uart_rx(rx_buffer,1); m_event_handler(&app_uart_event); } else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE) { // Get next byte from FIFO. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS) { (void)nrf_drv_uart_tx(tx_buffer,1); } if (FIFO_LENGTH(m_tx_fifo) == 0) { // Last byte from FIFO transmitted, notify the application. // Notify that new data is available if this was first byte put in the buffer. app_uart_event.evt_type = APP_UART_TX_EMPTY; m_event_handler(&app_uart_event); } } }
void RNG_IRQHandler(void) { if (nrf_rng_event_get(NRF_RNG_EVENT_VALRDY) && nrf_rng_int_get(NRF_RNG_INT_VALRDY_MASK)) { nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); uint32_t nrf_error = app_fifo_put(&m_rng_cb.rand_pool, nrf_rng_random_value_get()); if ((FIFO_LENGTH(m_rng_cb.rand_pool) > m_rng_cb.rand_pool.buf_size_mask) || (nrf_error == NRF_ERROR_NO_MEM)) { rng_stop(); } } }
uint32_t app_uart_put(uint8_t byte) { uint32_t err_code; tx_tmp = byte; err_code = nrf_drv_uart_tx(&tx_tmp, 1); if (err_code == NRF_ERROR_BUSY) { err_code = app_fifo_put(&m_tx_fifo, byte); } return err_code; }
static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context) { app_uart_evt_t app_uart_event; uint32_t err_code; switch (p_event->type) { case NRF_DRV_UART_EVT_RX_DONE: // Write received byte to FIFO. err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]); if (err_code != NRF_SUCCESS) { app_uart_event.evt_type = APP_UART_FIFO_ERROR; app_uart_event.data.error_code = err_code; m_event_handler(&app_uart_event); } // Notify that there are data available. else if (FIFO_LENGTH(m_rx_fifo) != 0) { app_uart_event.evt_type = APP_UART_DATA_READY; m_event_handler(&app_uart_event); } // Start new RX if size in buffer. if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask) { (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1); } else { // Overflow in RX FIFO. m_rx_ovf = true; } break; case NRF_DRV_UART_EVT_ERROR: app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR; app_uart_event.data.error_communication = p_event->data.error.error_mask; (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1); m_event_handler(&app_uart_event); break; case NRF_DRV_UART_EVT_TX_DONE: // Get next byte from FIFO. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS) { (void)nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1); } else { // Last byte from FIFO transmitted, notify the application. app_uart_event.evt_type = APP_UART_TX_EMPTY; m_event_handler(&app_uart_event); } break; default: break; } }