nrf_sdm.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2015 - 2018, 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, except as embedded into a Nordic
  12. * Semiconductor ASA integrated circuit in a product or a software update for
  13. * such product, must reproduce the above copyright notice, this list of
  14. * conditions and the following disclaimer in the documentation and/or other
  15. * materials provided with the distribution.
  16. *
  17. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * 4. This software, with or without modification, must only be used with a
  22. * Nordic Semiconductor ASA integrated circuit.
  23. *
  24. * 5. Any software provided in binary form under this license must not be reverse
  25. * engineered, decompiled, modified and/or disassembled.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  36. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /**
  39. @defgroup nrf_sdm_api SoftDevice Manager API
  40. @{
  41. @brief APIs for SoftDevice management.
  42. */
  43. #ifndef NRF_SDM_H__
  44. #define NRF_SDM_H__
  45. #include <stdint.h>
  46. #include "nrf.h"
  47. #include "nrf_svc.h"
  48. #include "nrf_error.h"
  49. #include "nrf_error_sdm.h"
  50. #include "nrf_soc.h"
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /** @addtogroup NRF_SDM_DEFINES Defines
  55. * @{ */
  56. #ifdef NRFSOC_DOXYGEN
  57. /// Declared in nrf_mbr.h
  58. #define MBR_SIZE 0
  59. #warning test
  60. #endif
  61. /** @brief The major version for the SoftDevice binary distributed with this header file. */
  62. #define SD_MAJOR_VERSION (7)
  63. /** @brief The minor version for the SoftDevice binary distributed with this header file. */
  64. #define SD_MINOR_VERSION (2)
  65. /** @brief The bugfix version for the SoftDevice binary distributed with this header file. */
  66. #define SD_BUGFIX_VERSION (0)
  67. /** @brief The SoftDevice variant of this firmware. */
  68. #define SD_VARIANT_ID 113
  69. /** @brief The full version number for the SoftDevice binary this header file was distributed
  70. * with, as a decimal number in the form Mmmmbbb, where:
  71. * - M is major version (one or more digits)
  72. * - mmm is minor version (three digits)
  73. * - bbb is bugfix version (three digits). */
  74. #define SD_VERSION (SD_MAJOR_VERSION * 1000000 + SD_MINOR_VERSION * 1000 + SD_BUGFIX_VERSION)
  75. /** @brief SoftDevice Manager SVC Base number. */
  76. #define SDM_SVC_BASE 0x10
  77. /** @brief SoftDevice unique string size in bytes. */
  78. #define SD_UNIQUE_STR_SIZE 20
  79. /** @brief Invalid info field. Returned when an info field does not exist. */
  80. #define SDM_INFO_FIELD_INVALID (0)
  81. /** @brief Defines the SoftDevice Information Structure location (address) as an offset from
  82. the start of the SoftDevice (without MBR)*/
  83. #define SOFTDEVICE_INFO_STRUCT_OFFSET (0x2000)
  84. /** @brief Defines the absolute SoftDevice Information Structure location (address) when the
  85. * SoftDevice is installed just above the MBR (the usual case). */
  86. #define SOFTDEVICE_INFO_STRUCT_ADDRESS (SOFTDEVICE_INFO_STRUCT_OFFSET + MBR_SIZE)
  87. /** @brief Defines the offset for the SoftDevice Information Structure size value relative to the
  88. * SoftDevice base address. The size value is of type uint8_t. */
  89. #define SD_INFO_STRUCT_SIZE_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET)
  90. /** @brief Defines the offset for the SoftDevice size value relative to the SoftDevice base address.
  91. * The size value is of type uint32_t. */
  92. #define SD_SIZE_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x08)
  93. /** @brief Defines the offset for FWID value relative to the SoftDevice base address. The FWID value
  94. * is of type uint16_t. */
  95. #define SD_FWID_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x0C)
  96. /** @brief Defines the offset for the SoftDevice ID relative to the SoftDevice base address. The ID
  97. * is of type uint32_t. */
  98. #define SD_ID_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x10)
  99. /** @brief Defines the offset for the SoftDevice version relative to the SoftDevice base address in
  100. * the same format as @ref SD_VERSION, stored as an uint32_t. */
  101. #define SD_VERSION_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x14)
  102. /** @brief Defines the offset for the SoftDevice unique string relative to the SoftDevice base address.
  103. * The SD_UNIQUE_STR is stored as an array of uint8_t. The size of array is @ref SD_UNIQUE_STR_SIZE.
  104. */
  105. #define SD_UNIQUE_STR_OFFSET (SOFTDEVICE_INFO_STRUCT_OFFSET + 0x18)
  106. /** @brief Defines a macro for retrieving the actual SoftDevice Information Structure size value
  107. * from a given base address. Use @ref MBR_SIZE as the argument when the SoftDevice is
  108. * installed just above the MBR (the usual case). */
  109. #define SD_INFO_STRUCT_SIZE_GET(baseaddr) (*((uint8_t *) ((baseaddr) + SD_INFO_STRUCT_SIZE_OFFSET)))
  110. /** @brief Defines a macro for retrieving the actual SoftDevice size value from a given base
  111. * address. Use @ref MBR_SIZE as the argument when the SoftDevice is installed just above
  112. * the MBR (the usual case). */
  113. #define SD_SIZE_GET(baseaddr) (*((uint32_t *) ((baseaddr) + SD_SIZE_OFFSET)))
  114. /** @brief Defines the amount of flash that is used by the SoftDevice.
  115. * Add @ref MBR_SIZE to find the first available flash address when the SoftDevice is installed
  116. * just above the MBR (the usual case).
  117. */
  118. #define SD_FLASH_SIZE 0x1B000
  119. /** @brief Defines a macro for retrieving the actual FWID value from a given base address. Use
  120. * @ref MBR_SIZE as the argument when the SoftDevice is installed just above the MBR (the usual
  121. * case). */
  122. #define SD_FWID_GET(baseaddr) (*((uint16_t *) ((baseaddr) + SD_FWID_OFFSET)))
  123. /** @brief Defines a macro for retrieving the actual SoftDevice ID from a given base address. Use
  124. * @ref MBR_SIZE as the argument when the SoftDevice is installed just above the MBR (the
  125. * usual case). */
  126. #define SD_ID_GET(baseaddr) ((SD_INFO_STRUCT_SIZE_GET(baseaddr) > (SD_ID_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) \
  127. ? (*((uint32_t *) ((baseaddr) + SD_ID_OFFSET))) : SDM_INFO_FIELD_INVALID)
  128. /** @brief Defines a macro for retrieving the actual SoftDevice version from a given base address.
  129. * Use @ref MBR_SIZE as the argument when the SoftDevice is installed just above the MBR
  130. * (the usual case). */
  131. #define SD_VERSION_GET(baseaddr) ((SD_INFO_STRUCT_SIZE_GET(baseaddr) > (SD_VERSION_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) \
  132. ? (*((uint32_t *) ((baseaddr) + SD_VERSION_OFFSET))) : SDM_INFO_FIELD_INVALID)
  133. /** @brief Defines a macro for retrieving the address of SoftDevice unique str based on a given base address.
  134. * Use @ref MBR_SIZE as the argument when the SoftDevice is installed just above the MBR
  135. * (the usual case). */
  136. #define SD_UNIQUE_STR_ADDR_GET(baseaddr) ((SD_INFO_STRUCT_SIZE_GET(baseaddr) > (SD_UNIQUE_STR_OFFSET - SOFTDEVICE_INFO_STRUCT_OFFSET)) \
  137. ? (((uint8_t *) ((baseaddr) + SD_UNIQUE_STR_OFFSET))) : SDM_INFO_FIELD_INVALID)
  138. /**@defgroup NRF_FAULT_ID_RANGES Fault ID ranges
  139. * @{ */
  140. #define NRF_FAULT_ID_SD_RANGE_START 0x00000000 /**< SoftDevice ID range start. */
  141. #define NRF_FAULT_ID_APP_RANGE_START 0x00001000 /**< Application ID range start. */
  142. /**@} */
  143. /**@defgroup NRF_FAULT_IDS Fault ID types
  144. * @{ */
  145. #define NRF_FAULT_ID_SD_ASSERT (NRF_FAULT_ID_SD_RANGE_START + 1) /**< SoftDevice assertion. The info parameter is reserved for future used. */
  146. #define NRF_FAULT_ID_APP_MEMACC (NRF_FAULT_ID_APP_RANGE_START + 1) /**< Application invalid memory access. The info parameter will contain 0x00000000,
  147. in case of SoftDevice RAM access violation. In case of SoftDevice peripheral
  148. register violation the info parameter will contain the sub-region number of
  149. PREGION[0], on whose address range the disallowed write access caused the
  150. memory access fault. */
  151. /**@} */
  152. /** @} */
  153. /** @addtogroup NRF_SDM_ENUMS Enumerations
  154. * @{ */
  155. /**@brief nRF SoftDevice Manager API SVC numbers. */
  156. enum NRF_SD_SVCS
  157. {
  158. SD_SOFTDEVICE_ENABLE = SDM_SVC_BASE, /**< ::sd_softdevice_enable */
  159. SD_SOFTDEVICE_DISABLE, /**< ::sd_softdevice_disable */
  160. SD_SOFTDEVICE_IS_ENABLED, /**< ::sd_softdevice_is_enabled */
  161. SD_SOFTDEVICE_VECTOR_TABLE_BASE_SET, /**< ::sd_softdevice_vector_table_base_set */
  162. SVC_SDM_LAST /**< Placeholder for last SDM SVC */
  163. };
  164. /** @} */
  165. /** @addtogroup NRF_SDM_DEFINES Defines
  166. * @{ */
  167. /**@defgroup NRF_CLOCK_LF_ACCURACY Clock accuracy
  168. * @{ */
  169. #define NRF_CLOCK_LF_ACCURACY_250_PPM (0) /**< Default: 250 ppm */
  170. #define NRF_CLOCK_LF_ACCURACY_500_PPM (1) /**< 500 ppm */
  171. #define NRF_CLOCK_LF_ACCURACY_150_PPM (2) /**< 150 ppm */
  172. #define NRF_CLOCK_LF_ACCURACY_100_PPM (3) /**< 100 ppm */
  173. #define NRF_CLOCK_LF_ACCURACY_75_PPM (4) /**< 75 ppm */
  174. #define NRF_CLOCK_LF_ACCURACY_50_PPM (5) /**< 50 ppm */
  175. #define NRF_CLOCK_LF_ACCURACY_30_PPM (6) /**< 30 ppm */
  176. #define NRF_CLOCK_LF_ACCURACY_20_PPM (7) /**< 20 ppm */
  177. #define NRF_CLOCK_LF_ACCURACY_10_PPM (8) /**< 10 ppm */
  178. #define NRF_CLOCK_LF_ACCURACY_5_PPM (9) /**< 5 ppm */
  179. #define NRF_CLOCK_LF_ACCURACY_2_PPM (10) /**< 2 ppm */
  180. #define NRF_CLOCK_LF_ACCURACY_1_PPM (11) /**< 1 ppm */
  181. /** @} */
  182. /**@defgroup NRF_CLOCK_LF_SRC Possible LFCLK oscillator sources
  183. * @{ */
  184. #define NRF_CLOCK_LF_SRC_RC (0) /**< LFCLK RC oscillator. */
  185. #define NRF_CLOCK_LF_SRC_XTAL (1) /**< LFCLK crystal oscillator. */
  186. #define NRF_CLOCK_LF_SRC_SYNTH (2) /**< LFCLK Synthesized from HFCLK. */
  187. /** @} */
  188. /** @} */
  189. /** @addtogroup NRF_SDM_TYPES Types
  190. * @{ */
  191. /**@brief Type representing LFCLK oscillator source. */
  192. typedef struct
  193. {
  194. uint8_t source; /**< LF oscillator clock source, see @ref NRF_CLOCK_LF_SRC. */
  195. uint8_t rc_ctiv; /**< Only for ::NRF_CLOCK_LF_SRC_RC: Calibration timer interval in 1/4 second
  196. units (nRF52: 1-32).
  197. @note To avoid excessive clock drift, 0.5 degrees Celsius is the
  198. maximum temperature change allowed in one calibration timer
  199. interval. The interval should be selected to ensure this.
  200. @note Must be 0 if source is not ::NRF_CLOCK_LF_SRC_RC. */
  201. uint8_t rc_temp_ctiv; /**< Only for ::NRF_CLOCK_LF_SRC_RC: How often (in number of calibration
  202. intervals) the RC oscillator shall be calibrated if the temperature
  203. hasn't changed.
  204. 0: Always calibrate even if the temperature hasn't changed.
  205. 1: Only calibrate if the temperature has changed (legacy - nRF51 only).
  206. 2-33: Check the temperature and only calibrate if it has changed,
  207. however calibration will take place every rc_temp_ctiv
  208. intervals in any case.
  209. @note Must be 0 if source is not ::NRF_CLOCK_LF_SRC_RC.
  210. @note For nRF52, the application must ensure calibration at least once
  211. every 8 seconds to ensure +/-500 ppm clock stability. The
  212. recommended configuration for ::NRF_CLOCK_LF_SRC_RC on nRF52 is
  213. rc_ctiv=16 and rc_temp_ctiv=2. This will ensure calibration at
  214. least once every 8 seconds and for temperature changes of 0.5
  215. degrees Celsius every 4 seconds. See the Product Specification
  216. for the nRF52 device being used for more information.*/
  217. uint8_t accuracy; /**< External clock accuracy used in the LL to compute timing
  218. windows, see @ref NRF_CLOCK_LF_ACCURACY.*/
  219. } nrf_clock_lf_cfg_t;
  220. /**@brief Fault Handler type.
  221. *
  222. * When certain unrecoverable errors occur within the application or SoftDevice the fault handler will be called back.
  223. * The protocol stack will be in an undefined state when this happens and the only way to recover will be to
  224. * perform a reset, using e.g. CMSIS NVIC_SystemReset().
  225. * If the application returns from the fault handler the SoftDevice will call NVIC_SystemReset().
  226. *
  227. * @note It is recommended to either perform a reset in the fault handler or to let the SoftDevice reset the device.
  228. * Otherwise SoC peripherals may behave in an undefined way. For example, the RADIO peripherial may
  229. * continously transmit packets.
  230. *
  231. * @note This callback is executed in HardFault context, thus SVC functions cannot be called from the fault callback.
  232. *
  233. * @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  234. * @param[in] pc The program counter of the instruction that triggered the fault.
  235. * @param[in] info Optional additional information regarding the fault. Refer to each Fault identifier for details.
  236. *
  237. * @note When id is set to @ref NRF_FAULT_ID_APP_MEMACC, pc will contain the address of the instruction being executed at the time when
  238. * the fault is detected by the CPU. The CPU program counter may have advanced up to 2 instructions (no branching) after the one that triggered the fault.
  239. */
  240. typedef void (*nrf_fault_handler_t)(uint32_t id, uint32_t pc, uint32_t info);
  241. /** @} */
  242. /** @addtogroup NRF_SDM_FUNCTIONS Functions
  243. * @{ */
  244. /**@brief Enables the SoftDevice and by extension the protocol stack.
  245. *
  246. * @note Some care must be taken if a low frequency clock source is already running when calling this function:
  247. * If the LF clock has a different source then the one currently running, it will be stopped. Then, the new
  248. * clock source will be started.
  249. *
  250. * @note This function has no effect when returning with an error.
  251. *
  252. * @post If return code is ::NRF_SUCCESS
  253. * - SoC library and protocol stack APIs are made available.
  254. * - A portion of RAM will be unavailable (see relevant SDS documentation).
  255. * - Some peripherals will be unavailable or available only through the SoC API (see relevant SDS documentation).
  256. * - Interrupts will not arrive from protected peripherals or interrupts.
  257. * - nrf_nvic_ functions must be used instead of CMSIS NVIC_ functions for reliable usage of the SoftDevice.
  258. * - Interrupt latency may be affected by the SoftDevice (see relevant SDS documentation).
  259. * - Chosen low frequency clock source will be running.
  260. *
  261. * @param p_clock_lf_cfg Low frequency clock source and accuracy.
  262. If NULL the clock will be configured as an RC source with rc_ctiv = 16 and .rc_temp_ctiv = 2
  263. 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.
  264. * @param fault_handler Callback to be invoked in case of fault, cannot be NULL.
  265. *
  266. * @retval ::NRF_SUCCESS
  267. * @retval ::NRF_ERROR_INVALID_ADDR Invalid or NULL pointer supplied.
  268. * @retval ::NRF_ERROR_INVALID_STATE SoftDevice is already enabled, and the clock source and fault handler cannot be updated.
  269. * @retval ::NRF_ERROR_SDM_INCORRECT_INTERRUPT_CONFIGURATION SoftDevice interrupt is already enabled, or an enabled interrupt has an illegal priority level.
  270. * @retval ::NRF_ERROR_SDM_LFCLK_SOURCE_UNKNOWN Unknown low frequency clock source selected.
  271. * @retval ::NRF_ERROR_INVALID_PARAM Invalid clock source configuration supplied in p_clock_lf_cfg.
  272. */
  273. 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));
  274. /**@brief Disables the SoftDevice and by extension the protocol stack.
  275. *
  276. * Idempotent function to disable the SoftDevice.
  277. *
  278. * @post SoC library and protocol stack APIs are made unavailable.
  279. * @post All interrupts that was protected by the SoftDevice will be disabled and initialized to priority 0 (highest).
  280. * @post All peripherals used by the SoftDevice will be reset to default values.
  281. * @post All of RAM become available.
  282. * @post All interrupts are forwarded to the application.
  283. * @post LFCLK source chosen in ::sd_softdevice_enable will be left running.
  284. *
  285. * @retval ::NRF_SUCCESS
  286. */
  287. SVCALL(SD_SOFTDEVICE_DISABLE, uint32_t, sd_softdevice_disable(void));
  288. /**@brief Check if the SoftDevice is enabled.
  289. *
  290. * @param[out] p_softdevice_enabled If the SoftDevice is enabled: 1 else 0.
  291. *
  292. * @retval ::NRF_SUCCESS
  293. */
  294. SVCALL(SD_SOFTDEVICE_IS_ENABLED, uint32_t, sd_softdevice_is_enabled(uint8_t * p_softdevice_enabled));
  295. /**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the SoftDevice
  296. *
  297. * This function is only intended to be called when a bootloader is enabled.
  298. *
  299. * @param[in] address The base address of the interrupt vector table for forwarded interrupts.
  300. * @retval ::NRF_SUCCESS
  301. */
  302. SVCALL(SD_SOFTDEVICE_VECTOR_TABLE_BASE_SET, uint32_t, sd_softdevice_vector_table_base_set(uint32_t address));
  303. /** @} */
  304. #ifdef __cplusplus
  305. }
  306. #endif
  307. #endif // NRF_SDM_H__
  308. /**
  309. @}
  310. */