From 72fd49b1468d6bbfa59e1c6334866d7aa34f31c1 Mon Sep 17 00:00:00 2001 From: yiancar Date: Wed, 18 Jul 2018 19:55:57 +0300 Subject: DC01 keyboard addition (#3428) * DC01 initial commit - Addition of directories - Left readme * Initial commit of left half * Initial files for right half * arrow * i2c adjustments * I2C slave and DC01 refractoring - Cleaned up state machine of I2C slave driver - Modified DC01 left to use already pressent I2C master driver - Modified DC01 matrixes * Fixed tabs to spaces * Addition of Numpad * Add keymaps - Orthopad keymap for numpad module - Numpad keymap for numpad module - ISO, ANSI and HHKB version of keymap for right module * Minor matrix.c fixes * Update Readmes --- drivers/avr/i2c_master.c | 110 +++++++++++++++++++------------------- drivers/avr/i2c_master.h | 2 +- drivers/avr/i2c_slave.c | 134 ++++++++++++++++++----------------------------- drivers/avr/i2c_slave.h | 10 ++-- 4 files changed, 114 insertions(+), 142 deletions(-) (limited to 'drivers') diff --git a/drivers/avr/i2c_master.c b/drivers/avr/i2c_master.c index 4e76e2e7c..47c6f8e6c 100755 --- a/drivers/avr/i2c_master.c +++ b/drivers/avr/i2c_master.c @@ -15,15 +15,15 @@ void i2c_init(void) { TWSR = 0; /* no prescaler */ - TWBR = (uint8_t)TWBR_val; + TWBR = (uint8_t)TWBR_val; } i2c_status_t i2c_start(uint8_t address, uint16_t timeout) { - // reset TWI control register - TWCR = 0; - // transmit START condition - TWCR = (1<= 0) { data[i] = status; } else { return status; } - } + } status = i2c_read_nack(timeout); if (status >= 0 ) { @@ -147,47 +147,47 @@ i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16 status = i2c_stop(timeout); if (status) return status; - return I2C_STATUS_SUCCESS; + return I2C_STATUS_SUCCESS; } i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t status = i2c_start(devaddr | 0x00, timeout); - if (status) return status; + if (status) return status; - status = i2c_write(regaddr, timeout); + status = i2c_write(regaddr, timeout); if (status) return status; - for (uint16_t i = 0; i < length; i++) { + for (uint16_t i = 0; i < length; i++) { status = i2c_write(data[i], timeout); - if (status) return status; - } + if (status) return status; + } - status = i2c_stop(timeout); + status = i2c_stop(timeout); if (status) return status; - return I2C_STATUS_SUCCESS; + return I2C_STATUS_SUCCESS; } i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t status = i2c_start(devaddr, timeout); - if (status) return status; + if (status) return status; status = i2c_write(regaddr, timeout); if (status) return status; status = i2c_start(devaddr | 0x01, timeout); - if (status) return status; + if (status) return status; - for (uint16_t i = 0; i < (length-1); i++) { - status = i2c_read_ack(timeout); + for (uint16_t i = 0; i < (length-1); i++) { + status = i2c_read_ack(timeout); if (status >= 0) { data[i] = status; } else { return status; } - } + } status = i2c_read_nack(timeout); if (status >= 0 ) { @@ -199,13 +199,13 @@ i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16 status = i2c_stop(timeout); if (status) return status; - return I2C_STATUS_SUCCESS; + return I2C_STATUS_SUCCESS; } i2c_status_t i2c_stop(uint16_t timeout) { - // transmit STOP condition - TWCR = (1< #include #include +#include #include "i2c_slave.h" void i2c_init(uint8_t address){ - // load address into TWI address register - TWAR = (address << 1); - // set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt - TWCR = (1<= RX_BUFFER_SIZE){ // address out of bounds dont ack + ack = 0; + buffer_address = 0; + } + slave_has_register_set = true; // address has been receaved now fill in buffer + } else { + rxbuffer[buffer_address] = TWDR; + buffer_address++; + } + break; + + case TW_ST_SLA_ACK: + case TW_ST_DATA_ACK: + // This device is a slave transmitter and master has requested data + TWDR = txbuffer[buffer_address]; + buffer_address++; + break; + + case TW_BUS_ERROR: + // We got an error, reset i2c + TWCR = 0; + default: + break; + } + + // Reset i2c state mahcine to be ready for next interrupt + TWCR |= (1 << TWIE) | (1 << TWINT) | (ack << TWEA) | (1 << TWEN); +} \ No newline at end of file diff --git a/drivers/avr/i2c_slave.h b/drivers/avr/i2c_slave.h index 3fda7f8c0..1c3b9ecc0 100755 --- a/drivers/avr/i2c_slave.h +++ b/drivers/avr/i2c_slave.h @@ -8,12 +8,16 @@ #ifndef I2C_SLAVE_H #define I2C_SLAVE_H +#define TX_BUFFER_SIZE 30 +#define RX_BUFFER_SIZE 30 + volatile uint8_t buffer_address; -volatile uint8_t txbuffer[0xFF]; -volatile uint8_t rxbuffer[0xFF]; +static volatile bool slave_has_register_set = false; +volatile uint8_t txbuffer[TX_BUFFER_SIZE]; +volatile uint8_t rxbuffer[RX_BUFFER_SIZE]; void i2c_init(uint8_t address); void i2c_stop(void); ISR(TWI_vect); -#endif // I2C_SLAVE_H +#endif // I2C_SLAVE_H \ No newline at end of file -- cgit v1.2.3