From 5e24f508fab7e8f0f7a55fd7135dded391556f8f Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Thu, 28 Nov 2019 19:50:17 +0100 Subject: [PATCH] initial commit --- example.c | 8 ++++++++ makefile | 40 ++++++++++++++++++++++++++++++++++++++++ uart.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ uart.h | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 example.c create mode 100644 makefile create mode 100644 uart.c create mode 100644 uart.h diff --git a/example.c b/example.c new file mode 100644 index 0000000..b7cce67 --- /dev/null +++ b/example.c @@ -0,0 +1,8 @@ +#include +#include "uart.h" + +int main(void){ + uart_init(); + printf("Moin!\n\r"); + while(1); +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..3ac4add --- /dev/null +++ b/makefile @@ -0,0 +1,40 @@ +TARGET = example +FILES = example uart +MCU = atmega328p +PROGC = m328pb +CC = avr-gcc +TOOL = usbasp + +BUILDDIR = build + +DEFINES = -DF_CPU=16000000UL + +CFLAGS =-mmcu=$(MCU) -O2 -Wall $(DEFINES) -std=c99 -ffunction-sections -fdata-sections +LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections + +LDFILES = $(foreach FILE,$(FILES),$(BUILDDIR)/$(FILE).o) + +all: clean $(BUILDDIR)/$(TARGET).elf + +$(BUILDDIR)/%.o: %.c + @mkdir -p $(BUILDDIR) + $(CC) $(CFLAGS) -c $< -o $(BUILDDIR)/$*.o + +$(BUILDDIR)/$(TARGET).elf: $(LDFILES) + mkdir -p $(BUILDDIR) + $(CC) $(LDFLAGS) $(LDFILES) -o $(BUILDDIR)/$(TARGET).elf + +$(BUILDDIR)/$(TARGET).hex : $(BUILDDIR)/$(TARGET).elf + avr-objcopy -j .data -j .text -O ihex $< $@ + +fuse: + avrdude -p $(PROGC) -c $(TOOL) -U lfuse:w:0xE8:m -U hfuse:w:0xD1:m + +load: $(BUILDDIR)/$(TARGET).hex + avrdude -p $(PROGC) -c $(TOOL) -U flash:w:$(BUILDDIR)/$(TARGET).hex -v -B 2 + +size: $(BUILDDIR)/$(TARGET).elf + avr-size -C --mcu=$(MCU) $(BUILDDIR)/$(TARGET).elf + +clean: + rm -rf $(BUILDDIR) diff --git a/uart.c b/uart.c new file mode 100644 index 0000000..195e9e7 --- /dev/null +++ b/uart.c @@ -0,0 +1,53 @@ +#include +#include "uart.h" + +static int uart_putchar(char c, FILE *stream); + +FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); + +void uart_init() +{ + DDRD |= 1 << 1; // TX + UART_BAUD_REGH = (BAUDRATE>>8); + UART_BAUD_REGL = BAUDRATE; // set baud rate + + UART_CTRL_REGB |= (1< + +#define BAUD 38400 +#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) + +#define UART_BAUD_REGH UBRR0H +#define UART_BAUD_REGL UBRR0L + +#define UART_CTRL_REGA UCSR0A +#define UART_CTRL_REGB UCSR0B +#define UART_CTRL_REGC UCSR0C + +// UCSRA +#define UART_UDRE_BM UDRE0 +#define UART_RXC_BM RXC0 + +// UCSRB +#define UART_TXEN_BM TXEN0 +#define UART_RXEN_BM RXEN0 +#define UART_RXCIE_BM RXCIE0 + +// UCSRC +#define UART_URSEL_BM 0 /* only for old atmega */ +#define UART_UCSZ0_BM UCSZ00 +#define UART_UCSZ1_BM UCSZ01 + +#define UART_DATA_REG UDR0 + +void uart_init (void); +void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex); + +#endif + +