4 #include <avr/interrupt.h>
8 #include "split_flags.h"
10 #if defined(USE_I2C) || defined(EH)
12 // Limits the amount of we wait for any one i2c transaction.
13 // Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
14 // 9 bits, a single transaction will take around 90μs to complete.
16 // (F_CPU/SCL_CLOCK) => # of μC cycles to transfer a bit
17 // poll loop takes at least 8 clock cycles to execute
18 #define I2C_LOOP_TIMEOUT (9+1)*(F_CPU/SCL_CLOCK)/8
20 #define BUFFER_POS_INC() (slave_buffer_pos = (slave_buffer_pos+1)%SLAVE_BUFFER_SIZE)
22 volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
24 static volatile uint8_t slave_buffer_pos;
25 static volatile bool slave_has_register_set = false;
27 // Wait for an i2c operation to finish
29 void i2c_delay(void) {
31 while(!(TWCR & (1<<TWINT)) && lim < I2C_LOOP_TIMEOUT)
34 // easier way, but will wait slightly longer
38 // Setup twi to run at 100kHz
39 void i2c_master_init(void) {
42 // Set TWI clock frequency to SCL_CLOCK. Need TWBR>10.
43 // Check datasheets for more info.
44 TWBR = ((F_CPU/SCL_CLOCK)-16)/2;
47 // Start a transaction with the given i2c slave address. The direction of the
48 // transfer is set with I2C_READ and I2C_WRITE.
49 // returns: 0 => success
51 uint8_t i2c_master_start(uint8_t address) {
52 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA);
56 // check that we started successfully
57 if ( (TW_STATUS != TW_START) && (TW_STATUS != TW_REP_START))
61 TWCR = (1<<TWINT) | (1<<TWEN);
65 if ( (TW_STATUS != TW_MT_SLA_ACK) && (TW_STATUS != TW_MR_SLA_ACK) )
66 return 1; // slave did not acknowledge
72 // Finish the i2c transaction.
73 void i2c_master_stop(void) {
74 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
77 while(!(TWCR & (1<<TWSTO)) && lim < I2C_LOOP_TIMEOUT)
81 // Write one byte to the i2c slave.
82 // returns 0 => slave ACK
84 uint8_t i2c_master_write(uint8_t data) {
86 TWCR = (1<<TWINT) | (1<<TWEN);
90 // check if the slave acknowledged us
91 return (TW_STATUS == TW_MT_DATA_ACK) ? 0 : 1;
94 uint8_t i2c_master_write_data(void *const TXdata, uint8_t dataLen) {
96 uint8_t *data = (uint8_t *)TXdata;
99 for (int i = 0; i < dataLen; i++) {
100 err = i2c_master_write(data[i]);
110 // Read one byte from the i2c slave. If ack=1 the slave is acknowledged,
111 // if ack=0 the acknowledge bit is not set.
112 // returns: byte read from i2c device
113 uint8_t i2c_master_read(int ack) {
114 TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA);
120 void i2c_reset_state(void) {
124 void i2c_slave_init(uint8_t address) {
125 TWAR = address << 0; // slave i2c address
127 // TWEA - enable address acknowledgement
128 // TWINT - twi interrupt flag
129 // TWIE - enable the twi interrupt
130 TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN);
139 // this device has been addressed as a slave receiver
140 slave_has_register_set = false;
144 // this device has received data as a slave receiver
145 // The first byte that we receive in this transaction sets the location
146 // of the read/write location of the slaves memory that it exposes over
147 // i2c. After that, bytes will be written at slave_buffer_pos, incrementing
148 // slave_buffer_pos after each write.
149 if(!slave_has_register_set) {
150 slave_buffer_pos = TWDR;
151 // don't acknowledge the master if this memory loctaion is out of bounds
152 if ( slave_buffer_pos >= SLAVE_BUFFER_SIZE ) {
154 slave_buffer_pos = 0;
157 slave_has_register_set = true;
159 i2c_slave_buffer[slave_buffer_pos] = TWDR;
161 if ( slave_buffer_pos == I2C_BACKLIT_START) {
162 BACKLIT_DIRTY = true;
163 } else if ( slave_buffer_pos == (I2C_RGB_START+3)) {
173 // master has addressed this device as a slave transmitter and is
175 TWDR = i2c_slave_buffer[slave_buffer_pos];
179 case TW_BUS_ERROR: // something went wrong, reset twi state
184 // Reset everything, so we are ready for the next TWI interrupt
185 TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);