watchdog.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @file watchdog.c
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. #include "rom_sym_def.h"
  11. #include "watchdog.h"
  12. #include "error.h"
  13. #include "pwrmgr.h"
  14. #include "clock.h"
  15. #include "jump_function.h"
  16. #include "watchdog.h"
  17. extern volatile uint8 g_clk32K_config;
  18. extern uint32_t s_config_swClk1;
  19. typedef struct {
  20. bool enable;
  21. WdtRspMode_t mode;
  22. pfnHdlCB_t handleCB;
  23. }WdtCtl_t;
  24. WdtCtl_t wdtCtl;
  25. /**
  26. * @fn static void __attribute__((used)) hal_WATCHDOG_IRQHandler(void)
  27. * @brief WDT interrupt handler funtion. wdt clock source is 32KHz low speed clock.
  28. * @param[in] none
  29. * @return none.
  30. */
  31. void __attribute__((used)) hal_WATCHDOG_IRQHandler(void)
  32. {
  33. // volatile uint32_t a;
  34. // a = AP_WDT->EOI;
  35. // AP_WDT->CRR = 0x76;
  36. if(wdtCtl.handleCB) {
  37. wdtCtl.handleCB();
  38. }
  39. }
  40. /**
  41. * @fn void HalWdtFeed(void)
  42. * @brief wdt feed
  43. * @param[in] none
  44. * @return none.
  45. */
  46. void HalWdtFeed(void)
  47. {
  48. AP_WDT->CRR = 0x76;
  49. }
  50. /**
  51. * @fn static bool watchdog_init(WDG_CYCLE_Type_e cycle)
  52. * @brief wdt initialize config
  53. * @param[in] cycle: WDG_CYCLE_Type_e
  54. * @return boot SUCCESS/.
  55. */
  56. static bool watchdog_init(void)
  57. {
  58. volatile uint32_t a;
  59. uint8_t delay;
  60. if(g_clk32K_config == CLK_32K_XTAL)//rtc use 32K XOSC,watchdog use the same
  61. {
  62. AP_PCRM->CLKSEL |= (1UL<<16);
  63. }
  64. else
  65. {
  66. AP_PCRM->CLKSEL &= ~(1UL<<16); //rtc use 32K RCOSC,watchdog use the same
  67. }
  68. hal_clk_gate_enable(MOD_WDT);
  69. s_config_swClk1|=_CLK_WDT; //add watchdog clk in pwrmg wakeup restore clk;
  70. if((AP_PCR->SW_RESET0 & 0x04)==0)
  71. {
  72. AP_PCR->SW_RESET0 |= 0x04;
  73. delay = 20;
  74. while(delay-->0);
  75. }
  76. if((AP_PCR->SW_RESET2 & 0x04)==0)
  77. {
  78. AP_PCR->SW_RESET2 |= 0x04;
  79. delay=20;
  80. while(delay-->0);
  81. }
  82. AP_PCR->SW_RESET2 &= ~0x20;
  83. delay=20;
  84. while(delay-->0);
  85. AP_PCR->SW_RESET2 |= 0x20;
  86. delay=20;
  87. while(delay-->0);
  88. a = AP_WDT->EOI;
  89. AP_WDT->TORR = HAL_WDG_CFG_CYCLE; //config wdt cycle
  90. if(wdtCtl.mode == WDG_USE_INT_MODE)
  91. {
  92. JUMP_FUNCTION(V10_IRQ_HANDLER) = (uint32_t)&hal_WATCHDOG_IRQHandler;
  93. AP_WDT->CR = 0x1F; //use intteruct mode
  94. NVIC_SetPriority((IRQn_Type)WDT_IRQn, IRQ_PRIO_HAL);
  95. NVIC_EnableIRQ((IRQn_Type)WDT_IRQn);
  96. }
  97. else if(wdtCtl.mode == WDG_USE_POLLING_MODE)
  98. {
  99. JUMP_FUNCTION(V10_IRQ_HANDLER) = 0;
  100. AP_WDT->CR = 0x1D; //no use intteruct mode
  101. NVIC_DisableIRQ((IRQn_Type)WDT_IRQn);
  102. }
  103. AP_WDT->CRR = 0x76;
  104. return SUCCESS;
  105. }
  106. /**
  107. * @fn void HalWdtInit(FunctionalState_t newState, WdtRspMode_t mode, pfnHdlCB_t irqHdlCallback)
  108. * @brief wdt initialize
  109. * @param[in] newState: ENABLE or DISABLE
  110. * @param[in] mode: WDT_RSP_MODE_NO_INT or WDT_RSP_MODE_INT
  111. * @param[in] irqHdlCallback: interrupt handle callback
  112. * @return none.
  113. */
  114. void HalWdtInit(FunctionalState_t newState, WdtRspMode_t mode,pfnHdlCB_t irqHdlCallback)
  115. {
  116. wdtCtl.mode = mode;
  117. wdtCtl.handleCB = irqHdlCallback;
  118. if(newState == ENABLE) {
  119. watchdog_init();
  120. JUMP_FUNCTION(HAL_WATCHDOG_INIT) = (uint32_t)&watchdog_init;
  121. }
  122. else{
  123. JUMP_FUNCTION(HAL_WATCHDOG_INIT) = NULL;
  124. }
  125. }