Started work on firmware

This commit is contained in:
Sebastian 2018-06-13 01:56:57 +02:00
parent 7c24554978
commit 218faa6182
5 changed files with 152 additions and 0 deletions

1
firmware/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin

56
firmware/Makefile Normal file
View File

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

12
firmware/include/twi.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __TWI_H__
#define __TWI_H__ __TWI_H__
#include <stdint.h>
#include <util/twi.h>
void twi_init(void);
uint8_t twi_write(uint8_t address, uint8_t data[], uint8_t len);
#endif

17
firmware/main.c Normal file
View File

@ -0,0 +1,17 @@
#include <avr/io.h>
#include <util/delay.h>
#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);
}

66
firmware/twi.c Normal file
View File

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