1 /* Library made by: g4lvanix
2 * Github repository: https://github.com/g4lvanix/I2C-master-lib
8 #include "i2c_master.h"
12 #define F_SCL 400000UL // SCL frequency
15 #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
19 TWSR = 0; /* no prescaler */
20 TWBR = (uint8_t)TWBR_val;
23 i2c_status_t i2c_start(uint8_t address, uint16_t timeout)
25 // reset TWI control register
27 // transmit START condition
28 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
30 uint16_t timeout_timer = timer_read();
31 while( !(TWCR & (1<<TWINT)) ) {
32 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
33 return I2C_STATUS_TIMEOUT;
37 // check if the start condition was successfully transmitted
38 if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return I2C_STATUS_ERROR; }
40 // load slave address into data register
42 // start transmission of address
43 TWCR = (1<<TWINT) | (1<<TWEN);
45 timeout_timer = timer_read();
46 while( !(TWCR & (1<<TWINT)) ) {
47 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
48 return I2C_STATUS_TIMEOUT;
52 // check if the device has acknowledged the READ / WRITE mode
53 uint8_t twst = TW_STATUS & 0xF8;
54 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return I2C_STATUS_ERROR;
56 return I2C_STATUS_SUCCESS;
59 i2c_status_t i2c_write(uint8_t data, uint16_t timeout)
61 // load data into data register
63 // start transmission of data
64 TWCR = (1<<TWINT) | (1<<TWEN);
66 uint16_t timeout_timer = timer_read();
67 while( !(TWCR & (1<<TWINT)) ) {
68 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
69 return I2C_STATUS_TIMEOUT;
73 if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return I2C_STATUS_ERROR; }
75 return I2C_STATUS_SUCCESS;
78 int16_t i2c_read_ack(uint16_t timeout)
81 // start TWI module and acknowledge data after reception
82 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
84 uint16_t timeout_timer = timer_read();
85 while( !(TWCR & (1<<TWINT)) ) {
86 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
87 return I2C_STATUS_TIMEOUT;
91 // return received data from TWDR
95 int16_t i2c_read_nack(uint16_t timeout)
98 // start receiving without acknowledging reception
99 TWCR = (1<<TWINT) | (1<<TWEN);
101 uint16_t timeout_timer = timer_read();
102 while( !(TWCR & (1<<TWINT)) ) {
103 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
104 return I2C_STATUS_TIMEOUT;
108 // return received data from TWDR
112 i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)
114 i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
115 if (status) return status;
117 for (uint16_t i = 0; i < length; i++) {
118 status = i2c_write(data[i], timeout);
119 if (status) return status;
122 status = i2c_stop(timeout);
123 if (status) return status;
125 return I2C_STATUS_SUCCESS;
128 i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)
130 i2c_status_t status = i2c_start(address | I2C_READ, timeout);
131 if (status) return status;
133 for (uint16_t i = 0; i < (length-1); i++) {
134 status = i2c_read_ack(timeout);
142 status = i2c_read_nack(timeout);
144 data[(length-1)] = status;
149 status = i2c_stop(timeout);
150 if (status) return status;
152 return I2C_STATUS_SUCCESS;
155 i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)
157 i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
158 if (status) return status;
160 status = i2c_write(regaddr, timeout);
161 if (status) return status;
163 for (uint16_t i = 0; i < length; i++) {
164 status = i2c_write(data[i], timeout);
165 if (status) return status;
168 status = i2c_stop(timeout);
169 if (status) return status;
171 return I2C_STATUS_SUCCESS;
174 i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)
176 i2c_status_t status = i2c_start(devaddr, timeout);
177 if (status) return status;
179 status = i2c_write(regaddr, timeout);
180 if (status) return status;
182 status = i2c_start(devaddr | 0x01, timeout);
183 if (status) return status;
185 for (uint16_t i = 0; i < (length-1); i++) {
186 status = i2c_read_ack(timeout);
194 status = i2c_read_nack(timeout);
196 data[(length-1)] = status;
201 status = i2c_stop(timeout);
202 if (status) return status;
204 return I2C_STATUS_SUCCESS;
207 i2c_status_t i2c_stop(uint16_t timeout)
209 // transmit STOP condition
210 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
212 uint16_t timeout_timer = timer_read();
213 while(TWCR & (1<<TWSTO)) {
214 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
215 return I2C_STATUS_TIMEOUT;
219 return I2C_STATUS_SUCCESS;