diff --git a/firmware/input.c b/firmware/input.c index 62417f8..6da5d06 100644 --- a/firmware/input.c +++ b/firmware/input.c @@ -14,6 +14,9 @@ void input_init() PORT_KEYPAD(DDR) &= ~(PIN_KEYPAD_MASK); // Key inputs PORT_KEYPAD(PORT) |= PIN_KEYPAD_MASK; // Key pullups + + PORT_SBCMON(DDR) &= ~PIN_SBCMON; + PORT_SBCMON(PORT) |= PIN_SBCMON; } inline int8_t get_rotary_delta() @@ -97,6 +100,11 @@ inline void input_clear_events() sw_event = 0; } +inline uint8_t get_sbc_state() +{ + return !(PORT_SBCMON(PIN) & PIN_SBCMON); +} + // Need to call timer_proc() before calling this function! void input_proc() { diff --git a/firmware/input.h b/firmware/input.h index 39d91fd..ce891c3 100644 --- a/firmware/input.h +++ b/firmware/input.h @@ -6,6 +6,9 @@ #define PORT_KEYAUX(t) t##B #define PIN_ROTARY_PUSH (1 << 0) +#define PORT_SBCMON(t) t##C +#define PIN_SBCMON (1 << 1) + #define PORT_ROTARY(t) t##D #define PIN_ROTARY_A (1 << 3) // INT1 #define PIN_ROTARY_B (1 << 4) @@ -15,6 +18,6 @@ void input_proc(); int8_t get_rotary_delta(); uint8_t get_switch_event(); void input_clear_events(); -extern uint8_t ctime; +uint8_t get_sbc_state(); #endif // INPUT_H diff --git a/firmware/main.c b/firmware/main.c index afafecd..d4bb7f4 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -8,6 +8,8 @@ #include "batmon.h" #include "pm.h" +#define TIME_POWEROFF 5000 + #define PORT_SPI(t) t##B #define PIN_SPI_CS (1 << 2) @@ -92,6 +94,7 @@ int main() suspend(); + pm_init(); lcd_init(LCD_DISP_ON); lcd_title(PSTR("Starting IOC Rev."), PSTR(GITREV)); spi_init(); @@ -104,9 +107,12 @@ int main() reset(); } - lcd_title(PSTR("Starting up."), PSTR("Please wait...")); - + lcd_title(PSTR("Starting up."), PSTR("Please wait.")); + pm_sbc_on(); _delay_ms(10); + while (!get_sbc_state()); + + lcd_title(PSTR("Starting up."), PSTR("Please wait...")); while (1) { if (!spi_proc()) { @@ -117,10 +123,15 @@ int main() case 1: input_proc(); break; + case 2: + if (get_sbc_state()) + timer_set(TIMID_POWEROFF, MS_TO_TICKS(TIME_POWEROFF)); + if (timer_expired(TIMID_POWEROFF)) + reset(); } } sched = sched + 1; - if (sched == 4) { + if (sched == 5) { sched = 0; if (batmon_proc()) reset(); diff --git a/firmware/pm.c b/firmware/pm.c index dc4af6f..7c30c6c 100644 --- a/firmware/pm.c +++ b/firmware/pm.c @@ -66,3 +66,19 @@ char* post() return 0; } + +void pm_init() +{ + PORT_SBCPOW(DDR) |= PIN_SBCPOW; + pm_sbc_off(); +} + +void pm_sbc_on() +{ + PORT_SBCPOW(PORT) |= PIN_SBCPOW; +} + +void pm_sbc_off() +{ + PORT_SBCPOW(PORT) &= ~PIN_SBCPOW; +} diff --git a/firmware/pm.h b/firmware/pm.h index 105275f..a0e2b07 100644 --- a/firmware/pm.h +++ b/firmware/pm.h @@ -2,7 +2,14 @@ #define PM_H #include +#define PORT_SBCPOW(t) t##C +#define PIN_SBCPOW (1 << 2) + +void pm_init(); void suspend(); char* post(); +void pm_init(); +void pm_sbc_on(); +void pm_sbc_off(); #endif // PM_H diff --git a/firmware/systick.h b/firmware/systick.h index 7b081d3..db167e6 100644 --- a/firmware/systick.h +++ b/firmware/systick.h @@ -4,7 +4,8 @@ #define TIMID_SW_DEBOUNCE 0 #define TIMID_SW_LONGPRESS 1 -#define NTIM 2 +#define TIMID_POWEROFF 2 +#define NTIM 3 #define MS_TO_TICKS(ms) (((ms/1000 * F_CPU/1024) / 256))