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)
68 #ifndef __AVR_ATmega32A__
70 if (USB_DeviceState == DEVICE_STATE_Configured) return;
74 // Watchdog Interrupt Mode
75 wdt_intr_enable(wdto);
77 #ifdef BACKLIGHT_ENABLE
81 // Turn off LED indicators
85 // This sometimes disables the start-up noise, so it's been disabled
87 #endif /* AUDIO_ENABLE */
89 // TODO: more power saving
90 // See PicoPower application note
91 // - I/O port input with pullup
94 // - Power Reduction Register PRR
95 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
101 // Disable watchdog after sleep
107 void suspend_power_down(void)
109 #ifndef NO_SUSPEND_POWER_DOWN
110 power_down(WDTO_15MS);
114 __attribute__ ((weak)) void matrix_power_up(void) {}
115 __attribute__ ((weak)) void matrix_power_down(void) {}
116 bool suspend_wakeup_condition(void)
121 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
122 if (matrix_get_row(r)) return true;
127 // run immediately after wakeup
128 void suspend_wakeup_init(void)
130 // clear keyboard state
132 #ifdef BACKLIGHT_ENABLE
135 led_set(host_keyboard_leds());
138 #ifndef NO_SUSPEND_POWER_DOWN
139 /* watchdog timeout */
142 // compensate timer for sleep
143 switch (wdt_timeout) {
145 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)