]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/suspend.c
28219f4ddd7b900d356e15c7062b070141639919
[qmk_firmware.git] / tmk_core / common / avr / suspend.c
1 #include <stdbool.h>
2 #include <avr/sleep.h>
3 #include <avr/wdt.h>
4 #include <avr/interrupt.h>
5 #include "matrix.h"
6 #include "action.h"
7 #include "backlight.h"
8 #include "suspend_avr.h"
9 #include "suspend.h"
10 #include "timer.h"
11 #include "led.h"
12 #include "host.h"
13 #include "rgblight_reconfig.h"
14 #ifdef SPLIT_KEYBOARD
15   #include "split_flags.h"
16 #endif
17
18 #ifdef PROTOCOL_LUFA
19         #include "lufa.h"
20 #endif
21
22 #ifdef AUDIO_ENABLE
23     #include "audio.h"
24 #endif /* AUDIO_ENABLE */
25
26 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
27   #include "rgblight.h"
28   extern rgblight_config_t rgblight_config;
29   static bool rgblight_enabled;
30   static bool is_suspended;
31 #endif
32
33
34 #define wdt_intr_enable(value)   \
35 __asm__ __volatile__ (  \
36     "in __tmp_reg__,__SREG__" "\n\t"    \
37     "cli" "\n\t"    \
38     "wdr" "\n\t"    \
39     "sts %0,%1" "\n\t"  \
40     "out __SREG__,__tmp_reg__" "\n\t"   \
41     "sts %0,%2" "\n\t" \
42     : /* no outputs */  \
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)) ) \
47     : "r0"  \
48 )
49
50
51 /** \brief Suspend idle
52  *
53  * FIXME: needs doc
54  */
55 void suspend_idle(uint8_t time) {
56     cli();
57     set_sleep_mode(SLEEP_MODE_IDLE);
58     sleep_enable();
59     sei();
60     sleep_cpu();
61     sleep_disable();
62 }
63
64
65 // TODO: This needs some cleanup
66
67 /** \brief Run keyboard level Power down
68  *
69  * FIXME: needs doc
70  */
71 __attribute__ ((weak))
72 void suspend_power_down_user (void) { }
73 /** \brief Run keyboard level Power down
74  *
75  * FIXME: needs doc
76  */
77 __attribute__ ((weak))
78 void suspend_power_down_kb(void) {
79   suspend_power_down_user();
80 }
81
82 #ifndef NO_SUSPEND_POWER_DOWN
83 /** \brief Power down MCU with watchdog timer
84  *
85  * wdto: watchdog timer timeout defined in <avr/wdt.h>
86  *          WDTO_15MS
87  *          WDTO_30MS
88  *          WDTO_60MS
89  *          WDTO_120MS
90  *          WDTO_250MS
91  *          WDTO_500MS
92  *          WDTO_1S
93  *          WDTO_2S
94  *          WDTO_4S
95  *          WDTO_8S
96  */
97 static uint8_t wdt_timeout = 0;
98
99 /** \brief Power down
100  *
101  * FIXME: needs doc
102  */
103 static void power_down(uint8_t wdto) {
104 #ifdef PROTOCOL_LUFA
105     if (USB_DeviceState == DEVICE_STATE_Configured) return;
106 #endif
107     wdt_timeout = wdto;
108
109     // Watchdog Interrupt Mode
110     wdt_intr_enable(wdto);
111
112 #ifdef BACKLIGHT_ENABLE
113         backlight_set(0);
114 #endif
115
116         // Turn off LED indicators
117         led_set(0);
118
119         #ifdef AUDIO_ENABLE
120         // This sometimes disables the start-up noise, so it's been disabled
121                 // stop_all_notes();
122         #endif /* AUDIO_ENABLE */
123 #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
124 #ifdef RGBLIGHT_ANIMATIONS
125   rgblight_timer_disable();
126 #endif
127   if (!is_suspended) {
128     is_suspended = true;
129     rgblight_enabled = rgblight_config.enable;
130     rgblight_disable_noeeprom();
131     #ifdef SPLIT_KEYBOARD
132         RGB_DIRTY = true;
133     #endif
134   }
135 #endif
136   suspend_power_down_kb();
137
138     // TODO: more power saving
139     // See PicoPower application note
140     // - I/O port input with pullup
141     // - prescale clock
142     // - BOD disable
143     // - Power Reduction Register PRR
144     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
145     sleep_enable();
146     sei();
147     sleep_cpu();
148     sleep_disable();
149
150     // Disable watchdog after sleep
151     wdt_disable();
152 }
153 #endif
154
155 /** \brief Suspend power down
156  *
157  * FIXME: needs doc
158  */
159 void suspend_power_down(void) {
160         suspend_power_down_kb();
161
162 #ifndef NO_SUSPEND_POWER_DOWN
163     power_down(WDTO_15MS);
164 #endif
165 }
166
167 __attribute__ ((weak)) void matrix_power_up(void) {}
168 __attribute__ ((weak)) void matrix_power_down(void) {}
169 bool suspend_wakeup_condition(void) {
170     matrix_power_up();
171     matrix_scan();
172     matrix_power_down();
173     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
174         if (matrix_get_row(r)) return true;
175     }
176      return false;
177 }
178
179 /** \brief run user level code immediately after wakeup
180  *
181  * FIXME: needs doc
182  */
183 __attribute__ ((weak))
184 void suspend_wakeup_init_user(void) { }
185
186 /** \brief run keyboard level code immediately after wakeup
187  *
188  * FIXME: needs doc
189  */
190 __attribute__ ((weak))
191 void suspend_wakeup_init_kb(void) {
192   suspend_wakeup_init_user();
193 }
194 /** \brief run immediately after wakeup
195  *
196  * FIXME: needs doc
197  */
198 void suspend_wakeup_init(void) {
199     // clear keyboard state
200     clear_keyboard();
201 #ifdef BACKLIGHT_ENABLE
202     backlight_init();
203 #endif
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
209       wait_ms(10);
210     #endif
211     rgblight_enable_noeeprom();
212     #ifdef SPLIT_KEYBOARD
213         RGB_DIRTY = true;
214     #endif
215   }
216 #ifdef RGBLIGHT_ANIMATIONS
217   rgblight_timer_enable();
218 #endif
219 #endif
220     suspend_wakeup_init_kb();
221 }
222
223 #ifndef NO_SUSPEND_POWER_DOWN
224 /* watchdog timeout */
225 ISR(WDT_vect) {
226     // compensate timer for sleep
227     switch (wdt_timeout) {
228         case WDTO_15MS:
229             timer_count += 15 + 2;  // WDTO_15MS + 2(from observation)
230             break;
231         default:
232             ;
233     }
234 }
235 #endif