4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
19 #endif /* AUDIO_ENABLE */
23 #define wdt_intr_enable(value) \
24 __asm__ __volatile__ ( \
25 "in __tmp_reg__,__SREG__" "\n\t" \
29 "out __SREG__,__tmp_reg__" "\n\t" \
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)) ) \
40 void suspend_idle(uint8_t time)
43 set_sleep_mode(SLEEP_MODE_IDLE);
50 #ifndef NO_SUSPEND_POWER_DOWN
51 /* Power down MCU with watchdog timer
52 * wdto: watchdog timer timeout defined in <avr/wdt.h>
64 static uint8_t wdt_timeout = 0;
66 static void power_down(uint8_t wdto)
69 if (USB_DeviceState == DEVICE_STATE_Configured) return;
73 // Watchdog Interrupt Mode
74 wdt_intr_enable(wdto);
76 #ifdef BACKLIGHT_ENABLE
80 // Turn off LED indicators
84 // This sometimes disables the start-up noise, so it's been disabled
86 #endif /* AUDIO_ENABLE */
88 // TODO: more power saving
89 // See PicoPower application note
90 // - I/O port input with pullup
93 // - Power Reduction Register PRR
94 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
100 // Disable watchdog after sleep
105 void suspend_power_down(void)
107 #ifndef NO_SUSPEND_POWER_DOWN
108 power_down(WDTO_15MS);
112 __attribute__ ((weak)) void matrix_power_up(void) {}
113 __attribute__ ((weak)) void matrix_power_down(void) {}
114 bool suspend_wakeup_condition(void)
119 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
120 if (matrix_get_row(r)) return true;
125 // run immediately after wakeup
126 void suspend_wakeup_init(void)
128 // clear keyboard state
130 #ifdef BACKLIGHT_ENABLE
133 led_set(host_keyboard_leds());
136 #ifndef NO_SUSPEND_POWER_DOWN
137 /* watchdog timeout */
140 // compensate timer for sleep
141 switch (wdt_timeout) {
143 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)