nrfx_spi.h 10 KB

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