avr/i2c/i2c.h

91 lines
2.4 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/>.
*/
#ifndef __I2C_H__
#define __I2C_H__
#include <dio.h>
typedef enum i2c_bus_state {
IDLE,
START_SDA,
START,
ACTIVE_LISTEN,
ACTIVE_IGNORE,
INACTIVE,
} i2c_bus_state_e;
typedef enum message_direction {
HOST_TO_DEVICE,
DEVICE_TO_HOST,
} message_direction_e;
typedef enum {
i2c_ok,
i2c_arbitration_lost,
i2c_timeout,
i2c_nak,
} i2c_error_e;
typedef struct {
bool sda_state;
dio_t sda;
bool scl_state;
dio_t scl;
} i2c_bus_t;
typedef struct {
dio_t sda;
dio_t scl;
uint8_t addr;
i2c_bus_state_e state;
message_direction_e direction;
uint8_t register_count;
int8_t target_register;
uint8_t registers[];
} i2c_device_t;
i2c_bus_t * i2c_init(dio_t clock, dio_t data);
void i2c_start(i2c_bus_t *bus, i2c_error_e *error);
void i2c_end(i2c_bus_t *bus, i2c_error_e *error);
void i2c_restart(i2c_bus_t *bus, i2c_error_e *error);
void i2c_write_byte(i2c_bus_t *bus, uint8_t value, i2c_error_e *error);
uint8_t i2c_read_byte(i2c_bus_t *bus, bool final_nak, i2c_error_e *error);
// void i2c_host_write(i2c_host_t *, uint8_t, uint8_t, size_t, i2c_error_e);
void i2c_status_to_string(i2c_error_e status, char msg[20]);
i2c_device_t * i2c_device_init(dio_t clock, dio_t data, uint8_t address, uint8_t register_count);
void i2c_device_step(i2c_device_t *);
void i2c_device_release_bus(i2c_device_t *);
/*
void i2c_device_bus_state_update(i2c_device_bus_t *bus);
void i2c_set_register(i2c_registers_t *regs, i2c_error_e *error) {
}
void i2c_register_write(i2c_registers_t *regs, i2c_error_e *error) {
}
void i2c_register_read(i2c_registers_t *regs, i2c_error_e *error) {
}
*/
#endif