summaryrefslogtreecommitdiff
path: root/src/arch/atmega2560
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-02-07 21:12:04 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-02-07 21:12:04 +0100
commit998419816c815175cd4c4f226e619c578d42e0aa (patch)
tree3efa872828bb973406a15469ac9ddfd245f29cfa /src/arch/atmega2560
parent30431427adbf05a3e5cc17f6fb4b1b1cbc105b39 (diff)
add arch atmega2560 (preliminary support)
Diffstat (limited to 'src/arch/atmega2560')
-rw-r--r--src/arch/atmega2560/Makefile.inc185
-rw-r--r--src/arch/atmega2560/arch.cc104
-rw-r--r--src/arch/atmega2560/driver/counter.cc15
-rw-r--r--src/arch/atmega2560/driver/gpio.cc26
-rw-r--r--src/arch/atmega2560/driver/stdout.cc40
-rw-r--r--src/arch/atmega2560/driver/timer.cc8
-rw-r--r--src/arch/atmega2560/driver/uptime.cc8
7 files changed, 386 insertions, 0 deletions
diff --git a/src/arch/atmega2560/Makefile.inc b/src/arch/atmega2560/Makefile.inc
new file mode 100644
index 0000000..d78db66
--- /dev/null
+++ b/src/arch/atmega2560/Makefile.inc
@@ -0,0 +1,185 @@
+# vim:ft=make
+#
+# Copyright 2021 Daniel Friesel
+#
+# SPDX-License-Identifier: BSD-2-Clause
+
+MCU = atmega2560
+SERIAL_PORT ?= /dev/ttyUSB0
+BAUD = 115200
+
+ifdef CONFIG_arch_atmega2560_cpufreq
+ cpu_freq = ${CONFIG_arch_atmega2560_cpufreq}
+endif
+
+cpu_freq ?= 16000000
+
+COMMON_FLAGS += -Werror=overflow
+COMMON_FLAGS += -mmcu=${MCU} -DMULTIPASS_ARCH_atmega2560
+COMMON_FLAGS += -DF_CPU=${cpu_freq}UL
+COMMON_FLAGS += -DMULTIPASS_ARCH_HAS_I2C
+
+ifeq (${stack_usage}, )
+ COMMON_FLAGS += -flto
+endif
+
+CC = avr-gcc
+CXX = avr-g++
+NM = avr-nm
+OBJCOPY = avr-objcopy
+OBJDUMP = avr-objdump
+
+ARCH_SHORTNAME = avr
+
+ifdef CONFIG_aspectc
+ CXX = ag++ -r build/repo.acp -v 0 --c_compiler avr-g++ -p . --Xcompiler
+endif
+
+CXX_TARGETS += src/arch/atmega2560/arch.cc
+CXX_TARGETS += src/arch/atmega2560/driver/gpio.cc
+CXX_TARGETS += src/arch/atmega2560/driver/stdout.cc
+
+# Command-line driver selection
+
+ifneq ($(findstring softi2c,${drivers}), )
+else ifneq ($(findstring i2c,${arch_drivers}), )
+ CONFIG_arch_atmega2560_driver_i2c = y
+endif
+
+ifneq ($(findstring adc,${arch_drivers}), )
+ CONFIG_arch_atmega2560_driver_adc = y
+endif
+
+ifneq ($(findstring spi,${arch_drivers}), )
+ CONFIG_arch_atmega2560_driver_spi = y
+endif
+
+ifneq ($(findstring stdin,${arch_drivers}), )
+ CONFIG_arch_atmega2560_driver_stdin = y
+endif
+
+ifneq ($(findstring timer,${arch_drivers}), )
+ CONFIG_arch_atmega2560_driver_timer = y
+endif
+
+ifneq ($(findstring counter,${arch_drivers}), )
+ CONFIG_arch_atmega2560_driver_counter = y
+endif
+
+ifneq ($(findstring neopixel,${arch_drivers}), )
+ CONFIG_arch_atmega2560_driver_neopixel = y
+endif
+
+ifeq (${timer_s}, 1)
+ CONFIG_arch_atmega2560_driver_uptime = y
+endif
+
+# Kconfig driver selection
+
+ifdef CONFIG_arch_atmega2560_driver_i2c
+ CXX_TARGETS += src/arch/atmega2560/driver/i2c.cc
+endif
+
+ifdef CONFIG_arch_atmega2560_driver_adc
+ CXX_TARGETS += src/arch/atmega2560/driver/adc.cc
+endif
+
+ifdef CONFIG_arch_atmega2560_driver_spi
+ CXX_TARGETS += src/arch/atmega2560/driver/spi.cc
+endif
+
+ifdef CONFIG_arch_atmega2560_driver_stdin
+ CXX_TARGETS += src/arch/atmega2560/driver/stdin.cc
+endif
+
+ifdef CONFIG_arch_atmega2560_driver_timer
+ CXX_TARGETS += src/arch/atmega2560/driver/timer.cc
+endif
+
+ifdef CONFIG_arch_atmega2560_driver_counter
+ CXX_TARGETS += src/arch/atmega2560/driver/counter.cc
+endif
+
+ifdef CONFIG_arch_atmega2560_driver_neopixel
+ CXX_TARGETS += src/arch/atmega2560/driver/neopixel.cc
+endif
+
+ifdef CONFIG_arch_atmega2560_driver_uptime
+ COMMON_FLAGS += -DTIMER_S
+ CXX_TARGETS += src/arch/atmega2560/driver/uptime.cc
+endif
+
+ifeq (${cpu_freq}, 16000000)
+ uart_baud = 57600
+else ifeq (${cpu_freq}, 8000000)
+ uart_baud = 38400
+else ifeq (${cpu_freq}, 4000000)
+ uart_baud = 38400
+else ifeq (${cpu_freq}, 2000000)
+ uart_baud = 19200
+else ifeq (${cpu_freq}, 1000000)
+ uart_baud = 9600
+else ifeq (${cpu_freq}, 500000)
+ uart_baud = 4800
+else ifeq (${cpu_freq}, 250000)
+ uart_baud = 2400
+else ifeq (${cpu_freq}, 125000)
+ uart_baud = 1200
+else ifeq (${cpu_freq}, 62500)
+ uart_baud = 300
+else
+ uart_baud = 9600
+endif
+
+COMMON_FLAGS += -DBAUD=${uart_baud}UL
+
+OBJECTS = ${CXX_TARGETS:.cc=.o} ${C_TARGETS:.c=.o}
+
+.cc.o:
+ ${QUIET}${CXX} ${INCLUDES} ${COMMON_FLAGS} ${CXXFLAGS} -c -o $@ ${@:.o=.cc}
+
+.c.o:
+ ${QUIET}${CC} ${INCLUDES} ${COMMON_FLAGS} ${CFLAGS} -c -o $@ ${@:.o=.c}
+
+build/system.elf: ${OBJECTS}
+ ${QUIET}mkdir -p build
+ ${QUIET}${CXX} ${COMMON_FLAGS} ${CXXFLAGS} -Wl,--gc-sections -o $@ ${OBJECTS}
+ ${QUIET}avr-size --format=avr --mcu=${MCU} $@
+ ${QUIET}test $$(avr-size --format=avr --mcu=${MCU} build/system.elf | fgrep Program | grep -o '[0-9.]*%' | cut -d . -f 1) -lt 100
+
+build/system.hex: build/system.elf
+ ${QUIET}${OBJCOPY} -O ihex ${@:.hex=.elf} $@
+
+program: build/system.hex
+ ${QUIET}avrdude -p ${MCU} -c wiring -P ${SERIAL_PORT} -b ${BAUD} -D -U flash:w:build/system.hex
+
+arch_clean:
+ ${QUIET}rm -f ${OBJECTS} build/system.hex
+
+cat:
+ ${QUIET}script/cat.py ${SERIAL_PORT} ${uart_baud} ${cpu_freq} 65536
+
+monitor:
+ ${QUIET}screen ${SERIAL_PORT} ${uart_baud}
+
+size: build/system.elf
+ ${QUIET}avr-size --format=avr --mcu=${MCU} build/system.elf | fgrep Program | perl -nE 'if (m{(\d+) bytes \(([0-9.]+%)}) { print("$$1;$$2;") }'
+ ${QUIET}avr-size --format=avr --mcu=${MCU} build/system.elf | fgrep Data | perl -nE 'if (m{(\d+) bytes \(([0-9.]+%)}) { print("$$1;$$2;") }'
+ ${QUIET}echo
+
+arch_help:
+ @echo "atmega2560 specific flags:"
+ @echo " SERIAL_PORT = ${SERIAL_PORT}"
+ @echo " BAUD = ${BAUD} (only used for programming)"
+
+arch_info:
+ @echo "CPU Freq: ${cpu_freq} Hz"
+ @echo "Timer Freq: ${timer_freq} Hz"
+ @echo "I2C Freq: ${i2c_freq} Hz"
+ @echo "Counter Overflow: 65536/255"
+ @echo "Monitor: ${SERIAL_PORT} ${uart_baud}"
+
+attributes: build/system.elf
+ ${QUIET}script/size.py avr-size text,data data,bss
+
+.PHONY: arch_clean arch_help arch_info attributes cat monitor program size
diff --git a/src/arch/atmega2560/arch.cc b/src/arch/atmega2560/arch.cc
new file mode 100644
index 0000000..9d7e101
--- /dev/null
+++ b/src/arch/atmega2560/arch.cc
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "arch.h"
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/wdt.h>
+#include <util/delay.h>
+
+void Arch::setup(void)
+{
+ wdt_disable();
+
+#if F_CPU == 16000000UL
+ /* default */
+#else
+#error Unsupported F_CPU
+#endif
+
+#if defined(WITH_LOOP) || defined(TIMER_S)
+ TCCR1A = 0;
+ TCCR1B = _BV(WGM12) | _BV(CS12) | _BV(CS10); // /1024
+ OCR1A = F_CPU / 1024;
+ TIMSK1 = _BV(OCIE1A);
+#endif
+
+ sei();
+}
+
+#ifdef WITH_WAKEUP
+void wakeup();
+#endif
+
+#if defined(WITH_LOOP) || defined(TIMER_S)
+
+#include "driver/uptime.h"
+
+#endif
+
+#if defined(WITH_LOOP)
+extern void loop();
+volatile char run_loop = 0;
+#endif
+
+void Arch::idle_loop(void)
+{
+ while (1) {
+ SMCR = _BV(SE);
+ asm("sleep");
+ SMCR = 0;
+ asm("wdr");
+#ifdef WITH_LOOP
+ if (run_loop) {
+ loop();
+ run_loop = 0;
+ }
+#endif
+#ifdef WITH_WAKEUP
+ wakeup();
+#endif
+ }
+}
+
+void Arch::idle(void)
+{
+ SMCR = _BV(SE);
+ asm("sleep");
+ SMCR = 0;
+ asm("wdr");
+}
+
+void Arch::delay_us(unsigned int const us)
+{
+ for (unsigned int i = 0; i < us; i++) {
+ _delay_us(1);
+ }
+}
+
+void Arch::delay_ms(unsigned int const ms)
+{
+ for (unsigned int i = 0; i < ms; i++) {
+ _delay_ms(1);
+ }
+}
+
+Arch arch;
+
+#if defined(WITH_LOOP) || defined(TIMER_S)
+
+#ifndef __acweaving
+ISR(TIMER1_COMPA_vect)
+{
+#ifdef WITH_LOOP
+ run_loop = 1;
+#endif
+#ifdef TIMER_S
+ uptime.tick_s();
+#endif
+}
+#endif
+
+#endif
diff --git a/src/arch/atmega2560/driver/counter.cc b/src/arch/atmega2560/driver/counter.cc
new file mode 100644
index 0000000..d6cde46
--- /dev/null
+++ b/src/arch/atmega2560/driver/counter.cc
@@ -0,0 +1,15 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "driver/counter.h"
+
+Counter counter;
+
+ISR(TIMER3_OVF_vect)
+{
+ if (counter.overflow < 255) {
+ counter.overflow++;
+ }
+}
diff --git a/src/arch/atmega2560/driver/gpio.cc b/src/arch/atmega2560/driver/gpio.cc
new file mode 100644
index 0000000..60e374e
--- /dev/null
+++ b/src/arch/atmega2560/driver/gpio.cc
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "driver/gpio.h"
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+GPIO gpio;
+
+#ifndef __acweaving
+ISR(PCINT0_vect)
+{
+}
+
+ISR(PCINT1_vect)
+{
+}
+
+/*
+ISR(PCINT2_vect)
+{
+}
+*/
+#endif
diff --git a/src/arch/atmega2560/driver/stdout.cc b/src/arch/atmega2560/driver/stdout.cc
new file mode 100644
index 0000000..58a3e7e
--- /dev/null
+++ b/src/arch/atmega2560/driver/stdout.cc
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "driver/stdout.h"
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+#ifndef BAUD
+#define BAUD 115200UL
+#endif
+
+#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/atmega2560/driver/timer.cc b/src/arch/atmega2560/driver/timer.cc
new file mode 100644
index 0000000..b6d4145
--- /dev/null
+++ b/src/arch/atmega2560/driver/timer.cc
@@ -0,0 +1,8 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "driver/timer.h"
+
+Timer timer;
diff --git a/src/arch/atmega2560/driver/uptime.cc b/src/arch/atmega2560/driver/uptime.cc
new file mode 100644
index 0000000..7e37a7b
--- /dev/null
+++ b/src/arch/atmega2560/driver/uptime.cc
@@ -0,0 +1,8 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "driver/uptime.h"
+
+Uptime uptime;