wdt_hal.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*******************************************************************************
  15. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. #pragma once
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #include <stdbool.h>
  24. #include "soc/timer_group_caps.h"
  25. #include "hal/wdt_types.h"
  26. #include "hal/mwdt_ll.h"
  27. #include "hal/rwdt_ll.h"
  28. /**
  29. * Context that should be maintained by both the driver and the HAL
  30. */
  31. typedef struct {
  32. wdt_inst_t inst; /**< Which WDT instance this HAL context is using (i.e. MWDT0, MWDT1, RWDT)*/
  33. union {
  34. timg_dev_t *mwdt_dev; /**< Starting address of the MWDT */
  35. rtc_cntl_dev_t *rwdt_dev; /**< Starting address of the RWDT*/
  36. };
  37. } wdt_hal_context_t;
  38. /* ---------------------------- Init and Config ----------------------------- */
  39. /**
  40. * @brief Initialize one of the WDTs associated HAL context
  41. *
  42. * This function initializes one of the WDTs (MWDT0, MWDT1, or RWDT) hardware by
  43. * doing the following:
  44. * - Disables the WDT and all of its stages
  45. * - Sets some registers with default values
  46. * - Sets the WDTs source clock prescaler (not applicable to RWDT)
  47. * - Optionally enables the level interrupt
  48. *
  49. * The HAL context is initialized by storing the type (i.e. MWDT or RWDT) of
  50. * this WDT instance, and a pointer to the associated registers.
  51. *
  52. * @param hal Context of HAL layer
  53. * @param wdt_inst Which WDT instance to initialize (MWDT0, MWDT1, or RWDT)
  54. * @param prescaler MWDT source clock prescaler. Unused for RWDT
  55. * @param enable_intr True to enable level interrupt. False to disable
  56. *
  57. * @note Although the WDTs on the ESP32 have an edge interrupt, this HAL does
  58. * not utilize it and will always disables it.
  59. * @note RWDT does not have a prescaler. Its tick rate is equal to the
  60. * frequency of its source clock (RTC slow clock).
  61. */
  62. void wdt_hal_init(wdt_hal_context_t *hal, wdt_inst_t wdt_inst, uint32_t prescaler, bool enable_intr);
  63. /**
  64. * @brief Deinitialize a WDT and its HAL context
  65. *
  66. * This function deinitializes a WDT by feeding then disabling it. The WDT's
  67. * interrupt is also cleared and disabled. The HAL context is cleared.
  68. *
  69. * @param hal Context of HAL layer
  70. */
  71. void wdt_hal_deinit(wdt_hal_context_t *hal);
  72. /**
  73. * @brief Configure a particular stage of a WDT
  74. *
  75. * @param hal Context of HAL layer
  76. * @param stage Stage to configure (0 to 3)
  77. * @param timeout Number of WDT ticks for the stage to time out
  78. * @param behavior What action to take when the stage times out. Note that only
  79. * the RWDT supports the RTC reset action.
  80. *
  81. * @note This function can only be called when the WDT is unlocked. Call
  82. * wdt_hal_write_protect_disable() first.
  83. */
  84. void wdt_hal_config_stage(wdt_hal_context_t *hal, wdt_stage_t stage, uint32_t timeout, wdt_stage_action_t behavior);
  85. /* -------------------------------- Runtime --------------------------------- */
  86. /**
  87. * @brief Disable write protection of the WDT registers
  88. *
  89. * @param hal Context of HAL layer
  90. */
  91. void wdt_hal_write_protect_disable(wdt_hal_context_t *hal);
  92. /**
  93. * @brief Enable write protection of the WDT registers
  94. *
  95. * @param hal Context of HAL layer
  96. */
  97. void wdt_hal_write_protect_enable(wdt_hal_context_t *hal);
  98. /**
  99. * @brief Enable the WDT
  100. *
  101. * The WDT will start counting when enabled. This function also feeds the WDT
  102. * before enabling it.
  103. *
  104. * @param hal Context of HAL layer
  105. *
  106. * @note This function can only be called when the WDT is unlocked. Call
  107. * wdt_hal_write_protect_disable() first.
  108. */
  109. void wdt_hal_enable(wdt_hal_context_t *hal);
  110. /**
  111. * @brief Disable the WDT
  112. *
  113. * @param hal Context of HAL layer
  114. *
  115. * @note This function can only be called when the WDT is unlocked. Call
  116. * wdt_hal_write_protect_disable() first.
  117. */
  118. void wdt_hal_disable(wdt_hal_context_t *hal);
  119. /**
  120. * @brief Handle WDT interrupt
  121. *
  122. * Clears the interrupt status bit and feeds the WDT
  123. *
  124. * @param hal Context of HAL layer
  125. *
  126. * @note This function can only be called when the WDT is unlocked. Call
  127. * wdt_hal_write_protect_disable() first.
  128. */
  129. void wdt_hal_handle_intr(wdt_hal_context_t *hal);
  130. /**
  131. * @brief Feed the WDT
  132. *
  133. * Feeding the WDT will reset the internal count and current stage.
  134. *
  135. * @param hal Context of HAL layer
  136. *
  137. * @note This function can only be called when the WDT is unlocked. Call
  138. * wdt_hal_write_protect_disable() first.
  139. */
  140. void wdt_hal_feed(wdt_hal_context_t *hal);
  141. /**
  142. * @brief Enable/Disable the WDT flash boot mode
  143. *
  144. * @param hal Context of HAL layer
  145. * @param enable True to enable flash boot mode, false to disable.
  146. *
  147. * @note Flash boot mode can trigger a time out even if the WDT is disabled.
  148. * @note This function can only be called when the WDT is unlocked. Call
  149. * wdt_hal_write_protect_disable() first.
  150. */
  151. void wdt_hal_set_flashboot_en(wdt_hal_context_t *hal, bool enable);
  152. /**
  153. * @brief Check if the WDT is enabled
  154. *
  155. * @param hal Context of HAL layer
  156. * @return True if enabled, false otherwise
  157. */
  158. bool wdt_hal_is_enabled(wdt_hal_context_t *hal);
  159. #ifdef __cplusplus
  160. }
  161. #endif