Compare commits

..

No commits in common. "ba2e5b84dcb2b439a6f08dfc92aa2c856095f385" and "529cf6067d331bbed8231c873eadc9eaebb88b03" have entirely different histories.

7 changed files with 52 additions and 467 deletions

View File

@ -1,15 +1,27 @@
#include <dio.h>
#include <display.h> #include <display.h>
#include <stdio.h>
int main(void) { int main(void) {
display_t display = { display_t display = {
.reg = { .reg = {
.output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 }, .output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 }, .shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }, .latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
}, },
}; };
int count = 0;
while (1) {
display_init(&display); display_init(&display);
display_write_message(&display, "ready"); char msg[15];
snprintf(msg, 15, "[%d]", count);
display_clear(&display);
display_write_message(&display, msg);
_delay_ms(1000);
count++;
}
return 0;
} }

View File

@ -90,7 +90,7 @@
(packages."x86_64-linux"."radio-rx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; }) (packages."x86_64-linux"."radio-rx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; })
(packages."x86_64-linux"."lantern" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; }) (packages."x86_64-linux"."lantern" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
(packages."x86_64-linux"."lantern-controller" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; }) (packages."x86_64-linux"."lantern-controller" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
(packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; }) (packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; })
]; ];
}; };
@ -195,23 +195,6 @@
]; ];
}; };
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"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."display" { inherit gcc cflags; })
];
};
packages."x86_64-linux"."prime-tx" = packages."x86_64-linux"."prime-tx" =
{ gcc, cflags }: { gcc, cflags }:
let let
@ -306,7 +289,7 @@
let let
pkgs = import nixpkgs { system = "x86_64-linux"; }; pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages; avr = pkgs.pkgsCross.avr.buildPackages;
in packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags attiny85; avr = true; }; in packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; };
packages."x86_64-linux"."display-i2c" = packages."x86_64-linux"."display-i2c" =
{ gcc, cflags, avr }: { gcc, cflags, avr }:
let let
@ -321,36 +304,9 @@
pbuildInputs = [ pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; }) (packages."x86_64-linux"."dio" { 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; }) (packages."x86_64-linux"."spi" { inherit gcc cflags; })
];
};
packages."x86_64-linux"."lux-sensor_" =
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in packages."x86_64-linux"."lux-sensor" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; };
packages."x86_64-linux"."lux-sensor" =
{ gcc, cflags, avr }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
in mkProgram {
pkgs = pkgs;
gcc = gcc;
cflags = cflags;
pname = "lux-sensor";
psrc = ./lux-sensor;
inherit avr;
pbuildInputs = [
(packages."x86_64-linux"."dio" { 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"."shift-register" { inherit gcc cflags; })
(packages."x86_64-linux"."spi" { inherit gcc cflags; }) (packages."x86_64-linux"."display" { inherit gcc cflags; })
]; ];
}; };

View File

@ -1 +0,0 @@
avrdude -p $1 -c usbtiny -U flash:w:$2:i

232
i2c/i2c.c
View File

@ -11,208 +11,44 @@ You should have received a copy of the GNU General Public License along with thi
*/ */
#include <util/delay.h> #include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include "i2c.h" #include "i2c.h"
void i2c_delay(void) { void i2c_init_host(i2c_host_t *bus, i2c_error_e *error) {
_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); dio_set_direction(&bus->sda, LINE_OUT);
dio_set(&bus->sda, 1);
dio_set_direction(&bus->scl, LINE_OUT);
dio_set(&bus->scl, 1);
} }
inline uint8_t read_sda(i2c_bus_t *bus) { void i2c_init_client(i2c_client_t *bus, i2c_error_e *error) {
return dio_read(&bus->sda); dio_set_direction(&bus->sda, LINE_IN);
}
inline void set_scl(i2c_bus_t *bus) {
bus->scl_state = true;
dio_set_direction(&bus->scl, LINE_IN); dio_set_direction(&bus->scl, LINE_IN);
} }
inline void clear_scl(i2c_bus_t *bus) { void i2c_host_write_packet(i2c_host_t *bus, uint8_t value, i2c_error_e *error) {
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->sda, 0);
dio_set(&bus->scl, 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--) { for (int i = 7; i >= 0; i--) {
i2c_write_bit(bus, value & _BV(i), error); dio_set(&bus->sda, value & _BV(i));
}
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;
}
/* /*
void i2c_host_write(i2c_bus_t *bus, uint8_t address, uint8_t *data, size_t length, i2c_error_e *error) { if (value & _BV(i)) {
if (*error != i2c_ok) return; 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); i2c_host_write_packet(bus, address << 1, error);
if (*error != ok) return; if (*error != ok) return;
@ -222,21 +58,3 @@ void i2c_host_write(i2c_bus_t *bus, uint8_t address, uint8_t *data, size_t lengt
if (*error != ok) return; if (*error != ok) return;
} }
} }
*/
void i2c_status_to_string(i2c_error_e status, char msg[20]) {
switch (status) {
case i2c_ok:
snprintf(msg, 20, "ok");
break;
case i2c_arbitration_lost:
snprintf(msg, 20, "arbitration lost");
break;
case i2c_timeout:
snprintf(msg, 20, "timeout");
break;
case i2c_nak:
snprintf(msg, 20, "nak");
break;
}
}

View File

@ -13,42 +13,27 @@ 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 <dio.h> #include <base.h>
typedef enum { typedef enum {
i2c_ok, ok,
i2c_arbitration_lost, nak
i2c_timeout,
i2c_nak,
} i2c_error_e; } i2c_error_e;
typedef struct { typedef struct {
bool sda_state;
dio_t sda; dio_t sda;
dio_t clk;
} i2c_host_t;
bool scl_state;
dio_t scl;
} i2c_bus_t;
/*
typedef struct { typedef struct {
dio_t sda; dio_t sda;
dio_t scl; dio_t clk;
uint8_t addr; uint8_t addr;
} i2c_client_t; }
*/
i2c_bus_t * i2c_init(dio_t, dio_t); void i2c_init_host(i2c_host_t *, i2c_error_e *);
void i2c_init_client(i2c_client_t *, i2c_error_e *);
void i2c_start(i2c_bus_t *bus, i2c_error_e *error); void i2c_host_write(i2c_host_t *, uint8_t, uint8_t, size_t, i2c_error_e);
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]);
#endif #endif

View File

@ -1,103 +0,0 @@
#include <dio.h>
#include <display.h>
#include <i2c.h>
#include <stdio.h>
#include <stdlib.h>
typedef enum {
ls_ok,
ls_unrecognized,
ls_not_responding,
ls_powered_down,
} lux_sensor_status_e;
typedef struct lux_sensor_s lux_sensor_t;
struct lux_sensor_s {
uint16_t (*read_total_lux)(void *, lux_sensor_status_e *);
uint16_t (*read_ir)(void *, lux_sensor_status_e *);
uint16_t (*read_visible)(void *, lux_sensor_status_e *);
void *impl;
};
uint16_t tsl2561_read_total_lux(void *sensor, lux_sensor_status_e *status) {
if (*status != ls_ok) return 0;
return 0;
}
uint16_t tsl2561_read_ir(void *sensor, lux_sensor_status_e *status) {
if (*status != ls_ok) return 0;
return 0;
}
uint16_t tsl2561_read_visible(void *sensor, lux_sensor_status_e *status) {
if (*status != ls_ok) return 0;
return 0;
}
lux_sensor_t tsl2561_init(dio_t clock, dio_t data) {
i2c_bus_t *bus = malloc(sizeof(i2c_bus_t));
bus->sda = data;
bus->scl = clock;
return (lux_sensor_t){
.read_total_lux = tsl2561_read_total_lux,
.read_ir = tsl2561_read_ir,
.read_visible = tsl2561_read_visible,
.impl = (void *)bus,
};
}
uint16_t ls_read_total_lux(lux_sensor_t *sensor, lux_sensor_status_e *status) {
return sensor->read_total_lux(sensor->impl, status);
}
uint16_t ls_read_ir(lux_sensor_t *sensor, lux_sensor_status_e *status) {
return sensor->read_ir(sensor->impl, status);
}
uint16_t ls_read_visible(lux_sensor_t *sensor, lux_sensor_status_e *status) {
return sensor->read_visible(sensor->impl, status);
}
int main(void) {
display_t display = {
.reg = {
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
},
};
lux_sensor_t sensor = tsl2561_init(
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 }
);
int count = 0;
while (1) {
lux_sensor_status_e sensor_status = ls_ok;
display_init(&display);
uint16_t total_lux = ls_read_total_lux(&sensor, &sensor_status);
uint16_t ir = ls_read_ir(&sensor, &sensor_status);
char msg[15];
if (sensor_status == ls_ok) {
snprintf(msg, 20, "[%d] %d %d", count, total_lux, ir);
} else {
snprintf(msg, 20, "[%d] error", count);
}
display_clear(&display);
display_write_message(&display, msg);
_delay_ms(1000);
count++;
}
return 0;
}

View File

@ -1,82 +0,0 @@
#include <dio.h>
#include <i2c.h>
#include <display.h>
#include <stdio.h>
int main(void) {
display_t display = {
.reg = {
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
},
};
display_init(&display);
i2c_bus_t *bus = i2c_init(
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
(dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 }
);
i2c_error_e status = i2c_ok;
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x80, &status);
i2c_write_byte(bus, 0x03, &status);
i2c_end(bus, &status);
if (status != i2c_ok) {
display_clear(&display);
display_write_message(&display, "early abort");
}
_delay_ms(500);
uint8_t count = 0;
while(1) {
/*
int id = 0;
status = i2c_ok;
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x80, &status);
i2c_restart(bus, &status);
i2c_write_byte(bus, (0x39 << 1) | 1, &status);
id = i2c_read_byte(bus, true, &status) & 0x03;
i2c_end(bus, &status);
*/
int lux = 0;
int ir = 0;
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x8c, &status);
i2c_restart(bus, &status);
i2c_write_byte(bus, 0x39 << 1 | 1, &status);
lux = i2c_read_byte(bus, false, &status);
lux |= i2c_read_byte(bus, true, &status) << 8;
i2c_end(bus, &status);
i2c_start(bus, &status);
i2c_write_byte(bus, 0x39 << 1, &status);
i2c_write_byte(bus, 0x8e, &status);
i2c_restart(bus, &status);
i2c_write_byte(bus, 0x39 << 1 | 1, &status);
ir = ((int)i2c_read_byte(bus, false, &status));
ir |= ((int)i2c_read_byte(bus, true, &status)) << 8;
i2c_end(bus, &status);
char msg[20] = "";
display_clear(&display);
snprintf(msg, 20, "[%d] %x, %x", count, lux, ir);
display_write_message(&display, msg);
display_set_location(&display, 1, 0);
i2c_status_to_string(status, msg);
display_write_message(&display, msg);
_delay_ms(1000);
count++;
}
}