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