nrf21540_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * Copyright (c) 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. #include "nrf21540_spi.h"
  41. #include <string.h>
  42. #include "nrf_assert.h"
  43. #include "boards.h"
  44. #include "nrf_ppi.h"
  45. #include "nrf21540_defs.h"
  46. #include "nrf_timer.h"
  47. #if NRF21540_USE_SPI_MANAGEMENT
  48. static uint8_t m_spi_tx_data[NRF21540_SPI_LENGTH_BYTES]; ///< SPI tx buffer.
  49. static uint8_t m_spi_rx_data[NRF21540_SPI_LENGTH_BYTES]; ///< SPI rx buffer.
  50. static volatile bool m_spi_xfer_done; ///< Flag indicates that SPI completed the transfer.
  51. /**@brief Structure keeps content of important registers of nRF21540.
  52. *
  53. * @details Driver keeps this data because it needs to operate at single bits
  54. * included in these registers (otherwise it should read it content
  55. * during every operation).
  56. */
  57. static struct {
  58. uint8_t CONFREG0; ///< CONFREG0 register's content.
  59. uint8_t CONFREG1; ///< CONFREG1 register's content.
  60. } m_confreg_statics;
  61. static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(NRF21540_SPIM_NO); /**< SPI instance. */
  62. /**@brief Function waits for SPI transfer has finished
  63. *
  64. * @details Used in blocking mode transfer
  65. */
  66. static inline void wait_for_transfer_end(void)
  67. {
  68. while (!m_spi_xfer_done)
  69. {}
  70. m_spi_xfer_done = false;
  71. }
  72. /**@brief Handler called by nrfx driver when SPI event occurs.
  73. *
  74. * @param[in] p_event Event which triggers the handler.
  75. * @param[in] p_context Context.
  76. */
  77. static void spim_event_handler(nrfx_spim_evt_t const *p_event, void *p_context)
  78. {
  79. m_spi_xfer_done = true;
  80. }
  81. /**@brief Function reads the content of nRF21540 chip register.
  82. *
  83. * @details Preparation of read register operation. Every register has one byte size.
  84. *
  85. * @param[in] reg Register address to read.
  86. * @param[in] mode if NRF21540_EXEC_MODE_BLOCKING the function will wait for data
  87. * received.
  88. * @param[in] start_now if enabled, transmision immediately initialized,
  89. * otherwise transfer will be triggered by external event.
  90. */
  91. static uint8_t spi_reg_read(nrf21540_reg_t reg, nrf21540_execution_mode_t mode, bool start_now)
  92. {
  93. ASSERT(!(mode == NRF21540_EXEC_MODE_BLOCKING && start_now == false));
  94. nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_spi_tx_data,
  95. NRF21540_SPI_LENGTH_BYTES,
  96. m_spi_rx_data,
  97. NRF21540_SPI_LENGTH_BYTES);
  98. m_spi_tx_data[NRF21540_SPI_COMMAND_ADDR_BYTE] =
  99. (NRF21540_SPI_COMMAND_READ << NRF21540_SPI_COMMAND_Pos) | (reg << NRF21540_SPI_REG_Pos);
  100. (void)nrfx_spim_xfer(&spi, &xfer_desc, 0);
  101. if (mode == NRF21540_EXEC_MODE_BLOCKING)
  102. {
  103. wait_for_transfer_end();
  104. }
  105. return m_spi_rx_data[NRF21540_SPI_DATA_BYTE];
  106. }
  107. /**@brief Function writes the content of nRF21540 chip register.
  108. *
  109. * @details Preparation of data to send. Every register has one byte size.
  110. *
  111. * @param[in] reg Register address to write.
  112. * @param[in] data Data to write.
  113. * @param[in] mode if NRF21540_EXEC_MODE_BLOCKING the function will wait for transfer
  114. * finished after sending data.
  115. * @param[in] start_now if enabled, transmision immediately initialized,
  116. * otherwise transfer will be triggered by external event.
  117. */
  118. static void spi_reg_write(nrf21540_reg_t reg, uint8_t data, nrf21540_execution_mode_t mode, bool start_now)
  119. {
  120. ASSERT(!(mode == NRF21540_EXEC_MODE_BLOCKING && start_now == false));
  121. nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_spi_tx_data,
  122. NRF21540_SPI_LENGTH_BYTES,
  123. m_spi_rx_data,
  124. NRF21540_SPI_LENGTH_BYTES);
  125. m_spi_tx_data[NRF21540_SPI_COMMAND_ADDR_BYTE] =
  126. (NRF21540_SPI_COMMAND_WRITE << NRF21540_SPI_COMMAND_Pos) | (reg << NRF21540_SPI_REG_Pos);
  127. m_spi_tx_data[NRF21540_SPI_DATA_BYTE] = data;
  128. uint32_t flags = start_now ? 0 : NRFX_SPIM_FLAG_HOLD_XFER;
  129. (void)nrfx_spim_xfer(&spi, &xfer_desc, flags);
  130. if (mode == NRF21540_EXEC_MODE_BLOCKING)
  131. {
  132. wait_for_transfer_end();
  133. }
  134. }
  135. /**@brief Function reads content of important nRF21540's registers and stores
  136. * it to dedicated structure (@ref m_confreg_statics).
  137. *
  138. * @return Return NRF based error code.
  139. */
  140. static ret_code_t m_confreg_statics_content_update(void)
  141. {
  142. ret_code_t ret = nrf21540_pdn_drive(true, NRF21540_EXEC_MODE_BLOCKING);
  143. if (ret != NRF_SUCCESS)
  144. {
  145. return ret;
  146. }
  147. m_confreg_statics.CONFREG0 = spi_reg_read(NRF21540_REG_CONFREG0,
  148. NRF21540_EXEC_MODE_BLOCKING, true);
  149. m_confreg_statics.CONFREG1 = spi_reg_read(NRF21540_REG_CONFREG1,
  150. NRF21540_EXEC_MODE_BLOCKING, true);
  151. return nrf21540_pdn_drive(false, NRF21540_EXEC_MODE_BLOCKING);
  152. }
  153. ret_code_t nrf21540_spi_init(void)
  154. {
  155. ret_code_t ret;
  156. nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
  157. spi_config.frequency = NRF_SPIM_FREQ_4M;
  158. spi_config.ss_pin = NRF21540_CS_PIN;
  159. spi_config.miso_pin = NRF21540_MISO_PIN;
  160. spi_config.mosi_pin = NRF21540_MOSI_PIN;
  161. spi_config.sck_pin = NRF21540_CLK_PIN;
  162. spi_config.ss_active_high = false;
  163. ret = nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL);
  164. if (ret != NRFX_SUCCESS)
  165. {
  166. return NRF_ERROR_INTERNAL;
  167. }
  168. return m_confreg_statics_content_update();
  169. }
  170. /**@brief Function enables or disables nRF21540 TX mode.
  171. *
  172. * @details Preparation of appropriate register content and tranfer initialization.
  173. *
  174. * @param[in] state NRF21540_DISABLE/NRF21540_ENABLE causes TX mode disabled/enabled.
  175. */
  176. static void tx_en_drive(nrf21540_bool_state_t state)
  177. {
  178. uint8_t reg_val;
  179. if (state == NRF21540_ENABLE)
  180. {
  181. reg_val = m_confreg_statics.CONFREG0 | NRF21540_BITS_CONFREG0_TX_EN_Enable;
  182. }
  183. else
  184. {
  185. reg_val = m_confreg_statics.CONFREG0 &(~NRF21540_BITS_CONFREG0_TX_EN_Enable);
  186. }
  187. spi_reg_write(NRF21540_REG_CONFREG0, reg_val, NRF21540_EXEC_MODE_NON_BLOCKING, false);
  188. }
  189. /**@brief Function enables or disables nRF21540 RX mode.
  190. *
  191. * @details Preparation of appropriate register content and tranfer initialization.
  192. *
  193. * @param[in] state NRF21540_DISABLE/NRF21540_ENABLE causes RX mode disabled/enabled.
  194. */
  195. static void rx_en_drive(nrf21540_bool_state_t state)
  196. {
  197. uint8_t reg_val;
  198. if (state == NRF21540_ENABLE)
  199. {
  200. reg_val = m_confreg_statics.CONFREG1 | NRF21540_BITS_CONFREG1_RX_EN_Enable;
  201. }
  202. else
  203. {
  204. reg_val = m_confreg_statics.CONFREG1 &(~NRF21540_BITS_CONFREG1_RX_EN_Disable);
  205. }
  206. spi_reg_write(NRF21540_REG_CONFREG1, reg_val, NRF21540_EXEC_MODE_NON_BLOCKING, false);
  207. }
  208. inline uint32_t nrf21540_spim_trx_task_start_address_get(void)
  209. {
  210. return nrfx_spim_start_task_get(&spi);
  211. }
  212. void nrf21540_spim_for_trx_configure(nrf21540_trx_t dir, nrf21540_bool_state_t required_state)
  213. {
  214. if (dir == NRF21540_TX)
  215. {
  216. tx_en_drive(required_state);
  217. }
  218. else
  219. {
  220. rx_en_drive(required_state);
  221. }
  222. if (required_state == NRF21540_ENABLE)
  223. {
  224. uint32_t task_start_address = nrfx_spim_start_task_get(&spi);
  225. nrf_ppi_channel_endpoint_setup(NRF21540_TRX_PPI_CHANNEL,
  226. (uint32_t)nrf_timer_event_address_get(NRF21540_TIMER,
  227. NRF21540_TIMER_CC_PD_PG_EVENT),
  228. task_start_address);
  229. nrf_ppi_channel_enable(NRF21540_TRX_PPI_CHANNEL);
  230. }
  231. }
  232. ret_code_t nrf21540_spi_pwr_mode_set(nrf21540_pwr_mode_t mode)
  233. {
  234. if (mode == NRF21540_PWR_MODE_A)
  235. {
  236. spi_reg_write(NRF21540_REG_CONFREG0, NRF21540_BITS_CONFREG0_MODE_0,
  237. NRF21540_EXEC_MODE_BLOCKING, true);
  238. }
  239. else if (mode == NRF21540_PWR_MODE_B)
  240. {
  241. spi_reg_write(NRF21540_REG_CONFREG0, NRF21540_BITS_CONFREG0_MODE_1,
  242. NRF21540_EXEC_MODE_BLOCKING, true);
  243. }
  244. else
  245. {
  246. return NRF_ERROR_INVALID_PARAM;
  247. }
  248. return NRF_SUCCESS;
  249. }
  250. #endif /*NRF21540_USE_SPI_MANAGEMENT*/