From 4ade0d3b6f545db13d54bad1c2eababfd3f7ee84 Mon Sep 17 00:00:00 2001 From: Michael Buesch Date: Fri, 17 Apr 2009 18:29:03 +0200 Subject: pressure_control: Fix bootup Signed-off-by: Michael Buesch --- pressure_control/firmware/Makefile | 22 ++++++++++++---------- pressure_control/firmware/main.c | 36 +++++++++++++++++++++++++++++++++--- pressure_control/firmware/valves.c | 12 +++++++++++- pressure_control/firmware/valves.h | 1 + 4 files changed, 57 insertions(+), 14 deletions(-) (limited to 'pressure_control/firmware') diff --git a/pressure_control/firmware/Makefile b/pressure_control/firmware/Makefile index 4502937..a4d380e 100644 --- a/pressure_control/firmware/Makefile +++ b/pressure_control/firmware/Makefile @@ -13,18 +13,15 @@ CFLAGS = -mmcu=$(ARCH) -std=gnu99 -g0 -O2 -Wall CFLAGS += "-Dinline=inline __attribute__((__always_inline__))" -# The fuse bits -LFUSE = 0xE0 -HFUSE = 0xD9 - OBJECTS = main.o util.o valves.o sensor.o remote.o NAME = pressure_control BIN = $(NAME).bin HEX = $(NAME).hex EEP = $(NAME).eep.hex +FUSES = $(NAME).fuses.bin -all: $(HEX) +all: $(HEX) fuses main.o: util.h calibration.h valves.h sensor.h remote.h main.h @@ -43,7 +40,7 @@ $(BIN): $(OBJECTS) $(CC) $(CFLAGS) -o $(BIN) $(OBJECTS) $(LDFLAGS) $(HEX): $(BIN) - $(OBJCOPY) -R.eeprom -O ihex $(BIN) $(HEX) + $(OBJCOPY) -R.eeprom -R.fuse -O ihex $(BIN) $(HEX) $(OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \ --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP) $(SIZE) $(BIN) @@ -68,17 +65,22 @@ reset: -c $(PROGRAMMER) -P $(PROGPORT) \ -U signature:r:/dev/null:i -q -q -writefuse: +fuses: $(BIN) + $(OBJCOPY) -j.fuse -O binary $(BIN) $(FUSES) + +writefuses: fuses + @echo Low fuse is: $$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 1) + @echo High fuse is: $$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 2) $(AVRDUDE) -B 100 -p $(AVRDUDE_ARCH) \ -c $(PROGRAMMER) -P $(PROGPORT) -q -q \ - -U lfuse:w:$(LFUSE):m \ - -U hfuse:w:$(HFUSE):m + -U lfuse:w:$$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 1):m \ + -U hfuse:w:$$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 2):m clean: -rm -f *~ *.o $(BIN) distclean: clean - -rm -f *.s $(HEX) $(EEP) + -rm -f *.s $(HEX) $(EEP) $(FUSES) help: @echo "Makefile" diff --git a/pressure_control/firmware/main.c b/pressure_control/firmware/main.c index 593e2a5..d2d849d 100644 --- a/pressure_control/firmware/main.c +++ b/pressure_control/firmware/main.c @@ -28,6 +28,8 @@ #include #include +#include +#include struct eeprom_data { @@ -42,7 +44,7 @@ struct pressure_state state; /* EEPROM contents */ static struct eeprom_data EEMEM eeprom = { .cfg = { - .desired = 4000, /* Millibar */ + .desired = 2500, /* Millibar */ .hysteresis = 200, /* Millibar */ .autoadjust_enable = 1, }, @@ -179,7 +181,20 @@ static void print_banner(void) int main(void) { + uint8_t mcucsr; + cli(); + mcucsr = MCUCSR; + MCUCSR = 0; + wdt_disable(); + if (mcucsr & (1 << BORF)) { + /* If we have a brownout, try to enter valve emergency state. */ + valves_emergency_state(); + mdelay(500); + /* This wasn't a real brownout, if we're still alife. + * Go on with initialization. */ + } + wdt_enable(WDTO_500MS); /* It's OK to init the remote interface that early, as we * have IRQs disabled throughout the init process. So we can't @@ -187,6 +202,8 @@ int main(void) * to send error messages early. */ remote_init(); print_banner(); + if (!(mcucsr & (1 << PORF)) && (mcucsr & (1 << WDRF))) + print("WATCHDOG RESET!\n"); valves_init(); sensor_init(); @@ -208,11 +225,24 @@ int main(void) } if (state.needs_checking) { check_pressure(); - /* Trigger another measurement in 50 milliseconds. */ - state.sensor_trigger_cnt = 50; + /* Trigger another measurement in 25 milliseconds. */ + state.sensor_trigger_cnt = 25; state.needs_checking = 0; mb(); } remote_work(); + wdt_reset(); } } + +/* The fuse bits - AT_Mega8 + * External clock, 0ms startup + * BOD 4.0V + * Boot vector disabled + * Preserve EEPROM disabled + * SPI enabled + */ +FUSES = { + .low = 0x00, + .high = 0xD9, +}; diff --git a/pressure_control/firmware/valves.c b/pressure_control/firmware/valves.c index ec15518..034dc99 100644 --- a/pressure_control/firmware/valves.c +++ b/pressure_control/firmware/valves.c @@ -85,9 +85,19 @@ uint8_t valves_get_global_state(void) return current_global_state; } -void valves_init(void) +static inline void valves_ddr_setup(void) { VALVE_DDR |= (1 << VALVE0_12) | (1 << VALVE0_14) | (1 << VALVE1_12) | (1 << VALVE1_14); +} + +void valves_emergency_state(void) +{ + valves_init(); +} + +void valves_init(void) +{ + valves_ddr_setup(); valves_global_switch(VALVES_IDLE); } diff --git a/pressure_control/firmware/valves.h b/pressure_control/firmware/valves.h index 320505d..aa71268 100644 --- a/pressure_control/firmware/valves.h +++ b/pressure_control/firmware/valves.h @@ -27,6 +27,7 @@ static inline void valve_wait_toggle(void) } void valves_init(void); +void valves_emergency_state(void); void valves_global_switch(uint8_t global_state); uint8_t valves_get_global_state(void); -- cgit v1.2.3