nrf_drv_spi.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_DRV_SPI_H__
  41. #define NRF_DRV_SPI_H__
  42. #include <nrfx.h>
  43. #ifdef SPIM_PRESENT
  44. #include <nrfx_spim.h>
  45. #else
  46. // Compilers (at least the smart ones) will remove the SPIM related code
  47. // (blocks starting with "if (NRF_DRV_SPI_USE_SPIM)") when it is not used,
  48. // but to perform the compilation they need the following definitions.
  49. #define nrfx_spim_init(...) 0
  50. #define nrfx_spim_uninit(...)
  51. #define nrfx_spim_start_task_get(...) 0
  52. #define nrfx_spim_end_event_get(...) 0
  53. #define nrfx_spim_abort(...)
  54. #endif
  55. #ifdef SPI_PRESENT
  56. #include <nrfx_spi.h>
  57. #else
  58. // Compilers (at least the smart ones) will remove the SPI related code
  59. // (blocks starting with "if (NRF_DRV_SPI_USE_SPI)") when it is not used,
  60. // but to perform the compilation they need the following definitions.
  61. #define nrfx_spi_init(...) 0
  62. #define nrfx_spi_uninit(...)
  63. #define nrfx_spi_start_task_get(...) 0
  64. #define nrfx_spi_end_event_get(...) 0
  65. #define nrfx_spi_abort(...)
  66. // This part is for old modules that use directly SPI HAL definitions
  67. // (to make them compilable for chips that have only SPIM).
  68. #define NRF_SPI_FREQ_125K NRF_SPIM_FREQ_125K
  69. #define NRF_SPI_FREQ_250K NRF_SPIM_FREQ_250K
  70. #define NRF_SPI_FREQ_500K NRF_SPIM_FREQ_500K
  71. #define NRF_SPI_FREQ_1M NRF_SPIM_FREQ_1M
  72. #define NRF_SPI_FREQ_2M NRF_SPIM_FREQ_2M
  73. #define NRF_SPI_FREQ_4M NRF_SPIM_FREQ_4M
  74. #define NRF_SPI_FREQ_8M NRF_SPIM_FREQ_8M
  75. #define NRF_SPI_MODE_0 NRF_SPIM_MODE_0
  76. #define NRF_SPI_MODE_1 NRF_SPIM_MODE_1
  77. #define NRF_SPI_MODE_2 NRF_SPIM_MODE_2
  78. #define NRF_SPI_MODE_3 NRF_SPIM_MODE_3
  79. #define NRF_SPI_BIT_ORDER_MSB_FIRST NRF_SPIM_BIT_ORDER_MSB_FIRST
  80. #define NRF_SPI_BIT_ORDER_LSB_FIRST NRF_SPIM_BIT_ORDER_LSB_FIRST
  81. #endif
  82. #ifdef __cplusplus
  83. extern "C" {
  84. #endif
  85. /**
  86. * @defgroup nrf_drv_spi SPI master driver
  87. * @{
  88. * @ingroup nrf_spi
  89. * @brief Layer providing compatibility with the former API.
  90. */
  91. /**
  92. * @brief SPI master driver instance data structure.
  93. */
  94. typedef struct
  95. {
  96. uint8_t inst_idx;
  97. union
  98. {
  99. #ifdef SPIM_PRESENT
  100. nrfx_spim_t spim;
  101. #endif
  102. #ifdef SPI_PRESENT
  103. nrfx_spi_t spi;
  104. #endif
  105. } u;
  106. bool use_easy_dma;
  107. } nrf_drv_spi_t;
  108. /**
  109. * @brief Macro for creating an SPI master driver instance.
  110. */
  111. #define NRF_DRV_SPI_INSTANCE(id) NRF_DRV_SPI_INSTANCE_(id)
  112. #define NRF_DRV_SPI_INSTANCE_(id) NRF_DRV_SPI_INSTANCE_ ## id
  113. #if NRFX_CHECK(NRFX_SPIM0_ENABLED)
  114. #define NRF_DRV_SPI_INSTANCE_0 \
  115. { 0, { .spim = NRFX_SPIM_INSTANCE(0) }, true }
  116. #elif NRFX_CHECK(NRFX_SPI0_ENABLED)
  117. #define NRF_DRV_SPI_INSTANCE_0 \
  118. { 0, { .spi = NRFX_SPI_INSTANCE(0) }, false }
  119. #endif
  120. #if NRFX_CHECK(NRFX_SPIM1_ENABLED)
  121. #define NRF_DRV_SPI_INSTANCE_1 \
  122. { 1, { .spim = NRFX_SPIM_INSTANCE(1) }, true }
  123. #elif NRFX_CHECK(NRFX_SPI1_ENABLED)
  124. #define NRF_DRV_SPI_INSTANCE_1 \
  125. { 1, { .spi = NRFX_SPI_INSTANCE(1) }, false }
  126. #endif
  127. #if NRFX_CHECK(NRFX_SPIM2_ENABLED)
  128. #define NRF_DRV_SPI_INSTANCE_2 \
  129. { 2, { .spim = NRFX_SPIM_INSTANCE(2) }, true }
  130. #elif NRFX_CHECK(NRFX_SPI2_ENABLED)
  131. #define NRF_DRV_SPI_INSTANCE_2 \
  132. { 2, { .spi = NRFX_SPI_INSTANCE(2) }, false }
  133. #endif
  134. /**
  135. * @brief This value can be provided instead of a pin number for signals MOSI,
  136. * MISO, and Slave Select to specify that the given signal is not used and
  137. * therefore does not need to be connected to a pin.
  138. */
  139. #define NRF_DRV_SPI_PIN_NOT_USED 0xFF
  140. /**
  141. * @brief SPI data rates.
  142. */
  143. typedef enum
  144. {
  145. NRF_DRV_SPI_FREQ_125K = NRF_SPI_FREQ_125K, ///< 125 kbps.
  146. NRF_DRV_SPI_FREQ_250K = NRF_SPI_FREQ_250K, ///< 250 kbps.
  147. NRF_DRV_SPI_FREQ_500K = NRF_SPI_FREQ_500K, ///< 500 kbps.
  148. NRF_DRV_SPI_FREQ_1M = NRF_SPI_FREQ_1M, ///< 1 Mbps.
  149. NRF_DRV_SPI_FREQ_2M = NRF_SPI_FREQ_2M, ///< 2 Mbps.
  150. NRF_DRV_SPI_FREQ_4M = NRF_SPI_FREQ_4M, ///< 4 Mbps.
  151. NRF_DRV_SPI_FREQ_8M = NRF_SPI_FREQ_8M ///< 8 Mbps.
  152. } nrf_drv_spi_frequency_t;
  153. /**
  154. * @brief SPI modes.
  155. */
  156. typedef enum
  157. {
  158. NRF_DRV_SPI_MODE_0 = NRF_SPI_MODE_0, ///< SCK active high, sample on leading edge of clock.
  159. NRF_DRV_SPI_MODE_1 = NRF_SPI_MODE_1, ///< SCK active high, sample on trailing edge of clock.
  160. NRF_DRV_SPI_MODE_2 = NRF_SPI_MODE_2, ///< SCK active low, sample on leading edge of clock.
  161. NRF_DRV_SPI_MODE_3 = NRF_SPI_MODE_3 ///< SCK active low, sample on trailing edge of clock.
  162. } nrf_drv_spi_mode_t;
  163. /**
  164. * @brief SPI bit orders.
  165. */
  166. typedef enum
  167. {
  168. NRF_DRV_SPI_BIT_ORDER_MSB_FIRST = NRF_SPI_BIT_ORDER_MSB_FIRST, ///< Most significant bit shifted out first.
  169. NRF_DRV_SPI_BIT_ORDER_LSB_FIRST = NRF_SPI_BIT_ORDER_LSB_FIRST ///< Least significant bit shifted out first.
  170. } nrf_drv_spi_bit_order_t;
  171. /**
  172. * @brief SPI master driver instance configuration structure.
  173. */
  174. typedef struct
  175. {
  176. uint8_t sck_pin; ///< SCK pin number.
  177. uint8_t mosi_pin; ///< MOSI pin number (optional).
  178. /**< Set to @ref NRF_DRV_SPI_PIN_NOT_USED
  179. * if this signal is not needed. */
  180. uint8_t miso_pin; ///< MISO pin number (optional).
  181. /**< Set to @ref NRF_DRV_SPI_PIN_NOT_USED
  182. * if this signal is not needed. */
  183. uint8_t ss_pin; ///< Slave Select pin number (optional).
  184. /**< Set to @ref NRF_DRV_SPI_PIN_NOT_USED
  185. * if this signal is not needed. The driver
  186. * supports only active low for this signal.
  187. * If the signal should be active high,
  188. * it must be controlled externally. */
  189. uint8_t irq_priority; ///< Interrupt priority.
  190. uint8_t orc; ///< Over-run character.
  191. /**< This character is used when all bytes from the TX buffer are sent,
  192. but the transfer continues due to RX. */
  193. nrf_drv_spi_frequency_t frequency; ///< SPI frequency.
  194. nrf_drv_spi_mode_t mode; ///< SPI mode.
  195. nrf_drv_spi_bit_order_t bit_order; ///< SPI bit order.
  196. } nrf_drv_spi_config_t;
  197. /**
  198. * @brief SPI master instance default configuration.
  199. */
  200. #define NRF_DRV_SPI_DEFAULT_CONFIG \
  201. { \
  202. .sck_pin = NRF_DRV_SPI_PIN_NOT_USED, \
  203. .mosi_pin = NRF_DRV_SPI_PIN_NOT_USED, \
  204. .miso_pin = NRF_DRV_SPI_PIN_NOT_USED, \
  205. .ss_pin = NRF_DRV_SPI_PIN_NOT_USED, \
  206. .irq_priority = SPI_DEFAULT_CONFIG_IRQ_PRIORITY, \
  207. .orc = 0xFF, \
  208. .frequency = NRF_DRV_SPI_FREQ_4M, \
  209. .mode = NRF_DRV_SPI_MODE_0, \
  210. .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, \
  211. }
  212. #define NRF_DRV_SPI_FLAG_TX_POSTINC (1UL << 0) /**< TX buffer address incremented after transfer. */
  213. #define NRF_DRV_SPI_FLAG_RX_POSTINC (1UL << 1) /**< RX buffer address incremented after transfer. */
  214. #define NRF_DRV_SPI_FLAG_NO_XFER_EVT_HANDLER (1UL << 2) /**< Interrupt after each transfer is suppressed, and the event handler is not called. */
  215. #define NRF_DRV_SPI_FLAG_HOLD_XFER (1UL << 3) /**< Set up the transfer but do not start it. */
  216. #define NRF_DRV_SPI_FLAG_REPEATED_XFER (1UL << 4) /**< Flag indicating that the transfer will be executed multiple times. */
  217. /**
  218. * @brief Single transfer descriptor structure.
  219. */
  220. typedef struct
  221. {
  222. uint8_t const * p_tx_buffer; ///< Pointer to TX buffer.
  223. uint8_t tx_length; ///< TX buffer length.
  224. uint8_t * p_rx_buffer; ///< Pointer to RX buffer.
  225. uint8_t rx_length; ///< RX buffer length.
  226. }nrf_drv_spi_xfer_desc_t;
  227. /**
  228. * @brief Macro for setting up single transfer descriptor.
  229. *
  230. * This macro is for internal use only.
  231. */
  232. #define NRF_DRV_SPI_SINGLE_XFER(p_tx, tx_len, p_rx, rx_len) \
  233. { \
  234. .p_tx_buffer = (uint8_t const *)(p_tx), \
  235. .tx_length = (tx_len), \
  236. .p_rx_buffer = (p_rx), \
  237. .rx_length = (rx_len), \
  238. }
  239. /**
  240. * @brief Macro for setting duplex TX RX transfer.
  241. */
  242. #define NRF_DRV_SPI_XFER_TRX(p_tx_buf, tx_length, p_rx_buf, rx_length) \
  243. NRF_DRV_SPI_SINGLE_XFER(p_tx_buf, tx_length, p_rx_buf, rx_length)
  244. /**
  245. * @brief Macro for setting TX transfer.
  246. */
  247. #define NRF_DRV_SPI_XFER_TX(p_buf, length) \
  248. NRF_DRV_SPI_SINGLE_XFER(p_buf, length, NULL, 0)
  249. /**
  250. * @brief Macro for setting RX transfer.
  251. */
  252. #define NRF_DRV_SPI_XFER_RX(p_buf, length) \
  253. NRF_DRV_SPI_SINGLE_XFER(NULL, 0, p_buf, length)
  254. /**
  255. * @brief SPI master driver event types, passed to the handler routine provided
  256. * during initialization.
  257. */
  258. typedef enum
  259. {
  260. NRF_DRV_SPI_EVENT_DONE, ///< Transfer done.
  261. } nrf_drv_spi_evt_type_t;
  262. typedef struct
  263. {
  264. nrf_drv_spi_evt_type_t type; ///< Event type.
  265. union
  266. {
  267. nrf_drv_spi_xfer_desc_t done; ///< Event data for DONE event.
  268. } data;
  269. } nrf_drv_spi_evt_t;
  270. /**
  271. * @brief SPI master driver event handler type.
  272. */
  273. typedef void (* nrf_drv_spi_evt_handler_t)(nrf_drv_spi_evt_t const * p_event,
  274. void * p_context);
  275. /**
  276. * @brief Function for initializing the SPI master driver instance.
  277. *
  278. * This function configures and enables the specified peripheral.
  279. *
  280. * @note MISO pin has pull down enabled.
  281. *
  282. * @param[in] p_instance Pointer to the driver instance structure.
  283. * @param[in] p_config Pointer to the structure with the initial configuration.
  284. *
  285. * @param handler Event handler provided by the user. If NULL, transfers
  286. * will be performed in blocking mode.
  287. * @param p_context Context passed to event handler.
  288. *
  289. * @retval NRF_SUCCESS If initialization was successful.
  290. * @retval NRF_ERROR_INVALID_STATE If the driver was already initialized.
  291. * @retval NRF_ERROR_BUSY If some other peripheral with the same
  292. * instance ID is already in use. This is
  293. * possible only if PERIPHERAL_RESOURCE_SHARING_ENABLED
  294. * is set to a value other than zero.
  295. */
  296. ret_code_t nrf_drv_spi_init(nrf_drv_spi_t const * const p_instance,
  297. nrf_drv_spi_config_t const * p_config,
  298. nrf_drv_spi_evt_handler_t handler,
  299. void * p_context);
  300. /**
  301. * @brief Function for uninitializing the SPI master driver instance.
  302. *
  303. * @note Configuration of pins is kept.
  304. *
  305. * @param[in] p_instance Pointer to the driver instance structure.
  306. */
  307. __STATIC_INLINE
  308. void nrf_drv_spi_uninit(nrf_drv_spi_t const * const p_instance);
  309. /**
  310. * @brief Function for starting the SPI data transfer.
  311. *
  312. * If an event handler was provided in the @ref nrf_drv_spi_init call, this function
  313. * returns immediately and the handler is called when the transfer is done.
  314. * Otherwise, the transfer is performed in blocking mode, which means that this function
  315. * returns when the transfer is finished.
  316. *
  317. * @note Peripherals using EasyDMA (for example, SPIM) require the transfer buffers
  318. * to be placed in the Data RAM region. If they are not and an SPIM instance is
  319. * used, this function will fail with the error code NRF_ERROR_INVALID_ADDR.
  320. *
  321. * @param[in] p_instance Pointer to the driver instance structure.
  322. * @param[in] p_tx_buffer Pointer to the transmit buffer. Can be NULL
  323. * if there is nothing to send.
  324. * @param tx_buffer_length Length of the transmit buffer.
  325. * @param[in] p_rx_buffer Pointer to the receive buffer. Can be NULL
  326. * if there is nothing to receive.
  327. * @param rx_buffer_length Length of the receive buffer.
  328. *
  329. * @retval NRF_SUCCESS If the operation was successful.
  330. * @retval NRF_ERROR_BUSY If a previously started transfer has not finished
  331. * yet.
  332. * @retval NRF_ERROR_INVALID_ADDR If the provided buffers are not placed in the Data
  333. * RAM region.
  334. */
  335. __STATIC_INLINE
  336. ret_code_t nrf_drv_spi_transfer(nrf_drv_spi_t const * const p_instance,
  337. uint8_t const * p_tx_buffer,
  338. uint8_t tx_buffer_length,
  339. uint8_t * p_rx_buffer,
  340. uint8_t rx_buffer_length);
  341. /**
  342. * @brief Function for starting the SPI data transfer with additional option flags.
  343. *
  344. * Function enables customizing the transfer by using option flags.
  345. *
  346. * Additional options are provided using the flags parameter:
  347. *
  348. * - @ref NRF_DRV_SPI_FLAG_TX_POSTINC and @ref NRF_DRV_SPI_FLAG_RX_POSTINC<span></span>:
  349. * Post-incrementation of buffer addresses. Supported only by SPIM.
  350. * - @ref NRF_DRV_SPI_FLAG_HOLD_XFER<span></span>: Driver is not starting the transfer. Use this
  351. * flag if the transfer is triggered externally by PPI. Supported only by SPIM. Use
  352. * @ref nrf_drv_spi_start_task_get to get the address of the start task.
  353. * - @ref NRF_DRV_SPI_FLAG_NO_XFER_EVT_HANDLER<span></span>: No user event handler after transfer
  354. * completion. This also means no interrupt at the end of the transfer. Supported only by SPIM.
  355. * If @ref NRF_DRV_SPI_FLAG_NO_XFER_EVT_HANDLER is used, the driver does not set the instance into
  356. * busy state, so you must ensure that the next transfers are set up when SPIM is not active.
  357. * @ref nrf_drv_spi_end_event_get function can be used to detect end of transfer. Option can be used
  358. * together with @ref NRF_DRV_SPI_FLAG_REPEATED_XFER to prepare a sequence of SPI transfers
  359. * without interruptions.
  360. * - @ref NRF_DRV_SPI_FLAG_REPEATED_XFER<span></span>: Prepare for repeated transfers. You can set
  361. * up a number of transfers that will be triggered externally (for example by PPI). An example is
  362. * a TXRX transfer with the options @ref NRF_DRV_SPI_FLAG_RX_POSTINC,
  363. * @ref NRF_DRV_SPI_FLAG_NO_XFER_EVT_HANDLER, and @ref NRF_DRV_SPI_FLAG_REPEATED_XFER. After the
  364. * transfer is set up, a set of transfers can be triggered by PPI that will read, for example,
  365. * the same register of an external component and put it into a RAM buffer without any interrupts.
  366. * @ref nrf_drv_spi_end_event_get can be used to get the address of the END event, which can be
  367. * used to count the number of transfers. If @ref NRF_DRV_SPI_FLAG_REPEATED_XFER is used,
  368. * the driver does not set the instance into busy state, so you must ensure that the next
  369. * transfers are set up when SPIM is not active. Supported only by SPIM.
  370. * @note Function is intended to be used only in non-blocking mode.
  371. *
  372. * @param p_instance Pointer to the driver instance structure.
  373. * @param p_xfer_desc Pointer to the transfer descriptor.
  374. * @param flags Transfer options (0 for default settings).
  375. *
  376. * @retval NRF_SUCCESS If the procedure was successful.
  377. * @retval NRF_ERROR_BUSY If the driver is not ready for a new transfer.
  378. * @retval NRF_ERROR_NOT_SUPPORTED If the provided parameters are not supported.
  379. * @retval NRF_ERROR_INVALID_ADDR If the provided buffers are not placed in the Data
  380. * RAM region.
  381. */
  382. __STATIC_INLINE
  383. ret_code_t nrf_drv_spi_xfer(nrf_drv_spi_t const * const p_instance,
  384. nrf_drv_spi_xfer_desc_t const * p_xfer_desc,
  385. uint32_t flags);
  386. /**
  387. * @brief Function for returning the address of a SPIM start task.
  388. *
  389. * This function should be used if @ref nrf_drv_spi_xfer was called with the flag @ref NRF_DRV_SPI_FLAG_HOLD_XFER.
  390. * In that case, the transfer is not started by the driver, but it must be started externally by PPI.
  391. *
  392. * @param[in] p_instance Pointer to the driver instance structure.
  393. *
  394. * @return Start task address.
  395. */
  396. __STATIC_INLINE
  397. uint32_t nrf_drv_spi_start_task_get(nrf_drv_spi_t const * p_instance);
  398. /**
  399. * @brief Function for returning the address of a END SPIM event.
  400. *
  401. * A END event can be used to detect the end of a transfer if the @ref NRF_DRV_SPI_FLAG_NO_XFER_EVT_HANDLER
  402. * option is used.
  403. *
  404. * @param[in] p_instance Pointer to the driver instance structure.
  405. *
  406. * @return END event address.
  407. */
  408. __STATIC_INLINE
  409. uint32_t nrf_drv_spi_end_event_get(nrf_drv_spi_t const * p_instance);
  410. /**
  411. * @brief Function for aborting ongoing transfer.
  412. *
  413. * @param[in] p_instance Pointer to the driver instance structure.
  414. */
  415. __STATIC_INLINE
  416. void nrf_drv_spi_abort(nrf_drv_spi_t const * p_instance);
  417. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  418. #if defined(SPI_PRESENT) && !defined(SPIM_PRESENT)
  419. #define NRF_DRV_SPI_WITH_SPI
  420. #elif !defined(SPI_PRESENT) && defined(SPIM_PRESENT)
  421. #define NRF_DRV_SPI_WITH_SPIM
  422. #else
  423. #if (NRFX_CHECK(SPI0_ENABLED) && NRFX_CHECK(SPI0_USE_EASY_DMA)) || \
  424. (NRFX_CHECK(SPI1_ENABLED) && NRFX_CHECK(SPI1_USE_EASY_DMA)) || \
  425. (NRFX_CHECK(SPI2_ENABLED) && NRFX_CHECK(SPI2_USE_EASY_DMA))
  426. #define NRF_DRV_SPI_WITH_SPIM
  427. #endif
  428. #if (NRFX_CHECK(SPI0_ENABLED) && !NRFX_CHECK(SPI0_USE_EASY_DMA)) || \
  429. (NRFX_CHECK(SPI1_ENABLED) && !NRFX_CHECK(SPI1_USE_EASY_DMA)) || \
  430. (NRFX_CHECK(SPI2_ENABLED) && !NRFX_CHECK(SPI2_USE_EASY_DMA))
  431. #define NRF_DRV_SPI_WITH_SPI
  432. #endif
  433. #endif
  434. #if defined(NRF_DRV_SPI_WITH_SPIM) && defined(NRF_DRV_SPI_WITH_SPI)
  435. #define NRF_DRV_SPI_USE_SPIM (p_instance->use_easy_dma)
  436. #elif defined(NRF_DRV_SPI_WITH_SPIM)
  437. #define NRF_DRV_SPI_USE_SPIM true
  438. #else
  439. #define NRF_DRV_SPI_USE_SPIM false
  440. #endif
  441. #define NRF_DRV_SPI_USE_SPI (!NRF_DRV_SPI_USE_SPIM)
  442. __STATIC_INLINE
  443. void nrf_drv_spi_uninit(nrf_drv_spi_t const * p_instance)
  444. {
  445. if (NRF_DRV_SPI_USE_SPIM)
  446. {
  447. nrfx_spim_uninit(&p_instance->u.spim);
  448. }
  449. else if (NRF_DRV_SPI_USE_SPI)
  450. {
  451. nrfx_spi_uninit(&p_instance->u.spi);
  452. }
  453. }
  454. __STATIC_INLINE
  455. ret_code_t nrf_drv_spi_transfer(nrf_drv_spi_t const * const p_instance,
  456. uint8_t const * p_tx_buffer,
  457. uint8_t tx_buffer_length,
  458. uint8_t * p_rx_buffer,
  459. uint8_t rx_buffer_length)
  460. {
  461. ret_code_t result = 0;
  462. if (NRF_DRV_SPI_USE_SPIM)
  463. {
  464. #ifdef SPIM_PRESENT
  465. nrfx_spim_xfer_desc_t const spim_xfer_desc =
  466. {
  467. .p_tx_buffer = p_tx_buffer,
  468. .tx_length = tx_buffer_length,
  469. .p_rx_buffer = p_rx_buffer,
  470. .rx_length = rx_buffer_length,
  471. };
  472. result = nrfx_spim_xfer(&p_instance->u.spim, &spim_xfer_desc, 0);
  473. #endif
  474. }
  475. else if (NRF_DRV_SPI_USE_SPI)
  476. {
  477. #ifdef SPI_PRESENT
  478. nrfx_spi_xfer_desc_t const spi_xfer_desc =
  479. {
  480. .p_tx_buffer = p_tx_buffer,
  481. .tx_length = tx_buffer_length,
  482. .p_rx_buffer = p_rx_buffer,
  483. .rx_length = rx_buffer_length,
  484. };
  485. result = nrfx_spi_xfer(&p_instance->u.spi, &spi_xfer_desc, 0);
  486. #endif
  487. }
  488. return result;
  489. }
  490. __STATIC_INLINE
  491. ret_code_t nrf_drv_spi_xfer(nrf_drv_spi_t const * const p_instance,
  492. nrf_drv_spi_xfer_desc_t const * p_xfer_desc,
  493. uint32_t flags)
  494. {
  495. ret_code_t result = 0;
  496. if (NRF_DRV_SPI_USE_SPIM)
  497. {
  498. #ifdef SPIM_PRESENT
  499. nrfx_spim_xfer_desc_t const spim_xfer_desc =
  500. {
  501. .p_tx_buffer = p_xfer_desc->p_tx_buffer,
  502. .tx_length = p_xfer_desc->tx_length,
  503. .p_rx_buffer = p_xfer_desc->p_rx_buffer,
  504. .rx_length = p_xfer_desc->rx_length,
  505. };
  506. result = nrfx_spim_xfer(&p_instance->u.spim, &spim_xfer_desc, flags);
  507. #endif
  508. }
  509. else if (NRF_DRV_SPI_USE_SPI)
  510. {
  511. #ifdef SPI_PRESENT
  512. nrfx_spi_xfer_desc_t const spi_xfer_desc =
  513. {
  514. .p_tx_buffer = p_xfer_desc->p_tx_buffer,
  515. .tx_length = p_xfer_desc->tx_length,
  516. .p_rx_buffer = p_xfer_desc->p_rx_buffer,
  517. .rx_length = p_xfer_desc->rx_length,
  518. };
  519. result = nrfx_spi_xfer(&p_instance->u.spi, &spi_xfer_desc, flags);
  520. #endif
  521. }
  522. return result;
  523. }
  524. __STATIC_INLINE
  525. uint32_t nrf_drv_spi_start_task_get(nrf_drv_spi_t const * p_instance)
  526. {
  527. uint32_t result = 0;
  528. if (NRF_DRV_SPI_USE_SPIM)
  529. {
  530. result = nrfx_spim_start_task_get(&p_instance->u.spim);
  531. }
  532. else if (NRF_DRV_SPI_USE_SPI)
  533. {
  534. NRFX_ASSERT(false); // not supported
  535. result = 0;
  536. }
  537. return result;
  538. }
  539. __STATIC_INLINE
  540. uint32_t nrf_drv_spi_end_event_get(nrf_drv_spi_t const * p_instance)
  541. {
  542. uint32_t result = 0;
  543. if (NRF_DRV_SPI_USE_SPIM)
  544. {
  545. result = nrfx_spim_end_event_get(&p_instance->u.spim);
  546. }
  547. else if (NRF_DRV_SPI_USE_SPI)
  548. {
  549. NRFX_ASSERT(false); // not supported
  550. result = 0;
  551. }
  552. return result;
  553. }
  554. __STATIC_INLINE
  555. void nrf_drv_spi_abort(nrf_drv_spi_t const * p_instance)
  556. {
  557. if (NRF_DRV_SPI_USE_SPIM)
  558. {
  559. nrfx_spim_abort(&p_instance->u.spim);
  560. }
  561. else if (NRF_DRV_SPI_USE_SPI)
  562. {
  563. nrfx_spi_abort(&p_instance->u.spi);
  564. }
  565. }
  566. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  567. /** @} */
  568. #ifdef __cplusplus
  569. }
  570. #endif
  571. #endif // NRF_DRV_SPI_H__