]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/avr/suspend.c
Backlight fix from reddit
[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     led_off();
73
74     // TODO: more power saving
75     // See PicoPower application note
76     // - I/O port input with pullup
77     // - prescale clock
78     // - BOD disable
79     // - Power Reduction Register PRR
80     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
81     sleep_enable();
82     sei();
83     sleep_cpu();
84     sleep_disable();
85
86     // Disable watchdog after sleep
87     wdt_disable();
88 }
89
90 void suspend_power_down(void)
91 {
92     power_down(WDTO_15MS);
93 }
94
95 __attribute__ ((weak)) void matrix_power_up(void) {}
96 __attribute__ ((weak)) void matrix_power_down(void) {}
97 bool suspend_wakeup_condition(void)
98 {
99 #ifdef BACKLIGHT_ENABLE
100     backlight_set(0);
101 #endif
102     matrix_power_up();
103     matrix_scan();
104     matrix_power_down();
105     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
106         if (matrix_get_row(r)) return true;
107     }
108     return false;
109 }
110
111 // run immediately after wakeup
112 void suspend_wakeup_init(void)
113 {
114     // clear keyboard state
115     clear_keyboard();
116 #ifdef BACKLIGHT_ENABLE
117     backlight_set(0);
118     backlight_init();
119 #endif
120 led_set(host_keyboard_leds());
121 }
122
123 #ifndef NO_SUSPEND_POWER_DOWN
124 /* watchdog timeout */
125 ISR(WDT_vect)
126 {
127     // compensate timer for sleep
128     switch (wdt_timeout) {
129         case WDTO_15MS:
130             timer_count += 15 + 2;  // WDTO_15MS + 2(from observation)
131             break;
132         default:
133             ;
134     }
135 }
136 #endif
137