avr/i2c/i2c.c

198 lines
4.2 KiB
C

/*
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <util/delay.h>
#include "i2c.h"
void i2c_delay(void) {
_delay_us(5);
}
inline void set_sda(i2c_bus_t *bus) {
dio_set_direction(&bus->sda, LINE_IN);
}
inline void clear_sda(i2c_bus_t *bus) {
dio_set_direction(&bus->sda, LINE_OUT);
}
inline uint8_t read_sda(i2c_bus_t *bus) {
return dio_read(&bus->sda);
}
inline void set_scl(i2c_bus_t *bus) {
dio_set_direction(&bus->scl, LINE_IN);
}
inline void clear_scl(i2c_bus_t *bus) {
dio_set_direction(&bus->scl, LINE_OUT);
}
inline uint8_t read_scl(i2c_bus_t *bus) {
return dio_read(&bus->scl);
}
void i2c_init(i2c_bus_t *bus) {
dio_set(&bus->sda, 0);
dio_set(&bus->scl, 0);
set_sda(bus);
set_scl(bus);
}
void i2c_start_condition(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return;
// TODO: handle the restart state
if (read_sda(bus) == 0) {
*error = i2c_arbitration_lost;
return;
}
clear_sda(bus);
i2c_delay();
clear_scl(bus);
}
void i2c_end_condition(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return;
clear_sda(bus);
i2c_delay();
set_scl(bus);
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return;
}
count++;
i2c_delay();
}
i2c_delay();
set_sda(bus);
i2c_delay();
if (read_sda(bus) == 0) {
*error = i2c_arbitration_lost;
}
}
void i2c_write_bit(i2c_bus_t *bus, uint8_t value, i2c_error_e *error) {
if (*error != i2c_ok) return;
if (value) {
clear_sda(bus);
} else {
set_sda(bus);
}
i2c_delay();
set_scl(bus);
i2c_delay();
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return;
}
count++;
i2c_delay();
}
if (value && (read_sda(bus) == 0)) {
*error = i2c_arbitration_lost;
}
clear_scl(bus);
}
uint8_t i2c_read_bit(i2c_bus_t *bus, i2c_error_e *error) {
uint8_t bit;
if (*error != i2c_ok) return 0;
set_sda(bus);
i2c_delay();
set_scl(bus);
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return 0;
}
count++;
i2c_delay();
}
i2c_delay();
bit = read_sda(bus);
clear_scl(bus);
return bit;
}
void i2c_write_byte(i2c_bus_t *bus, uint8_t value, i2c_error_e *error) {
if (*error != i2c_ok) return;
for (int i = 7; i >= 0; i--) {
i2c_write_bit(bus, value & _BV(i), error);
}
uint8_t nak = i2c_read_bit(bus, error);
if (*error != i2c_ok) return;
if (nak) {
*error = i2c_nak;
}
return;
}
uint8_t i2c_read_byte(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return 0;
uint8_t value = 0;
for (int i = 0; i < 8; i++) {
value = value << 1 | i2c_read_bit(bus, error);
}
if (*error != i2c_ok) {
i2c_error_e tmp_error;
i2c_write_bit(bus, 1, &tmp_error);
return 0;
}
i2c_write_bit(bus, 0, error);
return value;
}
/*
void i2c_host_write(i2c_bus_t *bus, uint8_t address, uint8_t *data, size_t length, i2c_error_e *error) {
if (*error != i2c_ok) return;
i2c_host_write_packet(bus, address << 1, error);
if (*error != ok) return;
for (int i = 0; i < length; i++) {
i2c_host_write_packet(bus, data[i], error);
if (*error != ok) return;
}
}
*/