This commit is contained in:
tkl 2019-12-26 10:15:25 +01:00 committed by Thomas Klaehn
parent 18913f5f6a
commit ec338fb930
2 changed files with 113 additions and 38 deletions

View File

@ -3,16 +3,15 @@ CROSS_COMPILE ?= avr-
TARGET_FILE ?= avr.elf
MCU = attiny85
# MCU = atmega8
# C_FLAGS += -Wall -Werror
C_FLAGS += -Wall -Werror
C_FLAGS += -Os
C_FLAGS += -mmcu=$(MCU)
# C_FLAGS += -ffunction-sections
# C_FLAGS += -fdata-sections
# C_FLAGS += -std=c11
# C_FLAGS += -pedantic
# C_FLAGS += -pedantic-errors
C_FLAGS += -ffunction-sections
C_FLAGS += -fdata-sections
C_FLAGS += -std=c11
C_FLAGS += -pedantic
C_FLAGS += -pedantic-errors
C_FLAGS += -Iinclude
LD_FLAGS += -mmcu=$(MCU)

View File

@ -15,25 +15,39 @@
#define POWER_PIN 3
#define IGNITION_PIN 4
#define SHUTDOWN_TIME_MS 5000UL
#define SHUTDOWN_TIME_MS 1000UL * 60
#define GUARD_TIME_MS 1000UL * 5
#define ignition_on() (PINB & (1 << IGNITION_PIN))
#define power_on() (PINB & (1 << POWER_PIN))
bool is_shutdown;
volatile uint32_t tick;
enum power {
off = 0,
on
};
enum state {
state_init = 0,
state_on,
state_shutdown,
state_guard,
state_off
};
volatile uint32_t tick;
uint32_t shutdown_expire, guard_expire;
enum state current_state;
void setup_gpio()
{
DDRB = 0xff;
PORTB = 0;
DDRB &= ~(1 << IGNITION_PIN);
PORTB |= (1 << IGNITION_PIN); // enable pull-up
PORTB &= ~(1 << MOSI_PIN);
PORTB &= ~(1 << MISO_PIN);
PORTB &= ~(1 << SCK_PIN);
GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts
PCMSK |= (1 << IGNITION_PIN); // Use PB4 as interrupt pin
@ -66,11 +80,88 @@ void switch_power(enum power power)
}
}
void handle_state_init()
{
if(ignition_on()) {
switch_power(on);
PORTB &= ~(1 << MOSI_PIN);
PORTB &= ~(1 << MISO_PIN);
PORTB &= ~(1 << SCK_PIN);
current_state = state_on;
} else {
shutdown_expire = tick + SHUTDOWN_TIME_MS;
PORTB |= (1 << MOSI_PIN);
PORTB |= (1 << MISO_PIN);
PORTB |= (1 << SCK_PIN);
current_state = state_shutdown;
}
}
void handle_state_on()
{
if(!ignition_on()) {
shutdown_expire = tick + SHUTDOWN_TIME_MS;
PORTB |= (1 << MOSI_PIN);
PORTB |= (1 << MISO_PIN);
PORTB |= (1 << SCK_PIN);
current_state = state_shutdown;
}
}
void handle_state_shutdown()
{
if(shutdown_expire < tick) {
if(power_on()) {
switch_power(off);
}
if(ignition_on()) {
guard_expire = tick + GUARD_TIME_MS;
current_state = state_guard;
} else {
current_state = state_off;
sleep_enable();
sleep_cpu();
sleep_disable();
}
}
}
void handle_state_guard()
{
if(guard_expire < tick) {
if(ignition_on()) {
switch_power(on);
PORTB &= ~(1 << MOSI_PIN);
PORTB &= ~(1 << MISO_PIN);
PORTB &= ~(1 << SCK_PIN);
current_state = state_on;
} else {
current_state = state_off;
sleep_enable();
sleep_cpu();
sleep_disable();
}
}
}
void handle_state_off()
{
if(ignition_on()) {
switch_power(on);
PORTB &= ~(1 << MOSI_PIN);
PORTB &= ~(1 << MISO_PIN);
PORTB &= ~(1 << SCK_PIN);
current_state = state_on;
}
}
int main(void)
{
uint32_t shutdown_expire = 0;
current_state = state_init;
tick = 0;
is_shutdown = false;
shutdown_expire = 0;
guard_expire = 0;
setup_gpio();
setup_timer0();
@ -82,31 +173,16 @@ int main(void)
sei();
while(1) {
if(ignition_on()) {
if(!power_on()) {
switch_power(on);
is_shutdown = false;
}
sleep_enable();
sleep_cpu();
sleep_disable();
} else {
if(!is_shutdown) {
is_shutdown = true;
shutdown_expire = tick + SHUTDOWN_TIME_MS;
}
else {
if(shutdown_expire < tick) {
if(power_on()) {
switch_power(off);
}
// setup_watchdog();
sleep_enable();
sleep_cpu();
sleep_disable();
// wdt_disable();
}
}
if(current_state == state_init) {
handle_state_init();
} else if(current_state == state_on) {
handle_state_on();
} else if(current_state == state_shutdown) {
handle_state_shutdown();
} else if(current_state == state_guard) {
handle_state_guard();
} else if(current_state == state_off) {
handle_state_off();
}
}