4 #include <avr/interrupt.h>
8 #include "suspend_avr.h"
13 #include "rgblight_reconfig.h"
15 #include "split_flags.h"
24 #endif /* AUDIO_ENABLE */
26 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
28 extern rgblight_config_t rgblight_config;
29 static bool rgblight_enabled;
30 static bool is_suspended;
34 #define wdt_intr_enable(value) \
35 __asm__ __volatile__ ( \
36 "in __tmp_reg__,__SREG__" "\n\t" \
40 "out __SREG__,__tmp_reg__" "\n\t" \
43 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
44 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
45 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
46 _BV(WDIE) | (value & 0x07)) ) \
51 /** \brief Suspend idle
55 void suspend_idle(uint8_t time) {
57 set_sleep_mode(SLEEP_MODE_IDLE);
65 // TODO: This needs some cleanup
67 /** \brief Run keyboard level Power down
71 __attribute__ ((weak))
72 void suspend_power_down_user (void) { }
73 /** \brief Run keyboard level Power down
77 __attribute__ ((weak))
78 void suspend_power_down_kb(void) {
79 suspend_power_down_user();
82 #ifndef NO_SUSPEND_POWER_DOWN
83 /** \brief Power down MCU with watchdog timer
85 * wdto: watchdog timer timeout defined in <avr/wdt.h>
97 static uint8_t wdt_timeout = 0;
103 static void power_down(uint8_t wdto) {
105 if (USB_DeviceState == DEVICE_STATE_Configured) return;
109 // Watchdog Interrupt Mode
110 wdt_intr_enable(wdto);
112 #ifdef BACKLIGHT_ENABLE
116 // Turn off LED indicators
120 // This sometimes disables the start-up noise, so it's been disabled
122 #endif /* AUDIO_ENABLE */
123 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
124 #ifdef RGBLIGHT_ANIMATIONS
125 rgblight_timer_disable();
129 rgblight_enabled = rgblight_config.enable;
130 rgblight_disable_noeeprom();
131 #ifdef SPLIT_KEYBOARD
136 suspend_power_down_kb();
138 // TODO: more power saving
139 // See PicoPower application note
140 // - I/O port input with pullup
143 // - Power Reduction Register PRR
144 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
150 // Disable watchdog after sleep
155 /** \brief Suspend power down
159 void suspend_power_down(void) {
160 suspend_power_down_kb();
162 #ifndef NO_SUSPEND_POWER_DOWN
163 power_down(WDTO_15MS);
167 __attribute__ ((weak)) void matrix_power_up(void) {}
168 __attribute__ ((weak)) void matrix_power_down(void) {}
169 bool suspend_wakeup_condition(void) {
173 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
174 if (matrix_get_row(r)) return true;
179 /** \brief run user level code immediately after wakeup
183 __attribute__ ((weak))
184 void suspend_wakeup_init_user(void) { }
186 /** \brief run keyboard level code immediately after wakeup
190 __attribute__ ((weak))
191 void suspend_wakeup_init_kb(void) {
192 suspend_wakeup_init_user();
194 /** \brief run immediately after wakeup
198 void suspend_wakeup_init(void) {
199 // clear keyboard state
201 #ifdef BACKLIGHT_ENABLE
204 led_set(host_keyboard_leds());
205 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
206 is_suspended = false;
207 if (rgblight_enabled) {
208 #ifdef BOOTLOADER_TEENSY
211 rgblight_enable_noeeprom();
212 #ifdef SPLIT_KEYBOARD
216 #ifdef RGBLIGHT_ANIMATIONS
217 rgblight_timer_enable();
220 suspend_wakeup_init_kb();
223 #ifndef NO_SUSPEND_POWER_DOWN
224 /* watchdog timeout */
226 // compensate timer for sleep
227 switch (wdt_timeout) {
229 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)