aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/sleep_led.h13
-rw-r--r--common/suspend.c51
-rw-r--r--common/suspend.h32
3 files changed, 95 insertions, 1 deletions
diff --git a/common/sleep_led.h b/common/sleep_led.h
index aebdbeaa5..6bdcf558a 100644
--- a/common/sleep_led.h
+++ b/common/sleep_led.h
@@ -1,10 +1,21 @@
1#ifndef SLEEP_LED_H 1#ifndef SLEEP_LED_H
2#define SLEEP_LED_H 2#define SLEEP_LED_H
3 3
4#define NO_SUSPEND_POWER_DOWN 4
5#ifdef SLEEP_LED_ENABLE
5 6
6void sleep_led_init(void); 7void sleep_led_init(void);
7void sleep_led_enable(void); 8void sleep_led_enable(void);
8void sleep_led_disable(void); 9void sleep_led_disable(void);
10void sleep_led_toggle(void);
11
12#else
13
14#define sleep_led_init()
15#define sleep_led_enable()
16#define sleep_led_disable()
17#define sleep_led_toggle()
18
19#endif
9 20
10#endif 21#endif
diff --git a/common/suspend.c b/common/suspend.c
new file mode 100644
index 000000000..397e0729a
--- /dev/null
+++ b/common/suspend.c
@@ -0,0 +1,51 @@
1#include "suspend.h"
2#include "matrix.h"
3#include "action.h"
4
5
6void suspend_power_down(void)
7{
8#ifndef NO_SUSPEND_POWER_DOWN
9 // Enable watchdog to wake from MCU sleep
10 cli();
11 wdt_reset();
12
13 // Watchdog Interrupt and System Reset Mode
14 //wdt_enable(WDTO_1S);
15 //WDTCSR |= _BV(WDIE);
16
17 // Watchdog Interrupt Mode
18 wdt_intr_enable(WDTO_120MS);
19
20 // TODO: more power saving
21 // See PicoPower application note
22 // - I/O port input with pullup
23 // - prescale clock
24 // - BOD disable
25 // - Power Reduction Register PRR
26 // sleep in power down mode
27 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
28 sleep_enable();
29 sei();
30 sleep_cpu();
31 sleep_disable();
32
33 // Disable watchdog after sleep
34 wdt_disable();
35#endif
36}
37
38bool suspend_wakeup_condition(void)
39{
40 matrix_scan();
41 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
42 if (matrix_get_row(r)) return true;
43 }
44 return false;
45}
46
47void suspend_wakeup_init(void)
48{
49 matrix_init();
50 clear_keyboard();
51}
diff --git a/common/suspend.h b/common/suspend.h
new file mode 100644
index 000000000..1c1e41ac3
--- /dev/null
+++ b/common/suspend.h
@@ -0,0 +1,32 @@
1#ifndef SUSPEND_H
2#define SUSPEND_H
3
4#include <stdint.h>
5#include <stdbool.h>
6#include <avr/sleep.h>
7#include <avr/wdt.h>
8#include <avr/interrupt.h>
9
10
11#define wdt_intr_enable(value) \
12__asm__ __volatile__ ( \
13 "in __tmp_reg__,__SREG__" "\n\t" \
14 "cli" "\n\t" \
15 "wdr" "\n\t" \
16 "sts %0,%1" "\n\t" \
17 "out __SREG__,__tmp_reg__" "\n\t" \
18 "sts %0,%2" "\n\t" \
19 : /* no outputs */ \
20 : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
21 "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
22 "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
23 _BV(WDIE) | (value & 0x07)) ) \
24 : "r0" \
25)
26
27
28void suspend_power_down(void);
29bool suspend_wakeup_condition(void);
30void suspend_wakeup_init(void);
31
32#endif