4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
17 #define wdt_intr_enable(value) \
18 __asm__ __volatile__ ( \
19 "in __tmp_reg__,__SREG__" "\n\t" \
23 "out __SREG__,__tmp_reg__" "\n\t" \
26 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
27 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
28 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
29 _BV(WDIE) | (value & 0x07)) ) \
34 void suspend_idle(uint8_t time)
37 set_sleep_mode(SLEEP_MODE_IDLE);
44 /* Power down MCU with watchdog timer
45 * wdto: watchdog timer timeout defined in <avr/wdt.h>
57 static uint8_t wdt_timeout = 0;
58 static void power_down(uint8_t wdto)
61 if (USB_DeviceState == DEVICE_STATE_Configured) return;
65 // Watchdog Interrupt Mode
66 wdt_intr_enable(wdto);
68 #ifdef BACKLIGHT_ENABLE
72 // Turn off LED indicators
75 // TODO: more power saving
76 // See PicoPower application note
77 // - I/O port input with pullup
80 // - Power Reduction Register PRR
81 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
87 // Disable watchdog after sleep
91 void suspend_power_down(void)
93 power_down(WDTO_15MS);
96 __attribute__ ((weak)) void matrix_power_up(void) {}
97 __attribute__ ((weak)) void matrix_power_down(void) {}
98 bool suspend_wakeup_condition(void)
100 #ifdef BACKLIGHT_ENABLE
106 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
107 if (matrix_get_row(r)) return true;
112 // run immediately after wakeup
113 void suspend_wakeup_init(void)
115 // clear keyboard state
117 #ifdef BACKLIGHT_ENABLE
121 led_set(host_keyboard_leds());
124 #ifndef NO_SUSPEND_POWER_DOWN
125 /* watchdog timeout */
128 // compensate timer for sleep
129 switch (wdt_timeout) {
131 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)