suspend.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #define wdt_intr_enable(value) \
  20. __asm__ __volatile__ ( \
  21. "in __tmp_reg__,__SREG__" "\n\t" \
  22. "cli" "\n\t" \
  23. "wdr" "\n\t" \
  24. "sts %0,%1" "\n\t" \
  25. "out __SREG__,__tmp_reg__" "\n\t" \
  26. "sts %0,%2" "\n\t" \
  27. : /* no outputs */ \
  28. : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
  29. "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
  30. "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
  31. _BV(WDIE) | (value & 0x07)) ) \
  32. : "r0" \
  33. )
  34. void suspend_idle(uint8_t time)
  35. {
  36. cli();
  37. set_sleep_mode(SLEEP_MODE_IDLE);
  38. sleep_enable();
  39. sei();
  40. sleep_cpu();
  41. sleep_disable();
  42. }
  43. #ifndef NO_SUSPEND_POWER_DOWN
  44. /* Power down MCU with watchdog timer
  45. * wdto: watchdog timer timeout defined in <avr/wdt.h>
  46. * WDTO_15MS
  47. * WDTO_30MS
  48. * WDTO_60MS
  49. * WDTO_120MS
  50. * WDTO_250MS
  51. * WDTO_500MS
  52. * WDTO_1S
  53. * WDTO_2S
  54. * WDTO_4S
  55. * WDTO_8S
  56. */
  57. static uint8_t wdt_timeout = 0;
  58. static void power_down(uint8_t wdto)
  59. {
  60. #ifdef PROTOCOL_LUFA
  61. if (USB_DeviceState == DEVICE_STATE_Configured) return;
  62. #endif
  63. wdt_timeout = wdto;
  64. // Watchdog Interrupt Mode
  65. wdt_intr_enable(wdto);
  66. #ifdef BACKLIGHT_ENABLE
  67. backlight_set(0);
  68. #endif
  69. // Turn off LED indicators
  70. led_set(0);
  71. #ifdef AUDIO_ENABLE
  72. // This sometimes disables the start-up noise, so it's been disabled
  73. // stop_all_notes();
  74. #endif /* AUDIO_ENABLE */
  75. // TODO: more power saving
  76. // See PicoPower application note
  77. // - I/O port input with pullup
  78. // - prescale clock
  79. // - BOD disable
  80. // - Power Reduction Register PRR
  81. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  82. sleep_enable();
  83. sei();
  84. sleep_cpu();
  85. sleep_disable();
  86. // Disable watchdog after sleep
  87. wdt_disable();
  88. }
  89. #endif
  90. void suspend_power_down(void)
  91. {
  92. #ifndef NO_SUSPEND_POWER_DOWN
  93. power_down(WDTO_15MS);
  94. #endif
  95. }
  96. __attribute__ ((weak)) void matrix_power_up(void) {}
  97. __attribute__ ((weak)) void matrix_power_down(void) {}
  98. bool suspend_wakeup_condition(void)
  99. {
  100. matrix_power_up();
  101. matrix_scan();
  102. matrix_power_down();
  103. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  104. if (matrix_get_row(r)) return true;
  105. }
  106. return false;
  107. }
  108. // run immediately after wakeup
  109. void suspend_wakeup_init(void)
  110. {
  111. // clear keyboard state
  112. clear_keyboard();
  113. #ifdef BACKLIGHT_ENABLE
  114. backlight_init();
  115. #endif
  116. led_set(host_keyboard_leds());
  117. }
  118. #ifndef NO_SUSPEND_POWER_DOWN
  119. /* watchdog timeout */
  120. ISR(WDT_vect)
  121. {
  122. // compensate timer for sleep
  123. switch (wdt_timeout) {
  124. case WDTO_15MS:
  125. timer_count += 15 + 2; // WDTO_15MS + 2(from observation)
  126. break;
  127. default:
  128. ;
  129. }
  130. }
  131. #endif