summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-08-08 14:59:02 +0200
committerDaniel Friesel <derf@finalrewind.org>2018-08-08 14:59:02 +0200
commite7f1024732ee9bac13fbf2e2439106f9f3577db7 (patch)
tree16e52dac6be007b3ca6b0f38f3226a12fb828bc7
parent4a2747f0c508cd5af830c14e43ffeaf3fe25e4e7 (diff)
Add Blinkenrocket arch
-rw-r--r--include/arch/blinkenrocket/driver/gpio.h178
-rw-r--r--include/arch/blinkenrocket/driver/i2c.h19
-rw-r--r--include/arch/blinkenrocket/driver/stdout.h19
-rw-r--r--include/arch/blinkenrocket/driver/uptime.h28
-rw-r--r--src/arch/blinkenrocket/Makefile.inc55
-rw-r--r--src/arch/blinkenrocket/arch.cc85
-rw-r--r--src/arch/blinkenrocket/driver/gpio.cc17
-rw-r--r--src/arch/blinkenrocket/driver/i2c.cc149
-rw-r--r--src/arch/blinkenrocket/driver/stdout.cc35
-rw-r--r--src/arch/blinkenrocket/driver/uptime.cc3
10 files changed, 588 insertions, 0 deletions
diff --git a/include/arch/blinkenrocket/driver/gpio.h b/include/arch/blinkenrocket/driver/gpio.h
new file mode 100644
index 0000000..be6eb38
--- /dev/null
+++ b/include/arch/blinkenrocket/driver/gpio.h
@@ -0,0 +1,178 @@
+#ifndef GPIO_H
+#define GPIO_H
+
+#include <avr/io.h>
+
+class GPIO {
+ private:
+ GPIO(const GPIO &copy);
+
+ public:
+ GPIO () {}
+
+ enum Pin : unsigned char {
+ pa0 = 0,
+ pa1 = 1,
+ pa2 = 2,
+ pa3 = 3,
+ pb0 = 8,
+ pb1 = 9,
+ pb2 = 10,
+ pb3 = 11,
+ pb4 = 12,
+ pb5 = 13,
+ pb6 = 14,
+ pb7 = 15,
+ pc0 = 16,
+ pc1 = 17,
+ pc2 = 18,
+ pc3 = 19,
+ pc4 = 20,
+ pc5 = 21,
+ pc7 = 23,
+ pd0 = 24,
+ pd1 = 25,
+ pd2 = 26,
+ pd3 = 27,
+ pd4 = 28,
+ pd5 = 29,
+ pd6 = 30,
+ pd7 = 31
+ };
+
+ inline void setup() {
+ PORTB = 0;
+ DDRB = 0;
+ PORTD = 0x7f;
+ DDRD = 0xff;
+ }
+ inline void led_on(unsigned char id) {
+ PORTB |= _BV(id);
+ }
+ inline void led_off(unsigned char id) {
+ PORTB &= ~_BV(id);
+ }
+ inline void led_toggle(unsigned char id) {
+ PORTB ^= _BV(id);
+ }
+ inline void input(unsigned char const pin) {
+ if (pin < 8) {
+ DDRA &= ~_BV(pin);
+ } else if (pin < 16) {
+ DDRB &= ~_BV(pin - 8);
+ } else if (pin < 24) {
+ DDRC &= ~_BV(pin - 16);
+ } else if (pin < 32) {
+ DDRD &= ~_BV(pin - 24);
+ }
+ }
+ inline void input(unsigned char const pin, unsigned char const pull) {
+ if (pin < 8) {
+ DDRA &= ~_BV(pin);
+ PORTA |= _BV(pin);
+ } else if (pin < 16) {
+ DDRB &= ~_BV(pin - 8);
+ PORTB |= _BV(pin - 8);
+ } else if (pin < 24) {
+ DDRC &= ~_BV(pin - 16);
+ PORTC |= _BV(pin - 16);
+ } else if (pin < 32) {
+ DDRD &= ~_BV(pin - 24);
+ PORTD |= _BV(pin - 24);
+ }
+ }
+ inline void output(unsigned char const pin) {
+ if (pin < 8) {
+ DDRA |= _BV(pin);
+ } else if (pin < 16) {
+ DDRB |= _BV(pin - 8);
+ } else if (pin < 24) {
+ DDRC |= _BV(pin - 16);
+ } else if (pin < 32) {
+ DDRD |= _BV(pin - 24);
+ }
+ }
+ inline void output(unsigned char const pin, unsigned char const value) {
+ if (pin < 8) {
+ PORTA = value ? (PORTA | _BV(pin)) : (PORTA & ~_BV(pin));
+ DDRA |= _BV(pin);
+ } else if (pin < 16) {
+ PORTB = value ? (PORTB | _BV(pin - 8)) : (PORTB & ~_BV(pin - 8));
+ DDRB |= _BV(pin - 8);
+ } else if (pin < 24) {
+ PORTC = value ? (PORTC | _BV(pin - 16)) : (PORTC & ~_BV(pin - 16));
+ DDRC |= _BV(pin - 16);
+ } else if (pin < 32) {
+ PORTD = value ? (PORTD | _BV(pin - 24)) : (PORTD & ~_BV(pin - 24));
+ DDRD |= _BV(pin - 24);
+ }
+ }
+ inline unsigned char read(unsigned char const pin) {
+ if (pin < 8) {
+ return (PINA & _BV(pin));
+ }
+ if (pin < 16) {
+ return (PINB & _BV(pin - 8));
+ }
+ if (pin < 24) {
+ return (PINC & _BV(pin - 16));
+ }
+ if (pin < 32) {
+ return (PIND & _BV(pin - 24));
+ }
+ return 0;
+ }
+ inline void write(unsigned char const pin, unsigned char value) {
+ if (pin < 8) {
+ if (value) {
+ PORTA |= _BV(pin);
+ } else {
+ PORTA &= ~_BV(pin);
+ }
+ } else if (pin < 16) {
+ if (value) {
+ PORTB |= _BV(pin - 8);
+ } else {
+ PORTB &= ~_BV(pin - 8);
+ }
+ } else if (pin < 24) {
+ if (value) {
+ PORTB |= _BV(pin - 16);
+ } else {
+ PORTB &= ~_BV(pin - 16);
+ }
+ } else if (pin < 32) {
+ if (value) {
+ PORTB |= _BV(pin - 24);
+ } else {
+ PORTB &= ~_BV(pin - 24);
+ }
+ }
+ }
+ /*
+ inline void enable_int(unsigned char const pin) {
+ if (pin < 8) {
+ } else if (pin < 16) {
+ PCMSK0 |= _BV(pin - 8);
+ } else if (pin < 24) {
+ PCMSK1 |= _BV(pin - 16);
+ } else if (pin < 32) {
+ PCMSK2 |= _BV(pin - 24);
+ }
+ }
+ inline void disable_int(unsigned char const pin) {
+ if (pin < 8) {
+ } else if (pin < 16) {
+ PCMSK0 &= ~_BV(pin - 8);
+ } else if (pin < 24) {
+ PCMSK1 &= ~_BV(pin - 16);
+ } else if (pin < 32) {
+ PCMSK2 &= ~_BV(pin - 24);
+ }
+ }
+ */
+};
+
+extern GPIO gpio;
+
+#endif
diff --git a/include/arch/blinkenrocket/driver/i2c.h b/include/arch/blinkenrocket/driver/i2c.h
new file mode 100644
index 0000000..6d6ea66
--- /dev/null
+++ b/include/arch/blinkenrocket/driver/i2c.h
@@ -0,0 +1,19 @@
+#ifndef I2C_H
+#define I2C_H
+
+class I2C {
+ private:
+ I2C(const I2C &copy);
+
+ public:
+ I2C () {}
+ signed char setup();
+ void scan(unsigned int *results);
+ signed char xmit(unsigned char address,
+ unsigned char tx_len, unsigned char *tx_buf,
+ unsigned char rx_len, unsigned char *rx_buf);
+};
+
+extern I2C i2c;
+
+#endif
diff --git a/include/arch/blinkenrocket/driver/stdout.h b/include/arch/blinkenrocket/driver/stdout.h
new file mode 100644
index 0000000..2eb669d
--- /dev/null
+++ b/include/arch/blinkenrocket/driver/stdout.h
@@ -0,0 +1,19 @@
+#ifndef STANDARDOUTPUT_H
+#define STANDARDOUTPUT_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/arch/blinkenrocket/driver/uptime.h b/include/arch/blinkenrocket/driver/uptime.h
new file mode 100644
index 0000000..d5fdd2e
--- /dev/null
+++ b/include/arch/blinkenrocket/driver/uptime.h
@@ -0,0 +1,28 @@
+#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; }
+#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/blinkenrocket/Makefile.inc b/src/arch/blinkenrocket/Makefile.inc
new file mode 100644
index 0000000..1d99d0b
--- /dev/null
+++ b/src/arch/blinkenrocket/Makefile.inc
@@ -0,0 +1,55 @@
+# vim:ft=make
+
+MCU = attiny88
+PORT = /dev/ttyUSB0
+
+COMMON_FLAGS += -mmcu=${MCU} -DF_CPU=8000000UL -DMULTIPASS_ARCH_blinkenrocket
+COMMON_FLAGS += -flto
+COMMON_FLAGS += -DMULTIPASS_ARCH_HAS_I2C
+
+CC = avr-gcc
+CXX = avr-g++
+NM = avr-nm
+OBJCOPY = avr-objcopy
+OBJDUMP = avr-objdump
+
+ifeq (${aspectc}, 1)
+ CXX = ag++ -r build/repo.acp -v 0 --c_compiler avr-g++ -p . --Xcompiler
+endif
+
+TARGETS += src/arch/blinkenrocket/arch.cc
+TARGETS += src/arch/blinkenrocket/driver/gpio.cc
+TARGETS += src/arch/blinkenrocket/driver/stdout.cc
+TARGETS += src/arch/blinkenrocket/driver/uptime.cc
+
+ifneq ($(findstring softi2c,${drivers}), )
+else ifneq ($(findstring i2c,${arch_drivers}), )
+ TARGETS += src/arch/blinkenrocket/driver/i2c.cc
+endif
+
+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}
+ avr-size --format=avr --mcu=${MCU} $@
+
+build/system.hex: build/system.elf
+ ${OBJCOPY} -O ihex ${@:.hex=.elf} $@
+
+program: build/system.hex
+ avrdude -p ${MCU} -c usbasp -U flash:w:build/system.hex
+
+arch_clean:
+ rm -f ${OBJECTS} build/system.hex
+
+monitor:
+ screen ${PORT} 115200
+
+arch_help:
+ @echo "blinkenrocket specific flags:"
+ @echo " PORT = ${PORT}"
+
+.PHONY: arch_clean arch_help monitor program
diff --git a/src/arch/blinkenrocket/arch.cc b/src/arch/blinkenrocket/arch.cc
new file mode 100644
index 0000000..8042679
--- /dev/null
+++ b/src/arch/blinkenrocket/arch.cc
@@ -0,0 +1,85 @@
+#include "arch.h"
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+
+void Arch::setup(void)
+{
+#ifdef TIMER_CYCLES
+ TCCR0A = _BV(CS00);
+#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
+
+/*
+#ifdef TIMER_US
+ // 16MHz/8 -> 2MHz timer
+ TCCR1A = 0;
+ TCCR2B = _BV(CS21);
+#endif
+*/
+ sei();
+}
+
+#ifdef WITH_WAKEUP
+void wakeup();
+#endif
+
+#if defined(WITH_LOOP) || defined(TIMER_S)
+
+#include "driver/uptime.h"
+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 WITH_WAKEUP
+ wakeup();
+#endif
+#ifdef TIMER_S
+ uptime.tick_s();
+#endif
+ }
+}
+
+void Arch::idle(void)
+{
+ SMCR = _BV(SE);
+ asm("sleep");
+ SMCR = 0;
+ asm("wdr");
+}
+
+void Arch::delay_us(unsigned char const us)
+{
+ _delay_us(us);
+}
+
+void Arch::delay_ms(unsigned char const ms)
+{
+ _delay_ms(ms);
+}
+
+Arch arch;
+
+#if defined(WITH_LOOP) || defined(TIMER_S)
+
+ISR(TIMER1_COMPA_vect)
+{
+}
+
+#endif
diff --git a/src/arch/blinkenrocket/driver/gpio.cc b/src/arch/blinkenrocket/driver/gpio.cc
new file mode 100644
index 0000000..707f2bd
--- /dev/null
+++ b/src/arch/blinkenrocket/driver/gpio.cc
@@ -0,0 +1,17 @@
+#include "driver/gpio.h"
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+GPIO gpio;
+
+ISR(PCINT0_vect)
+{
+}
+
+ISR(PCINT1_vect)
+{
+}
+
+ISR(PCINT2_vect)
+{
+}
diff --git a/src/arch/blinkenrocket/driver/i2c.cc b/src/arch/blinkenrocket/driver/i2c.cc
new file mode 100644
index 0000000..cfbea59
--- /dev/null
+++ b/src/arch/blinkenrocket/driver/i2c.cc
@@ -0,0 +1,149 @@
+#include "driver/i2c.h"
+#include "arch.h"
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+inline void await_twint(unsigned char twcr_values)
+{
+ TWCR = twcr_values | _BV(TWINT) | _BV(TWIE);
+ while (!(TWCR & _BV(TWINT))) {
+ arch.idle();
+ }
+}
+
+/*
+ * Send an I2C (re)start condition and the EEPROM address in read mode. Returns
+ * after it has been transmitted successfully.
+ */
+static signed char i2c_start_read(unsigned char addr)
+{
+ await_twint(_BV(TWSTA) | _BV(TWEN));
+ if (!(TWSR & 0x18)) // 0x08 == START ok, 0x10 == RESTART ok
+ return -1;
+
+ // Note: The R byte ("... | 1") causes the TWI momodule to switch to
+ // Master Receive mode
+ TWDR = (addr << 1) | 1;
+ await_twint(_BV(TWEN));
+ if (TWSR != 0x40) // 0x40 == SLA+R transmitted, ACK receveid
+ return -2;
+
+ return 0;
+}
+
+/*
+ * Send an I2C (re)start condition and the EEPROM address in write mode.
+ * Returns after it has been transmitted successfully.
+ */
+static signed char i2c_start_write(unsigned char addr)
+{
+ await_twint(_BV(TWSTA) | _BV(TWEN));
+ if (!(TWSR & 0x18)) // 0x08 == START ok, 0x10 == RESTART ok
+ return -1;
+
+ TWDR = (addr<< 1) | 0;
+ await_twint(_BV(TWEN));
+ if (TWSR != 0x18) // 0x18 == SLA+W transmitted, ACK received
+ return -2;
+
+ return 0;
+}
+
+/*
+ * Send an I2C stop condition.
+ */
+static signed char i2c_stop()
+{
+ TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
+}
+
+/*
+ * Sends len bytes to the EEPROM. Note that this method does NOT
+ * send I2C start or stop conditions.
+ */
+static signed char i2c_send(uint8_t len, uint8_t *data)
+{
+ uint8_t pos = 0;
+
+ for (pos = 0; pos < len; pos++) {
+ TWDR = data[pos];
+ await_twint(_BV(TWEN));
+ if (TWSR != 0x28) // 0x28 == byte transmitted, ACK received
+ return pos;
+ }
+
+ return pos;
+}
+
+/*
+ * Receives len bytes from the EEPROM into data. Note that this method does
+ * NOT send I2C start or stop conditions.
+ */
+static signed char i2c_receive(uint8_t len, uint8_t *data)
+{
+ uint8_t pos = 0;
+
+ for (pos = 0; pos < len; pos++) {
+ await_twint(_BV(TWEN) | ( _BV(TWEA) * (pos < len-1) ) );
+ data[pos] = TWDR;
+ /*
+ * No error handling here -- We send the acks, the EEPROM only
+ * supplies raw data, so there's no way of knowing whether it's still
+ * talking to us or we're just reading garbage.
+ */
+ }
+
+ return pos;
+}
+
+signed char I2C::setup()
+{
+ TWSR = 0;
+ TWBR = ((F_CPU / 100000UL) - 16) / 2;
+
+ return 0;
+}
+
+void I2C::scan(unsigned int *results)
+{
+ for (unsigned char address = 0; address < 128; address++) {
+ if (i2c_start_read(address) == 0) {
+ results[address / (8 * sizeof(unsigned int))] |= 1 << (address % (8 * sizeof(unsigned int)));
+ i2c_stop();
+ }
+ }
+ i2c_stop();
+}
+
+signed char I2C::xmit(unsigned char address,
+ unsigned char tx_len, unsigned char *tx_buf,
+ unsigned char rx_len, unsigned char *rx_buf)
+{
+ unsigned char i;
+
+ if (tx_len) {
+ if (i2c_start_write(address) < 0) {
+ return -1;
+ }
+ if (i2c_send(tx_len, tx_buf) < 0) {
+ return -1;
+ }
+ }
+ if (rx_len) {
+ if (i2c_start_read(address) < 0) {
+ return -1;
+ }
+ if (i2c_receive(rx_len, rx_buf) < 0) {
+ return -1;
+ }
+ }
+
+ i2c_stop();
+ return 0;
+}
+
+I2C i2c;
+
+ISR(TWI_vect)
+{
+}
diff --git a/src/arch/blinkenrocket/driver/stdout.cc b/src/arch/blinkenrocket/driver/stdout.cc
new file mode 100644
index 0000000..961cf46
--- /dev/null
+++ b/src/arch/blinkenrocket/driver/stdout.cc
@@ -0,0 +1,35 @@
+#include "driver/stdout.h"
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+void StandardOutput::setup()
+{
+ PORTC |= _BV(PC1);
+ DDRC |= _BV(DDC1);
+}
+
+void StandardOutput::put(char c)
+{
+ unsigned char i = 1;
+ PORTC &= ~_BV(PC1);
+ __builtin_avr_delay_cycles(59);
+ while (i < 0x80) {
+ if (c & i) {
+ PORTC |= _BV(PC1);
+ } else {
+ PORTC &= ~_BV(PC1);
+ }
+ i <<= 1;
+ __builtin_avr_delay_cycles(54);
+ }
+ __builtin_avr_delay_cycles(6);
+ PORTC &= ~_BV(PC1);
+ __builtin_avr_delay_cycles(67);
+ PORTC |= _BV(PC1);
+ __builtin_avr_delay_cycles(150);
+ if (c == '\n') {
+ put('\r');
+ }
+}
+
+StandardOutput kout;
diff --git a/src/arch/blinkenrocket/driver/uptime.cc b/src/arch/blinkenrocket/driver/uptime.cc
new file mode 100644
index 0000000..388edb6
--- /dev/null
+++ b/src/arch/blinkenrocket/driver/uptime.cc
@@ -0,0 +1,3 @@
+#include "driver/uptime.h"
+
+Uptime uptime;