1 /* Copyright (C) 2019 Elia Ritterbusch
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 /* Library made by: g4lvanix
17 * Github repository: https://github.com/g4lvanix/I2C-master-lib
23 #include "i2c_master.h"
28 # define F_SCL 400000UL // SCL frequency
31 #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16) / 2)
34 TWSR = 0; /* no prescaler */
35 TWBR = (uint8_t)TWBR_val;
37 #ifdef __AVR_ATmega32A__
38 // set pull-up resistors on I2C bus pins
41 // enable TWI (two-wire interface)
44 // enable TWI interrupt and slave address ACK
50 i2c_status_t i2c_start(uint8_t address, uint16_t timeout) {
51 // reset TWI control register
53 // transmit START condition
54 TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
56 uint16_t timeout_timer = timer_read();
57 while (!(TWCR & (1 << TWINT))) {
58 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
59 return I2C_STATUS_TIMEOUT;
63 // check if the start condition was successfully transmitted
64 if (((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)) {
65 return I2C_STATUS_ERROR;
68 // load slave address into data register
70 // start transmission of address
71 TWCR = (1 << TWINT) | (1 << TWEN);
73 timeout_timer = timer_read();
74 while (!(TWCR & (1 << TWINT))) {
75 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
76 return I2C_STATUS_TIMEOUT;
80 // check if the device has acknowledged the READ / WRITE mode
81 uint8_t twst = TW_STATUS & 0xF8;
82 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
83 return I2C_STATUS_ERROR;
86 return I2C_STATUS_SUCCESS;
89 i2c_status_t i2c_write(uint8_t data, uint16_t timeout) {
90 // load data into data register
92 // start transmission of data
93 TWCR = (1 << TWINT) | (1 << TWEN);
95 uint16_t timeout_timer = timer_read();
96 while (!(TWCR & (1 << TWINT))) {
97 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
98 return I2C_STATUS_TIMEOUT;
102 if ((TW_STATUS & 0xF8) != TW_MT_DATA_ACK) {
103 return I2C_STATUS_ERROR;
106 return I2C_STATUS_SUCCESS;
109 int16_t i2c_read_ack(uint16_t timeout) {
110 // start TWI module and acknowledge data after reception
111 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
113 uint16_t timeout_timer = timer_read();
114 while (!(TWCR & (1 << TWINT))) {
115 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
116 return I2C_STATUS_TIMEOUT;
120 // return received data from TWDR
124 int16_t i2c_read_nack(uint16_t timeout) {
125 // start receiving without acknowledging reception
126 TWCR = (1 << TWINT) | (1 << TWEN);
128 uint16_t timeout_timer = timer_read();
129 while (!(TWCR & (1 << TWINT))) {
130 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
131 return I2C_STATUS_TIMEOUT;
135 // return received data from TWDR
139 i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
140 i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
142 for (uint16_t i = 0; i < length && status >= 0; i++) {
143 status = i2c_write(data[i], timeout);
151 i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
152 i2c_status_t status = i2c_start(address | I2C_READ, timeout);
154 for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
155 status = i2c_read_ack(timeout);
162 status = i2c_read_nack(timeout);
164 data[(length - 1)] = status;
170 return (status < 0) ? status : I2C_STATUS_SUCCESS;
173 i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
174 i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
176 status = i2c_write(regaddr, timeout);
178 for (uint16_t i = 0; i < length && status >= 0; i++) {
179 status = i2c_write(data[i], timeout);
188 i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
189 i2c_status_t status = i2c_start(devaddr, timeout);
194 status = i2c_write(regaddr, timeout);
199 status = i2c_start(devaddr | 0x01, timeout);
201 for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
202 status = i2c_read_ack(timeout);
209 status = i2c_read_nack(timeout);
211 data[(length - 1)] = status;
218 return (status < 0) ? status : I2C_STATUS_SUCCESS;
221 void i2c_stop(void) {
222 // transmit STOP condition
223 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);