4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
13 #include "rgblight_reconfig.h"
21 #endif /* AUDIO_ENABLE */
23 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
28 #define wdt_intr_enable(value) \
29 __asm__ __volatile__ ( \
30 "in __tmp_reg__,__SREG__" "\n\t" \
34 "out __SREG__,__tmp_reg__" "\n\t" \
37 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
38 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
39 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
40 _BV(WDIE) | (value & 0x07)) ) \
45 /** \brief Suspend idle
49 void suspend_idle(uint8_t time)
52 set_sleep_mode(SLEEP_MODE_IDLE);
59 #ifndef NO_SUSPEND_POWER_DOWN
60 /** \brief Power down MCU with watchdog timer
62 * wdto: watchdog timer timeout defined in <avr/wdt.h>
74 static uint8_t wdt_timeout = 0;
76 /** \brief Run keyboard level Power down
80 __attribute__ ((weak))
81 void suspend_power_down_user (void) { }
82 /** \brief Run keyboard level Power down
86 __attribute__ ((weak))
87 void suspend_power_down_kb(void) {
88 suspend_power_down_user();
95 static void power_down(uint8_t wdto)
98 if (USB_DeviceState == DEVICE_STATE_Configured) return;
102 // Watchdog Interrupt Mode
103 wdt_intr_enable(wdto);
105 #ifdef BACKLIGHT_ENABLE
109 // Turn off LED indicators
113 // This sometimes disables the start-up noise, so it's been disabled
115 #endif /* AUDIO_ENABLE */
116 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
117 #ifdef RGBLIGHT_ANIMATIONS
118 rgblight_timer_disable();
120 rgblight_disable_noeeprom();
122 suspend_power_down_kb();
124 // TODO: more power saving
125 // See PicoPower application note
126 // - I/O port input with pullup
129 // - Power Reduction Register PRR
130 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
136 // Disable watchdog after sleep
141 /** \brief Suspend power down
145 void suspend_power_down(void)
147 #ifndef NO_SUSPEND_POWER_DOWN
148 power_down(WDTO_15MS);
152 __attribute__ ((weak)) void matrix_power_up(void) {}
153 __attribute__ ((weak)) void matrix_power_down(void) {}
154 bool suspend_wakeup_condition(void)
159 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
160 if (matrix_get_row(r)) return true;
165 /** \brief run user level code immediately after wakeup
169 __attribute__ ((weak))
170 void suspend_wakeup_init_user(void) { }
172 /** \brief run keyboard level code immediately after wakeup
176 __attribute__ ((weak))
177 void suspend_wakeup_init_kb(void) {
178 suspend_wakeup_init_user();
180 /** \brief run immediately after wakeup
184 void suspend_wakeup_init(void)
186 // clear keyboard state
188 #ifdef BACKLIGHT_ENABLE
191 led_set(host_keyboard_leds());
192 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
193 #ifdef BOOTLOADER_TEENSY
196 rgblight_enable_noeeprom();
197 #ifdef RGBLIGHT_ANIMATIONS
198 rgblight_timer_enable();
201 suspend_wakeup_init_kb();
204 #ifndef NO_SUSPEND_POWER_DOWN
205 /* watchdog timeout */
208 // compensate timer for sleep
209 switch (wdt_timeout) {
211 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)