/* Copyright 2022, Savanni D'Gerinel 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 . */ #include #include #include #include "i2c.h" void i2c_delay(void) { _delay_us(5); } inline void set_sda(i2c_bus_t *bus) { bus->sda_state = true; dio_set_direction(&bus->sda, LINE_IN); } inline void clear_sda(i2c_bus_t *bus) { bus->sda_state = false; 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) { bus->scl_state = true; dio_set_direction(&bus->scl, LINE_IN); } inline void clear_scl(i2c_bus_t *bus) { bus->scl_state = false; dio_set_direction(&bus->scl, LINE_OUT); } 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); set_sda(bus); set_scl(bus); return bus; } void i2c_start(i2c_bus_t *bus, i2c_error_e *error) { 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) { 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_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); } 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); } 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(); 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) { 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 || final_nak) { i2c_error_e tmp_status; i2c_write_bit(bus, 1, &tmp_status); return value; } i2c_write_bit(bus, 0, error); return value; }