]> git.gir.st - tmk_keyboard.git/blame_incremental - tmk_core/common/avr/suspend.c
core: Fix sleep_led
[tmk_keyboard.git] / tmk_core / common / avr / suspend.c
... / ...
CommitLineData
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 */
46static uint8_t wdt_timeout = 0;
47static 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
73static void standby(void)
74{
75 set_sleep_mode(SLEEP_MODE_STANDBY);
76 sleep_enable();
77 sei();
78 sleep_cpu();
79 sleep_disable();
80}
81
82static void idle(void)
83{
84 set_sleep_mode(SLEEP_MODE_IDLE);
85 sleep_enable();
86 sei();
87 sleep_cpu();
88 sleep_disable();
89}
90
91
92void suspend_idle(uint8_t time)
93{
94 idle();
95}
96
97void suspend_power_down(void)
98{
99#ifdef NO_SUSPEND_POWER_DOWN
100 ;
101#elif defined(SUSPEND_MODE_NOPOWERSAVE)
102 ;
103#elif defined(SUSPEND_MODE_STANDBY)
104 standby();
105#elif defined(SUSPEND_MODE_IDLE)
106 idle();
107#else
108 power_down(WDTO_15MS);
109#endif
110}
111
112bool suspend_wakeup_condition(void)
113{
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
124void suspend_wakeup_init(void)
125{
126 // clear keyboard state
127 matrix_clear();
128 clear_keyboard();
129#ifdef BACKLIGHT_ENABLE
130 backlight_init();
131#endif
132}
133
134#ifndef NO_SUSPEND_POWER_DOWN
135/* watchdog timeout */
136ISR(WDT_vect)
137{
138 // compensate timer for sleep
139 switch (wdt_timeout) {
140 case WDTO_15MS:
141 timer_count += 15 + 2; // WDTO_15MS + 2(from observation)
142 break;
143 default:
144 ;
145 }
146}
147#endif
Imprint / Impressum