summaryrefslogtreecommitdiff
path: root/src/arch
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2017-12-15 14:55:27 +0100
committerDaniel Friesel <derf@finalrewind.org>2017-12-15 14:55:27 +0100
commit972126fd2406030dbd3abd1a705bb0ae1af9abb3 (patch)
tree74fea3d91fcaabebdd3c354fdc594ddedc09037e /src/arch
parentbfcfa4cf67bad25ccba5ff735967d4c6abff899c (diff)
Add AVR (Arduino Nano) support
Diffstat (limited to 'src/arch')
-rw-r--r--src/arch/arduino-nano/Makefile.inc40
-rw-r--r--src/arch/arduino-nano/arch.cc58
-rw-r--r--src/arch/arduino-nano/driver/gpio.cc24
-rw-r--r--src/arch/arduino-nano/driver/stdout.cc32
-rw-r--r--src/arch/arduino-nano/driver/uptime.cc3
5 files changed, 157 insertions, 0 deletions
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 <avr/io.h>
+#include <avr/interrupt.h>
+
+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 <avr/io.h>
+
+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 <avr/io.h>
+#include <avr/interrupt.h>
+
+#define BAUD 119200L
+#include <util/setbaud.h>
+
+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;