avr/i2c/i2c.c

212 lines
4.4 KiB
C
Raw Normal View History

2022-07-18 03:34:20 +00:00
/*
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 <stdlib.h>
#include <stdio.h>
2022-07-18 03:34:20 +00:00
#include "i2c.h"
2022-07-20 03:00:35 +00:00
void i2c_delay(void) {
_delay_us(5);
}
inline void set_sda(i2c_bus_t *bus) {
bus->sda_state = true;
2022-07-20 03:00:35 +00:00
dio_set_direction(&bus->sda, LINE_IN);
}
inline void clear_sda(i2c_bus_t *bus) {
bus->sda_state = false;
2022-07-18 03:34:20 +00:00
dio_set_direction(&bus->sda, LINE_OUT);
2022-07-20 03:00:35 +00:00
}
2022-07-18 03:34:20 +00:00
2022-07-20 03:00:35 +00:00
inline uint8_t read_sda(i2c_bus_t *bus) {
return dio_read(&bus->sda);
2022-07-18 03:34:20 +00:00
}
2022-07-20 03:00:35 +00:00
inline void set_scl(i2c_bus_t *bus) {
bus->scl_state = true;
2022-07-18 03:34:20 +00:00
dio_set_direction(&bus->scl, LINE_IN);
}
2022-07-20 03:00:35 +00:00
inline void clear_scl(i2c_bus_t *bus) {
bus->scl_state = false;
2022-07-20 03:00:35 +00:00
dio_set_direction(&bus->scl, LINE_OUT);
}
2022-07-18 03:34:20 +00:00
2022-07-20 03:00:35 +00:00
inline uint8_t read_scl(i2c_bus_t *bus) {
return dio_read(&bus->scl);
}
i2c_bus_t *i2c_init(dio_t clock, dio_t data) {
i2c_bus_t *bus = malloc(sizeof(i2c_bus_t));
bus->sda = data;
bus->scl = clock;
dio_set(&bus->sda, 0);
dio_set(&bus->scl, 0);
2022-07-20 03:00:35 +00:00
set_sda(bus);
set_scl(bus);
return bus;
2022-07-20 03:00:35 +00:00
}
void i2c_start(i2c_bus_t *bus, i2c_error_e *error) {
2022-07-20 03:00:35 +00:00
if (*error != i2c_ok) return;
if (read_sda(bus) == 0) {
*error = i2c_arbitration_lost;
return;
}
clear_sda(bus);
i2c_delay();
clear_scl(bus);
}
void i2c_end(i2c_bus_t *bus, i2c_error_e *error) {
2022-07-20 03:00:35 +00:00
clear_sda(bus);
i2c_delay();
set_scl(bus);
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return;
2022-07-18 03:34:20 +00:00
}
2022-07-20 03:00:35 +00:00
count++;
i2c_delay();
2022-07-18 03:34:20 +00:00
}
2022-07-20 03:00:35 +00:00
i2c_delay();
set_sda(bus);
i2c_delay();
2022-07-18 03:34:20 +00:00
2022-07-20 03:00:35 +00:00
if (read_sda(bus) == 0) {
*error = i2c_arbitration_lost;
}
2022-07-18 03:34:20 +00:00
}
void i2c_restart(i2c_bus_t *bus, i2c_error_e *error) {
if (*error != i2c_ok) return;
set_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_start(bus, error);
}
2022-07-20 03:00:35 +00:00
void i2c_write_bit(i2c_bus_t *bus, uint8_t value, i2c_error_e *error) {
if (*error != i2c_ok) return;
if (value) {
set_sda(bus);
} else {
clear_sda(bus);
2022-07-20 03:00:35 +00:00
}
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);
i2c_delay();
2022-07-20 03:00:35 +00:00
int count = 0;
while (read_scl(bus) == 0) {
if (count == 5) {
*error = i2c_timeout;
return 0;
}
count++;
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, bool final_nak, i2c_error_e *error) {
2022-07-20 03:00:35 +00:00
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);
2022-07-20 03:00:35 +00:00
}
if (*error != i2c_ok || final_nak) {
i2c_error_e tmp_status;
i2c_write_bit(bus, 1, &tmp_status);
return value;
2022-07-20 03:00:35 +00:00
}
i2c_write_bit(bus, 0, error);
2022-07-20 03:00:35 +00:00
return value;
}