suspend.c 4.2 KB

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