Skip to content

Commit 39675ae

Browse files
clawpiladyada
andcommitted
Fix measurement lockup: clear AVALID between reads
readAllChannels() would lock up under rapidly changing light because AVALID persisted from the previous measurement. Now stops SP_EN, clears STATUS/ASTATUS, waits for AVALID to deassert, then starts fresh. Also stops measurement after reading so AVALID is clean for next call. Co-authored-by: ladyada <limor@ladyada.net>
1 parent 1e91ea2 commit 39675ae

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

Adafruit_AS7343.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,23 +317,43 @@ bool Adafruit_AS7343::readAllChannels(uint16_t *readings_buffer) {
317317
num_channels = 18;
318318
}
319319

320+
// Ensure SP_EN is off and AVALID is cleared before starting
321+
stopMeasurement();
322+
323+
// Clear any pending status by reading STATUS and ASTATUS
324+
Adafruit_BusIO_Register status_reg =
325+
Adafruit_BusIO_Register(i2c_dev, AS7343_STATUS);
326+
uint8_t status_val = status_reg.read();
327+
status_reg.write(status_val); // Write back to self-clear
328+
Adafruit_BusIO_Register astatus_reg =
329+
Adafruit_BusIO_Register(i2c_dev, AS7343_ASTATUS);
330+
astatus_reg.read();
331+
332+
// Wait for AVALID to actually clear
333+
uint32_t start = millis();
334+
while (dataReady()) {
335+
if (millis() - start > 100) {
336+
break; // Give up waiting, proceed anyway
337+
}
338+
delay(1);
339+
}
340+
320341
// Start one measurement — auto-SMUX runs all cycles internally
321342
if (!startMeasurement()) {
322343
return false;
323344
}
324345

325346
// Wait for AVALID (fires after all cycles complete)
326-
uint32_t start = millis();
347+
start = millis();
327348
while (!dataReady()) {
328349
if (millis() - start > 1000) {
350+
stopMeasurement();
329351
return false;
330352
}
331353
delay(1);
332354
}
333355

334-
// Read ASTATUS to latch data and clear AVALID
335-
Adafruit_BusIO_Register astatus_reg =
336-
Adafruit_BusIO_Register(i2c_dev, AS7343_ASTATUS);
356+
// Read ASTATUS to latch data
337357
astatus_reg.read();
338358

339359
// Read all data registers in one burst
@@ -344,6 +364,9 @@ bool Adafruit_AS7343::readAllChannels(uint16_t *readings_buffer) {
344364
return false;
345365
}
346366

367+
// Stop measurement so AVALID clears for next read
368+
stopMeasurement();
369+
347370
return true;
348371
}
349372

0 commit comments

Comments
 (0)