diff options
author | Joey Castillo <joeycastillo@utexas.edu> | 2022-02-17 16:36:21 -0500 |
---|---|---|
committer | Joey Castillo <joeycastillo@utexas.edu> | 2022-02-17 16:36:21 -0500 |
commit | 0c59c972e7296ac8c23049e713da9350a9af39a5 (patch) | |
tree | ee4d59f6269a3012636ea7477d790451e7fb78fd /watch-library/shared/driver | |
parent | deda65b3136b740a9504d80bacc2714978964119 (diff) | |
download | Sensor-Watch-0c59c972e7296ac8c23049e713da9350a9af39a5.tar.gz Sensor-Watch-0c59c972e7296ac8c23049e713da9350a9af39a5.tar.bz2 Sensor-Watch-0c59c972e7296ac8c23049e713da9350a9af39a5.zip |
lis2dw: add FIFO support
Diffstat (limited to 'watch-library/shared/driver')
-rw-r--r-- | watch-library/shared/driver/lis2dw.c | 39 | ||||
-rw-r--r-- | watch-library/shared/driver/lis2dw.h | 41 |
2 files changed, 69 insertions, 11 deletions
diff --git a/watch-library/shared/driver/lis2dw.c b/watch-library/shared/driver/lis2dw.c index 6e1ac56d..fce266b3 100644 --- a/watch-library/shared/driver/lis2dw.c +++ b/watch-library/shared/driver/lis2dw.c @@ -50,10 +50,10 @@ bool lis2dw_have_new_data(void) { return retval & LIS2DW_STATUS_VAL_DRDY; } -lis2dw_reading lis2dw_get_raw_reading(void) { +lis2dw_reading_t lis2dw_get_raw_reading(void) { uint8_t buffer[6]; uint8_t reg = LIS2DW_REG_OUT_X_L | 0x80; // set high bit for consecutive reads - lis2dw_reading retval; + lis2dw_reading_t retval; watch_i2c_send(LIS2DW_ADDRESS, ®, 1); watch_i2c_receive(LIS2DW_ADDRESS, (uint8_t *)&buffer, 6); @@ -68,8 +68,8 @@ lis2dw_reading lis2dw_get_raw_reading(void) { return retval; } - lis2dw_acceleration_measurement lis2dw_get_acceleration_measurement(lis2dw_reading *out_reading) { - lis2dw_reading reading = lis2dw_get_raw_reading(); + lis2dw_acceleration_measurement_t lis2dw_get_acceleration_measurement(lis2dw_reading_t *out_reading) { + lis2dw_reading_t reading = lis2dw_get_raw_reading(); uint8_t range = lis2dw_get_range(); if (out_reading != NULL) *out_reading = reading; @@ -82,7 +82,7 @@ lis2dw_reading lis2dw_get_raw_reading(void) { if (range == LIS2DW_RANGE_8_G) lsb_value = 16; if (range == LIS2DW_RANGE_16_G) lsb_value = 48; - lis2dw_acceleration_measurement retval; + lis2dw_acceleration_measurement_t retval; retval.x = lsb_value * ((float)reading.x / 64000.0); retval.y = lsb_value * ((float)reading.y / 64000.0); @@ -104,7 +104,6 @@ lis2dw_range_t lis2dw_get_range(void) { return (lis2dw_range_t)retval; } - void lis2dw_set_data_rate(lis2dw_data_rate_t dataRate) { uint8_t val = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & ~(0b1111 << 4); uint8_t bits = dataRate << 4; @@ -137,3 +136,31 @@ void lis2dw_set_low_noise_mode(bool on) { bool lis2dw_get_low_noise_mode(void) { return (watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & LIS2DW_CTRL6_VAL_LOW_NOISE) != 0; } + +inline void lis2dw_disable_fifo(void) { + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_CTRL, LIS2DW_FIFO_CTRL_MODE_OFF); +} + +inline void lis2dw_enable_fifo(void) { + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_CTRL, LIS2DW_FIFO_CTRL_MODE_COLLECT_AND_STOP | LIS2DW_FIFO_CTRL_FTH); +} + +bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data) { + uint8_t temp = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_SAMPLE); + bool overrun = !!(temp & LIS2DW_FIFO_SAMPLE_OVERRUN); + uint8_t buffer[6]; + + fifo_data->count = temp & LIS2DW_FIFO_SAMPLE_COUNT; + + for(int i = 0; i < fifo_data->count; i++) { + watch_i2c_receive(LIS2DW_ADDRESS, (uint8_t *)&buffer, 6); + fifo_data->readings[i] = lis2dw_get_raw_reading(); + } + + return overrun; +} + +void lis2dw_clear_fifo(void) { + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_CTRL, LIS2DW_FIFO_CTRL_MODE_OFF); + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_CTRL, LIS2DW_FIFO_CTRL_MODE_COLLECT_AND_STOP | LIS2DW_FIFO_CTRL_FTH); +} diff --git a/watch-library/shared/driver/lis2dw.h b/watch-library/shared/driver/lis2dw.h index 22b4484f..41611172 100644 --- a/watch-library/shared/driver/lis2dw.h +++ b/watch-library/shared/driver/lis2dw.h @@ -32,13 +32,18 @@ typedef struct { int16_t x; int16_t y; int16_t z; -} lis2dw_reading; +} lis2dw_reading_t; typedef struct { float x; float y; float z; -} lis2dw_acceleration_measurement; +} lis2dw_acceleration_measurement_t; + +typedef struct { + int8_t count; + lis2dw_reading_t readings[32]; +} lis2dw_fifo_t; typedef enum { LIS2DW_DATA_RATE_POWERDOWN = 0, @@ -61,6 +66,14 @@ typedef enum { } lis2dw_mode_t; typedef enum { + LIS2DW_FIFO_MODE_OFF = 0b000, + LIS2DW_FIFO_MODE_COLLECT_AND_STOP = 0b001, + LIS2DW_FIFO_MODE_CONTINUOUS_TO_FIFO = 0b011, + LIS2DW_FIFO_MODE_BYPASS_TO_CONTINUOUS = 0b100, + LIS2DW_FIFO_MODE_COLLECT_CONTINUOUS = 0b110, +} lis2dw_fifo_mode_t; + +typedef enum { LIS2DW_LP_MODE_1 = 0b00, // 12-bit LIS2DW_LP_MODE_2 = 0b01, // 14-bit LIS2DW_LP_MODE_3 = 0b10, // 14-bit @@ -177,7 +190,17 @@ typedef enum { #define LIS2DW_REG_OUT_Z_H 0x2D #define LIS2DW_REG_FIFO_CTRL 0x2E -#define LIS2DW_REG_FIFO_SRC 0x2F +#define LIS2DW_FIFO_CTRL_MODE_OFF (LIS2DW_FIFO_MODE_OFF << 5) +#define LIS2DW_FIFO_CTRL_MODE_COLLECT_AND_STOP (LIS2DW_FIFO_MODE_COLLECT_AND_STOP << 5) +#define LIS2DW_FIFO_CTRL_MODE_CONTINUOUS_TO_FIFO (LIS2DW_FIFO_MODE_CONTINUOUS_TO_FIFO << 5) +#define LIS2DW_FIFO_CTRL_MODE_BYPASS_TO_CONTINUOUS (LIS2DW_FIFO_MODE_BYPASS_TO_CONTINUOUS << 5) +#define LIS2DW_FIFO_CTRL_MODE_COLLECT_CONTINUOUS (LIS2DW_FIFO_MODE_COLLECT_CONTINUOUS << 5) +#define LIS2DW_FIFO_CTRL_FTH (0b00011111) + +#define LIS2DW_REG_FIFO_SAMPLE 0x2F +#define LIS2DW_FIFO_SAMPLE_THRESHOLD (1 << 7) +#define LIS2DW_FIFO_SAMPLE_OVERRUN (1 << 6) +#define LIS2DW_FIFO_SAMPLE_COUNT (0b00111111) #define LIS2DW_REG_TAP_THS_X 0x30 #define LIS2DW_REG_TAP_THS_Y 0x31 @@ -242,9 +265,9 @@ uint8_t lis2dw_get_device_id(void); bool lis2dw_have_new_data(void); -lis2dw_reading lis2dw_get_raw_reading(void); +lis2dw_reading_t lis2dw_get_raw_reading(void); -lis2dw_acceleration_measurement lis2dw_get_acceleration_measurement(lis2dw_reading *out_reading); +lis2dw_acceleration_measurement_t lis2dw_get_acceleration_measurement(lis2dw_reading_t *out_reading); void lis2dw_set_range(lis2dw_range_t range); @@ -262,4 +285,12 @@ void lis2dw_set_low_noise_mode(bool on); bool lis2dw_get_low_noise_mode(void); +void lis2dw_disable_fifo(void); + +void lis2dw_enable_fifo(void); + +bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data); + +void lis2dw_clear_fifo(void); + #endif // LIS2DW_H |