nrfx_spi.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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_SPI_H__
  41. #define NRFX_SPI_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_spi.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_spi SPI driver
  49. * @{
  50. * @ingroup nrf_spi
  51. * @brief SPI peripheral driver.
  52. */
  53. /**
  54. * @brief SPI master driver instance data structure.
  55. */
  56. typedef struct
  57. {
  58. NRF_SPI_Type * p_reg; ///< Pointer to a structure with SPI registers.
  59. uint8_t drv_inst_idx; ///< Driver instance index.
  60. } nrfx_spi_t;
  61. enum {
  62. #if NRFX_CHECK(NRFX_SPI0_ENABLED)
  63. NRFX_SPI0_INST_IDX,
  64. #endif
  65. #if NRFX_CHECK(NRFX_SPI1_ENABLED)
  66. NRFX_SPI1_INST_IDX,
  67. #endif
  68. #if NRFX_CHECK(NRFX_SPI2_ENABLED)
  69. NRFX_SPI2_INST_IDX,
  70. #endif
  71. NRFX_SPI_ENABLED_COUNT
  72. };
  73. /**
  74. * @brief Macro for creating an SPI master driver instance.
  75. */
  76. #define NRFX_SPI_INSTANCE(id) \
  77. { \
  78. .p_reg = NRFX_CONCAT_2(NRF_SPI, id), \
  79. .drv_inst_idx = NRFX_CONCAT_3(NRFX_SPI, id, _INST_IDX), \
  80. }
  81. /**
  82. * @brief This value can be provided instead of a pin number for signals MOSI,
  83. * MISO, and Slave Select to specify that the given signal is not used and
  84. * therefore does not need to be connected to a pin.
  85. */
  86. #define NRFX_SPI_PIN_NOT_USED 0xFF
  87. /**
  88. * @brief SPI master driver instance configuration structure.
  89. */
  90. typedef struct
  91. {
  92. uint8_t sck_pin; ///< SCK pin number.
  93. uint8_t mosi_pin; ///< MOSI pin number (optional).
  94. /**< Set to @ref NRFX_SPI_PIN_NOT_USED
  95. * if this signal is not needed. */
  96. uint8_t miso_pin; ///< MISO pin number (optional).
  97. /**< Set to @ref NRFX_SPI_PIN_NOT_USED
  98. * if this signal is not needed. */
  99. uint8_t ss_pin; ///< Slave Select pin number (optional).
  100. /**< Set to @ref NRFX_SPI_PIN_NOT_USED
  101. * if this signal is not needed. The driver
  102. * supports only active low for this signal.
  103. * If the signal should be active high,
  104. * it must be controlled externally. */
  105. uint8_t irq_priority; ///< Interrupt priority.
  106. uint8_t orc; ///< Over-run character.
  107. /**< This character is used when all bytes from the TX buffer are sent,
  108. but the transfer continues due to RX. */
  109. nrf_spi_frequency_t frequency; ///< SPI frequency.
  110. nrf_spi_mode_t mode; ///< SPI mode.
  111. nrf_spi_bit_order_t bit_order; ///< SPI bit order.
  112. } nrfx_spi_config_t;
  113. /**
  114. * @brief SPI master instance default configuration.
  115. */
  116. #define NRFX_SPI_DEFAULT_CONFIG \
  117. { \
  118. .sck_pin = NRFX_SPI_PIN_NOT_USED, \
  119. .mosi_pin = NRFX_SPI_PIN_NOT_USED, \
  120. .miso_pin = NRFX_SPI_PIN_NOT_USED, \
  121. .ss_pin = NRFX_SPI_PIN_NOT_USED, \
  122. .irq_priority = NRFX_SPI_DEFAULT_CONFIG_IRQ_PRIORITY, \
  123. .orc = 0xFF, \
  124. .frequency = NRF_SPI_FREQ_4M, \
  125. .mode = NRF_SPI_MODE_0, \
  126. .bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST, \
  127. }
  128. /**
  129. * @brief Single transfer descriptor structure.
  130. */
  131. typedef struct
  132. {
  133. uint8_t const * p_tx_buffer; ///< Pointer to TX buffer.
  134. size_t tx_length; ///< TX buffer length.
  135. uint8_t * p_rx_buffer; ///< Pointer to RX buffer.
  136. size_t rx_length; ///< RX buffer length.
  137. } nrfx_spi_xfer_desc_t;
  138. /**
  139. * @brief Macro for setting up single transfer descriptor.
  140. *
  141. * This macro is for internal use only.
  142. */
  143. #define NRFX_SPI_SINGLE_XFER(p_tx, tx_len, p_rx, rx_len) \
  144. { \
  145. .p_tx_buffer = (uint8_t const *)(p_tx), \
  146. .tx_length = (tx_len), \
  147. .p_rx_buffer = (p_rx), \
  148. .rx_length = (rx_len), \
  149. }
  150. /**
  151. * @brief Macro for setting duplex TX RX transfer.
  152. */
  153. #define NRFX_SPI_XFER_TRX(p_tx_buf, tx_length, p_rx_buf, rx_length) \
  154. NRFX_SPI_SINGLE_XFER(p_tx_buf, tx_length, p_rx_buf, rx_length)
  155. /**
  156. * @brief Macro for setting TX transfer.
  157. */
  158. #define NRFX_SPI_XFER_TX(p_buf, length) \
  159. NRFX_SPI_SINGLE_XFER(p_buf, length, NULL, 0)
  160. /**
  161. * @brief Macro for setting RX transfer.
  162. */
  163. #define NRFX_SPI_XFER_RX(p_buf, length) \
  164. NRFX_SPI_SINGLE_XFER(NULL, 0, p_buf, length)
  165. /**
  166. * @brief SPI master driver event types, passed to the handler routine provided
  167. * during initialization.
  168. */
  169. typedef enum
  170. {
  171. NRFX_SPI_EVENT_DONE, ///< Transfer done.
  172. } nrfx_spi_evt_type_t;
  173. typedef struct
  174. {
  175. nrfx_spi_evt_type_t type; ///< Event type.
  176. nrfx_spi_xfer_desc_t xfer_desc; ///< Transfer details.
  177. } nrfx_spi_evt_t;
  178. /**
  179. * @brief SPI master driver event handler type.
  180. */
  181. typedef void (* nrfx_spi_evt_handler_t)(nrfx_spi_evt_t const * p_event,
  182. void * p_context);
  183. /**
  184. * @brief Function for initializing the SPI master driver instance.
  185. *
  186. * This function configures and enables the specified peripheral.
  187. *
  188. * @param[in] p_instance Pointer to the driver instance structure.
  189. * @param[in] p_config Pointer to the structure with initial configuration.
  190. *
  191. * @param handler Event handler provided by the user. If NULL, transfers
  192. * will be performed in blocking mode.
  193. * @param p_context Context passed to event handler.
  194. *
  195. * @retval NRFX_SUCCESS If initialization was successful.
  196. * @retval NRFX_ERROR_INVALID_STATE If the driver was already initialized.
  197. * @retval NRFX_ERROR_BUSY If some other peripheral with the same
  198. * instance ID is already in use. This is
  199. * possible only if @ref nrfx_prs module
  200. * is enabled.
  201. */
  202. nrfx_err_t nrfx_spi_init(nrfx_spi_t const * const p_instance,
  203. nrfx_spi_config_t const * p_config,
  204. nrfx_spi_evt_handler_t handler,
  205. void * p_context);
  206. /**
  207. * @brief Function for uninitializing the SPI master driver instance.
  208. *
  209. * @param[in] p_instance Pointer to the driver instance structure.
  210. */
  211. void nrfx_spi_uninit(nrfx_spi_t const * const p_instance);
  212. /**
  213. * @brief Function for starting the SPI data transfer.
  214. *
  215. * If an event handler was provided in the @ref nrfx_spi_init call, this function
  216. * returns immediately and the handler is called when the transfer is done.
  217. * Otherwise, the transfer is performed in blocking mode, which means that this function
  218. * returns when the transfer is finished.
  219. *
  220. * @param p_instance Pointer to the driver instance structure.
  221. * @param p_xfer_desc Pointer to the transfer descriptor.
  222. * @param flags Transfer options (0 for default settings).
  223. * Currently, no additional flags are available.
  224. *
  225. * @retval NRFX_SUCCESS If the procedure was successful.
  226. * @retval NRFX_ERROR_BUSY If the driver is not ready for a new transfer.
  227. * @retval NRFX_ERROR_NOT_SUPPORTED If the provided parameters are not supported.
  228. */
  229. nrfx_err_t nrfx_spi_xfer(nrfx_spi_t const * const p_instance,
  230. nrfx_spi_xfer_desc_t const * p_xfer_desc,
  231. uint32_t flags);
  232. /**
  233. * @brief Function for aborting ongoing transfer.
  234. *
  235. * @param[in] p_instance Pointer to the driver instance structure.
  236. */
  237. void nrfx_spi_abort(nrfx_spi_t const * p_instance);
  238. void nrfx_spi_0_irq_handler(void);
  239. void nrfx_spi_1_irq_handler(void);
  240. void nrfx_spi_2_irq_handler(void);
  241. /** @} */
  242. #ifdef __cplusplus
  243. }
  244. #endif
  245. #endif // NRFX_SPI_H__