]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/suspend.c
8a7272bbc5298612bc935f51116d5e44973cddf2
[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
13 #ifdef PROTOCOL_LUFA
14         #include "lufa.h"
15 #endif
16
17 #ifdef AUDIO_ENABLE
18     #include "audio.h"
19 #endif /* AUDIO_ENABLE */
20
21
22
23 #define wdt_intr_enable(value)   \
24 __asm__ __volatile__ (  \
25     "in __tmp_reg__,__SREG__" "\n\t"    \
26     "cli" "\n\t"    \
27     "wdr" "\n\t"    \
28     "sts %0,%1" "\n\t"  \
29     "out __SREG__,__tmp_reg__" "\n\t"   \
30     "sts %0,%2" "\n\t" \
31     : /* no outputs */  \
32     : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
33     "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
34     "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
35         _BV(WDIE) | (value & 0x07)) ) \
36     : "r0"  \
37 )
38
39
40 void suspend_idle(uint8_t time)
41 {
42     cli();
43     set_sleep_mode(SLEEP_MODE_IDLE);
44     sleep_enable();
45     sei();
46     sleep_cpu();
47     sleep_disable();
48 }
49
50 /* Power down MCU with watchdog timer
51  * wdto: watchdog timer timeout defined in <avr/wdt.h>
52  *          WDTO_15MS
53  *          WDTO_30MS
54  *          WDTO_60MS
55  *          WDTO_120MS
56  *          WDTO_250MS
57  *          WDTO_500MS
58  *          WDTO_1S
59  *          WDTO_2S
60  *          WDTO_4S
61  *          WDTO_8S
62  */
63 static uint8_t wdt_timeout = 0;
64 static void power_down(uint8_t wdto)
65 {
66 #ifdef PROTOCOL_LUFA
67     if (USB_DeviceState == DEVICE_STATE_Configured) return;
68 #endif
69     wdt_timeout = wdto;
70
71     // Watchdog Interrupt Mode
72     wdt_intr_enable(wdto);
73
74 #ifdef BACKLIGHT_ENABLE
75         backlight_set(0);
76 #endif
77
78         // Turn off LED indicators
79         led_set(0);
80
81         #ifdef AUDIO_ENABLE
82         // This sometimes disables the start-up noise, so it's been disabled
83                 // stop_all_notes();
84         #endif /* AUDIO_ENABLE */
85
86     // TODO: more power saving
87     // See PicoPower application note
88     // - I/O port input with pullup
89     // - prescale clock
90     // - BOD disable
91     // - Power Reduction Register PRR
92     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
93     sleep_enable();
94     sei();
95     sleep_cpu();
96     sleep_disable();
97
98     // Disable watchdog after sleep
99     wdt_disable();
100 }
101
102 void suspend_power_down(void)
103 {
104     power_down(WDTO_15MS);
105 }
106
107 __attribute__ ((weak)) void matrix_power_up(void) {}
108 __attribute__ ((weak)) void matrix_power_down(void) {}
109 bool suspend_wakeup_condition(void)
110 {
111 #ifdef BACKLIGHT_ENABLE
112     backlight_set(0);
113 #endif
114     matrix_power_up();
115     matrix_scan();
116     matrix_power_down();
117     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
118         if (matrix_get_row(r)) return true;
119     }
120      return false;
121 }
122
123 // run immediately after wakeup
124 void suspend_wakeup_init(void)
125 {
126     // clear keyboard state
127     clear_keyboard();
128 #ifdef BACKLIGHT_ENABLE
129     backlight_set(0);
130     backlight_init();
131 #endif
132 led_set(host_keyboard_leds());
133 }
134
135 #ifndef NO_SUSPEND_POWER_DOWN
136 /* watchdog timeout */
137 ISR(WDT_vect)
138 {
139     // compensate timer for sleep
140     switch (wdt_timeout) {
141         case WDTO_15MS:
142             timer_count += 15 + 2;  // WDTO_15MS + 2(from observation)
143             break;
144         default:
145             ;
146     }
147 }
148 #endif