123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(APP_UART)
- #include "app_uart.h"
- #include "app_fifo.h"
- #include "nrf_drv_uart.h"
- #include "nrf_assert.h"
- static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);
- static __INLINE uint32_t fifo_length(app_fifo_t * const fifo)
- {
- uint32_t tmp = fifo->read_pos;
- return fifo->write_pos - tmp;
- }
- #define FIFO_LENGTH(F) fifo_length(&F)
- static app_uart_event_handler_t m_event_handler;
- static uint8_t tx_buffer[1];
- static uint8_t rx_buffer[1];
- static bool m_rx_ovf;
- static app_fifo_t m_rx_fifo;
- static app_fifo_t m_tx_fifo;
- 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:
-
- if(p_event->data.rxtx.bytes == 0)
- {
-
- (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
- break;
- }
-
- 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);
- }
-
- else if (FIFO_LENGTH(m_rx_fifo) != 0)
- {
- app_uart_event.evt_type = APP_UART_DATA_READY;
- m_event_handler(&app_uart_event);
- }
-
- if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
- {
- (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
- }
- else
- {
-
- 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:
-
- if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
- {
- (void)nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
- }
- else
- {
-
- app_uart_event.evt_type = APP_UART_TX_EMPTY;
- m_event_handler(&app_uart_event);
- }
- break;
- default:
- break;
- }
- }
- uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
- app_uart_buffers_t * p_buffers,
- app_uart_event_handler_t event_handler,
- app_irq_priority_t irq_priority)
- {
- uint32_t err_code;
- m_event_handler = event_handler;
- if (p_buffers == NULL)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
-
- err_code = app_fifo_init(&m_rx_fifo, p_buffers->rx_buf, p_buffers->rx_buf_size);
- VERIFY_SUCCESS(err_code);
-
- err_code = app_fifo_init(&m_tx_fifo, p_buffers->tx_buf, p_buffers->tx_buf_size);
- VERIFY_SUCCESS(err_code);
- nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
- config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
- config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
- NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
- config.interrupt_priority = irq_priority;
- config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
- config.pselcts = p_comm_params->cts_pin_no;
- config.pselrts = p_comm_params->rts_pin_no;
- config.pselrxd = p_comm_params->rx_pin_no;
- config.pseltxd = p_comm_params->tx_pin_no;
- err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
- VERIFY_SUCCESS(err_code);
- m_rx_ovf = false;
-
- if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
- {
- return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
- }
- else
- {
- return NRF_SUCCESS;
- }
- }
- uint32_t app_uart_flush(void)
- {
- uint32_t err_code;
- err_code = app_fifo_flush(&m_rx_fifo);
- VERIFY_SUCCESS(err_code);
- err_code = app_fifo_flush(&m_tx_fifo);
- VERIFY_SUCCESS(err_code);
- return NRF_SUCCESS;
- }
- uint32_t app_uart_get(uint8_t * p_byte)
- {
- ASSERT(p_byte);
- bool rx_ovf = m_rx_ovf;
- ret_code_t err_code = app_fifo_get(&m_rx_fifo, p_byte);
-
- if (rx_ovf)
- {
- m_rx_ovf = false;
- uint32_t uart_err_code = nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
-
- APP_ERROR_CHECK(uart_err_code);
- }
- return err_code;
- }
- 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)
- {
-
-
-
-
- if (!nrf_drv_uart_tx_in_progress(&app_uart_inst))
- {
-
-
-
-
- if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
- {
- err_code = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
- }
- }
- }
- return err_code;
- }
- uint32_t app_uart_close(void)
- {
- nrf_drv_uart_uninit(&app_uart_inst);
- return NRF_SUCCESS;
- }
- #endif
|