Compare commits

...

5 Commits

7 changed files with 472 additions and 57 deletions

View File

@ -1,27 +1,15 @@
#include <dio.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 },
.output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 },
.shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 },
.latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 },
},
};
display_init(&display);
int count = 0;
while (1) {
display_init(&display);
char msg[15];
snprintf(msg, 15, "[%d]", count);
display_clear(&display);
display_write_message(&display, msg);
_delay_ms(1000);
count++;
}
return 0;
display_write_message(&display, "ready");
}

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"."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"."display-i2c" { 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; })
];
};
@ -195,6 +195,23 @@
];
};
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" =
{ gcc, cflags }:
let
@ -289,7 +306,7 @@
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
avr = pkgs.pkgsCross.avr.buildPackages;
in packages."x86_64-linux"."display-i2c" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; avr = true; };
in 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, cflags, avr }:
let
@ -304,9 +321,36 @@
pbuildInputs = [
(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"."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"."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"."spi" { inherit gcc cflags; })
];
};

1
flash-tiny Executable file
View File

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

232
i2c/i2c.c
View File

@ -11,44 +11,208 @@ You should have received a copy of the GNU General Public License along with thi
*/
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.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_delay(void) {
_delay_us(5);
}
void i2c_init_client(i2c_client_t *bus, i2c_error_e *error) {
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);
}
void i2c_host_write_packet(i2c_host_t *bus, uint8_t value, i2c_error_e *error) {
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);
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);
return bus;
}
void i2c_host_write(i2c_host_t *bus, uint8_t address, uint8_t *data, size_t length, i2c_error_e *error) {
if (*error) return;
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;
}
/*
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);
if (*error != ok) return;
@ -58,3 +222,21 @@ void i2c_host_write(i2c_host_t *bus, uint8_t address, uint8_t *data, size_t leng
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,27 +13,42 @@ You should have received a copy of the GNU General Public License along with thi
#ifndef __I2C_H__
#define __I2C_H__
#include <base.h>
#include <dio.h>
typedef enum {
ok,
nak
i2c_ok,
i2c_arbitration_lost,
i2c_timeout,
i2c_nak,
} i2c_error_e;
typedef struct {
bool sda_state;
dio_t sda;
dio_t clk;
} i2c_host_t;
bool scl_state;
dio_t scl;
} i2c_bus_t;
/*
typedef struct {
dio_t sda;
dio_t clk;
dio_t scl;
uint8_t addr;
}
} i2c_client_t;
*/
void i2c_init_host(i2c_host_t *, i2c_error_e *);
void i2c_init_client(i2c_client_t *, i2c_error_e *);
i2c_bus_t * i2c_init(dio_t, dio_t);
void i2c_host_write(i2c_host_t *, uint8_t, uint8_t, size_t, i2c_error_e);
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]);
#endif

103
lux-sensor-reader/main.c Normal file
View File

@ -0,0 +1,103 @@
#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;
}

82
lux-sensor/main.c Normal file
View File

@ -0,0 +1,82 @@
#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++;
}
}