4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
20 #endif /* AUDIO_ENABLE */
22 #ifdef RGBLIGHT_ANIMATIONS
27 #define wdt_intr_enable(value) \
28 __asm__ __volatile__ ( \
29 "in __tmp_reg__,__SREG__" "\n\t" \
33 "out __SREG__,__tmp_reg__" "\n\t" \
36 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
37 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
38 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
39 _BV(WDIE) | (value & 0x07)) ) \
44 void suspend_idle(uint8_t time)
47 set_sleep_mode(SLEEP_MODE_IDLE);
54 #ifndef NO_SUSPEND_POWER_DOWN
55 /* Power down MCU with watchdog timer
56 * wdto: watchdog timer timeout defined in <avr/wdt.h>
68 static uint8_t wdt_timeout = 0;
70 static void power_down(uint8_t wdto)
73 if (USB_DeviceState == DEVICE_STATE_Configured) return;
77 // Watchdog Interrupt Mode
78 wdt_intr_enable(wdto);
80 #ifdef BACKLIGHT_ENABLE
84 // Turn off LED indicators
88 // This sometimes disables the start-up noise, so it's been disabled
90 #endif /* AUDIO_ENABLE */
92 #ifdef RGBLIGHT_ANIMATIONS
93 rgblight_timer_disable();
97 // TODO: more power saving
98 // See PicoPower application note
99 // - I/O port input with pullup
102 // - Power Reduction Register PRR
103 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
109 // Disable watchdog after sleep
114 void suspend_power_down(void)
116 #ifndef NO_SUSPEND_POWER_DOWN
117 power_down(WDTO_15MS);
121 __attribute__ ((weak)) void matrix_power_up(void) {}
122 __attribute__ ((weak)) void matrix_power_down(void) {}
123 bool suspend_wakeup_condition(void)
128 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
129 if (matrix_get_row(r)) return true;
134 // run immediately after wakeup
135 void suspend_wakeup_init(void)
137 // clear keyboard state
139 #ifdef BACKLIGHT_ENABLE
142 led_set(host_keyboard_leds());
143 #ifdef RGBLIGHT_SLEEP
145 #ifdef RGBLIGHT_ANIMATIONS
146 rgblight_timer_enable();
151 #ifndef NO_SUSPEND_POWER_DOWN
152 /* watchdog timeout */
155 // compensate timer for sleep
156 switch (wdt_timeout) {
158 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)