nrfx_spim.h 17 KB

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