From 218faa618209bbc22502c177b8d21e1631257e32 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Wed, 13 Jun 2018 01:56:57 +0200 Subject: [PATCH] Started work on firmware --- firmware/.gitignore | 1 + firmware/Makefile | 56 +++++++++++++++++++++++++++++++++++ firmware/include/twi.h | 12 ++++++++ firmware/main.c | 17 +++++++++++ firmware/twi.c | 66 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 firmware/.gitignore create mode 100644 firmware/Makefile create mode 100644 firmware/include/twi.h create mode 100644 firmware/main.c create mode 100644 firmware/twi.c diff --git a/firmware/.gitignore b/firmware/.gitignore new file mode 100644 index 0000000..ba077a4 --- /dev/null +++ b/firmware/.gitignore @@ -0,0 +1 @@ +bin diff --git a/firmware/Makefile b/firmware/Makefile new file mode 100644 index 0000000..30a03ce --- /dev/null +++ b/firmware/Makefile @@ -0,0 +1,56 @@ +AVRMCU ?= atmega8 +F_CPU ?= 16000000 +ISPPORT ?= /dev/kaboard + +VERSION = 0.1 + +HEADERS = include/twi.h +SRC = main.c twi.c +TARGET = cube-kl +OBJDIR = bin + +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size + +OBJ = $(SRC:%.c=$(OBJDIR)/$(AVRMCU)/%.o) + +CFLAGS = -I include -I images -I fonts -Os -Wall -Wstrict-prototypes +CFLAGS += -ffunction-sections -fdata-sections +CFLAGS += -fshort-enums -fpack-struct -funsigned-char -funsigned-bitfields +CFLAGS += -mmcu=$(AVRMCU) -DF_CPU=$(F_CPU)UL -DVERSION=$(VERSION) + +LDFLAGS = -mmcu=$(AVRMCU) -Wl,--gc-sections + +all: start $(OBJDIR)/$(AVRMCU)/$(TARGET).hex size + @echo ":: Done !" + +start: + @echo "cube-kl firmware $(VERSION)" + @echo "===========================" + @echo ":: Building for $(AVRMCU)" + @echo ":: MCU operating frequency is $(F_CPU)Hz" + +$(OBJDIR)/$(AVRMCU)/%.o : %.c $(HEADERS) Makefile + @mkdir -p $$(dirname $@) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJDIR)/$(AVRMCU)/$(TARGET).elf : $(OBJ) + $(CC) $(LDFLAGS) $+ -o $@ + +$(OBJDIR)/$(AVRMCU)/$(TARGET).hex : $(OBJDIR)/$(AVRMCU)/$(TARGET).elf + $(OBJCOPY) -O ihex $< $@ + +size : $(OBJDIR)/$(AVRMCU)/$(TARGET).elf + @echo + @$(SIZE) --mcu=$(AVRMCU) -C $(OBJDIR)/$(AVRMCU)/$(TARGET).elf + @echo + +clean : + @rm -rf $(OBJDIR) + +flash : all + avrdude -c arduino \ + -p $(AVRMCU) -P $(ISPPORT) \ + -U flash:w:$(OBJDIR)/$(AVRMCU)/$(TARGET).hex diff --git a/firmware/include/twi.h b/firmware/include/twi.h new file mode 100644 index 0000000..570e9ca --- /dev/null +++ b/firmware/include/twi.h @@ -0,0 +1,12 @@ +#ifndef __TWI_H__ +#define __TWI_H__ __TWI_H__ + +#include + +#include + + +void twi_init(void); +uint8_t twi_write(uint8_t address, uint8_t data[], uint8_t len); + +#endif diff --git a/firmware/main.c b/firmware/main.c new file mode 100644 index 0000000..a05c051 --- /dev/null +++ b/firmware/main.c @@ -0,0 +1,17 @@ +#include +#include + +#include "twi.h" + +int main(void) { + + twi_init(); + + while(1) { + uint8_t data[] = {'T','E','S','T'}; + twi_write(96, data, 4); + _delay_ms(10); + } + + while(1); +} diff --git a/firmware/twi.c b/firmware/twi.c new file mode 100644 index 0000000..ed4ca93 --- /dev/null +++ b/firmware/twi.c @@ -0,0 +1,66 @@ +#include "twi.h" + +void twi_init() { + // bitrate = 1600000MHz / (16 + 2 * TWBR * 4^TWPS) + + // Overall prescaler 40 = 16 + 2 * 3 * 4^1 + TWBR = 3; // 2 * 3 = 6 + TWSR |= (1 << TWPS0); // 4 + +} + +uint8_t twi_start(void) { + // Enable internal pull-ups + PORTC |= (1 << PC5) | (1 << PC4); + + // Setup for sending start condition, trigger sending, keep TWI enabled + TWCR |= (1 << TWSTA) | (1 << TWINT) | (1 << TWEN); + + // Poll for TWI unit to finish + while(!(TWCR & (1 << TWINT))); + + // Check if start was succesfull + return (TW_STATUS != TW_START); +} + +void twi_stop(void) { + // Setup for sending stop condition, trigger sending, keep TWI enabled + TWCR = (1 << TWSTO) | (1 << TWINT) | (1 << TWEN); +} + + +uint8_t twi_write(uint8_t address, uint8_t data[], uint8_t len) { + // Send start condition + if(twi_start()) { + return 1; + } + + TWDR = address | TW_WRITE; // We want to write to the slave + TWCR = (1 << TWINT) | (1 << TWEN); // Trigger transmission, keep TWI enabled + + // Wait for TWI unit to finish + while(!(TWCR & (1 << TWINT))); + + // Make sure the slave has acked + if(TW_STATUS != TW_MT_SLA_ACK) { + return 1; + } + + for(uint8_t i = 0; i < len; i++) { + TWDR = data[i]; // Send actual data + TWCR = (1 << TWINT ) | (1 << TWEN); // Trigger transmission, keep TWI enabled + + // Wait for TWI unit to finish + while(!(TWCR & (1 << TWINT))); + + // Make sure the data has been acked + if(TW_STATUS != TW_MT_DATA_ACK) { + return 1; + } + } + + // Send stop condition + twi_stop(); + + return 0; +}