123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- #ifndef NRF_DRV_SPI_H__
- #define NRF_DRV_SPI_H__
- #include <nrfx.h>
- #ifdef SPIM_PRESENT
- #include <nrfx_spim.h>
- #else
-
-
-
- #define nrfx_spim_init(...) 0
- #define nrfx_spim_uninit(...)
- #define nrfx_spim_start_task_get(...) 0
- #define nrfx_spim_end_event_get(...) 0
- #define nrfx_spim_abort(...)
- #endif
- #ifdef SPI_PRESENT
- #include <nrfx_spi.h>
- #else
-
-
-
- #define nrfx_spi_init(...) 0
- #define nrfx_spi_uninit(...)
- #define nrfx_spi_start_task_get(...) 0
- #define nrfx_spi_end_event_get(...) 0
- #define nrfx_spi_abort(...)
-
-
- #define NRF_SPI_FREQ_125K NRF_SPIM_FREQ_125K
- #define NRF_SPI_FREQ_250K NRF_SPIM_FREQ_250K
- #define NRF_SPI_FREQ_500K NRF_SPIM_FREQ_500K
- #define NRF_SPI_FREQ_1M NRF_SPIM_FREQ_1M
- #define NRF_SPI_FREQ_2M NRF_SPIM_FREQ_2M
- #define NRF_SPI_FREQ_4M NRF_SPIM_FREQ_4M
- #define NRF_SPI_FREQ_8M NRF_SPIM_FREQ_8M
- #define NRF_SPI_MODE_0 NRF_SPIM_MODE_0
- #define NRF_SPI_MODE_1 NRF_SPIM_MODE_1
- #define NRF_SPI_MODE_2 NRF_SPIM_MODE_2
- #define NRF_SPI_MODE_3 NRF_SPIM_MODE_3
- #define NRF_SPI_BIT_ORDER_MSB_FIRST NRF_SPIM_BIT_ORDER_MSB_FIRST
- #define NRF_SPI_BIT_ORDER_LSB_FIRST NRF_SPIM_BIT_ORDER_LSB_FIRST
- #endif
- #ifdef __cplusplus
- extern "C" {
- #endif
- typedef struct
- {
- uint8_t inst_idx;
- union
- {
- #ifdef SPIM_PRESENT
- nrfx_spim_t spim;
- #endif
- #ifdef SPI_PRESENT
- nrfx_spi_t spi;
- #endif
- } u;
- bool use_easy_dma;
- } nrf_drv_spi_t;
- #define NRF_DRV_SPI_INSTANCE(id) NRF_DRV_SPI_INSTANCE_(id)
- #define NRF_DRV_SPI_INSTANCE_(id) NRF_DRV_SPI_INSTANCE_ ## id
- #if NRFX_CHECK(NRFX_SPIM0_ENABLED)
- #define NRF_DRV_SPI_INSTANCE_0 \
- { 0, { .spim = NRFX_SPIM_INSTANCE(0) }, true }
- #elif NRFX_CHECK(NRFX_SPI0_ENABLED)
- #define NRF_DRV_SPI_INSTANCE_0 \
- { 0, { .spi = NRFX_SPI_INSTANCE(0) }, false }
- #endif
- #if NRFX_CHECK(NRFX_SPIM1_ENABLED)
- #define NRF_DRV_SPI_INSTANCE_1 \
- { 1, { .spim = NRFX_SPIM_INSTANCE(1) }, true }
- #elif NRFX_CHECK(NRFX_SPI1_ENABLED)
- #define NRF_DRV_SPI_INSTANCE_1 \
- { 1, { .spi = NRFX_SPI_INSTANCE(1) }, false }
- #endif
- #if NRFX_CHECK(NRFX_SPIM2_ENABLED)
- #define NRF_DRV_SPI_INSTANCE_2 \
- { 2, { .spim = NRFX_SPIM_INSTANCE(2) }, true }
- #elif NRFX_CHECK(NRFX_SPI2_ENABLED)
- #define NRF_DRV_SPI_INSTANCE_2 \
- { 2, { .spi = NRFX_SPI_INSTANCE(2) }, false }
- #endif
- #define NRF_DRV_SPI_PIN_NOT_USED 0xFF
- typedef enum
- {
- NRF_DRV_SPI_FREQ_125K = NRF_SPI_FREQ_125K,
- NRF_DRV_SPI_FREQ_250K = NRF_SPI_FREQ_250K,
- NRF_DRV_SPI_FREQ_500K = NRF_SPI_FREQ_500K,
- NRF_DRV_SPI_FREQ_1M = NRF_SPI_FREQ_1M,
- NRF_DRV_SPI_FREQ_2M = NRF_SPI_FREQ_2M,
- NRF_DRV_SPI_FREQ_4M = NRF_SPI_FREQ_4M,
- NRF_DRV_SPI_FREQ_8M = NRF_SPI_FREQ_8M
- } nrf_drv_spi_frequency_t;
- typedef enum
- {
- NRF_DRV_SPI_MODE_0 = NRF_SPI_MODE_0,
- NRF_DRV_SPI_MODE_1 = NRF_SPI_MODE_1,
- NRF_DRV_SPI_MODE_2 = NRF_SPI_MODE_2,
- NRF_DRV_SPI_MODE_3 = NRF_SPI_MODE_3
- } nrf_drv_spi_mode_t;
- typedef enum
- {
- NRF_DRV_SPI_BIT_ORDER_MSB_FIRST = NRF_SPI_BIT_ORDER_MSB_FIRST,
- NRF_DRV_SPI_BIT_ORDER_LSB_FIRST = NRF_SPI_BIT_ORDER_LSB_FIRST
- } nrf_drv_spi_bit_order_t;
- typedef struct
- {
- uint8_t sck_pin;
- uint8_t mosi_pin;
-
- uint8_t miso_pin;
-
- uint8_t ss_pin;
-
- uint8_t irq_priority;
- uint8_t orc;
-
- nrf_drv_spi_frequency_t frequency;
- nrf_drv_spi_mode_t mode;
- nrf_drv_spi_bit_order_t bit_order;
- } nrf_drv_spi_config_t;
- #define NRF_DRV_SPI_DEFAULT_CONFIG \
- { \
- .sck_pin = NRF_DRV_SPI_PIN_NOT_USED, \
- .mosi_pin = NRF_DRV_SPI_PIN_NOT_USED, \
- .miso_pin = NRF_DRV_SPI_PIN_NOT_USED, \
- .ss_pin = NRF_DRV_SPI_PIN_NOT_USED, \
- .irq_priority = SPI_DEFAULT_CONFIG_IRQ_PRIORITY, \
- .orc = 0xFF, \
- .frequency = NRF_DRV_SPI_FREQ_4M, \
- .mode = NRF_DRV_SPI_MODE_0, \
- .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, \
- }
- #define NRF_DRV_SPI_FLAG_TX_POSTINC (1UL << 0)
- #define NRF_DRV_SPI_FLAG_RX_POSTINC (1UL << 1)
- #define NRF_DRV_SPI_FLAG_NO_XFER_EVT_HANDLER (1UL << 2)
- #define NRF_DRV_SPI_FLAG_HOLD_XFER (1UL << 3)
- #define NRF_DRV_SPI_FLAG_REPEATED_XFER (1UL << 4)
- typedef struct
- {
- uint8_t const * p_tx_buffer;
- uint8_t tx_length;
- uint8_t * p_rx_buffer;
- uint8_t rx_length;
- }nrf_drv_spi_xfer_desc_t;
- #define NRF_DRV_SPI_SINGLE_XFER(p_tx, tx_len, p_rx, rx_len) \
- { \
- .p_tx_buffer = (uint8_t const *)(p_tx), \
- .tx_length = (tx_len), \
- .p_rx_buffer = (p_rx), \
- .rx_length = (rx_len), \
- }
- #define NRF_DRV_SPI_XFER_TRX(p_tx_buf, tx_length, p_rx_buf, rx_length) \
- NRF_DRV_SPI_SINGLE_XFER(p_tx_buf, tx_length, p_rx_buf, rx_length)
- #define NRF_DRV_SPI_XFER_TX(p_buf, length) \
- NRF_DRV_SPI_SINGLE_XFER(p_buf, length, NULL, 0)
- #define NRF_DRV_SPI_XFER_RX(p_buf, length) \
- NRF_DRV_SPI_SINGLE_XFER(NULL, 0, p_buf, length)
- typedef enum
- {
- NRF_DRV_SPI_EVENT_DONE,
- } nrf_drv_spi_evt_type_t;
- typedef struct
- {
- nrf_drv_spi_evt_type_t type;
- union
- {
- nrf_drv_spi_xfer_desc_t done;
- } data;
- } nrf_drv_spi_evt_t;
- typedef void (* nrf_drv_spi_evt_handler_t)(nrf_drv_spi_evt_t const * p_event,
- void * p_context);
- ret_code_t nrf_drv_spi_init(nrf_drv_spi_t const * const p_instance,
- nrf_drv_spi_config_t const * p_config,
- nrf_drv_spi_evt_handler_t handler,
- void * p_context);
- __STATIC_INLINE
- void nrf_drv_spi_uninit(nrf_drv_spi_t const * const p_instance);
- __STATIC_INLINE
- ret_code_t nrf_drv_spi_transfer(nrf_drv_spi_t const * const p_instance,
- uint8_t const * p_tx_buffer,
- uint8_t tx_buffer_length,
- uint8_t * p_rx_buffer,
- uint8_t rx_buffer_length);
- __STATIC_INLINE
- ret_code_t nrf_drv_spi_xfer(nrf_drv_spi_t const * const p_instance,
- nrf_drv_spi_xfer_desc_t const * p_xfer_desc,
- uint32_t flags);
- __STATIC_INLINE
- uint32_t nrf_drv_spi_start_task_get(nrf_drv_spi_t const * p_instance);
- __STATIC_INLINE
- uint32_t nrf_drv_spi_end_event_get(nrf_drv_spi_t const * p_instance);
- __STATIC_INLINE
- void nrf_drv_spi_abort(nrf_drv_spi_t const * p_instance);
- #ifndef SUPPRESS_INLINE_IMPLEMENTATION
- #if defined(SPI_PRESENT) && !defined(SPIM_PRESENT)
- #define NRF_DRV_SPI_WITH_SPI
- #elif !defined(SPI_PRESENT) && defined(SPIM_PRESENT)
- #define NRF_DRV_SPI_WITH_SPIM
- #else
- #if (NRFX_CHECK(SPI0_ENABLED) && NRFX_CHECK(SPI0_USE_EASY_DMA)) || \
- (NRFX_CHECK(SPI1_ENABLED) && NRFX_CHECK(SPI1_USE_EASY_DMA)) || \
- (NRFX_CHECK(SPI2_ENABLED) && NRFX_CHECK(SPI2_USE_EASY_DMA))
- #define NRF_DRV_SPI_WITH_SPIM
- #endif
- #if (NRFX_CHECK(SPI0_ENABLED) && !NRFX_CHECK(SPI0_USE_EASY_DMA)) || \
- (NRFX_CHECK(SPI1_ENABLED) && !NRFX_CHECK(SPI1_USE_EASY_DMA)) || \
- (NRFX_CHECK(SPI2_ENABLED) && !NRFX_CHECK(SPI2_USE_EASY_DMA))
- #define NRF_DRV_SPI_WITH_SPI
- #endif
- #endif
- #if defined(NRF_DRV_SPI_WITH_SPIM) && defined(NRF_DRV_SPI_WITH_SPI)
- #define NRF_DRV_SPI_USE_SPIM (p_instance->use_easy_dma)
- #elif defined(NRF_DRV_SPI_WITH_SPIM)
- #define NRF_DRV_SPI_USE_SPIM true
- #else
- #define NRF_DRV_SPI_USE_SPIM false
- #endif
- #define NRF_DRV_SPI_USE_SPI (!NRF_DRV_SPI_USE_SPIM)
- __STATIC_INLINE
- void nrf_drv_spi_uninit(nrf_drv_spi_t const * p_instance)
- {
- if (NRF_DRV_SPI_USE_SPIM)
- {
- nrfx_spim_uninit(&p_instance->u.spim);
- }
- else if (NRF_DRV_SPI_USE_SPI)
- {
- nrfx_spi_uninit(&p_instance->u.spi);
- }
- }
- __STATIC_INLINE
- ret_code_t nrf_drv_spi_transfer(nrf_drv_spi_t const * const p_instance,
- uint8_t const * p_tx_buffer,
- uint8_t tx_buffer_length,
- uint8_t * p_rx_buffer,
- uint8_t rx_buffer_length)
- {
- ret_code_t result = 0;
- if (NRF_DRV_SPI_USE_SPIM)
- {
- #ifdef SPIM_PRESENT
- nrfx_spim_xfer_desc_t const spim_xfer_desc =
- {
- .p_tx_buffer = p_tx_buffer,
- .tx_length = tx_buffer_length,
- .p_rx_buffer = p_rx_buffer,
- .rx_length = rx_buffer_length,
- };
- result = nrfx_spim_xfer(&p_instance->u.spim, &spim_xfer_desc, 0);
- #endif
- }
- else if (NRF_DRV_SPI_USE_SPI)
- {
- #ifdef SPI_PRESENT
- nrfx_spi_xfer_desc_t const spi_xfer_desc =
- {
- .p_tx_buffer = p_tx_buffer,
- .tx_length = tx_buffer_length,
- .p_rx_buffer = p_rx_buffer,
- .rx_length = rx_buffer_length,
- };
- result = nrfx_spi_xfer(&p_instance->u.spi, &spi_xfer_desc, 0);
- #endif
- }
- return result;
- }
- __STATIC_INLINE
- ret_code_t nrf_drv_spi_xfer(nrf_drv_spi_t const * const p_instance,
- nrf_drv_spi_xfer_desc_t const * p_xfer_desc,
- uint32_t flags)
- {
- ret_code_t result = 0;
- if (NRF_DRV_SPI_USE_SPIM)
- {
- #ifdef SPIM_PRESENT
- nrfx_spim_xfer_desc_t const spim_xfer_desc =
- {
- .p_tx_buffer = p_xfer_desc->p_tx_buffer,
- .tx_length = p_xfer_desc->tx_length,
- .p_rx_buffer = p_xfer_desc->p_rx_buffer,
- .rx_length = p_xfer_desc->rx_length,
- };
- result = nrfx_spim_xfer(&p_instance->u.spim, &spim_xfer_desc, flags);
- #endif
- }
- else if (NRF_DRV_SPI_USE_SPI)
- {
- #ifdef SPI_PRESENT
- nrfx_spi_xfer_desc_t const spi_xfer_desc =
- {
- .p_tx_buffer = p_xfer_desc->p_tx_buffer,
- .tx_length = p_xfer_desc->tx_length,
- .p_rx_buffer = p_xfer_desc->p_rx_buffer,
- .rx_length = p_xfer_desc->rx_length,
- };
- result = nrfx_spi_xfer(&p_instance->u.spi, &spi_xfer_desc, flags);
- #endif
- }
- return result;
- }
- __STATIC_INLINE
- uint32_t nrf_drv_spi_start_task_get(nrf_drv_spi_t const * p_instance)
- {
- uint32_t result = 0;
- if (NRF_DRV_SPI_USE_SPIM)
- {
- result = nrfx_spim_start_task_get(&p_instance->u.spim);
- }
- else if (NRF_DRV_SPI_USE_SPI)
- {
- NRFX_ASSERT(false);
- result = 0;
- }
- return result;
- }
- __STATIC_INLINE
- uint32_t nrf_drv_spi_end_event_get(nrf_drv_spi_t const * p_instance)
- {
- uint32_t result = 0;
- if (NRF_DRV_SPI_USE_SPIM)
- {
- result = nrfx_spim_end_event_get(&p_instance->u.spim);
- }
- else if (NRF_DRV_SPI_USE_SPI)
- {
- NRFX_ASSERT(false);
- result = 0;
- }
- return result;
- }
- __STATIC_INLINE
- void nrf_drv_spi_abort(nrf_drv_spi_t const * p_instance)
- {
- if (NRF_DRV_SPI_USE_SPIM)
- {
- nrfx_spim_abort(&p_instance->u.spim);
- }
- else if (NRF_DRV_SPI_USE_SPI)
- {
- nrfx_spi_abort(&p_instance->u.spi);
- }
- }
- #endif
- #ifdef __cplusplus
- }
- #endif
- #endif
|