diff options
Diffstat (limited to 'watch-library')
-rw-r--r-- | watch-library/shared/config/hpl_sercom_config.h | 2 | ||||
-rw-r--r-- | watch-library/shared/driver/lis2dw.c | 55 | ||||
-rw-r--r-- | watch-library/shared/driver/lis2dw.h | 22 | ||||
-rw-r--r-- | watch-library/shared/driver/spiflash.h | 2 |
4 files changed, 73 insertions, 8 deletions
diff --git a/watch-library/shared/config/hpl_sercom_config.h b/watch-library/shared/config/hpl_sercom_config.h index 6df4b08e..24525fbe 100644 --- a/watch-library/shared/config/hpl_sercom_config.h +++ b/watch-library/shared/config/hpl_sercom_config.h @@ -172,7 +172,7 @@ // <i> The SPI data transfer rate // <id> spi_master_baud_rate #ifndef CONF_SERCOM_3_SPI_BAUD -#define CONF_SERCOM_3_SPI_BAUD 50000 +#define CONF_SERCOM_3_SPI_BAUD 1000000 #endif // </h> diff --git a/watch-library/shared/driver/lis2dw.c b/watch-library/shared/driver/lis2dw.c index 4d60fcff..b90acc7e 100644 --- a/watch-library/shared/driver/lis2dw.c +++ b/watch-library/shared/driver/lis2dw.c @@ -31,12 +31,18 @@ bool lis2dw_begin(void) { } watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL2, LIS2DW_CTRL2_VAL_BOOT); watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL2, LIS2DW_CTRL2_VAL_SOFT_RESET); - // Start at lowest possible data rate and lowest possible power mode - watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1, LIS2DW_CTRL1_VAL_ODR_LOWEST | LIS2DW_CTRL1_VAL_MODE_LOW_POWER | LIS2DW_CTRL1_VAL_LPMODE_1); // Enable block data update (output registers not updated until MSB and LSB have been read) and address autoincrement watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL2, LIS2DW_CTRL2_VAL_BDU | LIS2DW_CTRL2_VAL_IF_ADD_INC); - // Set range to ±2G - watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6, LIS2DW_CTRL6_VAL_RANGE_2G); + + // Parameters at startup: + // * Data rate 0 (powered down) + // * Low power mode enabled + // * LP mode 1 (12-bit) + // * Bandwidth filtering ODR/2 + // * Low pass filter path + // * ±2g range + // * Low noise mode off + // * FIFO disabled return true; } @@ -91,6 +97,10 @@ lis2dw_reading_t lis2dw_get_raw_reading(void) { return retval; } +uint16_t lis2dw_get_temperature(void) { + return watch_i2c_read16(LIS2DW_ADDRESS, LIS2DW_REG_OUT_TEMP_L); +} + void lis2dw_set_range(lis2dw_range_t range) { uint8_t val = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6) & ~(LIS2DW_RANGE_16_G << 4); uint8_t bits = range << 4; @@ -115,6 +125,41 @@ lis2dw_data_rate_t lis2dw_get_data_rate(void) { return watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) >> 4; } +void lis2dw_set_filter_type(lis2dw_filter_t bwfilter) { + uint8_t val = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6) & ~(LIS2DW_CTRL6_VAL_FDS_HIGH); + uint8_t bits = bwfilter << 3; + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6, val | bits); +} + +lis2dw_filter_t lis2dw_get_filter_type(void) { + uint8_t retval = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6) & (LIS2DW_CTRL6_VAL_FDS_HIGH); + retval >>= 3; + return (lis2dw_filter_t)retval; +} + +void lis2dw_set_bandwidth_filtering(lis2dw_bandwidth_filtering_mode_t bwfilter) { + uint8_t val = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6) & ~(LIS2DW_CTRL6_VAL_BANDWIDTH_DIV20); + uint8_t bits = bwfilter << 6; + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6, val | bits); +} + +lis2dw_bandwidth_filtering_mode_t lis2dw_get_bandwidth_filtering(void) { + uint8_t retval = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6) & (LIS2DW_CTRL6_VAL_BANDWIDTH_DIV20); + retval >>= 6; + return (lis2dw_bandwidth_filtering_mode_t)retval; +} + +void lis2dw_set_mode(lis2dw_mode_t mode) { + uint8_t val = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & ~(0b1100); + uint8_t bits = (mode << 2) & 0b1100; + + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1, val | bits); +} + +lis2dw_mode_t lis2dw_get_mode(void) { + return (lis2dw_mode_t)(watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & 0b1100) >> 2; +} + void lis2dw_set_low_power_mode(lis2dw_low_power_mode_t mode) { uint8_t val = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & ~(0b11); uint8_t bits = mode & 0b11; @@ -148,12 +193,10 @@ inline void lis2dw_enable_fifo(void) { 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(); } diff --git a/watch-library/shared/driver/lis2dw.h b/watch-library/shared/driver/lis2dw.h index f9373e04..c791c461 100644 --- a/watch-library/shared/driver/lis2dw.h +++ b/watch-library/shared/driver/lis2dw.h @@ -88,6 +88,11 @@ typedef enum { } lis2dw_bandwidth_filtering_mode_t; typedef enum { + LIS2DW_FILTER_LOW_PASS = 0, + LIS2DW_FILTER_HIGH_PASS = 1, +} lis2dw_filter_t; + +typedef enum { LIS2DW_RANGE_16_G = 0b11, // +/- 16g LIS2DW_RANGE_8_G = 0b10, // +/- 8g LIS2DW_RANGE_4_G = 0b01, // +/- 4g @@ -185,7 +190,8 @@ typedef enum { #define LIS2DW_CTRL6_VAL_RANGE_4G (LIS2DW_RANGE_4_G << 4) #define LIS2DW_CTRL6_VAL_RANGE_8G (LIS2DW_RANGE_8_G << 4) #define LIS2DW_CTRL6_VAL_RANGE_16G (LIS2DW_RANGE_16_G << 4) -#define LIS2DW_CTRL6_VAL_FDS 0b00001000 +#define LIS2DW_CTRL6_VAL_FDS_LOW (LIS2DW_FILTER_LOW_PASS << 3) +#define LIS2DW_CTRL6_VAL_FDS_HIGH (LIS2DW_FILTER_HIGH_PASS << 3) #define LIS2DW_CTRL6_VAL_LOW_NOISE 0b00000100 #define LIS2DW_REG_OUT_TEMP 0x26 @@ -291,6 +297,8 @@ lis2dw_reading_t lis2dw_get_raw_reading(void); lis2dw_acceleration_measurement_t lis2dw_get_acceleration_measurement(lis2dw_reading_t *out_reading); +uint16_t lis2dw_get_temperature(void); + void lis2dw_set_range(lis2dw_range_t range); lis2dw_range_t lis2dw_get_range(void); @@ -299,6 +307,18 @@ void lis2dw_set_data_rate(lis2dw_data_rate_t dataRate); lis2dw_data_rate_t lis2dw_get_data_rate(void); +void lis2dw_set_filter_type(lis2dw_filter_t filter); + +lis2dw_filter_t lis2dw_get_filter_type(void); + +void lis2dw_set_bandwidth_filtering(lis2dw_bandwidth_filtering_mode_t bwfilter); + +lis2dw_bandwidth_filtering_mode_t lis2dw_get_bandwidth_filtering(void); + +void lis2dw_set_mode(lis2dw_mode_t mode); + +lis2dw_mode_t lis2dw_get_mode(void); + void lis2dw_set_low_power_mode(lis2dw_low_power_mode_t mode); lis2dw_low_power_mode_t lis2dw_get_low_power_mode(void); diff --git a/watch-library/shared/driver/spiflash.h b/watch-library/shared/driver/spiflash.h index 0d8641a7..9b1d00f5 100644 --- a/watch-library/shared/driver/spiflash.h +++ b/watch-library/shared/driver/spiflash.h @@ -33,6 +33,8 @@ #define CMD_FAST_READ_DATA 0x0B #define CMD_SECTOR_ERASE 0x20 // #define CMD_SECTOR_ERASE CMD_READ_JEDEC_ID +#define CMD_CHIP_ERASE 0xC7 +// #define CMD_CHIP_ERASE CMD_READ_JEDEC_ID #define CMD_DISABLE_WRITE 0x04 #define CMD_ENABLE_WRITE 0x06 #define CMD_PAGE_PROGRAM 0x02 |