watchdog.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Watchdog
  3. *
  4. * Copyright (c) 2018-2020 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "compat.h"
  21. #include "debug.h"
  22. #include "watchdog.h"
  23. #include "main.h"
  24. #include "util.h"
  25. #include "arithmetic.h"
  26. #include "battery.h"
  27. static struct {
  28. uint8_t state;
  29. uint8_t transition_delay;
  30. uint8_t active_wdto;
  31. bool standby;
  32. } watchdog;
  33. static const __flash struct {
  34. uint8_t wdto;
  35. uint16_t ms;
  36. } watchdog_timeouts[] = {
  37. { .wdto = WDTO_60MS, .ms = 60, }, /* First normal state */
  38. { .wdto = WDTO_120MS, .ms = 120, },
  39. { .wdto = WDTO_250MS, .ms = 250, },
  40. { .wdto = WDTO_500MS, .ms = 500, }, /* Last normal state */
  41. /* ----------------------------------- */
  42. { .wdto = WDTO_2S, .ms = 2000, }, /* Battery-low state */
  43. };
  44. #define WATCHDOG_FIRST_NORMAL_STATE 0u
  45. #define WATCHDOG_INIT_STATE (WATCHDOG_FIRST_NORMAL_STATE + 1u)
  46. #define WATCHDOG_LAST_NORMAL_STATE (ARRAY_SIZE(watchdog_timeouts) - 2u)
  47. #define WATCHDOG_BATCRIT_STATE (ARRAY_SIZE(watchdog_timeouts) - 1u)
  48. /* Watchdog state transition delay */
  49. #define WATCHDOG_TRANS_DELAY 100 /* Watchdog cycles */
  50. /* Write to the WDT hardware register. */
  51. static alwaysinline void wdt_setup(uint8_t wdto, bool wde, bool wdie)
  52. {
  53. const uint8_t first = (uint8_t)((1u << WDCE) | (1u << WDE));
  54. const uint8_t secnd = (uint8_t)((wde ? (1u << WDE) : 0u) |
  55. (wdie ? (1u << WDIE) : 0u) |
  56. (wdto & 0x07u));
  57. if (_SFR_IO_ADDR(WDTCR) > 0x3F) {
  58. __asm__ __volatile__(
  59. "wdr \n"
  60. "sts %[WDTCR_], %[FIRST_] \n"
  61. "sts %[WDTCR_], %[SECND_] \n"
  62. : /* no out */
  63. : [WDTCR_] "M" (_SFR_MEM_ADDR(WDTCR)),
  64. [FIRST_] "r" (first),
  65. [SECND_] "r" (secnd)
  66. );
  67. } else {
  68. __asm__ __volatile__(
  69. "wdr \n"
  70. "out %[WDTCR_], %[FIRST_] \n"
  71. "out %[WDTCR_], %[SECND_] \n"
  72. : /* no out */
  73. : [WDTCR_] "I" (_SFR_IO_ADDR(WDTCR)),
  74. [FIRST_] "r" (first),
  75. [SECND_] "r" (secnd)
  76. );
  77. }
  78. watchdog.active_wdto = wdto;
  79. }
  80. /* Get the currently active watchdog interrupt trigger interval, in milliseconds. */
  81. uint16_t watchdog_interval_ms(void)
  82. {
  83. if (USE_DEEP_SLEEP)
  84. return watchdog_timeouts[watchdog.state].ms;
  85. return 0;
  86. }
  87. /* Reconfigure the watchdog interval.
  88. * Interrupts shall be disabled before calling this function. */
  89. static void watchdog_reconfigure(void)
  90. {
  91. uint8_t wdto;
  92. if (USE_DEEP_SLEEP) {
  93. if (battery_voltage_is_critical()) {
  94. /* If the battery voltage is critical,
  95. * enforce the slowest watchdog interval. */
  96. watchdog.state = WATCHDOG_BATCRIT_STATE;
  97. watchdog.transition_delay = WATCHDOG_TRANS_DELAY;
  98. } else if (!watchdog.standby) {
  99. /* If the system is fully running,
  100. * enforce the fastest watchdog interval. */
  101. watchdog.state = WATCHDOG_FIRST_NORMAL_STATE;
  102. watchdog.transition_delay = WATCHDOG_TRANS_DELAY;
  103. } else {
  104. /* Limit the state to the normal range. */
  105. watchdog.state = min(watchdog.state,
  106. WATCHDOG_LAST_NORMAL_STATE);
  107. }
  108. /* Program the hardware, if required. */
  109. wdto = watchdog_timeouts[watchdog.state].wdto;
  110. if (wdto != watchdog.active_wdto)
  111. wdt_setup(wdto, true, USE_DEEP_SLEEP);
  112. }
  113. }
  114. /* Enable/disable watchdog standby state.
  115. * Interrupts shall be disabled before calling this function. */
  116. void watchdog_set_standby(bool standby)
  117. {
  118. if (USE_DEEP_SLEEP) {
  119. watchdog.standby = standby;
  120. watchdog_reconfigure();
  121. }
  122. }
  123. /* Watchdog timer interrupt service routine. */
  124. #if USE_DEEP_SLEEP
  125. ISR(WDT_vect)
  126. {
  127. memory_barrier();
  128. // dprintf("WDT_vect\r\n");
  129. /* Notify the system. */
  130. system_handle_watchdog_interrupt();
  131. /* Go to the next watchdog interval state,
  132. * if the system is in standby. */
  133. if (watchdog.standby) {
  134. watchdog.transition_delay = sub_sat_u8(watchdog.transition_delay, 1u);
  135. if (watchdog.transition_delay == 0u) {
  136. watchdog.transition_delay = WATCHDOG_TRANS_DELAY;
  137. if (watchdog.state < WATCHDOG_LAST_NORMAL_STATE)
  138. watchdog.state++;
  139. }
  140. }
  141. /* Configure the new interval, if required. */
  142. watchdog_reconfigure();
  143. memory_barrier();
  144. WDTCR |= (1 << WDIE);
  145. memory_barrier();
  146. }
  147. #endif /* USE_DEEP_SLEEP */
  148. /* Early watchdog timer initialization. */
  149. static void __attribute__((naked, used, section(".init3"))) wdt_early_init(void)
  150. {
  151. /* Clear WDRF (and all other reset info bits). */
  152. MCUSR = 0;
  153. /* Enable the watchdog. */
  154. wdt_setup(watchdog_timeouts[WATCHDOG_INIT_STATE].wdto,
  155. true, USE_DEEP_SLEEP);
  156. /* Enable watchdog interrupt for wake up from deep sleep. */
  157. if (USE_DEEP_SLEEP) {
  158. watchdog.state = WATCHDOG_INIT_STATE;
  159. watchdog.transition_delay = WATCHDOG_TRANS_DELAY;
  160. }
  161. }