nrfx_spim.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 NRFX_SPIM_H__
  41. #define NRFX_SPIM_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_spim.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_spim SPIM driver
  49. * @{
  50. * @ingroup nrf_spim
  51. * @brief SPIM peripheral driver.
  52. */
  53. /**
  54. * @brief SPIM master driver instance data structure.
  55. */
  56. typedef struct
  57. {
  58. NRF_SPIM_Type * p_reg; ///< Pointer to a structure with SPIM registers.
  59. uint8_t drv_inst_idx; ///< Driver instance index.
  60. } nrfx_spim_t;
  61. enum {
  62. #if NRFX_CHECK(NRFX_SPIM0_ENABLED)
  63. NRFX_SPIM0_INST_IDX,
  64. #endif
  65. #if NRFX_CHECK(NRFX_SPIM1_ENABLED)
  66. NRFX_SPIM1_INST_IDX,
  67. #endif
  68. #if NRFX_CHECK(NRFX_SPIM2_ENABLED)
  69. NRFX_SPIM2_INST_IDX,
  70. #endif
  71. #if NRFX_CHECK(NRFX_SPIM3_ENABLED)
  72. NRFX_SPIM3_INST_IDX,
  73. #endif
  74. NRFX_SPIM_ENABLED_COUNT
  75. };
  76. /**
  77. * @brief Macro for creating an SPIM master driver instance.
  78. */
  79. #define NRFX_SPIM_INSTANCE(id) \
  80. { \
  81. .p_reg = NRFX_CONCAT_2(NRF_SPIM, id), \
  82. .drv_inst_idx = NRFX_CONCAT_3(NRFX_SPIM, id, _INST_IDX), \
  83. }
  84. /**
  85. * @brief This value can be provided instead of a pin number for signals MOSI,
  86. * MISO, and Slave Select to specify that the given signal is not used and
  87. * therefore does not need to be connected to a pin.
  88. */
  89. #define NRFX_SPIM_PIN_NOT_USED 0xFF
  90. /**
  91. * @brief SPIM master driver instance configuration structure.
  92. */
  93. typedef struct
  94. {
  95. uint8_t sck_pin; ///< SCK pin number.
  96. uint8_t mosi_pin; ///< MOSI pin number (optional).
  97. /**< Set to @ref NRFX_SPIM_PIN_NOT_USED
  98. * if this signal is not needed. */
  99. uint8_t miso_pin; ///< MISO pin number (optional).
  100. /**< Set to @ref NRFX_SPIM_PIN_NOT_USED
  101. * if this signal is not needed. */
  102. uint8_t ss_pin; ///< Slave Select pin number (optional).
  103. /**< Set to @ref NRFX_SPIM_PIN_NOT_USED
  104. * if this signal is not needed. */
  105. bool ss_active_high; ///< Polarity of the Slave Select pin during transmission.
  106. uint8_t irq_priority; ///< Interrupt priority.
  107. uint8_t orc; ///< Over-run character.
  108. /**< This character is used when all bytes from the TX buffer are sent,
  109. but the transfer continues due to RX. */
  110. nrf_spim_frequency_t frequency; ///< SPI frequency.
  111. nrf_spim_mode_t mode; ///< SPI mode.
  112. nrf_spim_bit_order_t bit_order; ///< SPI bit order.
  113. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED) || defined(__NRFX_DOXYGEN__)
  114. uint8_t dcx_pin; ///< D/CX pin number (optional).
  115. uint8_t rx_delay; ///< Sample delay for input serial data on MISO.
  116. /**< The value specifies the delay, in number of 64 MHz clock cycles
  117. * (15.625 ns), from the the sampling edge of SCK (leading edge for
  118. * CONFIG.CPHA = 0, trailing edge for CONFIG.CPHA = 1) until
  119. * the input serial data is sampled.*/
  120. bool use_hw_ss; ///< Indication to use software or hardware controlled Slave Select pin.
  121. uint8_t ss_duration; ///< Slave Select duration before and after transmission.
  122. /**< Minimum duration between the edge of CSN and the edge of SCK and minimum
  123. * duration of CSN must stay inactive between transactions.
  124. * The value is specified in number of 64 MHz clock cycles (15.625 ns).
  125. * Supported only for hardware controlled Slave Select.*/
  126. #endif
  127. } nrfx_spim_config_t;
  128. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED) || defined(__NRFX_DOXYGEN__)
  129. /**
  130. * @brief SPIM master instance extended default configuration.
  131. */
  132. #define NRFX_SPIM_DEFAULT_EXTENDED_CONFIG \
  133. .dcx_pin = NRFX_SPIM_PIN_NOT_USED, \
  134. .rx_delay = 0x02, \
  135. .ss_duration = 0x02, \
  136. .use_hw_ss = false,
  137. #else
  138. #define NRFX_SPIM_DEFAULT_EXTENDED_CONFIG
  139. #endif
  140. /**
  141. * @brief SPIM master instance default configuration.
  142. */
  143. #define NRFX_SPIM_DEFAULT_CONFIG \
  144. { \
  145. .sck_pin = NRFX_SPIM_PIN_NOT_USED, \
  146. .mosi_pin = NRFX_SPIM_PIN_NOT_USED, \
  147. .miso_pin = NRFX_SPIM_PIN_NOT_USED, \
  148. .ss_pin = NRFX_SPIM_PIN_NOT_USED, \
  149. .ss_active_high = false, \
  150. .irq_priority = NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY, \
  151. .orc = 0xFF, \
  152. .frequency = NRF_SPIM_FREQ_4M, \
  153. .mode = NRF_SPIM_MODE_0, \
  154. .bit_order = NRF_SPIM_BIT_ORDER_MSB_FIRST, \
  155. NRFX_SPIM_DEFAULT_EXTENDED_CONFIG \
  156. }
  157. #define NRFX_SPIM_FLAG_TX_POSTINC (1UL << 0) /**< TX buffer address incremented after transfer. */
  158. #define NRFX_SPIM_FLAG_RX_POSTINC (1UL << 1) /**< RX buffer address incremented after transfer. */
  159. #define NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER (1UL << 2) /**< Interrupt after each transfer is suppressed, and the event handler is not called. */
  160. #define NRFX_SPIM_FLAG_HOLD_XFER (1UL << 3) /**< Set up the transfer but do not start it. */
  161. #define NRFX_SPIM_FLAG_REPEATED_XFER (1UL << 4) /**< Flag indicating that the transfer will be executed multiple times. */
  162. /**
  163. * @brief Single transfer descriptor structure.
  164. */
  165. typedef struct
  166. {
  167. uint8_t const * p_tx_buffer; ///< Pointer to TX buffer.
  168. size_t tx_length; ///< TX buffer length.
  169. uint8_t * p_rx_buffer; ///< Pointer to RX buffer.
  170. size_t rx_length; ///< RX buffer length.
  171. } nrfx_spim_xfer_desc_t;
  172. /**
  173. * @brief Macro for setting up single transfer descriptor.
  174. *
  175. * This macro is for internal use only.
  176. */
  177. #define NRFX_SPIM_SINGLE_XFER(p_tx, tx_len, p_rx, rx_len) \
  178. { \
  179. .p_tx_buffer = (uint8_t const *)(p_tx), \
  180. .tx_length = (tx_len), \
  181. .p_rx_buffer = (p_rx), \
  182. .rx_length = (rx_len), \
  183. }
  184. /**
  185. * @brief Macro for setting duplex TX RX transfer.
  186. */
  187. #define NRFX_SPIM_XFER_TRX(p_tx_buf, tx_length, p_rx_buf, rx_length) \
  188. NRFX_SPIM_SINGLE_XFER(p_tx_buf, tx_length, p_rx_buf, rx_length)
  189. /**
  190. * @brief Macro for setting TX transfer.
  191. */
  192. #define NRFX_SPIM_XFER_TX(p_buf, length) \
  193. NRFX_SPIM_SINGLE_XFER(p_buf, length, NULL, 0)
  194. /**
  195. * @brief Macro for setting RX transfer.
  196. */
  197. #define NRFX_SPIM_XFER_RX(p_buf, length) \
  198. NRFX_SPIM_SINGLE_XFER(NULL, 0, p_buf, length)
  199. /**
  200. * @brief SPIM master driver event types, passed to the handler routine provided
  201. * during initialization.
  202. */
  203. typedef enum
  204. {
  205. NRFX_SPIM_EVENT_DONE, ///< Transfer done.
  206. } nrfx_spim_evt_type_t;
  207. typedef struct
  208. {
  209. nrfx_spim_evt_type_t type; ///< Event type.
  210. nrfx_spim_xfer_desc_t xfer_desc; ///< Transfer details.
  211. } nrfx_spim_evt_t;
  212. /**
  213. * @brief SPIM master driver event handler type.
  214. */
  215. typedef void (* nrfx_spim_evt_handler_t)(nrfx_spim_evt_t const * p_event,
  216. void * p_context);
  217. /**
  218. * @brief Function for initializing the SPI master driver instance.
  219. *
  220. * This function configures and enables the specified peripheral.
  221. *
  222. * @param[in] p_instance Pointer to the driver instance structure.
  223. * @param[in] p_config Pointer to the structure with initial configuration.
  224. *
  225. * @param handler Event handler provided by the user. If NULL, transfers
  226. * will be performed in blocking mode.
  227. * @param p_context Context passed to event handler.
  228. *
  229. * @retval NRFX_SUCCESS If initialization was successful.
  230. * @retval NRFX_ERROR_INVALID_STATE If the driver was already initialized.
  231. * @retval NRFX_ERROR_BUSY If some other peripheral with the same
  232. * instance ID is already in use. This is
  233. * possible only if @ref nrfx_prs module
  234. * is enabled.
  235. * @retval NRFX_ERROR_NOT_SUPPORTED If requested configuration is not supported
  236. * by the SPIM instance.
  237. */
  238. nrfx_err_t nrfx_spim_init(nrfx_spim_t const * const p_instance,
  239. nrfx_spim_config_t const * p_config,
  240. nrfx_spim_evt_handler_t handler,
  241. void * p_context);
  242. /**
  243. * @brief Function for uninitializing the SPI master driver instance.
  244. *
  245. * @param[in] p_instance Pointer to the driver instance structure.
  246. */
  247. void nrfx_spim_uninit(nrfx_spim_t const * const p_instance);
  248. /**
  249. * @brief Function for starting the SPI data transfer.
  250. *
  251. * Additional options are provided using the @c flags parameter:
  252. *
  253. * - @ref NRFX_SPIM_FLAG_TX_POSTINC and @ref NRFX_SPIM_FLAG_RX_POSTINC<span></span>:
  254. * Post-incrementation of buffer addresses. Supported only by SPIM.
  255. * - @ref NRFX_SPIM_FLAG_HOLD_XFER<span></span>: Driver is not starting the transfer. Use this
  256. * flag if the transfer is triggered externally by PPI. Supported only by SPIM. Use
  257. * @ref nrfx_spim_start_task_get to get the address of the start task.
  258. * - @ref NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER<span></span>: No user event handler after transfer
  259. * completion. This also means no interrupt at the end of the transfer. Supported only by SPIM.
  260. * If @ref NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER is used, the driver does not set the instance into
  261. * busy state, so you must ensure that the next transfers are set up when SPIM is not active.
  262. * @ref nrfx_spim_end_event_get function can be used to detect end of transfer. Option can be used
  263. * together with @ref NRFX_SPIM_FLAG_REPEATED_XFER to prepare a sequence of SPI transfers
  264. * without interruptions.
  265. * - @ref NRFX_SPIM_FLAG_REPEATED_XFER<span></span>: Prepare for repeated transfers. You can set
  266. * up a number of transfers that will be triggered externally (for example by PPI). An example is
  267. * a TXRX transfer with the options @ref NRFX_SPIM_FLAG_RX_POSTINC,
  268. * @ref NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER, and @ref NRFX_SPIM_FLAG_REPEATED_XFER. After the
  269. * transfer is set up, a set of transfers can be triggered by PPI that will read, for example,
  270. * the same register of an external component and put it into a RAM buffer without any interrupts.
  271. * @ref nrfx_spim_end_event_get can be used to get the address of the END event, which can be
  272. * used to count the number of transfers. If @ref NRFX_SPIM_FLAG_REPEATED_XFER is used,
  273. * the driver does not set the instance into busy state, so you must ensure that the next
  274. * transfers are set up when SPIM is not active. Supported only by SPIM.
  275. *
  276. * @note Peripherals using EasyDMA (including SPIM) require the transfer buffers
  277. * to be placed in the Data RAM region. If this condition is not met,
  278. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  279. *
  280. * @param p_instance Pointer to the driver instance structure.
  281. * @param p_xfer_desc Pointer to the transfer descriptor.
  282. * @param flags Transfer options (0 for default settings).
  283. *
  284. * @retval NRFX_SUCCESS If the procedure was successful.
  285. * @retval NRFX_ERROR_BUSY If the driver is not ready for a new transfer.
  286. * @retval NRFX_ERROR_NOT_SUPPORTED If the provided parameters are not supported.
  287. * @retval NRFX_ERROR_INVALID_ADDR If the provided buffers are not placed in the Data
  288. * RAM region.
  289. */
  290. nrfx_err_t nrfx_spim_xfer(nrfx_spim_t const * const p_instance,
  291. nrfx_spim_xfer_desc_t const * p_xfer_desc,
  292. uint32_t flags);
  293. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED) || defined(__NRFX_DOXYGEN__)
  294. /**
  295. * @brief Function for starting the SPI data transfer with DCX control.
  296. *
  297. * See @ref nrfx_spim_xfer for description of additional options of transfer
  298. * provided by the @c flags parameter.
  299. *
  300. * @note Peripherals that use EasyDMA (including SPIM) require the transfer buffers
  301. * to be placed in the Data RAM region. If this condition is not met,
  302. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  303. *
  304. * @param p_instance Pointer to the driver instance structure.
  305. * @param p_xfer_desc Pointer to the transfer descriptor.
  306. * @param flags Transfer options (0 for default settings).
  307. * @param cmd_length Length of the command bytes preceding the data
  308. * bytes. The DCX line will be low during transmission
  309. * of command bytes and high during transmission of data bytes.
  310. * Maximum value available for dividing the transmitted bytes
  311. * into command bytes and data bytes is @ref NRF_SPIM_DCX_CNT_ALL_CMD - 1.
  312. * The @ref NRF_SPIM_DCX_CNT_ALL_CMD value passed as the
  313. * @c cmd_length parameter causes all transmitted bytes
  314. * to be marked as command bytes.
  315. *
  316. * @retval NRFX_SUCCESS If the procedure was successful.
  317. * @retval NRFX_ERROR_BUSY If the driver is not ready for a new transfer.
  318. * @retval NRFX_ERROR_NOT_SUPPORTED If the provided parameters are not supported.
  319. * @retval NRFX_ERROR_INVALID_ADDR If the provided buffers are not placed in the Data
  320. * RAM region.
  321. */
  322. nrfx_err_t nrfx_spim_xfer_dcx(nrfx_spim_t const * const p_instance,
  323. nrfx_spim_xfer_desc_t const * p_xfer_desc,
  324. uint32_t flags,
  325. uint8_t cmd_length);
  326. #endif
  327. /**
  328. * @brief Function for returning the address of a SPIM start task.
  329. *
  330. * This function should be used if @ref nrfx_spim_xfer was called with the flag @ref NRFX_SPIM_FLAG_HOLD_XFER.
  331. * In that case, the transfer is not started by the driver, but it must be started externally by PPI.
  332. *
  333. * @param[in] p_instance Pointer to the driver instance structure.
  334. *
  335. * @return Start task address.
  336. */
  337. uint32_t nrfx_spim_start_task_get(nrfx_spim_t const * p_instance);
  338. /**
  339. * @brief Function for returning the address of a END SPIM event.
  340. *
  341. * The END event can be used to detect the end of a transfer
  342. * if the @ref NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER option is used.
  343. *
  344. * @param[in] p_instance Pointer to the driver instance structure.
  345. *
  346. * @return END event address.
  347. */
  348. uint32_t nrfx_spim_end_event_get(nrfx_spim_t const * p_instance);
  349. /**
  350. * @brief Function for aborting ongoing transfer.
  351. *
  352. * @param[in] p_instance Pointer to the driver instance structure.
  353. */
  354. void nrfx_spim_abort(nrfx_spim_t const * p_instance);
  355. void nrfx_spim_0_irq_handler(void);
  356. void nrfx_spim_1_irq_handler(void);
  357. void nrfx_spim_2_irq_handler(void);
  358. void nrfx_spim_3_irq_handler(void);
  359. /** @} */
  360. #ifdef __cplusplus
  361. }
  362. #endif
  363. #endif // NRFX_SPIM_H__