nrf_wdt.h 13 KB

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