initial commit
commit
5e24f508fa
@ -0,0 +1,8 @@
|
||||
#include <avr/io.h>
|
||||
#include "uart.h"
|
||||
|
||||
int main(void){
|
||||
uart_init();
|
||||
printf("Moin!\n\r");
|
||||
while(1);
|
||||
}
|
||||
@ -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)
|
||||
@ -0,0 +1,53 @@
|
||||
#include <avr/io.h>
|
||||
#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<<UART_TXEN_BM)
|
||||
|(1<<UART_RXEN_BM)
|
||||
|(1<<UART_RXCIE_BM); // enable receiver and transmitter
|
||||
|
||||
UART_CTRL_REGC |= (1<<UART_URSEL_BM)
|
||||
|(1<<UART_UCSZ0_BM)
|
||||
|(1<<UART_UCSZ1_BM); // 8bit data format
|
||||
|
||||
stdout = &uart_output;
|
||||
}
|
||||
|
||||
static int uart_putchar(char c, FILE *stream) {
|
||||
if (c == '\n') {
|
||||
uart_putchar('\r', stream);
|
||||
}
|
||||
loop_until_bit_is_set(UART_CTRL_REGA, UART_UDRE_BM);
|
||||
UART_DATA_REG = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex){
|
||||
for(int i=0; i < buffersize; i++){
|
||||
buffer[i]=0;
|
||||
}
|
||||
*bufferindex=0;
|
||||
|
||||
char input;
|
||||
do{
|
||||
while ((UART_CTRL_REGA & (1 << UART_RXC_BM)) == 0);
|
||||
input = UART_DATA_REG;
|
||||
|
||||
putchar(input); //echo
|
||||
buffer[*bufferindex]=input;
|
||||
*bufferindex=*bufferindex+1;
|
||||
}while(!(input == '\n' || input == '\r'));
|
||||
buffer[*bufferindex]=0;
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
#ifndef _UART_H_
|
||||
#define _UART_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue