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