From 972126fd2406030dbd3abd1a705bb0ae1af9abb3 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 15 Dec 2017 14:55:27 +0100 Subject: Add AVR (Arduino Nano) support --- src/arch/arduino-nano/Makefile.inc | 40 +++++++++++++++++++++++ src/arch/arduino-nano/arch.cc | 58 ++++++++++++++++++++++++++++++++++ src/arch/arduino-nano/driver/gpio.cc | 24 ++++++++++++++ src/arch/arduino-nano/driver/stdout.cc | 32 +++++++++++++++++++ src/arch/arduino-nano/driver/uptime.cc | 3 ++ 5 files changed, 157 insertions(+) create mode 100644 src/arch/arduino-nano/Makefile.inc create mode 100644 src/arch/arduino-nano/arch.cc create mode 100644 src/arch/arduino-nano/driver/gpio.cc create mode 100644 src/arch/arduino-nano/driver/stdout.cc create mode 100644 src/arch/arduino-nano/driver/uptime.cc (limited to 'src/arch') diff --git a/src/arch/arduino-nano/Makefile.inc b/src/arch/arduino-nano/Makefile.inc new file mode 100644 index 0000000..e8ecd82 --- /dev/null +++ b/src/arch/arduino-nano/Makefile.inc @@ -0,0 +1,40 @@ +# vim:ft=make + +MCU = atmega328p +PROGRAMMER ?= usbasp + +INCLUDES += -Iinclude/arduino-nano +COMMON_FLAGS += -mmcu=${MCU} -DF_CPU=16000000UL + +CC = avr-gcc +CXX = avr-g++ +NM = avr-nm +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump + +TARGETS += src/arch/arduino-nano/arch.cc +TARGETS += src/arch/arduino-nano/driver/gpio.cc +TARGETS += src/arch/arduino-nano/driver/stdout.cc +TARGETS += src/arch/arduino-nano/driver/uptime.cc + +OBJECTS = ${TARGETS:.cc=.o} + +.cc.o: + ${CXX} ${INCLUDES} ${COMMON_FLAGS} ${CXXFLAGS} -c -o $@ ${@:.o=.cc} + +build/system.elf: ${OBJECTS} + ${CXX} ${COMMON_FLAGS} ${CXXFLAGS} -Wl,--gc-sections -o $@ ${OBJECTS} + +build/system.hex: build/system.elf + ${OBJCOPY} -O ihex ${@:.hex=.elf} $@ + +program: build/system.hex + avrdude -p ${MCU} -c arduino -P /dev/ttyUSB0 -b 57600 -U flash:w:build/system.hex + +arch_clean: + rm -f ${OBJECTS} build/system.hex + +monitor: + screen /dev/ttyUSB0 115200 + +.PHONY: arch_clean monitor program diff --git a/src/arch/arduino-nano/arch.cc b/src/arch/arduino-nano/arch.cc new file mode 100644 index 0000000..28fe34b --- /dev/null +++ b/src/arch/arduino-nano/arch.cc @@ -0,0 +1,58 @@ +#include "arch.h" +#include +#include + +void Arch::setup(void) +{ +#ifdef TIMER_CYCLES + TCCR0A = 0; + TCCR0B = _BV(CS00); +#endif + +#if defined(WITH_LOOP) || defined(TIMER_S) + TCCR1A = 0; + TCCR1B = _BV(WGM12) | _BV(CS12) | _BV(CS10); // /1024 + OCR1A = 15625; + TIMSK1 = _BV(OCIE1A); +#endif + +#ifdef TIMER_US + // 16MHz/8 -> 2MHz timer + TCCR2A = 0; + TCCR2B = _BV(CS21); +#endif + sei(); +} + +#if defined(WITH_LOOP) || defined(TIMER_S) + +#include "driver/uptime.h" +extern void loop(); + +#endif + +void Arch::idle_loop(void) +{ + while (1) { + SMCR = _BV(SE); + asm("sleep"); + SMCR = 0; + asm("wdr"); +#ifdef WITH_LOOP + loop(); +#endif +#ifdef TIMER_S + uptime.tick_s(); +#endif + } +} + +Arch arch; + +#if defined(WITH_LOOP) || defined(TIMER_S) + +ISR(TIMER1_COMPA_vect) +{ +} + +#endif diff --git a/src/arch/arduino-nano/driver/gpio.cc b/src/arch/arduino-nano/driver/gpio.cc new file mode 100644 index 0000000..66e24db --- /dev/null +++ b/src/arch/arduino-nano/driver/gpio.cc @@ -0,0 +1,24 @@ +#include "driver/gpio.h" +#include + +void GPIO::setup() +{ + DDRB = _BV(PB5); +} + +void GPIO::led_on(unsigned char id) +{ + PORTB |= _BV(PB5); +} + +void GPIO::led_off(unsigned char id) +{ + PORTB &= ~_BV(PB5); +} + +void GPIO::led_toggle(unsigned char id) +{ + PINB = _BV(PB5); +} + +GPIO gpio; diff --git a/src/arch/arduino-nano/driver/stdout.cc b/src/arch/arduino-nano/driver/stdout.cc new file mode 100644 index 0000000..a3d905d --- /dev/null +++ b/src/arch/arduino-nano/driver/stdout.cc @@ -0,0 +1,32 @@ +#include "driver/stdout.h" +#include +#include + +#define BAUD 119200L +#include + +void StandardOutput::setup() +{ + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; + +#if USE_2X + UCSR0A |= _BV(U2X0); +#else + UCSR0A &= ~_BV(U2X0); +#endif + + UCSR0B |= _BV(RXEN0) | _BV(TXEN0); + UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); +} + +void StandardOutput::put(char c) +{ + while (!(UCSR0A & _BV(UDRE0))); + UDR0 = c; + if (c == '\n') { + put('\r'); + } +} + +StandardOutput kout; diff --git a/src/arch/arduino-nano/driver/uptime.cc b/src/arch/arduino-nano/driver/uptime.cc new file mode 100644 index 0000000..388edb6 --- /dev/null +++ b/src/arch/arduino-nano/driver/uptime.cc @@ -0,0 +1,3 @@ +#include "driver/uptime.h" + +Uptime uptime; -- cgit v1.2.3