123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #ifndef NRF_LIBUARTE_DRV_H
- #define NRF_LIBUARTE_DRV_H
- #include "sdk_errors.h"
- #include "nrf_uarte.h"
- #include "nrfx_ppi.h"
- #include "nrfx_timer.h"
- #include <stdint.h>
- #include <stdbool.h>
- #define NRF_LIBUARTE_DRV_HWFC_BYTE_LIMIT 4
- typedef enum
- {
- NRF_LIBUARTE_DRV_EVT_RX_DATA,
- NRF_LIBUARTE_DRV_EVT_RX_BUF_REQ,
- NRF_LIBUARTE_DRV_EVT_TX_DONE,
- NRF_LIBUARTE_DRV_EVT_ERROR,
- NRF_LIBUARTE_DRV_EVT_OVERRUN_ERROR
- } nrf_libuarte_drv_evt_type_t;
- typedef enum
- {
- NRF_LIBUARTE_DRV_PPI_CH_EXT_TRIGGER_STARTRX_EN_ENDRX_STARTX,
- NRF_LIBUARTE_DRV_PPI_CH_RXSTARTED_EXT_TSK,
- NRF_LIBUARTE_DRV_PPI_CH_EXT_STOP_STOPRX,
- NRF_LIBUARTE_DRV_PPI_CH_EXT_STOP_GROUPS_EN,
- NRF_LIBUARTE_DRV_PPI_CH_RXRDY_TIMER_COUNT,
- NRF_LIBUARTE_DRV_PPI_CH_ENDRX_STARTRX,
- NRF_LIBUARTE_DRV_PPI_CH_ENDRX_EXT_TSK,
- NRF_LIBUARTE_DRV_PPI_CH_RTS_PIN,
- NRF_LIBUARTE_DRV_PPI_CH_RX_MAX,
- NRF_LIBUARTE_DRV_PPI_CH_RX_GROUP_MAX = NRF_LIBUARTE_DRV_PPI_CH_RX_MAX,
- NRF_LIBUARTE_DRV_PPI_CH_ENDTX_STARTTX = NRF_LIBUARTE_DRV_PPI_CH_RX_GROUP_MAX,
- NRF_LIBUARTE_DRV_PPI_CH_MAX
- } nrf_libuarte_drv_ppi_channel_t;
- typedef enum
- {
- NRF_LIBUARTE_DRV_PPI_GROUP_ENDRX_STARTRX,
- NRF_LIBUARTE_DRV_PPI_GROUP_ENDRX_EXT_RXDONE_TSK,
- NRF_LIBUARTE_DRV_PPI_GROUP_MAX
- } nrf_libuarte_drv_ppi_group_t;
- typedef struct
- {
- uint8_t * p_data;
- size_t length;
- } nrf_libuarte_drv_data_t;
- typedef struct
- {
- uint32_t overrun_length;
- } nrf_libuarte_drv_overrun_err_evt_t;
- typedef struct
- {
- nrf_libuarte_drv_evt_type_t type;
- union {
- nrf_libuarte_drv_data_t rxtx;
- uint8_t errorsrc;
- nrf_libuarte_drv_overrun_err_evt_t overrun_err;
- } data;
- } nrf_libuarte_drv_evt_t;
- typedef struct {
- uint32_t tx_pin;
- uint32_t rx_pin;
- uint32_t cts_pin;
- uint32_t rts_pin;
- uint32_t startrx_evt;
- uint32_t endrx_evt;
- uint32_t rxstarted_tsk;
- uint32_t rxdone_tsk;
- nrf_uarte_hwfc_t hwfc;
- nrf_uarte_parity_t parity;
- nrf_uarte_baudrate_t baudrate;
- uint8_t irq_priority;
- bool pullup_rx;
- } nrf_libuarte_drv_config_t;
- typedef void (*nrf_libuarte_drv_evt_handler_t)(void * context,
- nrf_libuarte_drv_evt_t * p_evt);
- extern const IRQn_Type libuarte_irqn[];
- typedef struct {
- nrf_ppi_channel_t ppi_channels[NRF_LIBUARTE_DRV_PPI_CH_MAX];
- nrf_ppi_channel_group_t ppi_groups[NRF_LIBUARTE_DRV_PPI_GROUP_MAX];
- uint8_t * p_tx;
- size_t tx_len;
- size_t tx_cur_idx;
- uint8_t * p_cur_rx;
- uint8_t * p_next_rx;
- uint8_t * p_next_next_rx;
- nrf_libuarte_drv_evt_handler_t evt_handler;
- uint32_t last_rx_byte_cnt;
- uint32_t last_pin_rx_byte_cnt;
- uint32_t chunk_size;
- void * context;
- uint16_t tx_chunk8;
- uint8_t rts_pin;
- bool rts_manual;
- bool enabled;
- } nrf_libuarte_drv_ctrl_blk_t;
- typedef struct {
- nrf_libuarte_drv_ctrl_blk_t * ctrl_blk;
- nrfx_timer_t timer;
- NRF_UARTE_Type * uarte;
- } nrf_libuarte_drv_t;
- #define NRF_LIBUARTE_DRV_DEFINE(_name, _uarte_idx, _timer_idx) \
- STATIC_ASSERT(_uarte_idx < UARTE_COUNT, "UARTE instance not present");\
- STATIC_ASSERT(CONCAT_2(NRF_LIBUARTE_DRV_UARTE,_uarte_idx) == 1, "UARTE instance not enabled");\
- STATIC_ASSERT(CONCAT_3(NRFX_TIMER,_timer_idx, _ENABLED) == 1, "Timer instance not enabled");\
- static nrf_libuarte_drv_ctrl_blk_t CONCAT_2(_name, ctrl_blk); \
- static const nrf_libuarte_drv_t _name = { \
- .ctrl_blk = &CONCAT_2(_name, ctrl_blk), \
- .timer = NRFX_TIMER_INSTANCE(_timer_idx), \
- .uarte = CONCAT_2(NRF_UARTE, _uarte_idx),\
- }
- ret_code_t nrf_libuarte_drv_init(const nrf_libuarte_drv_t * const p_libuarte,
- nrf_libuarte_drv_config_t * p_config,
- nrf_libuarte_drv_evt_handler_t evt_handler, void * context);
- void nrf_libuarte_drv_uninit(const nrf_libuarte_drv_t * const p_libuarte);
- ret_code_t nrf_libuarte_drv_tx(const nrf_libuarte_drv_t * const p_libuarte,
- uint8_t * p_data, size_t len);
- ret_code_t nrf_libuarte_drv_rx_start(const nrf_libuarte_drv_t * const p_libuarte,
- uint8_t * p_data, size_t len, bool ext_trigger_en);
- void nrf_libuarte_drv_rx_buf_rsp(const nrf_libuarte_drv_t * const p_libuarte,
- uint8_t * p_data, size_t len);
- void nrf_libuarte_drv_rx_stop(const nrf_libuarte_drv_t * const p_libuarte);
- void nrf_libuarte_drv_rts_clear(const nrf_libuarte_drv_t * const p_libuarte);
- void nrf_libuarte_drv_rts_set(const nrf_libuarte_drv_t * const p_libuarte);
- #endif
|