suspend.c 3.0 KB

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