Ignition monitor: Initial commit

Implemented:
* sleep mode
* monitoring portb pin3
* switching portb pin4

Missing:
* shutdown guard time
This commit is contained in:
tkl 2019-12-15 10:48:04 +01:00 committed by Thomas Klaehn
commit f5f29be68a
6 changed files with 186 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
bin/
objs/

14
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/lib/avr/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc"
}
],
"version": 4
}

9
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,9 @@
{
"files.associations": {
"interrupt.h": "c",
"io.h": "c",
"iotn85.h": "c",
"iotnx5.h": "c",
"sleep.h": "c"
}
}

43
.vscode/tasks.json vendored Normal file
View File

@ -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
}
}
]
}

75
Makefile Normal file
View File

@ -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

42
src/main.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdint.h>
#define F_CPU 1000000UL
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <util/delay.h>
#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;
}