wip
This commit is contained in:
		
							
								
								
									
										13
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								Makefile
									
									
									
									
									
								
							@@ -3,16 +3,15 @@ CROSS_COMPILE ?= avr-
 | 
				
			|||||||
TARGET_FILE ?= avr.elf
 | 
					TARGET_FILE ?= avr.elf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MCU = attiny85
 | 
					MCU = attiny85
 | 
				
			||||||
# MCU = atmega8
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# C_FLAGS += -Wall -Werror
 | 
					C_FLAGS += -Wall -Werror
 | 
				
			||||||
C_FLAGS += -Os
 | 
					C_FLAGS += -Os
 | 
				
			||||||
C_FLAGS += -mmcu=$(MCU)
 | 
					C_FLAGS += -mmcu=$(MCU)
 | 
				
			||||||
# C_FLAGS += -ffunction-sections
 | 
					C_FLAGS += -ffunction-sections
 | 
				
			||||||
# C_FLAGS += -fdata-sections
 | 
					C_FLAGS += -fdata-sections
 | 
				
			||||||
# C_FLAGS += -std=c11
 | 
					C_FLAGS += -std=c11
 | 
				
			||||||
# C_FLAGS += -pedantic
 | 
					C_FLAGS += -pedantic
 | 
				
			||||||
# C_FLAGS += -pedantic-errors
 | 
					C_FLAGS += -pedantic-errors
 | 
				
			||||||
C_FLAGS += -Iinclude
 | 
					C_FLAGS += -Iinclude
 | 
				
			||||||
 | 
					
 | 
				
			||||||
LD_FLAGS += -mmcu=$(MCU)
 | 
					LD_FLAGS += -mmcu=$(MCU)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										138
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										138
									
								
								src/main.c
									
									
									
									
									
								
							@@ -15,25 +15,39 @@
 | 
				
			|||||||
#define POWER_PIN       3
 | 
					#define POWER_PIN       3
 | 
				
			||||||
#define IGNITION_PIN    4
 | 
					#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 ignition_on() (PINB & (1 << IGNITION_PIN))
 | 
				
			||||||
#define power_on() (PINB & (1 << POWER_PIN))
 | 
					#define power_on() (PINB & (1 << POWER_PIN))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool is_shutdown;
 | 
					 | 
				
			||||||
volatile uint32_t tick;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
enum power {
 | 
					enum power {
 | 
				
			||||||
    off = 0,
 | 
					    off = 0,
 | 
				
			||||||
    on
 | 
					    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()
 | 
					void setup_gpio()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    DDRB = 0xff;
 | 
					    DDRB = 0xff;
 | 
				
			||||||
    PORTB = 0;
 | 
					    PORTB = 0;
 | 
				
			||||||
    DDRB &= ~(1 << IGNITION_PIN);
 | 
					    DDRB &= ~(1 << IGNITION_PIN);
 | 
				
			||||||
    PORTB |= (1 << IGNITION_PIN);   // enable pull-up
 | 
					    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
 | 
					    GIMSK |= (1 << PCIE);           // Enable Pin Change Interrupts
 | 
				
			||||||
    PCMSK |= (1 << IGNITION_PIN);   // Use PB4 as interrupt pin
 | 
					    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)
 | 
					int main(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    uint32_t shutdown_expire = 0;
 | 
					    current_state = state_init;
 | 
				
			||||||
    tick = 0;
 | 
					    tick = 0;
 | 
				
			||||||
    is_shutdown = false;
 | 
					    shutdown_expire = 0;
 | 
				
			||||||
 | 
					    guard_expire = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    setup_gpio();
 | 
					    setup_gpio();
 | 
				
			||||||
    setup_timer0();
 | 
					    setup_timer0();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -82,31 +173,16 @@ int main(void)
 | 
				
			|||||||
    sei();
 | 
					    sei();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while(1) {
 | 
					    while(1) {
 | 
				
			||||||
        if(ignition_on()) {
 | 
					        if(current_state == state_init) {
 | 
				
			||||||
            if(!power_on()) {
 | 
					            handle_state_init();
 | 
				
			||||||
                switch_power(on);
 | 
					        } else if(current_state == state_on) {
 | 
				
			||||||
                is_shutdown = false;
 | 
					            handle_state_on();
 | 
				
			||||||
            }
 | 
					        } else if(current_state == state_shutdown) {
 | 
				
			||||||
            sleep_enable();
 | 
					            handle_state_shutdown();
 | 
				
			||||||
            sleep_cpu();
 | 
					        } else if(current_state == state_guard) {
 | 
				
			||||||
            sleep_disable();
 | 
					            handle_state_guard();
 | 
				
			||||||
        } else {
 | 
					        } else if(current_state == state_off) {
 | 
				
			||||||
            if(!is_shutdown) {
 | 
					            handle_state_off();
 | 
				
			||||||
                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();
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user