aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-03-25 01:46:32 +0900
committertmk <nobody@nowhere>2013-03-25 01:46:32 +0900
commit2c0bb5e7e620b0472f110d2cbc5768bb6d97eec5 (patch)
treeb29845f872204470a011264ac5e5d7b6471cbfc5
parent1f19176867b1b1f760c0ec41cd78fbb6832d29ad (diff)
downloadqmk_firmware-2c0bb5e7e620b0472f110d2cbc5768bb6d97eec5.tar.gz
qmk_firmware-2c0bb5e7e620b0472f110d2cbc5768bb6d97eec5.zip
Add common/suspend.c
-rw-r--r--common.mk2
-rw-r--r--common/sleep_led.h13
-rw-r--r--common/suspend.c51
-rw-r--r--common/suspend.h32
4 files changed, 97 insertions, 1 deletions
diff --git a/common.mk b/common.mk
index de1c6c360..fa31b3c46 100644
--- a/common.mk
+++ b/common.mk
@@ -10,6 +10,7 @@ SRC += $(COMMON_DIR)/host.c \
10 $(COMMON_DIR)/print.c \ 10 $(COMMON_DIR)/print.c \
11 $(COMMON_DIR)/debug.c \ 11 $(COMMON_DIR)/debug.c \
12 $(COMMON_DIR)/bootloader.c \ 12 $(COMMON_DIR)/bootloader.c \
13 $(COMMON_DIR)/suspend.c \
13 $(COMMON_DIR)/util.c 14 $(COMMON_DIR)/util.c
14 15
15 16
@@ -58,6 +59,7 @@ endif
58ifdef SLEEP_LED_ENABLE 59ifdef SLEEP_LED_ENABLE
59 SRC += $(COMMON_DIR)/sleep_led.c 60 SRC += $(COMMON_DIR)/sleep_led.c
60 OPT_DEFS += -DSLEEP_LED_ENABLE 61 OPT_DEFS += -DSLEEP_LED_ENABLE
62 OPT_DEFS += -DNO_SUSPEND_POWER_DOWN
61endif 63endif
62 64
63 65
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