nrf_sdm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) Nordic Semiconductor ASA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
  16. * contributors to this software may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * 4. This software must only be used in a processor manufactured by Nordic
  20. * Semiconductor ASA, or in a processor manufactured by a third party that
  21. * is used in combination with a processor manufactured by Nordic Semiconductor.
  22. *
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  28. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  31. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. /**
  37. @defgroup nrf_sdm_api SoftDevice Manager API
  38. @{
  39. @brief APIs for SoftDevice management.
  40. */
  41. /* Header guard */
  42. #ifndef NRF_SDM_H__
  43. #define NRF_SDM_H__
  44. #include "nrf_svc.h"
  45. #include "nrf.h"
  46. #include "nrf_soc.h"
  47. #include "nrf_error_sdm.h"
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /** @addtogroup NRF_SDM_DEFINES Defines
  52. * @{ */
  53. #ifdef NRFSOC_DOXYGEN
  54. //Stuff defined elsewere, to satisfy doxygen
  55. #define MBR_SIZE 0
  56. #warning test
  57. #endif
  58. /** @brief SoftDevice Manager SVC Base number. */
  59. #define SDM_SVC_BASE 0x10
  60. /** @brief Defines the SoftDevice Information Structure location (address) as an offset from
  61. the start of the softdevice (without MBR)*/
  62. #define SOFTDEVICE_INFO_STRUCT_OFFSET (0x2000)
  63. /** @brief Defines the absolute Softdevice information structure location (address)*/
  64. #define SOFTDEVICE_INFO_STRUCT_ADDRESS (SOFTDEVICE_INFO_STRUCT_OFFSET + MBR_SIZE)
  65. /** @brief Defines the offset for Softdevice size value relative to Softdevice base address*/
  66. #define SD_SIZE_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x08)
  67. /** @brief Defines the offset for FWID value relative to Softdevice base address*/
  68. #define SD_FWID_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x0C)
  69. /** @brief Defines a macro for retreiving the actual Softdevice size value from a given base address
  70. use @ref MBR_SIZE when Softdevice is installed just above the MBR (the usual case)*/
  71. #define SD_SIZE_GET(baseaddr) (*((uint32_t *) ((baseaddr) + SD_SIZE_OFFSET)))
  72. /** @brief Defines a macro for retreiving the actual FWID value from a given base address
  73. use @ref MBR_SIZE when Softdevice is installed just above the MBR (the usual case)*/
  74. #define SD_FWID_GET(baseaddr) ((*((uint32_t *) ((baseaddr) + SD_FWID_OFFSET))) & 0xFFFF)
  75. /**@defgroup NRF_FAULT_ID_RANGES Fault ID ranges
  76. * @{ */
  77. #define NRF_FAULT_ID_SD_RANGE_START 0x00000000 /**< SoftDevice ID range start. */
  78. #define NRF_FAULT_ID_APP_RANGE_START 0x00001000 /**< Application ID range start. */
  79. /**@} */
  80. /**@defgroup NRF_FAULT_IDS Fault ID types
  81. * @{ */
  82. #define NRF_FAULT_ID_SD_ASSERT (NRF_FAULT_ID_SD_RANGE_START + 1) /**< SoftDevice assertion. The info parameter will be set to 0x00000000. */
  83. #define NRF_FAULT_ID_APP_MEMACC (NRF_FAULT_ID_APP_RANGE_START + 1) /**< Application invalid memory access. The info parameter will contain the address in memory that was accessed. */
  84. /**@} */
  85. /** @} */
  86. /** @addtogroup NRF_SDM_ENUMS Enumerations
  87. * @{ */
  88. /**@brief nRF SoftDevice Manager API SVC numbers. */
  89. enum NRF_SD_SVCS
  90. {
  91. SD_SOFTDEVICE_ENABLE = SDM_SVC_BASE, /**< ::sd_softdevice_enable */
  92. SD_SOFTDEVICE_DISABLE, /**< ::sd_softdevice_disable */
  93. SD_SOFTDEVICE_IS_ENABLED, /**< ::sd_softdevice_is_enabled */
  94. SD_SOFTDEVICE_VECTOR_TABLE_BASE_SET, /**< ::sd_softdevice_vector_table_base_set */
  95. SVC_SDM_LAST /**< Placeholder for last SDM SVC */
  96. };
  97. /** @} */
  98. /** @addtogroup NRF_SDM_DEFINES Defines
  99. * @{ */
  100. /**@defgroup NRF_CLOCK_LF_XTAL_ACCURACY Clock accuracy * @{ */
  101. #define NRF_CLOCK_LF_XTAL_ACCURACY_250_PPM (0) /* Default */
  102. #define NRF_CLOCK_LF_XTAL_ACCURACY_500_PPM (1)
  103. #define NRF_CLOCK_LF_XTAL_ACCURACY_150_PPM (2)
  104. #define NRF_CLOCK_LF_XTAL_ACCURACY_100_PPM (3)
  105. #define NRF_CLOCK_LF_XTAL_ACCURACY_75_PPM (4)
  106. #define NRF_CLOCK_LF_XTAL_ACCURACY_50_PPM (5)
  107. #define NRF_CLOCK_LF_XTAL_ACCURACY_30_PPM (6)
  108. #define NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM (7)
  109. /** @} */
  110. /**@defgroup NRF_CLOCK_LF_SRC Possible lfclk oscillator sources * @{ */
  111. #define NRF_CLOCK_LF_SRC_RC (0) /**< LFCLK RC oscillator. */
  112. #define NRF_CLOCK_LF_SRC_XTAL (1) /**< LFCLK crystal oscillator. */
  113. #define NRF_CLOCK_LF_SRC_SYNTH (2) /**< LFCLK Synthesized from HFCLK. */
  114. /** @} */
  115. /** @} */
  116. /** @addtogroup NRF_SDM_TYPES Types
  117. * @{ */
  118. /**@brief Type representing lfclk oscillator source. */
  119. typedef struct
  120. {
  121. uint8_t source; /**< LF oscillator clock source, see @ref NRF_CLOCK_LF_SRC. */
  122. uint8_t rc_ctiv; /**< Only for NRF_CLOCK_LF_SRC_RC: Calibration timer interval in 1/4 second
  123. units (nRF51: 1-64, nRF52: 1-32).
  124. @note To avoid excessive clock drift, 0.5 degrees Celsius is the
  125. maximum temperature change allowed in one calibration timer
  126. interval. The interval should be selected to ensure this.
  127. @note Must be 0 if source is not NRF_CLOCK_LF_SRC_RC. */
  128. uint8_t rc_temp_ctiv; /**< Only for NRF_CLOCK_LF_SRC_RC: How often (in number of calibration
  129. intervals) the RC oscillator shall be calibrated if the temperature
  130. hasn't changed.
  131. 0: Always calibrate even if the temperature hasn't changed.
  132. 1: Only calibrate if the temperature has changed (nRF51 only).
  133. 2-33: Check the temperature and only calibrate if it has changed,
  134. however calibration will take place every rc_temp_ctiv
  135. intervals in any case.
  136. @note Must be 0 if source is not NRF_CLOCK_LF_SRC_RC.
  137. @note For nRF52, the application must ensure calibration at least once
  138. every 8 seconds to ensure +/-250ppm clock stability. The
  139. recommended configuration for NRF_CLOCK_LF_SRC_RC on nRF52 is
  140. rc_ctiv=16 and rc_temp_ctiv=2. This will ensure calibration at
  141. least once every 8 seconds and for temperature changes of 0.5
  142. degrees Celsius every 4 seconds. See the Product Specification
  143. for the nRF52 device being used for more information.*/
  144. uint8_t xtal_accuracy; /**< External crystal clock accuracy used in the LL to compute timing windows.
  145. @note For the NRF_CLOCK_LF_SRC_RC clock source this parameter is ignored. */
  146. } nrf_clock_lf_cfg_t;
  147. /**@brief Fault Handler type.
  148. *
  149. * When certain unrecoverable errors occur within the application or SoftDevice the fault handler will be called back.
  150. * The protocol stack will be in an undefined state when this happens and the only way to recover will be to
  151. * perform a reset, using e.g. CMSIS NVIC_SystemReset().
  152. *
  153. * @note This callback is executed in HardFault context, thus SVC functions cannot be called from the fault callback.
  154. *
  155. * @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  156. * @param[in] pc The program counter of the instruction that triggered the fault.
  157. * @param[in] info Optional additional information regarding the fault. Refer to each Fault identifier for details.
  158. */
  159. typedef void (*nrf_fault_handler_t)(uint32_t id, uint32_t pc, uint32_t info);
  160. /** @} */
  161. /** @addtogroup NRF_SDM_FUNCTIONS Functions
  162. * @{ */
  163. /**@brief Enables the SoftDevice and by extension the protocol stack.
  164. *
  165. * @note Some care must be taken if a low frequency clock source is already running when calling this function:
  166. * If the LF clock has a different source then the one currently running, it will be stopped. Then, the new
  167. * clock source will be started.
  168. *
  169. * @note This function has no effect when returning with an error.
  170. *
  171. * @post If return code is ::NRF_SUCCESS
  172. * - SoC library and protocol stack APIs are made available.
  173. * - A portion of RAM will be unavailable (see relevant SDS documentation).
  174. * - Some peripherals will be unavailable or available only through the SoC API (see relevant SDS documentation).
  175. * - Interrupts will not arrive from protected peripherals or interrupts.
  176. * - nrf_nvic_ functions must be used instead of CMSIS NVIC_ functions for reliable usage of the SoftDevice.
  177. * - Interrupt latency may be affected by the SoftDevice (see relevant SDS documentation).
  178. * - Chosen low frequency clock source will be running.
  179. *
  180. * @param p_clock_lf_cfg Low frequency clock source and accuracy.
  181. If NULL the clock will be configured as an rc source with rc_ctiv = 16 and .rc_temp_ctiv = 2
  182. In the case of XTAL source, the PPM accuracy of the chosen clock source must be greater than or equal to the actual characteristics of your XTAL clock.
  183. * @param fault_handler Callback to be invoked in case of fault.
  184. *
  185. * @retval ::NRF_SUCCESS
  186. * @retval ::NRF_ERROR_INVALID_STATE SoftDevice is already enabled, and the clock source and fault handler cannot be updated.
  187. * @retval ::NRF_ERROR_SDM_INCORRECT_INTERRUPT_CONFIGURATION SoftDevice interrupt is already enabled, or an enabled interrupt has an illegal priority level.
  188. * @retval ::NRF_ERROR_SDM_LFCLK_SOURCE_UNKNOWN Unknown low frequency clock source selected.
  189. */
  190. SVCALL(SD_SOFTDEVICE_ENABLE, uint32_t, sd_softdevice_enable(nrf_clock_lf_cfg_t const * p_clock_lf_cfg, nrf_fault_handler_t fault_handler));
  191. /**@brief Disables the SoftDevice and by extension the protocol stack.
  192. *
  193. * Idempotent function to disable the SoftDevice.
  194. *
  195. * @post SoC library and protocol stack APIs are made unavailable.
  196. * @post All interrupts that was protected by the SoftDevice will be disabled and initialized to priority 0 (highest).
  197. * @post All peripherals used by the SoftDevice will be reset to default values.
  198. * @post All of RAM become available.
  199. * @post All interrupts are forwarded to the application.
  200. * @post LFCLK source chosen in ::sd_softdevice_enable will be left running.
  201. *
  202. * @retval ::NRF_SUCCESS
  203. */
  204. SVCALL(SD_SOFTDEVICE_DISABLE, uint32_t, sd_softdevice_disable(void));
  205. /**@brief Check if the SoftDevice is enabled.
  206. *
  207. * @param[out] p_softdevice_enabled If the SoftDevice is enabled: 1 else 0.
  208. *
  209. * @retval ::NRF_SUCCESS
  210. */
  211. SVCALL(SD_SOFTDEVICE_IS_ENABLED, uint32_t, sd_softdevice_is_enabled(uint8_t * p_softdevice_enabled));
  212. /**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the SoftDevice
  213. *
  214. * This function is only intended to be called when a bootloader is enabled.
  215. *
  216. * @param[in] address The base address of the interrupt vector table for forwarded interrupts.
  217. * @retval ::NRF_SUCCESS
  218. */
  219. SVCALL(SD_SOFTDEVICE_VECTOR_TABLE_BASE_SET, uint32_t, sd_softdevice_vector_table_base_set(uint32_t address));
  220. /** @} */
  221. #ifdef __cplusplus
  222. }
  223. #endif
  224. #endif // NRF_SDM_H__
  225. /**
  226. @}
  227. */