suspend.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <stdbool.h>
  2. #include <avr/sleep.h>
  3. #include <avr/wdt.h>
  4. #include <avr/interrupt.h>
  5. #include "matrix.h"
  6. #include "action.h"
  7. #include "suspend.h"
  8. #include "timer.h"
  9. #include "led.h"
  10. #include "host.h"
  11. #ifdef PROTOCOL_LUFA
  12. # include "lufa.h"
  13. #endif
  14. #ifdef PROTOCOL_VUSB
  15. # include "vusb.h"
  16. #endif
  17. #ifdef BACKLIGHT_ENABLE
  18. # include "backlight.h"
  19. #endif
  20. #ifdef AUDIO_ENABLE
  21. # include "audio.h"
  22. #endif /* AUDIO_ENABLE */
  23. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  24. # include "rgblight.h"
  25. #endif
  26. /** \brief Suspend idle
  27. *
  28. * FIXME: needs doc
  29. */
  30. void suspend_idle(uint8_t time) {
  31. cli();
  32. set_sleep_mode(SLEEP_MODE_IDLE);
  33. sleep_enable();
  34. sei();
  35. sleep_cpu();
  36. sleep_disable();
  37. }
  38. // TODO: This needs some cleanup
  39. /** \brief Run keyboard level Power down
  40. *
  41. * FIXME: needs doc
  42. */
  43. __attribute__((weak)) void suspend_power_down_user(void) {}
  44. /** \brief Run keyboard level Power down
  45. *
  46. * FIXME: needs doc
  47. */
  48. __attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }
  49. #if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect)
  50. // clang-format off
  51. #define wdt_intr_enable(value) \
  52. __asm__ __volatile__ ( \
  53. "in __tmp_reg__,__SREG__" "\n\t" \
  54. "cli" "\n\t" \
  55. "wdr" "\n\t" \
  56. "sts %0,%1" "\n\t" \
  57. "out __SREG__,__tmp_reg__" "\n\t" \
  58. "sts %0,%2" "\n\t" \
  59. : /* no outputs */ \
  60. : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
  61. "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
  62. "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \
  63. : "r0" \
  64. )
  65. // clang-format on
  66. /** \brief Power down MCU with watchdog timer
  67. *
  68. * wdto: watchdog timer timeout defined in <avr/wdt.h>
  69. * WDTO_15MS
  70. * WDTO_30MS
  71. * WDTO_60MS
  72. * WDTO_120MS
  73. * WDTO_250MS
  74. * WDTO_500MS
  75. * WDTO_1S
  76. * WDTO_2S
  77. * WDTO_4S
  78. * WDTO_8S
  79. */
  80. static uint8_t wdt_timeout = 0;
  81. /** \brief Power down
  82. *
  83. * FIXME: needs doc
  84. */
  85. static void power_down(uint8_t wdto) {
  86. wdt_timeout = wdto;
  87. // Watchdog Interrupt Mode
  88. wdt_intr_enable(wdto);
  89. // TODO: more power saving
  90. // See PicoPower application note
  91. // - I/O port input with pullup
  92. // - prescale clock
  93. // - BOD disable
  94. // - Power Reduction Register PRR
  95. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  96. sleep_enable();
  97. sei();
  98. sleep_cpu();
  99. sleep_disable();
  100. // Disable watchdog after sleep
  101. wdt_disable();
  102. }
  103. #endif
  104. /** \brief Suspend power down
  105. *
  106. * FIXME: needs doc
  107. */
  108. void suspend_power_down(void) {
  109. #ifdef PROTOCOL_LUFA
  110. if (USB_DeviceState == DEVICE_STATE_Configured) return;
  111. #endif
  112. #ifdef PROTOCOL_VUSB
  113. if (!vusb_suspended) return;
  114. #endif
  115. suspend_power_down_kb();
  116. #ifndef NO_SUSPEND_POWER_DOWN
  117. // Turn off backlight
  118. # ifdef BACKLIGHT_ENABLE
  119. backlight_set(0);
  120. # endif
  121. // Turn off LED indicators
  122. uint8_t leds_off = 0;
  123. # if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  124. if (is_backlight_enabled()) {
  125. // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
  126. leds_off |= (1 << USB_LED_CAPS_LOCK);
  127. }
  128. # endif
  129. led_set(leds_off);
  130. // Turn off audio
  131. # ifdef AUDIO_ENABLE
  132. // This sometimes disables the start-up noise, so it's been disabled
  133. // stop_all_notes();
  134. # endif
  135. // Turn off underglow
  136. # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  137. rgblight_suspend();
  138. # endif
  139. // Enter sleep state if possible (ie, the MCU has a watchdog timeout interrupt)
  140. # if defined(WDT_vect)
  141. power_down(WDTO_15MS);
  142. # endif
  143. #endif
  144. }
  145. __attribute__((weak)) void matrix_power_up(void) {}
  146. __attribute__((weak)) void matrix_power_down(void) {}
  147. bool suspend_wakeup_condition(void) {
  148. matrix_power_up();
  149. matrix_scan();
  150. matrix_power_down();
  151. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  152. if (matrix_get_row(r)) return true;
  153. }
  154. return false;
  155. }
  156. /** \brief run user level code immediately after wakeup
  157. *
  158. * FIXME: needs doc
  159. */
  160. __attribute__((weak)) void suspend_wakeup_init_user(void) {}
  161. /** \brief run keyboard level code immediately after wakeup
  162. *
  163. * FIXME: needs doc
  164. */
  165. __attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }
  166. /** \brief run immediately after wakeup
  167. *
  168. * FIXME: needs doc
  169. */
  170. void suspend_wakeup_init(void) {
  171. // clear keyboard state
  172. clear_keyboard();
  173. // Turn on backlight
  174. #ifdef BACKLIGHT_ENABLE
  175. backlight_init();
  176. #endif
  177. // Restore LED indicators
  178. led_set(host_keyboard_leds());
  179. // Wake up underglow
  180. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  181. rgblight_wakeup();
  182. #endif
  183. suspend_wakeup_init_kb();
  184. }
  185. #if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect)
  186. /* watchdog timeout */
  187. ISR(WDT_vect) {
  188. // compensate timer for sleep
  189. switch (wdt_timeout) {
  190. case WDTO_15MS:
  191. timer_count += 15 + 2; // WDTO_15MS + 2(from observation)
  192. break;
  193. default:;
  194. }
  195. }
  196. #endif