feat(firmware): support configurable SEN54 and SEN66 boards
Add the pinned Sensirion SEN66 driver, central sensor and board profiles, and a separate GPIO I2C bus for SEN66 with display. Preserve the original display layout and keep NOx and CO2 in InfluxDB. Include protocol tests, hardware workflow documentation and verified deployment results without credentials.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.."
|
||||
test_dir=$(mktemp -d)
|
||||
trap 'rm -rf -- "$test_dir"' EXIT
|
||||
driver=components/sen5x
|
||||
cc -std=c11 -Wall -Wextra -I"$driver" test/sensors/test_protocol.c \
|
||||
"$driver/sen5x_i2c.c" "$driver/sen66_i2c.c" \
|
||||
"$driver/sensirion_common.c" "$driver/sensirion_i2c.c" \
|
||||
-o "$test_dir/protocol"
|
||||
"$test_dir/protocol"
|
||||
cc -std=c11 -Wall -Wextra -Werror -I"$driver" \
|
||||
test/sensors/test_soft_i2c.c "$driver/sensor_soft_i2c.c" -o "$test_dir/soft-i2c"
|
||||
"$test_dir/soft-i2c"
|
||||
|
||||
# Compile all four hardware combinations; invalid selections must fail.
|
||||
for board in 1 2; do
|
||||
for sensor in 54 66; do
|
||||
printf '#include "hardware_config.h"\nint main(void) {return 0;}\n' |
|
||||
cc -std=c11 -Werror -Icomponents/hardware_config -DNANOPSX_BOARD="$board" \
|
||||
-DNANOPSX_SENSOR="$sensor" -x c - -o "$test_dir/config"
|
||||
done
|
||||
done
|
||||
for invalid in NANOPSX_BOARD=999 NANOPSX_SENSOR=999; do
|
||||
if printf '#include "hardware_config.h"\n' |
|
||||
cc -Icomponents/hardware_config -D"$invalid" -x c -c -o "$test_dir/invalid.o" - 2>/dev/null; then
|
||||
echo "Invalid configuration was accepted: $invalid" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
echo 'PASS: all four board/sensor selections and invalid-profile rejection'
|
||||
@@ -0,0 +1,83 @@
|
||||
/* Host-side tests exercise the real vendor drivers and shared CRC helpers. */
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "sen5x_i2c.h"
|
||||
#include "sen66_i2c.h"
|
||||
|
||||
static uint8_t expected_address;
|
||||
static uint16_t command;
|
||||
static uint16_t words[9];
|
||||
static bool bad_crc, fail_read, fail_write;
|
||||
|
||||
static uint8_t crc(const uint8_t *data) {
|
||||
uint8_t value = 0xff;
|
||||
for (unsigned n = 0; n < 2; n++) {
|
||||
value ^= data[n];
|
||||
for (unsigned bit = 0; bit < 8; bit++)
|
||||
value = (value & 0x80) ? (uint8_t)((value << 1) ^ 0x31) : (uint8_t)(value << 1);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t *data, uint16_t size) {
|
||||
assert(address == expected_address && size >= 2);
|
||||
command = (uint16_t)((data[0] << 8) | data[1]);
|
||||
return fail_write ? -1 : 0;
|
||||
}
|
||||
|
||||
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t *data, uint16_t size) {
|
||||
assert(address == expected_address && size % 3 == 0 && size <= 27);
|
||||
if (fail_read) return -1;
|
||||
for (unsigned i = 0; i < size / 3; i++) {
|
||||
data[3*i] = (uint8_t)(words[i] >> 8);
|
||||
data[3*i+1] = (uint8_t)words[i];
|
||||
data[3*i+2] = crc(&data[3*i]);
|
||||
}
|
||||
if (bad_crc) data[2] ^= 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sensirion_i2c_hal_sleep_usec(uint32_t usec) { (void)usec; }
|
||||
|
||||
int main(void) {
|
||||
bool ready = false;
|
||||
uint8_t padding;
|
||||
uint16_t pm1, pm2, pm4, pm10, co2;
|
||||
int16_t rh, temp, voc, nox;
|
||||
expected_address = 0x6b;
|
||||
sen66_init(expected_address);
|
||||
words[0] = 1;
|
||||
assert(sen66_get_data_ready(&padding, &ready) == 0 && ready && padding == 0);
|
||||
assert(command == 0x0202);
|
||||
uint16_t measured[] = {10, 25, 30, 40, 4567, 5000, 1200, 0x7fff, 1456};
|
||||
memcpy(words, measured, sizeof(measured));
|
||||
#define READ66() sen66_read_measured_values_as_integers(&pm1, &pm2, &pm4, &pm10, &rh, &temp, &voc, &nox, &co2)
|
||||
assert(READ66() == 0 && command == 0x0300);
|
||||
assert(pm2 == 25 && rh == 4567 && temp == 5000 && voc == 1200 && nox == 0x7fff && co2 == 1456);
|
||||
words[5] = (uint16_t)-1000;
|
||||
words[8] = 0xffff;
|
||||
assert(READ66() == 0 && temp == -1000 && co2 == 0xffff);
|
||||
bad_crc = true;
|
||||
assert(READ66() != 0);
|
||||
bad_crc = false; fail_read = true;
|
||||
assert(READ66() != 0);
|
||||
fail_read = false; fail_write = true;
|
||||
assert(READ66() != 0);
|
||||
fail_write = false;
|
||||
|
||||
expected_address = 0x69;
|
||||
words[0] = 1;
|
||||
assert(sen5x_read_data_ready(&ready) == 0 && ready && command == 0x0202);
|
||||
memcpy(words, measured, sizeof(measured));
|
||||
#define READ54() sen5x_read_measured_values(&pm1, &pm2, &pm4, &pm10, &rh, &temp, &voc, &nox)
|
||||
assert(READ54() == 0 && command == 0x03c4);
|
||||
assert(pm2 == 25 && rh == 4567 && temp == 5000 && voc == 1200 && nox == 0x7fff);
|
||||
bad_crc = true;
|
||||
assert(READ54() != 0);
|
||||
bad_crc = false; fail_write = true;
|
||||
assert(READ54() != 0);
|
||||
puts("PASS: SEN54/SEN66 address and command selection, measured payloads, signed values, sentinels, CRC and transport errors");
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
#include "sensor_soft_i2c.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int sda, scl;
|
||||
int samples[64], rises[64];
|
||||
unsigned sample_count, sample_index, rise_count, starts, stops;
|
||||
unsigned stretch, one_us_delays;
|
||||
bool stuck;
|
||||
} fake_t;
|
||||
|
||||
static void sda(void *ctx, int value) {
|
||||
fake_t *f = ctx;
|
||||
if (f->scl && f->sda != value) {
|
||||
if (value) f->stops++; else f->starts++;
|
||||
}
|
||||
f->sda = value;
|
||||
}
|
||||
static void scl(void *ctx, int value) {
|
||||
fake_t *f = ctx;
|
||||
if (value && !f->scl) {
|
||||
assert(f->rise_count < 64);
|
||||
f->rises[f->rise_count++] = f->sda;
|
||||
}
|
||||
f->scl = value;
|
||||
}
|
||||
static int read_sda(void *ctx) {
|
||||
fake_t *f = ctx;
|
||||
assert(f->sample_index < f->sample_count);
|
||||
return f->samples[f->sample_index++];
|
||||
}
|
||||
static int read_scl(void *ctx) {
|
||||
fake_t *f = ctx;
|
||||
if (f->stuck) return 0;
|
||||
if (f->stretch) { f->stretch--; return 0; }
|
||||
return f->scl;
|
||||
}
|
||||
static void delay_us(void *ctx, unsigned us) {
|
||||
if (us == 1) ((fake_t *)ctx)->one_us_delays++;
|
||||
}
|
||||
static unsigned transmitted_byte(fake_t *f, unsigned offset) {
|
||||
unsigned b = 0;
|
||||
for (unsigned n = 0; n < 8; n++) b = (b << 1) | f->rises[offset+n];
|
||||
return b;
|
||||
}
|
||||
static void reset(fake_t *f) { memset(f, 0, sizeof(*f)); f->sda = f->scl = 1; }
|
||||
|
||||
int main(void) {
|
||||
fake_t f;
|
||||
sensor_soft_i2c_t p = { .context=&f, .sda=sda, .scl=scl,
|
||||
.read_sda=read_sda, .read_scl=read_scl, .delay_us=delay_us,
|
||||
.half_period_us=5, .stretch_timeout_us=10 };
|
||||
uint8_t command[] = {0, 0x21};
|
||||
reset(&f);
|
||||
f.samples[0]=1; // idle bus, followed by three slave ACK bits.
|
||||
f.sample_count=4;
|
||||
assert(sensor_soft_i2c_transfer(&p,0x6b,command,2,false)==0);
|
||||
assert(f.starts==1 && f.stops==1 && f.sda==1 && f.scl==1);
|
||||
assert(transmitted_byte(&f,0)==0xd6 && transmitted_byte(&f,9)==0 && transmitted_byte(&f,18)==0x21);
|
||||
|
||||
reset(&f);
|
||||
f.samples[0]=1; f.samples[1]=1; f.sample_count=2; // address NACK.
|
||||
assert(sensor_soft_i2c_transfer(&p,0x6b,command,2,false)==1);
|
||||
assert(f.starts==1 && f.stops==1 && f.sda==1 && f.scl==1);
|
||||
|
||||
reset(&f);
|
||||
f.samples[0]=1; f.samples[1]=0;
|
||||
for (unsigned n=0;n<16;n++) f.samples[n+2]=((0xa55aU>>(15-n))&1);
|
||||
f.sample_count=18;
|
||||
uint8_t data[2];
|
||||
assert(sensor_soft_i2c_transfer(&p,0x6b,data,2,true)==0);
|
||||
assert(data[0]==0xa5 && data[1]==0x5a && transmitted_byte(&f,0)==0xd7);
|
||||
assert(f.rises[17]==0 && f.rises[26]==1); // ACK then final NACK.
|
||||
assert(f.starts==1 && f.stops==1);
|
||||
|
||||
reset(&f); f.samples[0]=1; f.sample_count=2; f.stretch=3;
|
||||
assert(sensor_soft_i2c_transfer(&p,0x6b,NULL,0,false)==0 && f.one_us_delays==3);
|
||||
reset(&f); f.stuck=true;
|
||||
assert(sensor_soft_i2c_transfer(&p,0x6b,NULL,0,false)==-1);
|
||||
assert(f.one_us_delays==11 && f.sda==1 && f.scl==1);
|
||||
assert(sensor_soft_i2c_transfer(&p,0x80,NULL,0,false)==-1);
|
||||
assert(sensor_soft_i2c_transfer(&p,0x6b,NULL,1,true)==-1);
|
||||
puts("PASS: GPIO I2C START/STOP, address/data bits, ACK/NACK, reads, stretching, timeout and line release");
|
||||
}
|
||||
Reference in New Issue
Block a user