nrfx_spis.h 11 KB

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