nrf_wdt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * Copyright (c) 2015 - 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 NRF_WDT_H__
  41. #define NRF_WDT_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_wdt_hal WDT HAL
  48. * @{
  49. * @ingroup nrf_wdt
  50. * @brief Hardware access layer for managing the Watchdog Timer (WDT) peripheral.
  51. */
  52. /** @brief Number of WDT channels. */
  53. #define NRF_WDT_CHANNEL_NUMBER 0x8UL
  54. /** @brief WDT register reload value. */
  55. #define NRF_WDT_RR_VALUE 0x6E524635UL /* Fixed value; should not be modified. */
  56. /** @brief WDT tasks. */
  57. typedef enum
  58. {
  59. NRF_WDT_TASK_START = offsetof(NRF_WDT_Type, TASKS_START), /**< Task for starting WDT. */
  60. } nrf_wdt_task_t;
  61. /** @brief WDT events. */
  62. typedef enum
  63. {
  64. NRF_WDT_EVENT_TIMEOUT = offsetof(NRF_WDT_Type, EVENTS_TIMEOUT), /**< Event from WDT time-out. */
  65. } nrf_wdt_event_t;
  66. /** @brief WDT behavior in the SLEEP or HALT CPU modes. */
  67. typedef enum
  68. {
  69. NRF_WDT_BEHAVIOUR_RUN_SLEEP = WDT_CONFIG_SLEEP_Msk, /**< WDT will run when CPU is in SLEEP mode. */
  70. NRF_WDT_BEHAVIOUR_RUN_HALT = WDT_CONFIG_HALT_Msk, /**< WDT will run when CPU is in HALT mode. */
  71. NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT = WDT_CONFIG_SLEEP_Msk | WDT_CONFIG_HALT_Msk, /**< WDT will run when CPU is in SLEEP or HALT mode. */
  72. NRF_WDT_BEHAVIOUR_PAUSE_SLEEP_HALT = 0, /**< WDT will be paused when CPU is in SLEEP or HALT mode. */
  73. } nrf_wdt_behaviour_t;
  74. /** @brief WDT reload request registers. */
  75. typedef enum
  76. {
  77. NRF_WDT_RR0 = 0, /**< Reload request register 0. */
  78. NRF_WDT_RR1, /**< Reload request register 1. */
  79. NRF_WDT_RR2, /**< Reload request register 2. */
  80. NRF_WDT_RR3, /**< Reload request register 3. */
  81. NRF_WDT_RR4, /**< Reload request register 4. */
  82. NRF_WDT_RR5, /**< Reload request register 5. */
  83. NRF_WDT_RR6, /**< Reload request register 6. */
  84. NRF_WDT_RR7 /**< Reload request register 7. */
  85. } nrf_wdt_rr_register_t;
  86. /** @brief WDT interrupts. */
  87. typedef enum
  88. {
  89. NRF_WDT_INT_TIMEOUT_MASK = WDT_INTENSET_TIMEOUT_Msk, /**< WDT interrupt from time-out event. */
  90. } nrf_wdt_int_mask_t;
  91. /**
  92. * @brief Function for configuring the watchdog behavior when the CPU is sleeping or halted.
  93. *
  94. * @param behaviour Watchdog behavior when CPU is in SLEEP or HALT mode.
  95. */
  96. __STATIC_INLINE void nrf_wdt_behaviour_set(nrf_wdt_behaviour_t behaviour);
  97. /**
  98. * @brief Function for starting the WDT task.
  99. *
  100. * @param[in] task Task.
  101. */
  102. __STATIC_INLINE void nrf_wdt_task_trigger(nrf_wdt_task_t task);
  103. /**
  104. * @brief Function for clearing the WDT event register.
  105. *
  106. * @param[in] event Event.
  107. */
  108. __STATIC_INLINE void nrf_wdt_event_clear(nrf_wdt_event_t event);
  109. /**
  110. * @brief Function for retrieving the state of the WDT event.
  111. *
  112. * @param[in] event Event to be checked.
  113. *
  114. * @retval true The event has been generated.
  115. * @retval false The event has not been generated.
  116. */
  117. __STATIC_INLINE bool nrf_wdt_event_check(nrf_wdt_event_t event);
  118. /**
  119. * @brief Function for enabling the specified interrupt.
  120. *
  121. * @param[in] int_mask Interrupt.
  122. */
  123. __STATIC_INLINE void nrf_wdt_int_enable(uint32_t int_mask);
  124. /**
  125. * @brief Function for retrieving the state of given interrupt.
  126. *
  127. * @param[in] int_mask Interrupt.
  128. *
  129. * @retval true Interrupt is enabled.
  130. * @retval false Interrupt is not enabled.
  131. */
  132. __STATIC_INLINE bool nrf_wdt_int_enable_check(uint32_t int_mask);
  133. /**
  134. * @brief Function for disabling a specific interrupt.
  135. *
  136. * @param[in] int_mask Interrupt.
  137. */
  138. __STATIC_INLINE void nrf_wdt_int_disable(uint32_t int_mask);
  139. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  140. /**
  141. * @brief Function for setting the subscribe configuration for a given
  142. * WDT task.
  143. *
  144. * @param[in] task Task for which to set the configuration.
  145. * @param[in] channel Channel through which to subscribe events.
  146. */
  147. __STATIC_INLINE void nrf_wdt_subscribe_set(nrf_wdt_task_t task,
  148. uint8_t channel);
  149. /**
  150. * @brief Function for clearing the subscribe configuration for a given
  151. * WDT task.
  152. *
  153. * @param[in] task Task for which to clear the configuration.
  154. */
  155. __STATIC_INLINE void nrf_wdt_subscribe_clear(nrf_wdt_task_t task);
  156. /**
  157. * @brief Function for setting the publish configuration for a given
  158. * WDT event.
  159. *
  160. * @param[in] event Event for which to set the configuration.
  161. * @param[in] channel Channel through which to publish the event.
  162. */
  163. __STATIC_INLINE void nrf_wdt_publish_set(nrf_wdt_event_t event,
  164. uint8_t channel);
  165. /**
  166. * @brief Function for clearing the publish configuration for a given
  167. * WDT event.
  168. *
  169. * @param[in] event Event for which to clear the configuration.
  170. */
  171. __STATIC_INLINE void nrf_wdt_publish_clear(nrf_wdt_event_t event);
  172. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  173. /**
  174. * @brief Function for returning the address of a specific WDT task register.
  175. *
  176. * @param[in] task Task.
  177. */
  178. __STATIC_INLINE uint32_t nrf_wdt_task_address_get(nrf_wdt_task_t task);
  179. /**
  180. * @brief Function for returning the address of a specific WDT event register.
  181. *
  182. * @param[in] event Event.
  183. *
  184. * @return Address of requested event register
  185. */
  186. __STATIC_INLINE uint32_t nrf_wdt_event_address_get(nrf_wdt_event_t event);
  187. /**
  188. * @brief Function for retrieving the watchdog status.
  189. *
  190. * @retval true The watchdog is started.
  191. * @retval false The watchdog is not started.
  192. */
  193. __STATIC_INLINE bool nrf_wdt_started(void);
  194. /**
  195. * @brief Function for retrieving the watchdog reload request status.
  196. *
  197. * @param[in] rr_register Reload request register to be checked.
  198. *
  199. * @retval true Reload request is running.
  200. * @retval false No reload requests are running.
  201. */
  202. __STATIC_INLINE bool nrf_wdt_request_status(nrf_wdt_rr_register_t rr_register);
  203. /**
  204. * @brief Function for setting the watchdog reload value.
  205. *
  206. * @param[in] reload_value Watchdog counter initial value.
  207. */
  208. __STATIC_INLINE void nrf_wdt_reload_value_set(uint32_t reload_value);
  209. /**
  210. * @brief Function for retrieving the watchdog reload value.
  211. *
  212. * @return Reload value.
  213. */
  214. __STATIC_INLINE uint32_t nrf_wdt_reload_value_get(void);
  215. /**
  216. * @brief Function for enabling a specific reload request register.
  217. *
  218. * @param[in] rr_register Reload request register to be enabled.
  219. */
  220. __STATIC_INLINE void nrf_wdt_reload_request_enable(nrf_wdt_rr_register_t rr_register);
  221. /**
  222. * @brief Function for disabling a specific reload request register.
  223. *
  224. * @param[in] rr_register Reload request register to be disabled.
  225. */
  226. __STATIC_INLINE void nrf_wdt_reload_request_disable(nrf_wdt_rr_register_t rr_register);
  227. /**
  228. * @brief Function for retrieving the status of a specific reload request register.
  229. *
  230. * @param[in] rr_register Reload request register to be checked.
  231. *
  232. * @retval true The reload request register is enabled.
  233. * @retval false The reload request register is not enabled.
  234. */
  235. __STATIC_INLINE bool nrf_wdt_reload_request_is_enabled(nrf_wdt_rr_register_t rr_register);
  236. /**
  237. * @brief Function for setting a specific reload request register.
  238. *
  239. * @param[in] rr_register Reload request register to set.
  240. */
  241. __STATIC_INLINE void nrf_wdt_reload_request_set(nrf_wdt_rr_register_t rr_register);
  242. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  243. __STATIC_INLINE void nrf_wdt_behaviour_set(nrf_wdt_behaviour_t behaviour)
  244. {
  245. NRF_WDT->CONFIG = behaviour;
  246. }
  247. __STATIC_INLINE void nrf_wdt_task_trigger(nrf_wdt_task_t task)
  248. {
  249. *((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)task)) = 0x01UL;
  250. }
  251. __STATIC_INLINE void nrf_wdt_event_clear(nrf_wdt_event_t event)
  252. {
  253. *((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)event)) = 0x0UL;
  254. #if __CORTEX_M == 0x04
  255. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)event));
  256. (void)dummy;
  257. #endif
  258. }
  259. __STATIC_INLINE bool nrf_wdt_event_check(nrf_wdt_event_t event)
  260. {
  261. return (bool)*((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)event));
  262. }
  263. __STATIC_INLINE void nrf_wdt_int_enable(uint32_t int_mask)
  264. {
  265. NRF_WDT->INTENSET = int_mask;
  266. }
  267. __STATIC_INLINE bool nrf_wdt_int_enable_check(uint32_t int_mask)
  268. {
  269. return (bool)(NRF_WDT->INTENSET & int_mask);
  270. }
  271. __STATIC_INLINE void nrf_wdt_int_disable(uint32_t int_mask)
  272. {
  273. NRF_WDT->INTENCLR = int_mask;
  274. }
  275. #if defined(DPPI_PRESENT)
  276. __STATIC_INLINE void nrf_wdt_subscribe_set(nrf_wdt_task_t task,
  277. uint8_t channel)
  278. {
  279. *((volatile uint32_t *) ((uint8_t *) NRF_WDT + (uint32_t) task + 0x80uL)) =
  280. ((uint32_t)channel | WDT_SUBSCRIBE_START_EN_Msk);
  281. }
  282. __STATIC_INLINE void nrf_wdt_subscribe_clear(nrf_wdt_task_t task)
  283. {
  284. *((volatile uint32_t *) ((uint8_t *) NRF_WDT + (uint32_t) task + 0x80uL)) = 0;
  285. }
  286. __STATIC_INLINE void nrf_wdt_publish_set(nrf_wdt_event_t event,
  287. uint8_t channel)
  288. {
  289. *((volatile uint32_t *) ((uint8_t *) NRF_WDT + (uint32_t) event + 0x80uL)) =
  290. ((uint32_t)channel | WDT_PUBLISH_TIMEOUT_EN_Msk);
  291. }
  292. __STATIC_INLINE void nrf_wdt_publish_clear(nrf_wdt_event_t event)
  293. {
  294. *((volatile uint32_t *) ((uint8_t *) NRF_WDT + (uint32_t) event + 0x80uL)) = 0;
  295. }
  296. #endif // defined(DPPI_PRESENT)
  297. __STATIC_INLINE uint32_t nrf_wdt_task_address_get(nrf_wdt_task_t task)
  298. {
  299. return ((uint32_t)NRF_WDT + (uint32_t)task);
  300. }
  301. __STATIC_INLINE uint32_t nrf_wdt_event_address_get(nrf_wdt_event_t event)
  302. {
  303. return ((uint32_t)NRF_WDT + (uint32_t)event);
  304. }
  305. __STATIC_INLINE bool nrf_wdt_started(void)
  306. {
  307. return (bool)(NRF_WDT->RUNSTATUS);
  308. }
  309. __STATIC_INLINE bool nrf_wdt_request_status(nrf_wdt_rr_register_t rr_register)
  310. {
  311. return (bool)(((NRF_WDT->REQSTATUS) >> rr_register) & 0x1UL);
  312. }
  313. __STATIC_INLINE void nrf_wdt_reload_value_set(uint32_t reload_value)
  314. {
  315. NRF_WDT->CRV = reload_value;
  316. }
  317. __STATIC_INLINE uint32_t nrf_wdt_reload_value_get(void)
  318. {
  319. return (uint32_t)NRF_WDT->CRV;
  320. }
  321. __STATIC_INLINE void nrf_wdt_reload_request_enable(nrf_wdt_rr_register_t rr_register)
  322. {
  323. NRF_WDT->RREN |= 0x1UL << rr_register;
  324. }
  325. __STATIC_INLINE void nrf_wdt_reload_request_disable(nrf_wdt_rr_register_t rr_register)
  326. {
  327. NRF_WDT->RREN &= ~(0x1UL << rr_register);
  328. }
  329. __STATIC_INLINE bool nrf_wdt_reload_request_is_enabled(nrf_wdt_rr_register_t rr_register)
  330. {
  331. return (bool)(NRF_WDT->RREN & (0x1UL << rr_register));
  332. }
  333. __STATIC_INLINE void nrf_wdt_reload_request_set(nrf_wdt_rr_register_t rr_register)
  334. {
  335. NRF_WDT->RR[rr_register] = NRF_WDT_RR_VALUE;
  336. }
  337. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  338. /** @} */
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif // NRF_WDT_H__