From 27321e05de35b494c2b282652e1c40a18435b68b Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Fri, 4 Jun 2021 19:09:07 +0200 Subject: implement uart --- Makefile | 19 +++++++++++++--- src/main.c | 40 ++++++++++++++++++++++++++++++++- src/uart.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/uart.h | 36 ++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 src/uart.c create mode 100644 src/uart.h diff --git a/Makefile b/Makefile index 38f5292..386814d 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,18 @@ MCU = atmega8 -CPUFREQ = 160000000 PROGRAMMER = dragon_isp +CPUFREQ = 8000000 #8MHz +BAUD = 9600 + +# https://www.engbedded.com/fusecalc/ +# Int. RC Osc. 8MHz +# No WD +# No BOD +LFUSE = 0xc4 +HFUSE = 0xd9 + CC = avr-gcc -CFLAGS = -std=c89 -Wall -mmcu=$(MCU) -DF_CPU=$(CPUFREQ) +CFLAGS = -std=c89 -Wall -mmcu=$(MCU) -DF_CPU=$(CPUFREQ) -DBAUD=$(BAUD) LDFLAGS = -mmcu=$(MCU) BUILDDIR = build SOURCEDIR = src @@ -19,6 +28,7 @@ build: dir $(OBJ) @echo LD $(OBJ) @$(CC) $(CFLAGS) -o $(BUILDDIR)/$(OUTPUT) $(OBJ) $(LDFLAGS) @avr-objcopy -O ihex $(BUILDDIR)/$(OUTPUT) $(BUILDDIR)/$(OUTPUT).hex + @avr-size --mcu=$(MCU) --format=avr $(BUILDDIR)/$(OUTPUT) debug: -D _DEBUG debug: build; @@ -41,7 +51,10 @@ clean: all: clean build flash: build - @avrdude -p $(MCU) -c $(PROGRAMMER) -U flash:w:$(BUILDDIR)/$(OUTPUT).hex:i + @sudo avrdude -p $(MCU) -c $(PROGRAMMER) -U flash:w:$(BUILDDIR)/$(OUTPUT).hex:i + +fuse: + @sudo avrdude -p $(MCU) -c $(PROGRAMMER) -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m devsetup: @echo "--target=avr -isystem /usr/avr/include/ $(CFLAGS)" | tr ' ' '\n' > compile_flags.txt diff --git a/src/main.c b/src/main.c index c0f35d7..a73e7ef 100644 --- a/src/main.c +++ b/src/main.c @@ -2,24 +2,62 @@ #include #include +#include "uart.h" + +uint8_t t0_ovf_cnt = 0; + +uint8_t pb0_thresh = 128; + ISR(TIMER0_OVF_vect) { cli(); + /*TCNT0 = (1<<7);*/ /* Hack-increase Interrupt trigger freq */ + + t0_ovf_cnt ++; + + if ( t0_ovf_cnt >= pb0_thresh ) + PORTB &= ~(1< + * License: All rights reserved. + */ + +#include "uart.h" + +struct ringbuff_s { + uint8_t buffer[256]; + uint8_t read; + uint8_t write; +} rxc_buf, txc_buf ; + +ISR(USART_RXC_vect) { + cli(); + + /* Discard if buffer full */ + if( rxc_buf.read - 1 == rxc_buf.write ) + goto end; + + rxc_buf.buffer[rxc_buf.write] = UDR; + rxc_buf.write++; + +end: + sei(); +} + +ISR(USART_UDRE_vect) { + cli(); + + /* Prevent interrupt loops */ + if( txc_buf.read == txc_buf.write ) { + UCSRB &= ~(1<>8); + UBRRL = (uint8_t)(UBRR); + + UCSRB |= (1< + * License: All rights reserved. + */ + +/* + * Interrupt controlled UART + */ + +#ifndef _UART_H_ +#define _UART_H_ + +#include +#include +#include +#include + +#ifndef BAUD +#warning BAUD "BAUD not defined. Dafaulting to 9600" +#define BAUD 9600 +#endif + +#ifndef F_CPU +#error "F_CPU not defined" +#endif + +#define UBRR F_CPU/16/BAUD-1 + +void uart_init(); + +uint8_t uart_putchar(char _c); + +uint8_t uart_getchar(char *_c); + +#endif -- cgit v1.2.3