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.
87 lines
3.0 KiB
C
87 lines
3.0 KiB
C
#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");
|
|
}
|