nrf_wdt.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**
  13. * @defgroup nrf_wdt_hal WDT HAL
  14. * @{
  15. * @ingroup nrf_wdt
  16. *
  17. * @brief Hardware access layer for accessing the watchdog timer (WDT) peripheral.
  18. */
  19. #ifndef NRF_WDT_H__
  20. #define NRF_WDT_H__
  21. #include <stddef.h>
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include "nrf.h"
  25. #define NRF_WDT_CHANNEL_NUMBER 0x8UL
  26. #define NRF_WDT_RR_VALUE 0x6E524635UL /* Fixed value, shouldn't be modified.*/
  27. #define NRF_WDT_TASK_SET 1UL
  28. #define NRF_WDT_EVENT_CLEAR 0UL
  29. /**
  30. * @enum nrf_wdt_task_t
  31. * @brief WDT tasks.
  32. */
  33. typedef enum
  34. {
  35. /*lint -save -e30 -esym(628,__INTADDR__)*/
  36. NRF_WDT_TASK_START = offsetof(NRF_WDT_Type, TASKS_START), /**< Task for starting WDT. */
  37. /*lint -restore*/
  38. } nrf_wdt_task_t;
  39. /**
  40. * @enum nrf_wdt_event_t
  41. * @brief WDT events.
  42. */
  43. typedef enum
  44. {
  45. /*lint -save -e30*/
  46. NRF_WDT_EVENT_TIMEOUT = offsetof(NRF_WDT_Type, EVENTS_TIMEOUT), /**< Event from WDT time-out. */
  47. /*lint -restore*/
  48. } nrf_wdt_event_t;
  49. /**
  50. * @enum nrf_wdt_behaviour_t
  51. * @brief WDT behavior in CPU SLEEP or HALT mode.
  52. */
  53. typedef enum
  54. {
  55. NRF_WDT_BEHAVIOUR_RUN_SLEEP = WDT_CONFIG_SLEEP_Msk, /**< WDT will run when CPU is in SLEEP mode. */
  56. NRF_WDT_BEHAVIOUR_RUN_HALT = WDT_CONFIG_HALT_Msk, /**< WDT will run when CPU is in HALT mode. */
  57. 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. */
  58. NRF_WDT_BEHAVIOUR_PAUSE_SLEEP_HALT = 0, /**< WDT will be paused when CPU is in SLEEP or HALT mode. */
  59. } nrf_wdt_behaviour_t;
  60. /**
  61. * @enum nrf_wdt_rr_register_t
  62. * @brief WDT reload request registers.
  63. */
  64. typedef enum
  65. {
  66. NRF_WDT_RR0 = 0, /**< Reload request register 0. */
  67. NRF_WDT_RR1, /**< Reload request register 1. */
  68. NRF_WDT_RR2, /**< Reload request register 2. */
  69. NRF_WDT_RR3, /**< Reload request register 3. */
  70. NRF_WDT_RR4, /**< Reload request register 4. */
  71. NRF_WDT_RR5, /**< Reload request register 5. */
  72. NRF_WDT_RR6, /**< Reload request register 6. */
  73. NRF_WDT_RR7 /**< Reload request register 7. */
  74. } nrf_wdt_rr_register_t;
  75. /**
  76. * @enum nrf_wdt_int_mask_t
  77. * @brief WDT interrupts.
  78. */
  79. typedef enum
  80. {
  81. NRF_WDT_INT_TIMEOUT_MASK = WDT_INTENSET_TIMEOUT_Msk, /**< WDT interrupt from time-out event. */
  82. } nrf_wdt_int_mask_t;
  83. /**
  84. * @brief Function for configuring the watchdog behavior when the CPU is sleeping or halted.
  85. *
  86. * @param behaviour Watchdog behavior when CPU is in SLEEP or HALT mode.
  87. */
  88. __STATIC_INLINE void nrf_wdt_behaviour_set(nrf_wdt_behaviour_t behaviour)
  89. {
  90. NRF_WDT->CONFIG = behaviour;
  91. }
  92. /**
  93. * @brief Function for starting the watchdog.
  94. *
  95. * @param[in] task Task.
  96. */
  97. __STATIC_INLINE void nrf_wdt_task_trigger(nrf_wdt_task_t task)
  98. {
  99. *((volatile uint32_t *)((uint8_t *)NRF_WDT + task)) = NRF_WDT_TASK_SET;
  100. }
  101. /**
  102. * @brief Function for clearing the WDT event.
  103. *
  104. * @param[in] event Event.
  105. */
  106. __STATIC_INLINE void nrf_wdt_event_clear(nrf_wdt_event_t event)
  107. {
  108. *((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)event)) = NRF_WDT_EVENT_CLEAR;
  109. }
  110. /**
  111. * @brief Function for retrieving the state of the WDT event.
  112. *
  113. * @param[in] event Event.
  114. *
  115. * @retval true If the event is set.
  116. * @retval false If the event is not set.
  117. */
  118. __STATIC_INLINE bool nrf_wdt_event_check(nrf_wdt_event_t event)
  119. {
  120. return (bool)*((volatile uint32_t *)((uint8_t *)NRF_WDT + event));
  121. }
  122. /**
  123. * @brief Function for enabling a specific interrupt.
  124. *
  125. * @param[in] int_mask Interrupt.
  126. */
  127. __STATIC_INLINE void nrf_wdt_int_enable(uint32_t int_mask)
  128. {
  129. NRF_WDT->INTENSET = int_mask;
  130. }
  131. /**
  132. * @brief Function for retrieving the state of given interrupt.
  133. *
  134. * @param[in] int_mask Interrupt.
  135. *
  136. * @retval true Interrupt is enabled.
  137. * @retval false Interrupt is not enabled.
  138. */
  139. __STATIC_INLINE bool nrf_wdt_int_enable_check(uint32_t int_mask)
  140. {
  141. return (bool)(NRF_WDT->INTENSET & int_mask);
  142. }
  143. /**
  144. * @brief Function for disabling a specific interrupt.
  145. *
  146. * @param[in] int_mask Interrupt.
  147. */
  148. __STATIC_INLINE void nrf_wdt_int_disable(uint32_t int_mask)
  149. {
  150. NRF_WDT->INTENCLR = int_mask;
  151. }
  152. /**
  153. * @brief Function for returning the address of a specific WDT task register.
  154. *
  155. * @param[in] task Task.
  156. */
  157. __STATIC_INLINE uint32_t nrf_wdt_task_address_get(nrf_wdt_task_t task)
  158. {
  159. return ((uint32_t)NRF_WDT + task);
  160. }
  161. /**
  162. * @brief Function for returning the address of a specific WDT event register.
  163. *
  164. * @param[in] event Event.
  165. *
  166. * @retval address of requested event register
  167. */
  168. __STATIC_INLINE uint32_t nrf_wdt_event_address_get(nrf_wdt_event_t event)
  169. {
  170. return ((uint32_t)NRF_WDT + event);
  171. }
  172. /**
  173. * @brief Function for retrieving the watchdog status.
  174. *
  175. * @retval true If the watchdog is started.
  176. * @retval false If the watchdog is not started.
  177. */
  178. __STATIC_INLINE bool nrf_wdt_started(void)
  179. {
  180. return (bool)(NRF_WDT->RUNSTATUS);
  181. }
  182. /**
  183. * @brief Function for retrieving the watchdog reload request status.
  184. *
  185. * @param[in] rr_register Reload request register to check.
  186. *
  187. * @retval true If a reload request is running.
  188. * @retval false If no reload request is running.
  189. */
  190. __STATIC_INLINE bool nrf_wdt_request_status(nrf_wdt_rr_register_t rr_register)
  191. {
  192. return (bool)(((NRF_WDT->REQSTATUS) >> rr_register) & 0x1UL);
  193. }
  194. /**
  195. * @brief Function for setting the watchdog reload value.
  196. *
  197. * @param[in] reload_value Watchdog counter initial value.
  198. */
  199. __STATIC_INLINE void nrf_wdt_reload_value_set(uint32_t reload_value)
  200. {
  201. NRF_WDT->CRV = reload_value;
  202. }
  203. /**
  204. * @brief Function for retrieving the watchdog reload value.
  205. *
  206. * @retval Reload value.
  207. */
  208. __STATIC_INLINE uint32_t nrf_wdt_reload_value_get(void)
  209. {
  210. return (uint32_t)NRF_WDT->CRV;
  211. }
  212. /**
  213. * @brief Function for enabling a specific reload request register.
  214. *
  215. * @param[in] rr_register Reload request register to enable.
  216. */
  217. __STATIC_INLINE void nrf_wdt_reload_request_enable(nrf_wdt_rr_register_t rr_register)
  218. {
  219. NRF_WDT->RREN |= 0x1UL << rr_register;
  220. }
  221. /**
  222. * @brief Function for disabling a specific reload request register.
  223. *
  224. * @param[in] rr_register Reload request register to disable.
  225. */
  226. __STATIC_INLINE void nrf_wdt_reload_request_disable(nrf_wdt_rr_register_t rr_register)
  227. {
  228. NRF_WDT->RREN &= ~(0x1UL << rr_register);
  229. }
  230. /**
  231. * @brief Function for retrieving the status of a specific reload request register.
  232. *
  233. * @param[in] rr_register Reload request register to check.
  234. *
  235. * @retval true If the reload request register is enabled.
  236. * @retval false If the reload request register is not enabled.
  237. */
  238. __STATIC_INLINE bool nrf_wdt_reload_request_is_enabled(nrf_wdt_rr_register_t rr_register)
  239. {
  240. return (bool)(NRF_WDT->RREN & (0x1UL << rr_register));
  241. }
  242. /**
  243. * @brief Function for setting a specific reload request register.
  244. *
  245. * @param[in] rr_register Reload request register to set.
  246. */
  247. __STATIC_INLINE void nrf_wdt_reload_request_set(nrf_wdt_rr_register_t rr_register)
  248. {
  249. NRF_WDT->RR[rr_register] = NRF_WDT_RR_VALUE;
  250. }
  251. #endif
  252. /** @} */