nrf_wdt.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. NRF_WDT->CONFIG = behaviour;
  118. }
  119. /**
  120. * @brief Function for starting the watchdog.
  121. *
  122. * @param[in] task Task.
  123. */
  124. __STATIC_INLINE void nrf_wdt_task_trigger(nrf_wdt_task_t task)
  125. {
  126. *((volatile uint32_t *)((uint8_t *)NRF_WDT + task)) = NRF_WDT_TASK_SET;
  127. }
  128. /**
  129. * @brief Function for clearing the WDT event.
  130. *
  131. * @param[in] event Event.
  132. */
  133. __STATIC_INLINE void nrf_wdt_event_clear(nrf_wdt_event_t event)
  134. {
  135. *((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)event)) = NRF_WDT_EVENT_CLEAR;
  136. #if __CORTEX_M == 0x04
  137. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)event));
  138. (void)dummy;
  139. #endif
  140. }
  141. /**
  142. * @brief Function for retrieving the state of the WDT event.
  143. *
  144. * @param[in] event Event.
  145. *
  146. * @retval true If the event is set.
  147. * @retval false If the event is not set.
  148. */
  149. __STATIC_INLINE bool nrf_wdt_event_check(nrf_wdt_event_t event)
  150. {
  151. return (bool)*((volatile uint32_t *)((uint8_t *)NRF_WDT + event));
  152. }
  153. /**
  154. * @brief Function for enabling a specific interrupt.
  155. *
  156. * @param[in] int_mask Interrupt.
  157. */
  158. __STATIC_INLINE void nrf_wdt_int_enable(uint32_t int_mask)
  159. {
  160. NRF_WDT->INTENSET = int_mask;
  161. }
  162. /**
  163. * @brief Function for retrieving the state of given interrupt.
  164. *
  165. * @param[in] int_mask Interrupt.
  166. *
  167. * @retval true Interrupt is enabled.
  168. * @retval false Interrupt is not enabled.
  169. */
  170. __STATIC_INLINE bool nrf_wdt_int_enable_check(uint32_t int_mask)
  171. {
  172. return (bool)(NRF_WDT->INTENSET & int_mask);
  173. }
  174. /**
  175. * @brief Function for disabling a specific interrupt.
  176. *
  177. * @param[in] int_mask Interrupt.
  178. */
  179. __STATIC_INLINE void nrf_wdt_int_disable(uint32_t int_mask)
  180. {
  181. NRF_WDT->INTENCLR = int_mask;
  182. }
  183. /**
  184. * @brief Function for returning the address of a specific WDT task register.
  185. *
  186. * @param[in] task Task.
  187. */
  188. __STATIC_INLINE uint32_t nrf_wdt_task_address_get(nrf_wdt_task_t task)
  189. {
  190. return ((uint32_t)NRF_WDT + task);
  191. }
  192. /**
  193. * @brief Function for returning the address of a specific WDT event register.
  194. *
  195. * @param[in] event Event.
  196. *
  197. * @retval address of requested event register
  198. */
  199. __STATIC_INLINE uint32_t nrf_wdt_event_address_get(nrf_wdt_event_t event)
  200. {
  201. return ((uint32_t)NRF_WDT + event);
  202. }
  203. /**
  204. * @brief Function for retrieving the watchdog status.
  205. *
  206. * @retval true If the watchdog is started.
  207. * @retval false If the watchdog is not started.
  208. */
  209. __STATIC_INLINE bool nrf_wdt_started(void)
  210. {
  211. return (bool)(NRF_WDT->RUNSTATUS);
  212. }
  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. return (bool)(((NRF_WDT->REQSTATUS) >> rr_register) & 0x1UL);
  224. }
  225. /**
  226. * @brief Function for setting the watchdog reload value.
  227. *
  228. * @param[in] reload_value Watchdog counter initial value.
  229. */
  230. __STATIC_INLINE void nrf_wdt_reload_value_set(uint32_t reload_value)
  231. {
  232. NRF_WDT->CRV = reload_value;
  233. }
  234. /**
  235. * @brief Function for retrieving the watchdog reload value.
  236. *
  237. * @retval Reload value.
  238. */
  239. __STATIC_INLINE uint32_t nrf_wdt_reload_value_get(void)
  240. {
  241. return (uint32_t)NRF_WDT->CRV;
  242. }
  243. /**
  244. * @brief Function for enabling a specific reload request register.
  245. *
  246. * @param[in] rr_register Reload request register to enable.
  247. */
  248. __STATIC_INLINE void nrf_wdt_reload_request_enable(nrf_wdt_rr_register_t rr_register)
  249. {
  250. NRF_WDT->RREN |= 0x1UL << rr_register;
  251. }
  252. /**
  253. * @brief Function for disabling a specific reload request register.
  254. *
  255. * @param[in] rr_register Reload request register to disable.
  256. */
  257. __STATIC_INLINE void nrf_wdt_reload_request_disable(nrf_wdt_rr_register_t rr_register)
  258. {
  259. NRF_WDT->RREN &= ~(0x1UL << rr_register);
  260. }
  261. /**
  262. * @brief Function for retrieving the status of a specific reload request register.
  263. *
  264. * @param[in] rr_register Reload request register to check.
  265. *
  266. * @retval true If the reload request register is enabled.
  267. * @retval false If the reload request register is not enabled.
  268. */
  269. __STATIC_INLINE bool nrf_wdt_reload_request_is_enabled(nrf_wdt_rr_register_t rr_register)
  270. {
  271. return (bool)(NRF_WDT->RREN & (0x1UL << rr_register));
  272. }
  273. /**
  274. * @brief Function for setting a specific reload request register.
  275. *
  276. * @param[in] rr_register Reload request register to set.
  277. */
  278. __STATIC_INLINE void nrf_wdt_reload_request_set(nrf_wdt_rr_register_t rr_register)
  279. {
  280. NRF_WDT->RR[rr_register] = NRF_WDT_RR_VALUE;
  281. }
  282. /** @} */
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif