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)
25 extern rgblight_config_t rgblight_config;
26 static bool rgblight_enabled;
27 static bool is_suspended;
31 #define wdt_intr_enable(value) \
32 __asm__ __volatile__ ( \
33 "in __tmp_reg__,__SREG__" "\n\t" \
37 "out __SREG__,__tmp_reg__" "\n\t" \
40 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
41 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
42 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
43 _BV(WDIE) | (value & 0x07)) ) \
48 /** \brief Suspend idle
52 void suspend_idle(uint8_t time) {
54 set_sleep_mode(SLEEP_MODE_IDLE);
62 // TODO: This needs some cleanup
64 /** \brief Run keyboard level Power down
68 __attribute__ ((weak))
69 void suspend_power_down_user (void) { }
70 /** \brief Run keyboard level Power down
74 __attribute__ ((weak))
75 void suspend_power_down_kb(void) {
76 suspend_power_down_user();
79 #ifndef NO_SUSPEND_POWER_DOWN
80 /** \brief Power down MCU with watchdog timer
82 * wdto: watchdog timer timeout defined in <avr/wdt.h>
94 static uint8_t wdt_timeout = 0;
100 static void power_down(uint8_t wdto) {
102 if (USB_DeviceState == DEVICE_STATE_Configured) return;
106 // Watchdog Interrupt Mode
107 wdt_intr_enable(wdto);
109 #ifdef BACKLIGHT_ENABLE
113 // Turn off LED indicators
114 uint8_t leds_off = 0;
115 #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
116 if (is_backlight_enabled()) {
117 // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
118 leds_off |= (1<<USB_LED_CAPS_LOCK);
124 // This sometimes disables the start-up noise, so it's been disabled
126 #endif /* AUDIO_ENABLE */
127 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
128 #ifdef RGBLIGHT_ANIMATIONS
129 rgblight_timer_disable();
133 rgblight_enabled = rgblight_config.enable;
134 rgblight_disable_noeeprom();
137 suspend_power_down_kb();
139 // TODO: more power saving
140 // See PicoPower application note
141 // - I/O port input with pullup
144 // - Power Reduction Register PRR
145 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
151 // Disable watchdog after sleep
156 /** \brief Suspend power down
160 void suspend_power_down(void) {
161 suspend_power_down_kb();
163 #ifndef NO_SUSPEND_POWER_DOWN
164 power_down(WDTO_15MS);
168 __attribute__ ((weak)) void matrix_power_up(void) {}
169 __attribute__ ((weak)) void matrix_power_down(void) {}
170 bool suspend_wakeup_condition(void) {
174 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
175 if (matrix_get_row(r)) return true;
180 /** \brief run user level code immediately after wakeup
184 __attribute__ ((weak))
185 void suspend_wakeup_init_user(void) { }
187 /** \brief run keyboard level code immediately after wakeup
191 __attribute__ ((weak))
192 void suspend_wakeup_init_kb(void) {
193 suspend_wakeup_init_user();
195 /** \brief run immediately after wakeup
199 void suspend_wakeup_init(void) {
200 // clear keyboard state
202 #ifdef BACKLIGHT_ENABLE
205 led_set(host_keyboard_leds());
206 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
207 is_suspended = false;
208 if (rgblight_enabled) {
209 #ifdef BOOTLOADER_TEENSY
212 rgblight_enable_noeeprom();
214 #ifdef RGBLIGHT_ANIMATIONS
215 rgblight_timer_enable();
218 suspend_wakeup_init_kb();
221 #ifndef NO_SUSPEND_POWER_DOWN
222 /* watchdog timeout */
224 // compensate timer for sleep
225 switch (wdt_timeout) {
227 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)