commit f5f29be68a852716b2bdea016707e00741a74ff9 Author: tkl Date: Sun Dec 15 10:48:04 2019 +0100 Ignition monitor: Initial commit Implemented: * sleep mode * monitoring portb pin3 * switching portb pin4 Missing: * shutdown guard time diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf273aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin/ +objs/ + diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..cf0ff83 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/lib/avr/include" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..357432b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "files.associations": { + "interrupt.h": "c", + "io.h": "c", + "iotn85.h": "c", + "iotnx5.h": "c", + "sleep.h": "c" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..680014d --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,43 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "type": "shell", + "command": "make", + "echoCommand": true, + "problemMatcher": { + "base": "$gcc", + }, + "presentation": { + "focus": true, + "reveal": "always", + "panel": "shared", + "clear": true, + }, + "tasks": [ + { + "label": "all", + "args": ["all"], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "clean", + "args": ["clean"], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "flash", + "args": ["flash"], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..06c930f --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +CROSS_COMPILE ?= avr- + +TARGET_FILE ?= avr.elf + +MCU = attiny85 +DUDE_MCU = t85 + +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 += -Iinclude + +DUDE_FLAGS = -p $(DUDE_MCU) +DUDE_FLAGS += -P /dev/spidev0.0 +DUDE_FLAGS += -c linuxspi +DUDE_FLAGS += -b 10000 + +CC = $(CROSS_COMPILE)gcc +AS = $(CROSS_COMPILE)as +LD = $(CROSS_COMPILE)ld +OBJCOPY = $(CROSS_COMPILE)objcopy +SIZE = $(CROSS_COMPILE)size +AVRDUDE = avrdude + +OBJ_DIR := objs +BIN_DIR := bin +SRC_DIR := src + +C_SRCS = $(wildcard $(SRC_DIR)/*.c) +C_OBJS = $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(C_SRCS))) + +OBJS = $(C_OBJS) +TARGET = $(BIN_DIR)/$(TARGET_FILE) + +.PHONY: all +all: $(TARGET) + +$(TARGET): $(OBJS) Makefile + @mkdir -p $(BIN_DIR) + $(CC) $(LD_FLAGS) $(LINKER_SCRIPT) $(OBJS) $(LD_LIBS) -o $@ -Wl,-Map=$(BIN_DIR)/$(notdir $(basename $@)).map + $(OBJCOPY) -O ihex $(TARGET) $(subst .elf,.hex,$(TARGET)) + $(SIZE) $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(dir $@) + @$(call makedep,$<,$@,$(patsubst %.c,%.d,$(OBJ_DIR)/$(notdir $<)),$(C_FLAGS)) + $(CC) -c $(C_FLAGS) $(CCFLAGS) -o $@ $< + +.PHONY: clean +clean: + rm -rf \ + $(OBJS) \ + $(patsubst %.o,%.d,$(OBJS)) \ + $(TARGET) \ + $(subst .elf,.map,$(TARGET)) \ + $(subst .elf,.hex,$(TARGET)) + +.PHONY: flash +flash: $(TARGET) + gpio 22 off + $(AVRDUDE) $(DUDE_FLAGS) -U flash:w:$(subst .elf,.hex,$(TARGET)) + gpio 22 on + +define makedep + $(CC) -MM -MF $3 -MP -MT $2 $4 $1 +endef + +ifneq ($(MAKECMDGOALS),clean) +-include $($(subst .o,.d,$(OBJS)) +endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..0c9545b --- /dev/null +++ b/src/main.c @@ -0,0 +1,42 @@ +#include + +#define F_CPU 1000000UL + +#include +#include +#include +#include +#include + +#define IGNITION_PIN 4 +#define POWER_PIN 3 + +int main(void) +{ + DDRB = 0xff; + DDRB &= ~(1 << IGNITION_PIN); + PORTB |= (1 << PB0) | (1 << PB1) | (1 << PB2); + + wdt_enable(WDTO_8S); + while(1) { + wdt_reset(); + if(PINB & (1 << IGNITION_PIN)) { + PORTB |= (1 << POWER_PIN); + } else { + if(PINB & (1 << POWER_PIN)) { + for(uint8_t i = 0; i < 100; ++i) { + wdt_reset(); + _delay_ms(100); + } + PORTB &= ~(1 << POWER_PIN); + } + wdt_enable(WDTO_8S); + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleep_enable(); + sleep_cpu(); + sleep_disable(); + } + } + + return 0; +}