summaryrefslogtreecommitdiff
path: root/include/arch/arduino-nano/driver
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-07-17 09:57:10 +0200
committerDaniel Friesel <derf@finalrewind.org>2018-07-17 09:57:10 +0200
commit3fddb519f01ca53f1fd6f5c24e154912b25d1138 (patch)
tree62efb5ea5431f543f0ea899aa809e262e47980ba /include/arch/arduino-nano/driver
parent658ca283452c5944c55d9c751868eef6c6f34138 (diff)
Move include/$arch to include/arch/$arch to be consistent with src/ hierarchy
Diffstat (limited to 'include/arch/arduino-nano/driver')
-rw-r--r--include/arch/arduino-nano/driver/gpio.h111
-rw-r--r--include/arch/arduino-nano/driver/i2c.h19
-rw-r--r--include/arch/arduino-nano/driver/stdin.h24
-rw-r--r--include/arch/arduino-nano/driver/stdout.h19
-rw-r--r--include/arch/arduino-nano/driver/uptime.h29
5 files changed, 202 insertions, 0 deletions
diff --git a/include/arch/arduino-nano/driver/gpio.h b/include/arch/arduino-nano/driver/gpio.h
new file mode 100644
index 0000000..6acf977
--- /dev/null
+++ b/include/arch/arduino-nano/driver/gpio.h
@@ -0,0 +1,111 @@
+#ifndef GPIO_H
+#define GPIO_H
+
+#include <avr/io.h>
+
+class GPIO {
+ private:
+ GPIO(const GPIO &copy);
+
+ public:
+ GPIO () {}
+
+ enum Pin : unsigned char {
+ 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,
+ pc6 = 22,
+ pd0 = 24,
+ pd1 = 25,
+ pd2 = 26,
+ pd3 = 27,
+ pd4 = 28,
+ pd5 = 29,
+ pd6 = 30,
+ pd7 = 31
+ };
+
+ inline void setup() {
+ DDRB = _BV(PB5);
+ }
+ inline void led_on(unsigned char id) {
+ PORTB |= _BV(PB5);
+ }
+ inline void led_off(unsigned char id) {
+ PORTB &= ~_BV(PB5);
+ }
+ inline void led_toggle(unsigned char id) {
+ PINB = _BV(PB5);
+ }
+ inline void input(unsigned char const pin) {
+ if (pin < 8) {
+ } 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) {
+ if (pin < 8) {
+ } 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 unsigned char read(unsigned char const pin) {
+ if (pin < 8) {
+ }
+ 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) {
+ } 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);
+ }
+ }
+ }
+};
+
+extern GPIO gpio;
+
+#endif
diff --git a/include/arch/arduino-nano/driver/i2c.h b/include/arch/arduino-nano/driver/i2c.h
new file mode 100644
index 0000000..6d6ea66
--- /dev/null
+++ b/include/arch/arduino-nano/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/arduino-nano/driver/stdin.h b/include/arch/arduino-nano/driver/stdin.h
new file mode 100644
index 0000000..6462e0c
--- /dev/null
+++ b/include/arch/arduino-nano/driver/stdin.h
@@ -0,0 +1,24 @@
+#ifndef STANDARDINPUT_H
+#define STANDARDINPUT_H
+
+class StandardInput {
+ private:
+ StandardInput(const StandardInput &copy);
+ char buffer[8];
+ unsigned char write_pos, read_pos;
+
+ public:
+ StandardInput() : write_pos(0), read_pos(0) {}
+ void setup();
+ bool hasKey();
+ char getKey();
+
+ inline void addKey(char key) {
+ buffer[write_pos++] = key;
+ write_pos %= 8;
+ }
+};
+
+extern StandardInput kin;
+
+#endif
diff --git a/include/arch/arduino-nano/driver/stdout.h b/include/arch/arduino-nano/driver/stdout.h
new file mode 100644
index 0000000..2eb669d
--- /dev/null
+++ b/include/arch/arduino-nano/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/arduino-nano/driver/uptime.h b/include/arch/arduino-nano/driver/uptime.h
new file mode 100644
index 0000000..86a8bb5
--- /dev/null
+++ b/include/arch/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