nrfx_spis.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_SPIS_H__
  41. #define NRFX_SPIS_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_spis.h>
  44. #include <hal/nrf_gpio.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup nrfx_spis SPI slave driver
  50. * @{
  51. * @ingroup nrf_spis
  52. * @brief SPI Slave peripheral driver.
  53. */
  54. /** @brief SPI slave driver instance data structure. */
  55. typedef struct
  56. {
  57. NRF_SPIS_Type * p_reg; //!< Pointer to a structure with SPIS registers.
  58. uint8_t drv_inst_idx; //!< Driver instance index.
  59. } nrfx_spis_t;
  60. enum {
  61. #if NRFX_CHECK(NRFX_SPIS0_ENABLED)
  62. NRFX_SPIS0_INST_IDX,
  63. #endif
  64. #if NRFX_CHECK(NRFX_SPIS1_ENABLED)
  65. NRFX_SPIS1_INST_IDX,
  66. #endif
  67. #if NRFX_CHECK(NRFX_SPIS2_ENABLED)
  68. NRFX_SPIS2_INST_IDX,
  69. #endif
  70. #if NRFX_CHECK(NRFX_SPIS3_ENABLED)
  71. NRFX_SPIS3_INST_IDX,
  72. #endif
  73. NRFX_SPIS_ENABLED_COUNT
  74. };
  75. /** @brief Macro for creating an SPI slave driver instance. */
  76. #define NRFX_SPIS_INSTANCE(id) \
  77. { \
  78. .p_reg = NRFX_CONCAT_2(NRF_SPIS, id), \
  79. .drv_inst_idx = NRFX_CONCAT_3(NRFX_SPIS, id, _INST_IDX), \
  80. }
  81. /**
  82. * @brief This value can be provided instead of a pin number for the signals MOSI
  83. * and MISO to specify that the given signal is not used and therefore
  84. * does not need to be connected to a pin.
  85. */
  86. #define NRFX_SPIS_PIN_NOT_USED 0xFF
  87. /** @brief Default pull-up configuration of the SPI CS. */
  88. #define NRFX_SPIS_DEFAULT_CSN_PULLUP NRF_GPIO_PIN_NOPULL
  89. /** @brief Default drive configuration of the SPI MISO. */
  90. #define NRFX_SPIS_DEFAULT_MISO_DRIVE NRF_GPIO_PIN_S0S1
  91. /** @brief SPI slave driver event types. */
  92. typedef enum
  93. {
  94. NRFX_SPIS_BUFFERS_SET_DONE, //!< Memory buffer set event. Memory buffers have been set successfully to the SPI slave device, and SPI transaction can be done.
  95. NRFX_SPIS_XFER_DONE, //!< SPI transaction event. SPI transaction has been completed.
  96. NRFX_SPIS_EVT_TYPE_MAX //!< Enumeration upper bound.
  97. } nrfx_spis_evt_type_t;
  98. /** @brief SPI slave driver event structure. */
  99. typedef struct
  100. {
  101. nrfx_spis_evt_type_t evt_type; //!< Type of the event.
  102. size_t rx_amount; //!< Number of bytes received in the last transaction. This parameter is only valid for @ref NRFX_SPIS_XFER_DONE events.
  103. size_t tx_amount; //!< Number of bytes transmitted in the last transaction. This parameter is only valid for @ref NRFX_SPIS_XFER_DONE events.
  104. } nrfx_spis_evt_t;
  105. /** @brief SPI slave instance default configuration. */
  106. #define NRFX_SPIS_DEFAULT_CONFIG \
  107. { \
  108. .sck_pin = NRFX_SPIS_PIN_NOT_USED, \
  109. .mosi_pin = NRFX_SPIS_PIN_NOT_USED, \
  110. .miso_pin = NRFX_SPIS_PIN_NOT_USED, \
  111. .csn_pin = NRFX_SPIS_PIN_NOT_USED, \
  112. .mode = NRF_SPIS_MODE_0, \
  113. .bit_order = NRF_SPIS_BIT_ORDER_MSB_FIRST, \
  114. .csn_pullup = NRFX_SPIS_DEFAULT_CSN_PULLUP, \
  115. .miso_drive = NRFX_SPIS_DEFAULT_MISO_DRIVE, \
  116. .def = NRFX_SPIS_DEFAULT_DEF, \
  117. .orc = NRFX_SPIS_DEFAULT_ORC, \
  118. .irq_priority = NRFX_SPIS_DEFAULT_CONFIG_IRQ_PRIORITY, \
  119. }
  120. /** @brief SPI peripheral device configuration data. */
  121. typedef struct
  122. {
  123. uint32_t miso_pin; //!< SPI MISO pin (optional).
  124. /**< Set @ref NRFX_SPIS_PIN_NOT_USED
  125. * if this signal is not needed. */
  126. uint32_t mosi_pin; //!< SPI MOSI pin (optional).
  127. /**< Set @ref NRFX_SPIS_PIN_NOT_USED
  128. * if this signal is not needed. */
  129. uint32_t sck_pin; //!< SPI SCK pin.
  130. uint32_t csn_pin; //!< SPI CSN pin.
  131. nrf_spis_mode_t mode; //!< SPI mode.
  132. nrf_spis_bit_order_t bit_order; //!< SPI transaction bit order.
  133. nrf_gpio_pin_pull_t csn_pullup; //!< CSN pin pull-up configuration.
  134. nrf_gpio_pin_drive_t miso_drive; //!< MISO pin drive configuration.
  135. uint8_t def; //!< Character clocked out in case of an ignored transaction.
  136. uint8_t orc; //!< Character clocked out after an over-read of the transmit buffer.
  137. uint8_t irq_priority; //!< Interrupt priority.
  138. } nrfx_spis_config_t;
  139. /**
  140. * @brief SPI slave driver event handler type.
  141. *
  142. * @param[in] p_event Pointer to the event structure. The structure is
  143. * allocated on the stack so it is valid only until
  144. * the event handler returns.
  145. * @param[in] p_context Context set on initialization.
  146. */
  147. typedef void (*nrfx_spis_event_handler_t)(nrfx_spis_evt_t const * p_event,
  148. void * p_context);
  149. /**
  150. * @brief Function for initializing the SPI slave driver instance.
  151. *
  152. * @note When the nRF52 Anomaly 109 workaround for SPIS is enabled, this function
  153. * initializes the GPIOTE driver as well, and uses one of GPIOTE channels
  154. * to detect falling edges on CSN pin.
  155. *
  156. * @param[in] p_instance Pointer to the driver instance structure.
  157. * @param[in] p_config Pointer to the structure with initial configuration.
  158. * @param[in] event_handler Function to be called by the SPI slave driver upon event.
  159. * Must not be NULL.
  160. * @param[in] p_context Context passed to the event handler.
  161. *
  162. * @retval NRFX_SUCCESS If the initialization was successful.
  163. * @retval NRFX_ERROR_INVALID_STATE If the instance is already initialized.
  164. * @retval NRFX_ERROR_INVALID_PARAM If an invalid parameter is supplied.
  165. * @retval NRFX_ERROR_BUSY If some other peripheral with the same
  166. * instance ID is already in use. This is
  167. * possible only if @ref nrfx_prs module
  168. * is enabled.
  169. * @retval NRFX_ERROR_INTERNAL GPIOTE channel for detecting falling edges
  170. * on CSN pin cannot be initialized. Possible
  171. * only when using nRF52 Anomaly 109 workaround.
  172. */
  173. nrfx_err_t nrfx_spis_init(nrfx_spis_t const * const p_instance,
  174. nrfx_spis_config_t const * p_config,
  175. nrfx_spis_event_handler_t event_handler,
  176. void * p_context);
  177. /**
  178. * @brief Function for uninitializing the SPI slave driver instance.
  179. *
  180. * @param[in] p_instance Pointer to the driver instance structure.
  181. */
  182. void nrfx_spis_uninit(nrfx_spis_t const * const p_instance);
  183. /**
  184. * @brief Function for preparing the SPI slave instance for a single SPI transaction.
  185. *
  186. * This function prepares the SPI slave device to be ready for a single SPI transaction. It configures
  187. * the SPI slave device to use the memory supplied with the function call in SPI transactions.
  188. *
  189. * When either the memory buffer configuration or the SPI transaction has been
  190. * completed, the event callback function will be called with the appropriate event
  191. * @ref nrfx_spis_evt_type_t. Note that the callback function can be called before returning from
  192. * this function, because it is called from the SPI slave interrupt context.
  193. *
  194. * @note This function can be called from the callback function context.
  195. *
  196. * @note Client applications must call this function after every @ref NRFX_SPIS_XFER_DONE event if
  197. * the SPI slave driver should be prepared for a possible new SPI transaction.
  198. *
  199. * @note Peripherals using EasyDMA (including SPIS) require the transfer buffers
  200. * to be placed in the Data RAM region. If this condition is not met,
  201. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  202. *
  203. * @param[in] p_instance Pointer to the driver instance structure.
  204. * @param[in] p_tx_buffer Pointer to the TX buffer. Can be NULL when the buffer length is zero.
  205. * @param[in] p_rx_buffer Pointer to the RX buffer. Can be NULL when the buffer length is zero.
  206. * @param[in] tx_buffer_length Length of the TX buffer in bytes.
  207. * @param[in] rx_buffer_length Length of the RX buffer in bytes.
  208. *
  209. * @retval NRFX_SUCCESS If the operation was successful.
  210. * @retval NRFX_ERROR_INVALID_STATE If the operation failed because the SPI slave device is in an incorrect state.
  211. * @retval NRFX_ERROR_INVALID_ADDR If the provided buffers are not placed in the Data
  212. * RAM region.
  213. * @retval NRFX_ERROR_INVALID_LENGTH If provided lengths exceed the EasyDMA limits for the peripheral.
  214. * @retval NRFX_ERROR_INTERNAL If the operation failed because of an internal error.
  215. */
  216. nrfx_err_t nrfx_spis_buffers_set(nrfx_spis_t const * const p_instance,
  217. uint8_t const * p_tx_buffer,
  218. size_t tx_buffer_length,
  219. uint8_t * p_rx_buffer,
  220. size_t rx_buffer_length);
  221. void nrfx_spis_0_irq_handler(void);
  222. void nrfx_spis_1_irq_handler(void);
  223. void nrfx_spis_2_irq_handler(void);
  224. void nrfx_spis_3_irq_handler(void);
  225. /** @} */
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. #endif // NRFX_SPIS_H__