1 /* Library made by: g4lvanix
2 * Github repository: https://github.com/g4lvanix/I2C-master-lib
8 #include "i2c_master.h"
11 #define F_SCL 400000UL // SCL frequency
13 #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
17 TWSR = 0; /* no prescaler */
18 TWBR = (uint8_t)TWBR_val;
22 i2c_status_t i2c_start(uint8_t address, uint8_t timeout)
24 // reset TWI control register
26 // transmit START condition
27 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
29 uint16_t timeout_timer = timer_read();
30 while( !(TWCR & (1<<TWINT)) ) {
31 if (timeout && (timer_read() - timeout_timer) > timeout) {
32 return I2C_STATUS_TIMEOUT;
36 // check if the start condition was successfully transmitted
37 if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return 1; }
39 // load slave address into data register
41 // start transmission of address
42 TWCR = (1<<TWINT) | (1<<TWEN);
44 timeout_timer = timer_read();
45 while( !(TWCR & (1<<TWINT)) ) {
46 if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
47 return I2C_STATUS_TIMEOUT;
51 // check if the device has acknowledged the READ / WRITE mode
52 uint8_t twst = TW_STATUS & 0xF8;
53 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
58 i2c_status_t i2c_write(uint8_t data, uint8_t timeout)
60 // load data into data register
62 // start transmission of data
63 TWCR = (1<<TWINT) | (1<<TWEN);
65 uint16_t timeout_timer = timer_read();
66 while( !(TWCR & (1<<TWINT)) ) {
67 if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
68 return I2C_STATUS_TIMEOUT;
72 if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
77 i2c_status_t i2c_read_ack(uint8_t timeout)
80 // start TWI module and acknowledge data after reception
81 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
83 uint16_t timeout_timer = timer_read();
84 while( !(TWCR & (1<<TWINT)) ) {
85 if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
86 return I2C_STATUS_TIMEOUT;
90 // return received data from TWDR
94 i2c_status_t i2c_read_nack(uint8_t timeout)
97 // start receiving without acknowledging reception
98 TWCR = (1<<TWINT) | (1<<TWEN);
100 uint16_t timeout_timer = timer_read();
101 while( !(TWCR & (1<<TWINT)) ) {
102 if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
103 return I2C_STATUS_TIMEOUT;
107 // return received data from TWDR
111 i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
113 if (i2c_start(address | I2C_WRITE)) return 1;
115 for (uint16_t i = 0; i < length; i++)
117 if (i2c_write(data[i])) return 1;
125 uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
127 if (i2c_start(address | I2C_READ)) return 1;
129 for (uint16_t i = 0; i < (length-1); i++)
131 data[i] = i2c_read_ack();
133 data[(length-1)] = i2c_read_nack();
140 uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
142 if (i2c_start(devaddr | 0x00)) return 1;
146 for (uint16_t i = 0; i < length; i++)
148 if (i2c_write(data[i])) return 1;
156 uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
158 if (i2c_start(devaddr)) return 1;
162 if (i2c_start(devaddr | 0x01)) return 1;
164 for (uint16_t i = 0; i < (length-1); i++)
166 data[i] = i2c_read_ack();
168 data[(length-1)] = i2c_read_nack();
175 i2c_status_t i2c_stop(uint8_t timeout)
177 // transmit STOP condition
178 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
180 uint16_t timeout_timer = timer_read();
181 while(TWCR & (1<<TWSTO)) {
182 if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
183 return I2C_STATUS_TIMEOUT;