summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/arduino-nano/driver/gpio.h18
-rw-r--r--include/arduino-nano/driver/stdout.h19
-rw-r--r--include/arduino-nano/driver/uptime.h29
-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
8 files changed, 223 insertions, 0 deletions
diff --git a/include/arduino-nano/driver/gpio.h b/include/arduino-nano/driver/gpio.h
new file mode 100644
index 0000000..177e7c5
--- /dev/null
+++ b/include/arduino-nano/driver/gpio.h
@@ -0,0 +1,18 @@
+#ifndef GPIO_H
+#define GPIO_H
+
+class GPIO {
+ private:
+ GPIO(const GPIO &copy);
+
+ public:
+ GPIO () {}
+ void setup();
+ void led_on(unsigned char id);
+ void led_off(unsigned char id);
+ void led_toggle(unsigned char id);
+};
+
+extern GPIO gpio;
+
+#endif
diff --git a/include/arduino-nano/driver/stdout.h b/include/arduino-nano/driver/stdout.h
new file mode 100644
index 0000000..d6cb2b0
--- /dev/null
+++ b/include/arduino-nano/driver/stdout.h
@@ -0,0 +1,19 @@
+#ifndef STANDARDOUTPUT_H
+#define STANDANDOUTPUT_H
+
+#include "object/outputstream.h"
+
+class StandardOutput : public OutputStream {
+ private:
+ StandardOutput(const StandardOutput &copy);
+
+ public:
+ StandardOutput () {}
+ void setup();
+
+ virtual void put(char c) override;
+};
+
+extern StandardOutput kout;
+
+#endif
diff --git a/include/arduino-nano/driver/uptime.h b/include/arduino-nano/driver/uptime.h
new file mode 100644
index 0000000..86a8bb5
--- /dev/null
+++ b/include/arduino-nano/driver/uptime.h
@@ -0,0 +1,29 @@
+#ifndef UPTIME_H
+#define UPTIME_H
+
+#include <avr/io.h>
+
+class Uptime {
+ private:
+ Uptime(const Uptime &copy);
+#ifdef TIMER_S
+ uint8_t seconds;
+#endif
+
+ public:
+#ifdef TIMER_S
+ Uptime () : seconds(0) {}
+#else
+ Uptime () {}
+#endif
+ inline uint8_t get_cycles() { return TCNT0; }
+ inline uint8_t get_us() { return TCNT2/2; }
+#ifdef TIMER_S
+ inline uint8_t get_s() { return seconds; }
+ inline void tick_s() { seconds++; }
+#endif
+};
+
+extern Uptime uptime;
+
+#endif
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;