Start on the i2c prototype
This commit is contained in:
parent
e03485b1e5
commit
aa41bdc067
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
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_init_host(i2c_host_t *bus, i2c_error_e *error) {
|
||||
dio_set_direction(&bus->sda, LINE_OUT);
|
||||
dio_set(&bus->sda, 1);
|
||||
|
||||
dio_set_direction(&bus->scl, LINE_OUT);
|
||||
dio_set(&bus->scl, 1);
|
||||
}
|
||||
|
||||
void i2c_init_client(i2c_client_t *bus, i2c_error_e *error) {
|
||||
dio_set_direction(&bus->sda, LINE_IN);
|
||||
dio_set_direction(&bus->scl, LINE_IN);
|
||||
}
|
||||
|
||||
void i2c_host_write_packet(i2c_host_t *bus, uint8_t value, i2c_error_e *error) {
|
||||
dio_set(&bus->sda, 0);
|
||||
dio_set(&bus->scl, 0);
|
||||
|
||||
for (int i = 7; i >= 0; i--) {
|
||||
dio_set(&bus->sda, value & _BV(i));
|
||||
/*
|
||||
if (value & _BV(i)) {
|
||||
dio_set(&bus->sda, 1);
|
||||
} else {
|
||||
dio_set(&bus->sda, 0);
|
||||
}
|
||||
*/
|
||||
dio_set(&bus->scl, 1);
|
||||
dio_set(&bus->scl, 0);
|
||||
}
|
||||
|
||||
dio_set_direction(&bus->scl, LINE_IN);
|
||||
|
||||
}
|
||||
|
||||
void i2c_host_write(i2c_host_t *bus, uint8_t address, uint8_t *data, size_t length, i2c_error_e *error) {
|
||||
if (*error) 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
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 <base.h>
|
||||
|
||||
typedef enum {
|
||||
ok,
|
||||
nak
|
||||
} i2c_error_e;
|
||||
|
||||
typedef struct {
|
||||
dio_t sda;
|
||||
dio_t clk;
|
||||
} i2c_host_t;
|
||||
|
||||
typedef struct {
|
||||
dio_t sda;
|
||||
dio_t clk;
|
||||
uint8_t addr;
|
||||
}
|
||||
|
||||
void i2c_init_host(i2c_host_t *, i2c_error_e *);
|
||||
void i2c_init_client(i2c_client_t *, i2c_error_e *);
|
||||
|
||||
void i2c_host_write(i2c_host_t *, uint8_t, uint8_t, size_t, i2c_error_e);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue