4 #include <avr/interrupt.h>
11 // Limits the amount of we wait for any one i2c transaction.
12 // Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
13 // 9 bits, a single transaction will take around 90μs to complete.
15 // (F_CPU/SCL_CLOCK) => # of μC cycles to transfer a bit
16 // poll loop takes at least 8 clock cycles to execute
17 #define I2C_LOOP_TIMEOUT (9+1)*(F_CPU/SCL_CLOCK)/8
19 #define BUFFER_POS_INC() (slave_buffer_pos = (slave_buffer_pos+1)%SLAVE_BUFFER_SIZE)
21 volatile uint8_t i2c_slave_buffer
[SLAVE_BUFFER_SIZE
];
23 static volatile uint8_t slave_buffer_pos
;
24 static volatile bool slave_has_register_set
= false;
26 // Wait for an i2c operation to finish
28 void i2c_delay(void) {
30 while(!(TWCR
& (1<<TWINT
)) && lim
< I2C_LOOP_TIMEOUT
)
33 // easier way, but will wait slightly longer
37 // Setup twi to run at 100kHz or 400kHz (see ./i2c.h SCL_CLOCK)
38 void i2c_master_init(void) {
41 // Set TWI clock frequency to SCL_CLOCK. Need TWBR>10.
42 // Check datasheets for more info.
43 TWBR
= ((F_CPU
/SCL_CLOCK
)-16)/2;
46 // Start a transaction with the given i2c slave address. The direction of the
47 // transfer is set with I2C_READ and I2C_WRITE.
48 // returns: 0 => success
50 uint8_t i2c_master_start(uint8_t address
) {
51 TWCR
= (1<<TWINT
) | (1<<TWEN
) | (1<<TWSTA
);
55 // check that we started successfully
56 if ( (TW_STATUS
!= TW_START
) && (TW_STATUS
!= TW_REP_START
))
60 TWCR
= (1<<TWINT
) | (1<<TWEN
);
64 if ( (TW_STATUS
!= TW_MT_SLA_ACK
) && (TW_STATUS
!= TW_MR_SLA_ACK
) )
65 return 1; // slave did not acknowledge
71 // Finish the i2c transaction.
72 void i2c_master_stop(void) {
73 TWCR
= (1<<TWINT
) | (1<<TWEN
) | (1<<TWSTO
);
76 while(!(TWCR
& (1<<TWSTO
)) && lim
< I2C_LOOP_TIMEOUT
)
80 // Write one byte to the i2c slave.
81 // returns 0 => slave ACK
83 uint8_t i2c_master_write(uint8_t data
) {
85 TWCR
= (1<<TWINT
) | (1<<TWEN
);
89 // check if the slave acknowledged us
90 return (TW_STATUS
== TW_MT_DATA_ACK
) ? 0 : 1;
93 // Read one byte from the i2c slave. If ack=1 the slave is acknowledged,
94 // if ack=0 the acknowledge bit is not set.
95 // returns: byte read from i2c device
96 uint8_t i2c_master_read(int ack
) {
97 TWCR
= (1<<TWINT
) | (1<<TWEN
) | (ack
<<TWEA
);
103 void i2c_reset_state(void) {
107 void i2c_slave_init(uint8_t address
) {
108 TWAR
= address
<< 0; // slave i2c address
110 // TWEA - enable address acknowledgement
111 // TWINT - twi interrupt flag
112 // TWIE - enable the twi interrupt
113 TWCR
= (1<<TWIE
) | (1<<TWEA
) | (1<<TWINT
) | (1<<TWEN
);
122 // this device has been addressed as a slave receiver
123 slave_has_register_set
= false;
127 // this device has received data as a slave receiver
128 // The first byte that we receive in this transaction sets the location
129 // of the read/write location of the slaves memory that it exposes over
130 // i2c. After that, bytes will be written at slave_buffer_pos, incrementing
131 // slave_buffer_pos after each write.
132 if(!slave_has_register_set
) {
133 slave_buffer_pos
= TWDR
;
134 // don't acknowledge the master if this memory loctaion is out of bounds
135 if ( slave_buffer_pos
>= SLAVE_BUFFER_SIZE
) {
137 slave_buffer_pos
= 0;
139 slave_has_register_set
= true;
141 i2c_slave_buffer
[slave_buffer_pos
] = TWDR
;
148 // master has addressed this device as a slave transmitter and is
150 TWDR
= i2c_slave_buffer
[slave_buffer_pos
];
154 case TW_BUS_ERROR
: // something went wrong, reset twi state
159 // Reset everything, so we are ready for the next TWI interrupt
160 TWCR
|= (1<<TWIE
) | (1<<TWINT
) | (ack
<<TWEA
) | (1<<TWEN
);