Merge pull request #229 from rieck/lis2dw_fifo_fix

Clamp the lis2dw FIFO count and read it in one burst
This commit is contained in:
voloved
2026-08-07 06:37:45 -04:00
committed by GitHub
4 changed files with 19 additions and 15 deletions

View File

@ -448,13 +448,13 @@ static void start_reading(accelerometer_data_acquisition_state_t *state) {
state->records[state->pos++] = record; state->records[state->pos++] = record;
lis2dw_fifo_t fifo; lis2dw_fifo_t fifo;
lis2dw_read_fifo(&fifo, LIS2DW_FIFO_TIMEOUT); // dump the fifo, this starts a fresh round of data in continue_reading lis2dw_read_fifo(&fifo); // dump the fifo, this starts a fresh round of data in continue_reading
} }
static void continue_reading(accelerometer_data_acquisition_state_t *state) { static void continue_reading(accelerometer_data_acquisition_state_t *state) {
printf("Continue reading\n"); printf("Continue reading\n");
lis2dw_fifo_t fifo; lis2dw_fifo_t fifo;
lis2dw_read_fifo(&fifo, LIS2DW_FIFO_TIMEOUT); lis2dw_read_fifo(&fifo);
fifo.count = min(fifo.count, 25); // hacky, but we need a consistent data rate; if we got a 26th data point, chuck it. fifo.count = min(fifo.count, 25); // hacky, but we need a consistent data rate; if we got a 26th data point, chuck it.
uint8_t offset = 4 * (25 - fifo.count); // also hacky: we're sometimes short at the start. align to beginning of next second. uint8_t offset = 4 * (25 - fifo.count); // also hacky: we're sometimes short at the start. align to beginning of next second.

View File

@ -417,7 +417,7 @@ static void _monitor_update(lis2dw_monitor_state_t *state)
lis2dw_fifo_t fifo; lis2dw_fifo_t fifo;
float x = 0, y = 0, z = 0; float x = 0, y = 0, z = 0;
lis2dw_read_fifo(&fifo, LIS2DW_FIFO_TIMEOUT / DISPLAY_FREQUENCY); lis2dw_read_fifo(&fifo);
if (fifo.count == 0) { if (fifo.count == 0) {
return; return;
} }

View File

@ -277,26 +277,32 @@ inline void lis2dw_disable_fifo(void) {
#endif #endif
} }
bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data, uint32_t timeout) { bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data) {
// timeout is in terms of 1/RTC_CNT_HZ seconds (likely 128 timeouts is one second)
#ifdef I2C_SERCOM #ifdef I2C_SERCOM
uint8_t temp = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_SAMPLE); uint8_t temp = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_SAMPLE);
bool overrun = !!(temp & LIS2DW_FIFO_SAMPLE_OVERRUN); bool overrun = !!(temp & LIS2DW_FIFO_SAMPLE_OVERRUN);
fifo_data->count = temp & LIS2DW_FIFO_SAMPLE_COUNT; fifo_data->count = temp & LIS2DW_FIFO_SAMPLE_COUNT;
rtc_counter_t timeout_counter = watch_rtc_get_counter() + timeout; /* The count field can report up to 63, but readings[] only holds 32.
for(int i = 0; i < fifo_data->count; i++) { Clamp so a glitched or overrun count can't overflow the buffer. */
if (watch_rtc_get_counter() > timeout_counter) { if (fifo_data->count > 32) {
break; fifo_data->count = 32;
} }
fifo_data->readings[i] = lis2dw_get_raw_reading();
/* Read the whole FIFO in one burst. With address auto-increment (IF_ADD_INC)
the pointer walks OUT_X_L..OUT_Z_H and rolls over into the next FIFO slot,
so count*6 bytes returns the samples back-to-back. On this little-endian
target the raw bytes map straight onto lis2dw_reading_t (x, y, z int16). */
if (fifo_data->count > 0) {
uint8_t reg = LIS2DW_REG_OUT_X_L | 0x80;
watch_i2c_send(LIS2DW_ADDRESS, &reg, 1);
watch_i2c_receive(LIS2DW_ADDRESS, (uint8_t *) fifo_data->readings, fifo_data->count * 6);
} }
return overrun; return overrun;
#else #else
(void) fifo_data; (void) fifo_data;
(void) timeout;
return false; return false;
#endif #endif
} }

View File

@ -301,8 +301,6 @@ typedef enum {
#define LIS2DW_CTRL7_VAL_HP_REF_MODE 0b00000010 #define LIS2DW_CTRL7_VAL_HP_REF_MODE 0b00000010
#define LIS2DW_CTRL7_VAL_LPASS_ON6D 0b00000001 #define LIS2DW_CTRL7_VAL_LPASS_ON6D 0b00000001
#define LIS2DW_FIFO_TIMEOUT 100 // timeout is in terms of 1/RTC_CNT_HZ seconds (likely 128 timeouts is one second)
bool lis2dw_begin(void); bool lis2dw_begin(void);
uint8_t lis2dw_get_device_id(void); uint8_t lis2dw_get_device_id(void);
@ -347,7 +345,7 @@ void lis2dw_enable_fifo(void);
void lis2dw_disable_fifo(void); void lis2dw_disable_fifo(void);
bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data, uint32_t timeout); bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data);
void lis2dw_clear_fifo(void); void lis2dw_clear_fifo(void);