suspend.c 4.4 KB

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