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