Hack out an initial i2c bus driver

This commit is contained in:
Savanni D'Gerinel 2022-07-19 23:00:35 -04:00
parent 529cf6067d
commit 4a97284b49
4 changed files with 201 additions and 36 deletions

View File

@ -1,5 +1,6 @@
#include <display.h> #include <display.h>
#include <stdio.h> #include <stdio.h>
#include <i2c.h>
int main(void) { int main(void) {
display_t display = { display_t display = {

View File

@ -195,6 +195,21 @@
]; ];
}; };
packages."x86_64-linux"."i2c" =
{ gcc, cflags }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkLibrary {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "i2c";
psrc = ./i2c;
pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; })
];
};
packages."x86_64-linux"."prime-tx" = packages."x86_64-linux"."prime-tx" =
{ gcc, cflags }: { gcc, cflags }:
let let
@ -304,9 +319,10 @@
pbuildInputs = [ pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; }) (packages."x86_64-linux"."dio" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."display" { inherit gcc cflags; }) (packages."x86_64-linux"."display" { inherit gcc cflags; })
(packages."x86_64-linux"."i2c" { inherit gcc cflags; })
(packages."x86_64-linux"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; })
]; ];
}; };

192
i2c/i2c.c
View File

@ -13,42 +13,185 @@ You should have received a copy of the GNU General Public License along with thi
#include <util/delay.h> #include <util/delay.h>
#include "i2c.h" #include "i2c.h"
void i2c_init_host(i2c_host_t *bus, i2c_error_e *error) { void i2c_delay(void) {
dio_set_direction(&bus->sda, LINE_OUT); _delay_us(5);
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) { inline void set_sda(i2c_bus_t *bus) {
dio_set_direction(&bus->sda, LINE_IN); dio_set_direction(&bus->sda, LINE_IN);
}
inline void clear_sda(i2c_bus_t *bus) {
dio_set(&bus->sda, 0);
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); dio_set_direction(&bus->scl, LINE_IN);
} }
void i2c_host_write_packet(i2c_host_t *bus, uint8_t value, i2c_error_e *error) { inline void clear_scl(i2c_bus_t *bus) {
dio_set(&bus->sda, 0);
dio_set(&bus->scl, 0); dio_set(&bus->scl, 0);
dio_set_direction(&bus->scl, LINE_OUT);
}
for (int i = 7; i >= 0; i--) { inline uint8_t read_scl(i2c_bus_t *bus) {
dio_set(&bus->sda, value & _BV(i)); return dio_read(&bus->scl);
/* }
if (value & _BV(i)) {
dio_set(&bus->sda, 1); void i2c_init_host(i2c_bus_t *bus) {
} else { set_sda(bus);
dio_set(&bus->sda, 0); set_scl(bus);
} }
*/
dio_set(&bus->scl, 1); /*
dio_set(&bus->scl, 0); void i2c_init_client(i2c_client_t *bus) {
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;
} }
dio_set_direction(&bus->scl, LINE_IN); clear_sda(bus);
i2c_delay();
clear_scl(bus);
} }
void i2c_host_write(i2c_host_t *bus, uint8_t address, uint8_t *data, size_t length, i2c_error_e *error) { void i2c_end_condition(i2c_bus_t *bus, i2c_error_e *error) {
if (*error) return; 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); i2c_host_write_packet(bus, address << 1, error);
if (*error != ok) return; if (*error != ok) return;
@ -58,3 +201,4 @@ void i2c_host_write(i2c_host_t *bus, uint8_t address, uint8_t *data, size_t leng
if (*error != ok) return; if (*error != ok) return;
} }
} }
*/

View File

@ -13,27 +13,31 @@ You should have received a copy of the GNU General Public License along with thi
#ifndef __I2C_H__ #ifndef __I2C_H__
#define __I2C_H__ #define __I2C_H__
#include <base.h> #include <dio.h>
typedef enum { typedef enum {
ok, i2c_ok,
nak i2c_arbitration_lost,
i2c_timeout,
i2c_nak,
} i2c_error_e; } i2c_error_e;
typedef struct { typedef struct {
dio_t sda; dio_t sda;
dio_t clk; dio_t scl;
} i2c_host_t; } i2c_bus_t;
/*
typedef struct { typedef struct {
dio_t sda; dio_t sda;
dio_t clk; dio_t scl;
uint8_t addr; uint8_t addr;
} } i2c_client_t;
*/
void i2c_init_host(i2c_host_t *, i2c_error_e *); void i2c_init_host(i2c_bus_t *);
void i2c_init_client(i2c_client_t *, i2c_error_e *); // void i2c_init_client(i2c_client_t *);
void i2c_host_write(i2c_host_t *, uint8_t, uint8_t, size_t, i2c_error_e); // void i2c_host_write(i2c_host_t *, uint8_t, uint8_t, size_t, i2c_error_e);
#endif #endif