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 uint8_t i2c_start(uint8_t address)
24 // reset TWI control register
26 // transmit START condition
27 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
30 uint16_t timeout_timer = timer_read();
31 while( !(TWCR & (1<<TWINT)) ) {
32 if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
33 return 2; // should make these codes standard
37 // wait for end of transmission
38 while( !(TWCR & (1<<TWINT)) );
41 // check if the start condition was successfully transmitted
42 if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return 1; }
44 // load slave address into data register
46 // start transmission of address
47 TWCR = (1<<TWINT) | (1<<TWEN);
50 timeout_timer = timer_read();
51 while( !(TWCR & (1<<TWINT)) ) {
52 if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
53 return 2; // should make these codes standard
57 // wait for end of transmission
58 while( !(TWCR & (1<<TWINT)) );
61 // check if the device has acknowledged the READ / WRITE mode
62 uint8_t twst = TW_STATUS & 0xF8;
63 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
68 uint8_t i2c_write(uint8_t data)
70 // load data into data register
72 // start transmission of data
73 TWCR = (1<<TWINT) | (1<<TWEN);
76 uint16_t timeout_timer = timer_read();
77 while( !(TWCR & (1<<TWINT)) ) {
78 if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
79 return 2; // should make these codes standard
83 // wait for end of transmission
84 while( !(TWCR & (1<<TWINT)) );
87 if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
92 uint8_t i2c_read_ack(void)
95 // start TWI module and acknowledge data after reception
96 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
99 uint16_t timeout_timer = timer_read();
100 while( !(TWCR & (1<<TWINT)) ) {
101 if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
102 return 2; // should make these codes standard
106 // wait for end of transmission
107 while( !(TWCR & (1<<TWINT)) );
110 // return received data from TWDR
114 uint8_t i2c_read_nack(void)
117 // start receiving without acknowledging reception
118 TWCR = (1<<TWINT) | (1<<TWEN);
121 uint16_t timeout_timer = timer_read();
122 while( !(TWCR & (1<<TWINT)) ) {
123 if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
124 return 2; // should make these codes standard
128 // wait for end of transmission
129 while( !(TWCR & (1<<TWINT)) );
132 // return received data from TWDR
136 uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
138 if (i2c_start(address | I2C_WRITE)) return 1;
140 for (uint16_t i = 0; i < length; i++)
142 if (i2c_write(data[i])) return 1;
150 uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
152 if (i2c_start(address | I2C_READ)) return 1;
154 for (uint16_t i = 0; i < (length-1); i++)
156 data[i] = i2c_read_ack();
158 data[(length-1)] = i2c_read_nack();
165 uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
167 if (i2c_start(devaddr | 0x00)) return 1;
171 for (uint16_t i = 0; i < length; i++)
173 if (i2c_write(data[i])) return 1;
181 uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
183 if (i2c_start(devaddr)) return 1;
187 if (i2c_start(devaddr | 0x01)) return 1;
189 for (uint16_t i = 0; i < (length-1); i++)
191 data[i] = i2c_read_ack();
193 data[(length-1)] = i2c_read_nack();
200 uint8_t i2c_stop(void)
202 // transmit STOP condition
203 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
206 uint16_t timeout_timer = timer_read();
207 while(TWCR & (1<<TWSTO)) {
208 if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
209 return 2; // should make these codes standard
213 // wait for end of transmission
214 while(TWCR & (1<<TWSTO));