nrf_spi_mngr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * Copyright (c) 2017 - 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 NRF_SPI_MNGR_H__
  41. #define NRF_SPI_MNGR_H__
  42. #include <stdint.h>
  43. #include "nrf_drv_spi.h"
  44. #include "sdk_errors.h"
  45. #include "nrf_queue.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /*lint -save -e491*/
  50. #ifndef NRF_SPI_MNGR_BUFFERS_IN_RAM
  51. #define NRF_SPI_MNGR_BUFFERS_IN_RAM defined(SPIM_PRESENT)
  52. #endif
  53. #if NRF_SPI_MNGR_BUFFERS_IN_RAM
  54. #define NRF_SPI_MNGR_BUFFER_LOC_IND
  55. #else
  56. #define NRF_SPI_MNGR_BUFFER_LOC_IND const
  57. #endif
  58. /*lint -restore*/
  59. /**
  60. * @defgroup nrf_spi_mngr SPI transaction manager
  61. * @{
  62. * @ingroup app_common
  63. *
  64. * @brief Module for scheduling SPI transactions.
  65. */
  66. /**
  67. * @brief Macro for creating a simple SPI transfer.
  68. *
  69. * @param[in] _p_tx_data Pointer to the data to be sent.
  70. * @param[in] _tx_length Number of bytes to send.
  71. * @param[in] _p_rx_data Pointer to a buffer for received data.
  72. * @param[in] _rx_length Number of bytes to receive.
  73. */
  74. #define NRF_SPI_MNGR_TRANSFER(_p_tx_data, _tx_length, _p_rx_data, _rx_length) \
  75. { \
  76. .p_tx_data = (uint8_t const *)_p_tx_data, \
  77. .tx_length = (uint8_t) _tx_length, \
  78. .p_rx_data = (uint8_t *) _p_rx_data, \
  79. .rx_length = (uint8_t) _rx_length, \
  80. }
  81. /**
  82. * @brief SPI transaction end callback prototype.
  83. *
  84. * @param result Result of operation (NRF_SUCCESS on success,
  85. * otherwise a relevant error code).
  86. * @param[in] p_user_data Pointer to user data defined in transaction
  87. * descriptor.
  88. */
  89. typedef void (* nrf_spi_mngr_callback_end_t)(ret_code_t result, void * p_user_data);
  90. /**
  91. * @brief SPI transaction begin callback prototype.
  92. *
  93. * @param[in] p_user_data Pointer to user data defined in transaction
  94. * descriptor.
  95. */
  96. typedef void (* nrf_spi_mngr_callback_begin_t)(void * p_user_data);
  97. /**
  98. * @brief SPI transfer descriptor.
  99. */
  100. typedef struct
  101. {
  102. uint8_t const * p_tx_data; ///< Pointer to the data to be sent.
  103. uint8_t tx_length; ///< Number of bytes to send.
  104. uint8_t * p_rx_data; ///< Pointer to a buffer for received data.
  105. uint8_t rx_length; ///< Number of bytes to receive.
  106. } nrf_spi_mngr_transfer_t;
  107. /**
  108. * @brief SPI transaction descriptor.
  109. */
  110. typedef struct
  111. {
  112. nrf_spi_mngr_callback_begin_t begin_callback;
  113. ///< User-specified function to be called before the transaction is started.
  114. nrf_spi_mngr_callback_end_t end_callback;
  115. ///< User-specified function to be called after the transaction is finished.
  116. void * p_user_data;
  117. ///< Pointer to user data to be passed to the end_callback.
  118. nrf_spi_mngr_transfer_t const * p_transfers;
  119. ///< Pointer to the array of transfers that make up the transaction.
  120. uint8_t number_of_transfers;
  121. ///< Number of transfers that make up the transaction.
  122. nrf_drv_spi_config_t const * p_required_spi_cfg;
  123. ///< Pointer to instance hardware configuration.
  124. } nrf_spi_mngr_transaction_t;
  125. /**
  126. * @brief SPI instance control block.
  127. */
  128. typedef struct
  129. {
  130. nrf_spi_mngr_transaction_t const * volatile p_current_transaction;
  131. ///< Currently realized transaction.
  132. nrf_drv_spi_config_t default_configuration;
  133. ///< Default hardware configuration.
  134. nrf_drv_spi_config_t const * p_current_configuration;
  135. ///< Pointer to current hardware configuration.
  136. uint8_t volatile current_transfer_idx;
  137. ///< Index of currently performed transfer (within current transaction).
  138. } nrf_spi_mngr_cb_t;
  139. /**
  140. * @brief SPI transaction manager instance.
  141. */
  142. typedef struct
  143. {
  144. nrf_spi_mngr_cb_t * p_nrf_spi_mngr_cb;
  145. ///< Control block of instance.
  146. nrf_queue_t const * p_queue;
  147. ///< Transaction queue.
  148. nrf_drv_spi_t spi;
  149. ///< Pointer to SPI master driver instance.
  150. } nrf_spi_mngr_t;
  151. /**
  152. * @brief Macro for simplifying the defining of an SPI transaction manager
  153. * instance.
  154. *
  155. * This macro allocates a static buffer for the transaction queue.
  156. * Therefore, it should be used in only one place in the code for a given
  157. * instance.
  158. *
  159. * @note The queue size is the maximum number of pending transactions
  160. * not counting the one that is currently realized. This means that
  161. * for an empty queue with size of for example 4 elements, it is
  162. * possible to schedule up to 5 transactions.
  163. *
  164. * @param[in] _nrf_spi_mngr_name Name of instance to be created.
  165. * @param[in] _queue_size Size of the transaction queue (maximum number
  166. * of pending transactions).
  167. * @param[in] _spi_idx Index of hardware SPI instance to be used.
  168. */
  169. #define NRF_SPI_MNGR_DEF(_nrf_spi_mngr_name, _queue_size, _spi_idx) \
  170. NRF_QUEUE_DEF(nrf_spi_mngr_transaction_t const *, \
  171. _nrf_spi_mngr_name##_queue, \
  172. (_queue_size), \
  173. NRF_QUEUE_MODE_NO_OVERFLOW); \
  174. static nrf_spi_mngr_cb_t CONCAT_2(_nrf_spi_mngr_name, _cb); \
  175. static const nrf_spi_mngr_t _nrf_spi_mngr_name = \
  176. { \
  177. .p_nrf_spi_mngr_cb = &CONCAT_2(_nrf_spi_mngr_name, _cb), \
  178. .p_queue = &_nrf_spi_mngr_name##_queue, \
  179. .spi = NRF_DRV_SPI_INSTANCE(_spi_idx) \
  180. }
  181. /**
  182. * @brief Function for initializing an SPI transaction manager instance.
  183. *
  184. * @param[in] p_nrf_spi_mngr Pointer to the instance to be initialized.
  185. * @param[in] p_default_spi_config Pointer to the SPI driver configuration. This configuration
  186. * will be used whenever the scheduled transaction will have
  187. * p_spi_config set to NULL value.
  188. *
  189. * @return Values returned by the @ref nrf_drv_spi_init function.
  190. */
  191. ret_code_t nrf_spi_mngr_init(nrf_spi_mngr_t const * p_nrf_spi_mngr,
  192. nrf_drv_spi_config_t const * p_default_spi_config);
  193. /**
  194. * @brief Function for uninitializing an SPI transaction manager instance.
  195. *
  196. * @param[in] p_nrf_spi_mngr Pointer to the instance to be uninitialized.
  197. */
  198. void nrf_spi_mngr_uninit(nrf_spi_mngr_t const * p_nrf_spi_mngr);
  199. /**
  200. * @brief Function for scheduling an SPI transaction.
  201. *
  202. * The transaction is enqueued and started as soon as the SPI bus is
  203. * available, thus when all previously scheduled transactions have been
  204. * finished (possibly immediately).
  205. *
  206. * @note If @ref nrf_spi_mngr_transaction_t::p_required_spi_cfg
  207. * is set to a non-NULL value the module will compare it with
  208. * @ref nrf_spi_mngr_cb_t::p_current_configuration and reinitialize hardware
  209. * SPI instance with new parameters if any differences are found.
  210. * If @ref nrf_spi_mngr_transaction_t::p_required_spi_cfg is set to NULL then
  211. * it will treat it as it would be set to @ref nrf_spi_mngr_cb_t::default_configuration.
  212. *
  213. * @param[in] p_nrf_spi_mngr Pointer to the SPI transaction manager instance.
  214. * @param[in] p_transaction Pointer to the descriptor of the transaction to be
  215. * scheduled.
  216. *
  217. * @retval NRF_SUCCESS If the transaction has been successfully scheduled.
  218. * @retval NRF_ERROR_NO_MEM If the queue is full (Only if queue in
  219. * @ref NRF_QUEUE_MODE_NO_OVERFLOW).
  220. */
  221. ret_code_t nrf_spi_mngr_schedule(nrf_spi_mngr_t const * p_nrf_spi_mngr,
  222. nrf_spi_mngr_transaction_t const * p_transaction);
  223. /**
  224. * @brief Function for scheduling a transaction and waiting until it is finished.
  225. *
  226. * This function schedules a transaction that consists of one or more transfers
  227. * and waits until it is finished.
  228. *
  229. * @param[in] p_nrf_spi_mngr Pointer to the SPI transaction manager instance.
  230. * @param[in] p_config Required SPI configuration.
  231. * @param[in] p_transfers Pointer to an array of transfers to be performed.
  232. * @param number_of_transfers Number of transfers to be performed.
  233. * @param user_function User-specified function to be called while
  234. * waiting. NULL if such functionality
  235. * is not needed.
  236. *
  237. * @retval NRF_SUCCESS If the transfers have been successfully realized.
  238. * @retval NRF_ERROR_BUSY If some transfers are already being performed.
  239. * @retval - Other error codes mean that the transaction has failed
  240. * with the error reported by @ref nrf_drv_spi_transfer().
  241. */
  242. ret_code_t nrf_spi_mngr_perform(nrf_spi_mngr_t const * p_nrf_spi_mngr,
  243. nrf_drv_spi_config_t const * p_config,
  244. nrf_spi_mngr_transfer_t const * p_transfers,
  245. uint8_t number_of_transfers,
  246. void (* user_function)(void));
  247. /**
  248. * @brief Function for getting the current state of an SPI transaction manager
  249. * instance.
  250. *
  251. * @param[in] p_nrf_spi_mngr Pointer to the SPI transaction manager instance.
  252. *
  253. * @retval true If all scheduled transactions have been finished.
  254. * @retval false Otherwise.
  255. */
  256. __STATIC_INLINE bool nrf_spi_mngr_is_idle(nrf_spi_mngr_t const * p_nrf_spi_mngr)
  257. {
  258. return (p_nrf_spi_mngr->p_nrf_spi_mngr_cb->p_current_transaction == NULL);
  259. }
  260. /**
  261. *@}
  262. **/
  263. //typedef int p_current_transaction;
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif // NRF_SPI_MNGR_H__