Browse Source

new folder 'avr' with test project to test the AVR on USB-AVR-CPLD

* makefile with compile, program & clean targets
* src/avrtest.c file with test code (@ the moment a simple uart loop)
master
T. Meissner 12 years ago
parent
commit
05322a603a
2 changed files with 109 additions and 0 deletions
  1. +24
    -0
      avr/makefile
  2. +85
    -0
      avr/src/avrtest.c

+ 24
- 0
avr/makefile View File

@ -0,0 +1,24 @@
#AVR-GCC Makefile
PROJECT = avrtest
SOURCES = src/avrtest.c
CC = avr-gcc
OBJCOPY = avr-objcopy
MMCU = atmega88
DUMMY_BINARY:=$(shell mkdir -p binary)
CFLAGS = -mmcu=$(MMCU) -Wall -Os -std=gnu99
compile : binary/$(PROJECT).out
binary/$(PROJECT).hex: binary/$(PROJECT).out
$(OBJCOPY) -j .text -O ihex binary/$(PROJECT).out binary/$(PROJECT).hex
binary/$(PROJECT).out: $(SOURCES)
$(CC) $(CFLAGS) -I./ -o binary/$(PROJECT).out $(SOURCES)
program: binary/$(PROJECT).hex
avrdude -p m88 -c buspirate -P /dev/cu.PL2303-003012FA -e -U flash:w:binary/$(PROJECT).hex
clean:
rm -rf binary/

+ 85
- 0
avr/src/avrtest.c View File

@ -0,0 +1,85 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#ifndef F_CPU
#warning "F_CPU wasn't set in makefile, so we do it now with 8.0 MHz"
#define F_CPU 1000000UL
#endif
// setting baud rate
#define BAUD 4800UL
// uart baud rate checks
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD)
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error baudrate error higher than 1%!
#endif
static void uart_init (void);
static void uart_putc (unsigned char c);
volatile uint8_t got_char;
volatile unsigned char rx_char;
int main (void)
{
// uart rx initialisation
uart_init();
// activate global interrupts
sei();
while(1)
{
if(got_char) {
UCSR0B &= ~(1<<RXCIE0);
uart_putc(rx_char);
got_char = 0;
UCSR0B |= (1<<RXCIE0);
}
}
return 0;
}
// function to init uart
static void uart_init (void) {
// set baudrate registers (high byte first!)
UBRR0H = UBRR_VAL >> 8;
UBRR0L = UBRR_VAL & 0xFF;
// tx enable
UCSR0B |= (1<<TXEN0) | (1<<RXEN0) | (1<<RXCIE0);
// frame format: async mode, no parity, 1 stop bit, 8 bit size
UCSR0C |= (3<<UCSZ00);
}
// send a byte on uart
static void uart_putc (unsigned char c) {
while (!(UCSR0A & (1<<UDRE0))) {}
UDR0 = c;
}
ISR(USART_RX_vect) {
uint8_t rx_error = 0;
//check for frame error
if (UCSR0A & (1<<FE0)) {
rx_error = 1;
}
// save rx buffer
rx_char = UDR0;
// signalise the new rx char if no frame error occured
if (!rx_error) {
got_char = 1;
}
}

Loading…
Cancel
Save