initial commit

A simple program that uses the avr-gcc environment with an arduino board.

Signed-off-by: Jeff Epler <jepler@unpythonic.net>
This commit is contained in:
Jeff Epler 2015-08-26 21:56:16 -05:00
commit 3aa19bb298
3 changed files with 106 additions and 0 deletions

10
.gitignore vendored Normal file
View file

@ -0,0 +1,10 @@
Makefile.local
*-stamp
*.*##
.*.sw?
.sw?
*.elf
*.hex
eagle/*-bom.txt
eagle/*.erc
eagle/*.pro

43
Makefile Normal file
View file

@ -0,0 +1,43 @@
DEV := /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A9007N2c-if00-port0
MCU := atmega328
AMCU := m328p
AVRDUDE := avrdude
AVRDUDE += -p${AMCU} -c arduino -P ${DEV} -b57600
# Place your settings here, such as overriding DEV
-include Makefile.local
ifeq ($(shell [ -e ${DEV} ] && echo 1),1)
default: program-stamp
else
default: main.elf
echo "Board not detected, skipping programming step"
endif
# note: the arduino bootloader can't program eeprom
program-stamp: program
touch $@
program: main.hex
${AVRDUDE} -q -q -D -U flash:w:main.hex:i
communicate:
screen ${DEV} 57600
.PRECIOUS: %.hex
%.hex: %.elf
avr-objcopy -O ihex -R eeprom $< $@
.PRECIOUS: %.elf
%.elf: %.cc
avr-gcc -funit-at-a-time -finline-functions-called-once -fno-exceptions -fno-rtti -mmcu=${MCU} -DF_CPU=16000000ull -O3 -g $< -o $@
clean:
rm -f main.elf main.hex
.PHONY: default program clean communicate
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.

53
main.cc Normal file
View file

@ -0,0 +1,53 @@
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdint.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#define PIN_LED (5) // onboard LED is PORTD5
static int serial_putc(char c, FILE *stream) {
if (c == '\n') {
serial_putc('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
static int serial_getc(FILE *stream) {
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
return UDR0;
}
void serial_install_stdio() {
static FILE uart_stdio;
fdev_setup_stream(&uart_stdio, serial_putc, serial_getc, _FDEV_SETUP_RW);
stdin = stdout = stderr = &uart_stdio;
#define BAUD 57600
#include <util/setbaud.h>
UBRR0 = UBRR_VALUE;
#if USE_2X
UCSR0A |= (1<<U2X0);
#else
UCSR0A &= ~(1<<U2X0);
#endif
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); /* 8-bit data */
UCSR0B = (1<<RXEN0) | (1<<TXEN0); /* enable transmitter and receiver */
}
int main() {
serial_install_stdio();
DDRD |= (1<<PIN_LED);
printf_P(PSTR("HELLO AVR - press any key to toggle LED. characters will be echoed\n"));
while(1) { int c = getchar(); putchar(c); PINB = (1<<PIN_LED); }
}
/*
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
*/