Added start of firmware

This commit is contained in:
Sebastian 2020-07-10 19:01:20 +02:00
parent dd7ce0201d
commit 7fc1e4c3ec
20 changed files with 198 additions and 0 deletions

5
.gitignore vendored
View File

@ -10,3 +10,8 @@ _autosave-*
*-save.pro
*-save.kicad_pcb
fp-info-cache
# Firmware
*.o
*.hex
*.elf

61
firmware/Makefile Normal file
View File

@ -0,0 +1,61 @@
AVRMCU = attiny2313
F_CPU = 8000000
VERSION = 0.1
HEADERS =
SRC = main.c
TARGET = mate-driver-$(AVRMCU)
OBJDIR = bin
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
OBJ = $(SRC:%.c=$(OBJDIR)/%.o)
CFLAGS = -Os -Wall -Wstrict-prototypes
CFLAGS += -fshort-enums -fpack-struct -funsigned-char -funsigned-bitfields
CFLAGS += -mmcu=$(AVRMCU) -DF_CPU=$(F_CPU)UL -DVERSION=$(VERSION)
all: start $(OBJDIR)/$(TARGET).hex size
@echo ":: Done !"
start:
@echo "Mate driver firmare version $(VERSION)"
@echo "======================================"
@echo ":: Building for $(AVRMCU)"
@echo ":: MCU operating frequency is $(F_CPU)Hz"
$(OBJDIR)/%.o: %.c $(HEADERS)
@mkdir -p $$(dirname $@)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/$(TARGET).elf: $(OBJ)
$(CC) $(CFLAGS) $+ -o $@
$(OBJDIR)/$(TARGET).hex: $(OBJDIR)/$(TARGET).elf
$(OBJCOPY) -O ihex $< $@
size: $(OBJDIR)/$(TARGET).elf
@echo
@$(SIZE) --mcu=$(AVRMCU) -C $(OBJDIR)/$(TARGET).elf
@echo
clean:
@rm -rf $(OBJDIR)
fuse:
avrdude -c usbasp \
-p $(AVRMCU) \
-B 10 \
-U lfuse:w:0xde:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m
flash: all
avrdude -c usbasp \
-p $(AVRMCU) \
-U flash:w:$(OBJDIR)/$(TARGET).hex

132
firmware/main.c Normal file
View File

@ -0,0 +1,132 @@
#include <avr/io.h>
#include <util/delay.h>
static inline void hbridge_init() {
DDRB |= (1 << PB2); // PWM
DDRB |= (1 << PB0) | (1 << PB1); // H1 L1
DDRB |= (1 << PB3) | (1 << PB4); // H2 L2
}
static inline void hbridge_set_h1() {
PORTB |= (1 << PB0);
}
static inline void hbridge_unset_h1() {
PORTB &= ~(1 << PB0);
}
static inline void hbridge_set_l1() {
PORTB |= (1 << PB1);
}
static inline void hbridge_unset_l1() {
PORTB &= ~(1 << PB1);
}
static inline void hbridge_set_h2() {
PORTB |= (1 << PB3);
}
static inline void hbridge_unset_h2() {
PORTB &= ~(1 << PB3);
}
static inline void hbridge_set_l2() {
PORTB |= (1 << PB4);
}
static inline void hbridge_unset_l2() {
PORTB &= ~(1 << PB4);
}
static inline void pwm_cycle_backwards(uint8_t speed) {
hbridge_unset_l2();
hbridge_set_l1();
for(uint8_t i = 0; i < 8; i++) {
if((speed & (1 << i)) != 0) {
hbridge_set_h2();
}
for(uint8_t j = 0; j < (1 << i); j++) {
_delay_us(2);
}
hbridge_unset_h2();
}
}
static inline void pwm_cycle_forwards(uint8_t speed) {
hbridge_unset_l1();
hbridge_set_l2();
for(uint8_t i = 0; i < 8; i++) {
if((speed & (1 << i)) != 0) {
hbridge_set_h1();
}
for(uint8_t j = 0; j < (1 << i); j++) {
_delay_us(2);
}
hbridge_unset_h1();
}
}
int main(void) {
_delay_ms(5000);
hbridge_init();
while(1) {
//hbridge_set_l1();
for(uint8_t speed = 20; speed <= 230; speed++) {
for(uint16_t i = 0; i < 250; i++) {
pwm_cycle_forwards(speed);
}
}
hbridge_unset_h2();
hbridge_unset_h2();
hbridge_set_l1();
hbridge_set_l2();
_delay_ms(1000);
hbridge_unset_l2();
hbridge_unset_l2();
for(uint8_t speed = 20; speed <= 230; speed++) {
for(uint16_t i = 0; i < 250; i++) {
pwm_cycle_backwards(speed);
}
}
hbridge_unset_h2();
hbridge_unset_h2();
hbridge_set_l1();
hbridge_set_l2();
_delay_ms(1000);
hbridge_unset_l2();
hbridge_unset_l2();
/*
for(uint16_t i = 0; i < 5000; i++) {
hbridge_set_h2();
_delay_ms(0.9);
hbridge_unset_h2();
_delay_ms(0.1);
}
hbridge_set_l2();
_delay_ms(100);
hbridge_unset_l1();
for(uint16_t i = 0; i < 5000; i++) {
hbridge_set_h1();
_delay_ms(0.9);
hbridge_unset_h1();
_delay_ms(0.1);
}
*/
}
}