diff options
author | Daniel Friesel <derf@finalrewind.org> | 2018-08-08 14:59:02 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2018-08-08 14:59:02 +0200 |
commit | e7f1024732ee9bac13fbf2e2439106f9f3577db7 (patch) | |
tree | 16e52dac6be007b3ca6b0f38f3226a12fb828bc7 /src/arch/blinkenrocket/driver | |
parent | 4a2747f0c508cd5af830c14e43ffeaf3fe25e4e7 (diff) |
Add Blinkenrocket arch
Diffstat (limited to 'src/arch/blinkenrocket/driver')
-rw-r--r-- | src/arch/blinkenrocket/driver/gpio.cc | 17 | ||||
-rw-r--r-- | src/arch/blinkenrocket/driver/i2c.cc | 149 | ||||
-rw-r--r-- | src/arch/blinkenrocket/driver/stdout.cc | 35 | ||||
-rw-r--r-- | src/arch/blinkenrocket/driver/uptime.cc | 3 |
4 files changed, 204 insertions, 0 deletions
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; |