nrf21540_spi.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #ifndef NRF21540_SPI_H_
  41. #define NRF21540_SPI_H_
  42. #include <stdbool.h>
  43. #include <stdint.h>
  44. #include "nrfx_spim.h"
  45. #include "nrf21540_types.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * @brief nRF21540 SPI interface parameters defines.
  51. */
  52. #define NRF21540_SPI_LENGTH_BYTES 2 ///< SPI tx/rx buffer size in bytes.
  53. #define NRF21540_SPI_COMMAND_ADDR_BYTE 0 ///< Position of command field in SPI frame.
  54. #define NRF21540_SPI_DATA_BYTE 1 ///< Position of data field in SPI frame.
  55. #define NRF21540_SPI_COMMAND_Pos 6 ///< Command code bit-position in command field.
  56. #define NRF21540_SPI_REG_Pos 0 ///< Register address bit-position in command field.
  57. #define NRF21540_SPI_COMMAND_NOP 0x00 ///< 'NOP' command code.
  58. #define NRF21540_SPI_COMMAND_READ 0x02 ///< 'READ' command code.
  59. #define NRF21540_SPI_COMMAND_WRITE 0x03 ///< 'WRITE' command code.
  60. /**@brief CONFREG0 register bitfields.
  61. */
  62. #define NRF21540_BITS_CONFREG0_TX_EN_Pos 0 ///< Position of TX_EN field.
  63. #define NRF21540_BITS_CONFREG0_TX_EN_Msk (1 << NRF21540_BITS_CONFREG0_TX_EN_Pos) ///< Bit mask of TX_EN field.
  64. #define NRF21540_BITS_CONFREG0_TX_EN_Disable 0 ///< Disable TX mode.
  65. #define NRF21540_BITS_CONFREG0_TX_EN_Enable 1 ///< Enable TX mode.
  66. #define NRF21540_BITS_CONFREG0_MODE_Pos 1 ///< Position of MODE field.
  67. #define NRF21540_BITS_CONFREG0_MODE_Msk (1 << NRF21540_BITS_CONFREG0_MODE_Pos) ///< Bit mask of MODE field.
  68. #define NRF21540_BITS_CONFREG0_MODE_0 0 ///< Selects MODE 0.
  69. #define NRF21540_BITS_CONFREG0_MODE_1 1 ///< Selects MODE 1.
  70. #define NRF21540_BITS_CONFREG0_TX_GAIN_Pos 2 ///< Position of TX_GAIN field.
  71. #define NRF21540_BITS_CONFREG0_TX_GAIN_Msk (0x1F << NRF21540_BITS_CONFREG0_TX_GAIN_Pos) ///< Bit mask of TX_GAIN field.
  72. #define NRF21540_BITS_CONFREG0_TX_GAIN_Min 0 ///< Minimum TX_GAIN register value
  73. #define NRF21540_BITS_CONFREG0_TX_GAIN_Max 31 ///< Maximum TX_GAIN register value
  74. /**@brief CONFREG1 register bitfields.
  75. */
  76. #define NRF21540_BITS_CONFREG1_RX_EN_Pos 0 ///< Position of RX_EN field.
  77. #define NRF21540_BITS_CONFREG1_RX_EN_Msk (1 << NRF21540_BITS_CONFREG1_RX_EN_Pos) ///< Bit mask of TX_EN field.
  78. #define NRF21540_BITS_CONFREG1_RX_EN_Disable 0 ///< Disable RX mode.
  79. #define NRF21540_BITS_CONFREG1_RX_EN_Enable 1 ///< Enable RX mode.
  80. #define NRF21540_BITS_CONFREG1_UICR_EN_Pos 2 ///< Position of UICR_EN field.
  81. #define NRF21540_BITS_CONFREG1_UICR_EN_Msk (1 << NRF21540_BITS_CONFREG1_UICR_EN_Pos) ///< Bit mask of UICR_EN field.
  82. #define NRF21540_BITS_CONFREG1_UICR_EN_Disable 0 ///< Disable UICR program mode.
  83. #define NRF21540_BITS_CONFREG1_UICR_EN_Enable 1 ///< Enable UICR program mode.
  84. #define NRF21540_BITS_CONFREG1_KEY_Pos 4 ///< Position of KEY field.
  85. #define NRF21540_BITS_CONFREG1_KEY_Msk (0x0F << NRF21540_BITS_CONFREG1_KEY_Pos) ///< Bit mask of KEY field.
  86. #define NRF21540_BITS_CONFREG1_KEY_Enter 15 ///< Enter UICR program mode.
  87. #define NRF21540_BITS_CONFREG1_KEY_Leave 0 ///< Leave UICR program mode.
  88. /**@brief CONFREG2 register bitfields.
  89. */
  90. #define NRF21540_BITS_CONFREG2_POUTA_UICR_Pos 0 ///< Position of POUTA_UICR field.
  91. #define NRF21540_BITS_CONFREG2_POUTA_UICR_Msk (0x1F << NRF21540_BITS_CONFREG2_POUTA_UICR_Pos) ///< Bit mask of POUTA_UICR field.
  92. #define NRF21540_BITS_CONFREG2_POUTA_UICR_Min 0 ///< Minimum POUTA_UICR register value
  93. #define NRF21540_BITS_CONFREG2_POUTA_UICR_Max 31 ///< Maximum POUTA_UICR register value
  94. #define NRF21540_BITS_CONFREG2_POUTA_SEL_Pos 5 ///< Position of POUTA_SEL field.
  95. #define NRF21540_BITS_CONFREG2_POUTA_SEL_Msk (1 << NRF21540_BITS_CONFREG2_POUTA_SEL_Pos) ///< Bit mask of POUTA_SEL field.
  96. #define NRF21540_BITS_CONFREG2_POUTA_SEL_PROD 0 ///< Initialize TX_GAIN register with 20dBm value.
  97. #define NRF21540_BITS_CONFREG2_POUTA_SEL_UICR 1 ///< Initialize TX_GAIN register with POUTA_UICR value.
  98. #define NRF21540_BITS_CONFREG2_WR_UICR_Pos 7 ///< Position of WR_UICR field.
  99. #define NRF21540_BITS_CONFREG2_WR_UICR_Msk (1 << NRF21540_BITS_CONFREG2_WR_UICR_Pos) ///< Bit mask of WR_UICR field.
  100. #define NRF21540_BITS_CONFREG2_WR_UICR_IDLE 0 ///< EFUSE idle .
  101. #define NRF21540_BITS_CONFREG2_WR_UICR_WRITE 1 ///< EFUSE write.
  102. /**@brief CONFREG3 register bitfields.
  103. */
  104. #define NRF21540_BITS_CONFREG3_POUTB_UICR_Pos 0 ///< Position of POUTB_UICR field.
  105. #define NRF21540_BITS_CONFREG3_POUTB_UICR_Msk (0x1F << NRF21540_BITS_CONFREG3_POUTB_SEL_Pos) ///< Bit mask of POUTB_UICR field.
  106. #define NRF21540_BITS_CONFREG3_POUTB_UICR_Min 0 ///< Minimum POUTB_UICR register value
  107. #define NRF21540_BITS_CONFREG3_POUTB_UICR_Max 31 ///< Maximum POUTB_UICR register value
  108. #define NRF21540_BITS_CONFREG3_POUTB_SEL_Pos 5 ///< Position of POUTB_SEL field.
  109. #define NRF21540_BITS_CONFREG3_POUTB_SEL_Msk (1 << NRF21540_BITS_CONFREG3_POUTB_SEL_Pos) ///< Bit mask of POUTB_SEL field.
  110. #define NRF21540_BITS_CONFREG3_POUTB_SEL_PROD 0 ///< Initialize TX_GAIN register with 20dBm value.
  111. #define NRF21540_BITS_CONFREG3_POUTB_SEL_UICR 1 ///< Initialize TX_GAIN register with POUTB_UICR value.
  112. /**@brief PARTNUMBER register bitfields.
  113. */
  114. #define NRF21540_PARTNUMBER_PARTNUMBER_Pos 0 ///< Position of PARTNUMBER field.
  115. #define NRF21540_PARTNUMBER_PARTNUMBER_Msk (0xFF << NRF21540_PARTNUMBER_PARTNUMBER_Pos) ///< Bit mask of PARTNUMBER field.
  116. /**@brief HW_REVISON register bitfields.
  117. */
  118. #define NRF21540_HW_REVISON_HW_REVISION_Pos 4 ///< Position of HW_REVISON field.
  119. #define NRF21540_HW_REVISON_HW_REVISION_Msk (0xF << NRF21540_HW_REVISON_HW_REVISION_Pos) ///< Bit mask of HW_REVISON field.
  120. /**@brief HW_ID0 register bitfields.
  121. */
  122. #define NRF21540_HW_ID0_Pos 0 ///< Position of HW_ID0 field.
  123. #define NRF21540_HW_ID0_Msk (0xFF << NRF21540_HW_ID0_Pos) ///< Bit mask of HW_ID0 field.
  124. /**@brief HW_ID1 register bitfields.
  125. */
  126. #define NRF21540_HW_ID1_Pos 0 ///< Position of HW_ID1 field.
  127. #define NRF21540_HW_ID1_Msk (0xFF << NRF21540_HW_ID1_Pos) ///< Bit mask of HW_ID1 field.
  128. /**@brief nRF21540 internal registers.
  129. */
  130. typedef enum
  131. {
  132. NRF21540_REG_CONFREG0 = 0x00, ///< CONFREG0 register address.
  133. NRF21540_REG_CONFREG1 = 0x01, ///< CONFREG1 register address.
  134. NRF21540_REG_CONFREG2 = 0x02, ///< CONFREG2 register address.
  135. NRF21540_REG_CONFREG3 = 0x03, ///< CONFREG3 register address.
  136. NRF21540_REG_PARTNUMBER = 0x14, ///< PARTNUMBER register address.
  137. NRF21540_REG_HW_REVISION = 0x15, ///< HW_REVISION register address.
  138. NRF21540_REG_HW_ID0 = 0x16, ///< HW_ID0 register address.
  139. NRF21540_REG_HW_ID1 = 0x17, ///< HW_ID1 register address.
  140. } nrf21540_reg_t;
  141. /**@brief Function initializes SPI interface.
  142. *
  143. * @return NRF_ERROR_INTERNAL when SPIM driver initialization error occured.
  144. * NRF_ERROR_INVALID_STATE when nRF21540's state isn't proper
  145. * to perform the operation.
  146. * NRF_SUCCESS on success.
  147. */
  148. ret_code_t nrf21540_spi_init(void);
  149. /**@brief Function returns address of task which triggers SPI transfer.
  150. *
  151. * @return address of appropriate task.
  152. */
  153. uint32_t nrf21540_spim_trx_task_start_address_get(void);
  154. /**@brief Function configures the chip and peripherals for TX/RX transfer purpose.
  155. *
  156. * @details It can enable/disable RX/TX transfers.
  157. *
  158. * @param[in] dir Direction of the radio transmission. See @ref nrf21540_trx_t.
  159. * @param[in] required_state State of RX/TX transfer. See @ref nrf21540_bool_state_t.
  160. * chosen transfer type.
  161. */
  162. void nrf21540_spim_for_trx_configure(nrf21540_trx_t dir, nrf21540_bool_state_t required_state);
  163. /**@brief Function choses one of predefined power modes in nRF21540.
  164. *
  165. * @details Refer to nRF21540 Objective Product Specification, section: TX power control.
  166. *
  167. * @param[in] mode Power mode. See @ref nrf21540_pwr_mode_t.
  168. * @return NRF_ERROR_INVALID_PARAM when invalid argument given.
  169. * NRF_SUCCESS on success.
  170. */
  171. ret_code_t nrf21540_spi_pwr_mode_set(nrf21540_pwr_mode_t mode);
  172. /**@brief Function sets nRF21540 power state by driving PDN pin.
  173. *
  174. * @param[in] state Required PDN pin state.
  175. * @param[in] mode Execution mode. See @ref nrf21540_execution_mode_t.
  176. * @return NRF_ERROR_INVALID_PARAM when invalid argument given.
  177. * NRF_ERROR_INVALID_STATE when nRF21540's state isn't proper
  178. * to perform the operation (@sa nrf21540_state_t).
  179. * NRF_ERROR_INTERNAL when driver is in error state.
  180. * Reinitialization is required.
  181. * NRF_SUCCESS on success.
  182. */
  183. ret_code_t nrf21540_pdn_drive(bool state, nrf21540_execution_mode_t mode);
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif // NRF21540_SPI_H_