diff options
author | Alexsander Akers <me@a2.io> | 2022-01-25 15:03:22 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-25 15:03:22 -0500 |
commit | b8de35658ffd78ad8b22f91ccbbd3d63663afda9 (patch) | |
tree | 1f265ddfcc8e5abf0316b81b15f80bf5c70fa7b7 /watch-library/shared/driver | |
parent | 9e24f6c336773c7404139ab4db0eaab2f99504e2 (diff) | |
download | Sensor-Watch-b8de35658ffd78ad8b22f91ccbbd3d63663afda9.tar.gz Sensor-Watch-b8de35658ffd78ad8b22f91ccbbd3d63663afda9.tar.bz2 Sensor-Watch-b8de35658ffd78ad8b22f91ccbbd3d63663afda9.zip |
Sensor Watch Simulator (#35)
* Put something on screen
* Use the 32bit watch_date_time repr to pass from JS
* Implement periodic callbacks
* Clear display on enabling
* Hook up watch_set_led_color() to SVG (green-only)
* Make debug output full-width
* Remove default Emscripten canvas
* Implement sleep and button clicks
* Fix time zone conversion bug in beats-time app
* Clean up warnings
* Fix pin levels
* Set time zone to browser value (if available)
* Add basic backup data saving
* Silence format specifier warnings in both targets
* Remove unnecessary, copied files
* Use RTC pointer to clear callbacks (if available)
* Use preprocessor define to avoid hardcoding MOVEMENT_NUM_FACES
* Change each face to const preprocessor definition
* Remove Intl.DateTimeFormat usage
* Update shell.html title, header
* Add touch start/end event handlers on SVG buttons
* Update shell.html
* Update folder structure (shared, simulator, hardware under watch-library)
* Tease out shared components from watch_slcd
* Clean up simulator watch_slcd.c inline JS calls
* Fix missing newlines at end of file
* Add simulator warnings (except format, unused-paremter)
* Implement remaining watch_rtc functions
* Fix button bug on mouse down then drag out
* Implement remaining watch_slcd functions
* Link keyboard events to buttons (for keys A, L, M)
* Rewrite event handling (mouse, touch, keyboard) in C
* Set explicit text UTF-8 charset in shell.html
* Address PR comments
* Remove unused directories from include paths
Diffstat (limited to 'watch-library/shared/driver')
-rw-r--r-- | watch-library/shared/driver/lis2dh.c | 143 | ||||
-rw-r--r-- | watch-library/shared/driver/lis2dh.h | 222 | ||||
-rw-r--r-- | watch-library/shared/driver/lis2dw.c | 139 | ||||
-rw-r--r-- | watch-library/shared/driver/lis2dw.h | 265 |
4 files changed, 769 insertions, 0 deletions
diff --git a/watch-library/shared/driver/lis2dh.c b/watch-library/shared/driver/lis2dh.c new file mode 100644 index 00000000..20702bf5 --- /dev/null +++ b/watch-library/shared/driver/lis2dh.c @@ -0,0 +1,143 @@ +/* + * MIT License + * + * Copyright (c) 2021 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "lis2dh.h" +#include "watch.h" + +bool lis2dh_begin(void) { + if (lis2dh_get_device_id() != LIS2DH_WHO_AM_I_VAL) { + return false; + } + // Enable all axes, start at lowest possible data rate + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1, LIS2DH_CTRL1_VAL_XEN | + LIS2DH_CTRL1_VAL_YEN | + LIS2DH_CTRL1_VAL_ZEN | + LIS2DH_CTRL1_VAL_ODR_1HZ); + // Set range to ±2G and enable block data update (output registers not updated until MSB and LSB have been read) + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4, LIS2DH_CTRL4_VAL_BDU | LIS2DH_RANGE_2_G); + + return true; +} + +uint8_t lis2dh_get_device_id(void) { + return watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_WHO_AM_I); +} + +bool lis2dh_have_new_data(void) { + uint8_t retval = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_STATUS); + return !!retval; // return true if any bit is set +} + +lis2dh_reading lis2dh_get_raw_reading(void) { + uint8_t buffer[6]; + uint8_t reg = LIS2DH_REG_OUT_X_L | 0x80; // set high bit for consecutive reads + lis2dh_reading retval; + + watch_i2c_send(LIS2DH_ADDRESS, ®, 1); + watch_i2c_receive(LIS2DH_ADDRESS, (uint8_t *)&buffer, 6); + + retval.x = buffer[0]; + retval.x |= ((uint16_t)buffer[1]) << 8; + retval.y = buffer[2]; + retval.y |= ((uint16_t)buffer[3]) << 8; + retval.z = buffer[4]; + retval.z |= ((uint16_t)buffer[5]) << 8; + + return retval; +} + + lis2dh_acceleration_measurement lis2dh_get_acceleration_measurement(lis2dh_reading *out_reading) { + lis2dh_reading reading = lis2dh_get_raw_reading(); + uint8_t range = lis2dh_get_range(); + if (out_reading != NULL) *out_reading = reading; + + // this bit is cribbed from Adafruit's LIS3DH driver; from their notes, the magic number below + // converts from 16-bit lsb to 10-bit and divides by 1k to convert from milli-gs. + // final value is raw_lsb => 10-bit lsb -> milli-gs -> gs + uint8_t lsb_value = 1; + if (range == LIS2DH_RANGE_2_G) lsb_value = 4; + if (range == LIS2DH_RANGE_4_G) lsb_value = 8; + if (range == LIS2DH_RANGE_8_G) lsb_value = 16; + if (range == LIS2DH_RANGE_16_G) lsb_value = 48; + + lis2dh_acceleration_measurement retval; + + retval.x = lsb_value * ((float)reading.x / 64000.0); + retval.y = lsb_value * ((float)reading.y / 64000.0); + retval.z = lsb_value * ((float)reading.z / 64000.0); + + return retval; +} + +void lis2dh_set_range(lis2dh_range_t range) { + uint8_t val = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4) & 0xCF; + uint8_t bits = range << 4; + + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4, val | bits); +} + +lis2dh_range_t lis2dh_get_range(void) { + uint8_t retval = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL4) & 0x30; + retval >>= 4; + return (lis2dh_range_t)retval; +} + + +void lis2dh_set_data_rate(lis2dh_data_rate_t dataRate) { + uint8_t val = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1) & 0x0F; + uint8_t bits = dataRate << 4; + + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1, val | bits); +} + +lis2dh_data_rate_t lis2dh_get_data_rate(void) { + return watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL1) >> 4; +} + +void lis2dh_configure_aoi_int1(lis2dh_interrupt_configuration configuration, uint8_t threshold, uint8_t duration, bool latch) { + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL3, LIS2DH_CTRL3_VAL_I1_AOI1); + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_CFG, configuration); + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_THS, threshold); + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_DUR, duration); + uint8_t val = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL5) & 0xF7; + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL5, val | latch ? LIS2DH_CTRL5_VAL_LIR_INT1 : 0); +} + +lis2dh_interrupt_state lis2dh_get_int1_state(void) { + return (lis2dh_interrupt_state) watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_INT1_SRC); +} + +void lis2dh_configure_aoi_int2(lis2dh_interrupt_configuration configuration, uint8_t threshold, uint8_t duration, bool latch) { + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL6, LIS2DH_CTRL6_VAL_I2_INT2); + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT2_CFG, configuration); + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT2_THS, threshold); + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_INT2_DUR, duration); + uint8_t val = watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL5) & 0xFD; + watch_i2c_write8(LIS2DH_ADDRESS, LIS2DH_REG_CTRL5, val | latch ? LIS2DH_CTRL5_VAL_LIR_INT2 : 0); +} + +lis2dh_interrupt_state lis2dh_get_int2_state(void) { + return (lis2dh_interrupt_state) watch_i2c_read8(LIS2DH_ADDRESS, LIS2DH_REG_INT2_SRC); +} + diff --git a/watch-library/shared/driver/lis2dh.h b/watch-library/shared/driver/lis2dh.h new file mode 100644 index 00000000..68831519 --- /dev/null +++ b/watch-library/shared/driver/lis2dh.h @@ -0,0 +1,222 @@ +/* + * MIT License + * + * Copyright (c) 2021 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef LIS2DH_H +#define LIS2DH_H + +#include <stdbool.h> +#include <stdint.h> + +typedef struct { + int16_t x; + int16_t y; + int16_t z; +} lis2dh_reading; + +typedef struct { + float x; + float y; + float z; +} lis2dh_acceleration_measurement; + +typedef enum { + LIS2DH_RANGE_16_G = 0b11, // +/- 16g + LIS2DH_RANGE_8_G = 0b10, // +/- 8g + LIS2DH_RANGE_4_G = 0b01, // +/- 4g + LIS2DH_RANGE_2_G = 0b00 // +/- 2g (default value) +} lis2dh_range_t; + +typedef enum { + LIS2DH_DATA_RATE_POWERDOWN = 0, + LIS2DH_DATA_RATE_1_HZ = 0b0001, + LIS2DH_DATA_RATE_10_HZ = 0b0010, + LIS2DH_DATA_RATE_25_HZ = 0b0011, + LIS2DH_DATA_RATE_50_HZ = 0b0100, + LIS2DH_DATA_RATE_100_HZ = 0b0101, + LIS2DH_DATA_RATE_200_HZ = 0b0110, + LIS2DH_DATA_RATE_400_HZ = 0b0111, + LIS2DH_DATA_RATE_LP1620HZ = 0b1000, + LIS2DH_DATA_RATE_LP5376HZ = 0b1001, + +} lis2dh_data_rate_t; + +typedef enum { + LIS2DH_INTERRUPT_CONFIGURATION_OR = 0b00000000, + LIS2DH_INTERRUPT_CONFIGURATION_AND = 0b10000000, + LIS2DH_INTERRUPT_CONFIGURATION_6D_MOVEMENT = 0b01000000, + LIS2DH_INTERRUPT_CONFIGURATION_6D_POSITION = 0b11000000, // in 6D mode, these have an alternate meaning: + LIS2DH_INTERRUPT_CONFIGURATION_Z_HIGH_ENABLE = 0b00100000, // Z up enable + LIS2DH_INTERRUPT_CONFIGURATION_Z_LOW_ENABLE = 0b00010000, // Z down enable + LIS2DH_INTERRUPT_CONFIGURATION_Y_HIGH_ENABLE = 0b00001000, // Y up enable + LIS2DH_INTERRUPT_CONFIGURATION_Y_LOW_ENABLE = 0b00000100, // Y down enable + LIS2DH_INTERRUPT_CONFIGURATION_X_HIGH_ENABLE = 0b00000010, // X up enable + LIS2DH_INTERRUPT_CONFIGURATION_X_LOW_ENABLE = 0b00000001, // X down enable +} lis2dh_interrupt_configuration; + +typedef enum { + LIS2DH_INTERRUPT_STATE_ACTIVE = 0b01000000, + LIS2DH_INTERRUPT_STATE_Z_HIGH = 0b00100000, // Z up + LIS2DH_INTERRUPT_STATE_Z_LOW = 0b00010000, // Z down + LIS2DH_INTERRUPT_STATE_Y_HIGH = 0b00001000, // Y up + LIS2DH_INTERRUPT_STATE_Y_LOW = 0b00000100, // Y down + LIS2DH_INTERRUPT_STATE_X_HIGH = 0b00000010, // X up + LIS2DH_INTERRUPT_STATE_X_LOW = 0b00000001, // X down +} lis2dh_interrupt_state; + +bool lis2dh_begin(void); + +uint8_t lis2dh_get_device_id(void); + +bool lis2dh_have_new_data(void); + +lis2dh_reading lis2dh_get_raw_reading(void); + +lis2dh_acceleration_measurement lis2dh_get_acceleration_measurement(lis2dh_reading *out_reading); + +void lis2dh_set_range(lis2dh_range_t range); + +lis2dh_range_t lis2dh_get_range(void); + +void lis2dh_set_data_rate(lis2dh_data_rate_t dataRate); + +lis2dh_data_rate_t lis2dh_get_data_rate(void); + +void lis2dh_configure_aoi_int1(lis2dh_interrupt_configuration configuration, uint8_t threshold, uint8_t duration, bool latch); + +lis2dh_interrupt_state lis2dh_get_int1_state(void); + +void lis2dh_configure_aoi_int2(lis2dh_interrupt_configuration configuration, uint8_t threshold, uint8_t duration, bool latch); + +lis2dh_interrupt_state lis2dh_get_int2_state(void); + +// Assumes SA0 is high; if low, its 0x18 +#define LIS2DH_ADDRESS (0x19) + +#define LIS2DH_REG_STATUS_AUX 0x07 ///< Auxiliary status register +#define LIS2DH_REG_STATUS_AUX_TDA (1 << 2) ///< Temperature data available +#define LIS2DH_REG_STATUS_AUX_TOR (1 << 6) ///< Temperature data overrun + +#define LIS2DH_REG_OUT_TEMP_L 0x0C ///< Temperature data low bit +#define LIS2DH_REG_OUT_TEMP_H 0x0D ///< Temperature data high bit + +#define LIS2DH_REG_INT_COUNTER 0x0E + +#define LIS2DH_REG_WHO_AM_I 0x0F ///< Device identification, will read 0x33 +#define LIS2DH_WHO_AM_I_VAL 0x33 ///< Expected value of the WHO_AM_I register + +#define LIS2DH_REG_TEMP_CFG 0x1F ///< Temperature configuration; 0 to disable, 0xC0 to enable. +#define LIS2DH_TEMP_CFG_VAL_ENABLE 0xC0 ///< Value for LIS2DH_REG_TEMP_CFG that enables temperature sensing. +#define LIS2DH_TEMP_CFG_VAL_DISABLE 0x00 ///< Value for LIS2DH_REG_TEMP_CFG that disables temperature sensing. + +#define LIS2DH_REG_CTRL1 0x20 ///< CTRL_REG1 in the data sheet. +#define LIS2DH_CTRL1_VAL_XEN 0b00000001 ///< Enable X-axis +#define LIS2DH_CTRL1_VAL_YEN 0b00000010 ///< Enable Y-axis +#define LIS2DH_CTRL1_VAL_ZEN 0b00000100 ///< Enable Z-axis +#define LIS2DH_CTRL1_VAL_LPEN 0b00001000 ///< Enable low power mode +#define LIS2DH_CTRL1_VAL_ODR_POWERDOWN 0 ///< Power down +#define LIS2DH_CTRL1_VAL_ODR_1HZ (LIS2DH_DATA_RATE_1_HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_10HZ (LIS2DH_DATA_RATE_10_HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_25HZ (LIS2DH_DATA_RATE_25_HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_50HZ (LIS2DH_DATA_RATE_50_HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_100HZ (LIS2DH_DATA_RATE_100_HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_200HZ (LIS2DH_DATA_RATE_200_HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_400HZ (LIS2DH_DATA_RATE_400_HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_LP1620HZ (LIS2DH_DATA_RATE_LP1620HZ << 4) +#define LIS2DH_CTRL1_VAL_ODR_LP5376HZ (LIS2DH_DATA_RATE_LP5376HZ << 4) + +#define LIS2DH_REG_CTRL2 0x21 + +#define LIS2DH_REG_CTRL3 0x22 +#define LIS2DH_CTRL3_VAL_I1_CLICK 0b10000000 +#define LIS2DH_CTRL3_VAL_I1_AOI1 0b01000000 +#define LIS2DH_CTRL3_VAL_I1_AOI2 0b00100000 +#define LIS2DH_CTRL3_VAL_I1_DRDY1 0b00010000 +#define LIS2DH_CTRL3_VAL_I1_DRDY2 0b00001000 +#define LIS2DH_CTRL3_VAL_I1_WTM 0b00000100 +#define LIS2DH_CTRL3_VAL_I1_OVERRUN 0b00000010 + +#define LIS2DH_REG_CTRL4 0x23 +#define LIS2DH_CTRL4_VAL_BDU 0b10000000 +#define LIS2DH_CTRL4_VAL_BLE 0b01000000 +#define LIS2DH_CTRL4_VAL_RANGE_2G (LIS2DH_RANGE_2_G << 4) +#define LIS2DH_CTRL4_VAL_RANGE_4G (LIS2DH_RANGE_4_G << 4) +#define LIS2DH_CTRL4_VAL_RANGE_8G (LIS2DH_RANGE_8_G << 4) +#define LIS2DH_CTRL4_VAL_RANGE_16G (LIS2DH_RANGE_16_G << 4) +#define LIS2DH_CTRL4_VAL_HR 0b00001000 +#define LIS2DH_CTRL4_VAL_ST0 0b00000000 +#define LIS2DH_CTRL4_VAL_ST1 0b00000000 + +#define LIS2DH_REG_CTRL5 0x24 +#define LIS2DH_CTRL5_VAL_BOOT 0b10000000 +#define LIS2DH_CTRL5_VAL_FIFO_EN 0b01000000 +#define LIS2DH_CTRL5_VAL_LIR_INT1 0b00001000 +#define LIS2DH_CTRL5_VAL_D4D_INT1 0b00000100 +#define LIS2DH_CTRL5_VAL_LIR_INT2 0b00000010 +#define LIS2DH_CTRL5_VAL_D4D_INT2 0b00000001 + +#define LIS2DH_REG_CTRL6 0x25 +#define LIS2DH_CTRL6_VAL_I2_CLICK 0b10000000 +#define LIS2DH_CTRL6_VAL_I2_INT1 0b01000000 +#define LIS2DH_CTRL6_VAL_I2_INT2 0b00100000 +#define LIS2DH_CTRL6_VAL_BOOT_I2 0b00010000 +#define LIS2DH_CTRL6_VAL_P2_ACT 0b00001000 +#define LIS2DH_CTRL6_VAL_H_L_ACTIVE 0b00000000 + +#define LIS2DH_REG_REFERENCE 0x26 + +#define LIS2DH_REG_STATUS 0x27 +#define LIS2DH_STATUS_VAL_ZYXOR 0b10000000 +#define LIS2DH_STATUS_VAL_ZOR 0b01000000 +#define LIS2DH_STATUS_VAL_YOR 0b00100000 +#define LIS2DH_STATUS_VAL_XOR 0b00010000 +#define LIS2DH_STATUS_VAL_ZYXDA 0b00001000 +#define LIS2DH_STATUS_VAL_ZDA 0b00000100 +#define LIS2DH_STATUS_VAL_YDA 0b00000010 +#define LIS2DH_STATUS_VAL_XDA 0b00000001 + +#define LIS2DH_REG_OUT_X_L 0x28 +#define LIS2DH_REG_OUT_X_H 0x29 +#define LIS2DH_REG_OUT_Y_L 0x2A +#define LIS2DH_REG_OUT_Y_H 0x2B +#define LIS2DH_REG_OUT_Z_L 0x2C +#define LIS2DH_REG_OUT_Z_H 0x2D + +#define LIS2DH_REG_FIFO_CTRL 0x2E +#define LIS2DH_REG_FIFO_SRC 0x2F +#define LIS2DH_REG_INT1_CFG 0x30 +#define LIS2DH_REG_INT1_SRC 0x31 +#define LIS2DH_REG_INT1_THS 0x32 +#define LIS2DH_REG_INT1_DUR 0x33 +#define LIS2DH_REG_INT2_CFG 0x34 +#define LIS2DH_REG_INT2_SRC 0x35 +#define LIS2DH_REG_INT2_THS 0x36 +#define LIS2DH_REG_INT2_DUR 0x37 +#define LIS2DH_REG_CLICK_CFG 0x38 +#define LIS2DH_REG_CLICK_SRC 0x39 +#define LIS2DH_REG_CLICK_THS 0x3A +#define LIS2DH_REG_TIME_LIMIT 0x3B +#define LIS2DH_REG_TIME_LATENCY 0x3C +#define LIS2DH_REG_TIME_WINDOW 0x3D + +#endif // LIS2DH_H diff --git a/watch-library/shared/driver/lis2dw.c b/watch-library/shared/driver/lis2dw.c new file mode 100644 index 00000000..6e1ac56d --- /dev/null +++ b/watch-library/shared/driver/lis2dw.c @@ -0,0 +1,139 @@ +/* + * MIT License + * + * Copyright (c) 2021 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "lis2dw.h" +#include "watch.h" + +bool lis2dw_begin(void) { + if (lis2dw_get_device_id() != LIS2DW_WHO_AM_I_VAL) { + return false; + } + 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); + + return true; +} + +uint8_t lis2dw_get_device_id(void) { + return watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WHO_AM_I); +} + +bool lis2dw_have_new_data(void) { + uint8_t retval = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_STATUS); + return retval & LIS2DW_STATUS_VAL_DRDY; +} + +lis2dw_reading 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; + + watch_i2c_send(LIS2DW_ADDRESS, ®, 1); + watch_i2c_receive(LIS2DW_ADDRESS, (uint8_t *)&buffer, 6); + + retval.x = buffer[0]; + retval.x |= ((uint16_t)buffer[1]) << 8; + retval.y = buffer[2]; + retval.y |= ((uint16_t)buffer[3]) << 8; + retval.z = buffer[4]; + retval.z |= ((uint16_t)buffer[5]) << 8; + + return retval; +} + + lis2dw_acceleration_measurement lis2dw_get_acceleration_measurement(lis2dw_reading *out_reading) { + lis2dw_reading reading = lis2dw_get_raw_reading(); + uint8_t range = lis2dw_get_range(); + if (out_reading != NULL) *out_reading = reading; + + // this bit is cribbed from Adafruit's LIS3DH driver; from their notes, the magic number below + // converts from 16-bit lsb to 10-bit and divides by 1k to convert from milli-gs. + // final value is raw_lsb => 10-bit lsb -> milli-gs -> gs + uint8_t lsb_value = 1; + if (range == LIS2DW_RANGE_2_G) lsb_value = 4; + if (range == LIS2DW_RANGE_4_G) lsb_value = 8; + if (range == LIS2DW_RANGE_8_G) lsb_value = 16; + if (range == LIS2DW_RANGE_16_G) lsb_value = 48; + + lis2dw_acceleration_measurement retval; + + retval.x = lsb_value * ((float)reading.x / 64000.0); + retval.y = lsb_value * ((float)reading.y / 64000.0); + retval.z = lsb_value * ((float)reading.z / 64000.0); + + return retval; +} + +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; + + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6, val | bits); +} + +lis2dw_range_t lis2dw_get_range(void) { + uint8_t retval = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL6) & (LIS2DW_RANGE_16_G << 4); + retval >>= 4; + 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; + + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1, val | bits); +} + +lis2dw_data_rate_t lis2dw_get_data_rate(void) { + return watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) >> 4; +} + +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; + + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1, val | bits); +} + +lis2dw_low_power_mode_t lis2dw_get_low_power_mode(void) { + return watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & 0b11; +} + +void lis2dw_set_low_noise_mode(bool on) { + uint8_t val = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & ~(LIS2DW_CTRL6_VAL_LOW_NOISE); + uint8_t bits = on ? LIS2DW_CTRL6_VAL_LOW_NOISE : 0; + + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1, val | bits); +} + +bool lis2dw_get_low_noise_mode(void) { + return (watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL1) & LIS2DW_CTRL6_VAL_LOW_NOISE) != 0; +} diff --git a/watch-library/shared/driver/lis2dw.h b/watch-library/shared/driver/lis2dw.h new file mode 100644 index 00000000..22b4484f --- /dev/null +++ b/watch-library/shared/driver/lis2dw.h @@ -0,0 +1,265 @@ +/* + * MIT License + * + * Copyright (c) 2021 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef LIS2DW_H +#define LIS2DW_H + +#include <stdbool.h> +#include <stdint.h> + +typedef struct { + int16_t x; + int16_t y; + int16_t z; +} lis2dw_reading; + +typedef struct { + float x; + float y; + float z; +} lis2dw_acceleration_measurement; + +typedef enum { + LIS2DW_DATA_RATE_POWERDOWN = 0, + LIS2DW_DATA_RATE_LOWEST = 0b0001, // 12.5 Hz in high performance mode, 1.6 Hz in low power + LIS2DW_DATA_RATE_12_5_HZ = 0b0010, + LIS2DW_DATA_RATE_25_HZ = 0b0011, + LIS2DW_DATA_RATE_50_HZ = 0b0100, + LIS2DW_DATA_RATE_100_HZ = 0b0101, + LIS2DW_DATA_RATE_200_HZ = 0b0110, + LIS2DW_DATA_RATE_HP_400_HZ = 0b0111, + LIS2DW_DATA_RATE_HP_800_HZ = 0b1000, + LIS2DW_DATA_RATE_HP_1600_HZ = 0b1001, + +} lis2dw_data_rate_t; + +typedef enum { + LIS2DW_MODE_LOW_POWER = 0b00, + LIS2DW_MODE_HIGH_PERFORMANCE = 0b01, + LIS2DW_MODE_ON_DEMAND = 0b10, +} lis2dw_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 + LIS2DW_LP_MODE_4 = 0b11, // 14-bit +} lis2dw_low_power_mode_t; + +typedef enum { + LIS2DW_BANDWIDTH_FILTER_DIV2 = 0b00, + LIS2DW_BANDWIDTH_FILTER_DIV4 = 0b01, + LIS2DW_BANDWIDTH_FILTER_DIV10 = 0b10, + LIS2DW_BANDWIDTH_FILTER_DIV20 = 0b11, +} lis2dw_bandwidth_filtering_mode_t; + +typedef enum { + LIS2DW_RANGE_16_G = 0b11, // +/- 16g + LIS2DW_RANGE_8_G = 0b10, // +/- 8g + LIS2DW_RANGE_4_G = 0b01, // +/- 4g + LIS2DW_RANGE_2_G = 0b00 // +/- 2g (default value) +} lis2dw_range_t; + +// Assumes SA0 is high; if low, its 0x18 +#define LIS2DW_ADDRESS (0x19) + +#define LIS2DW_REG_OUT_TEMP_L 0x0D ///< Temperature data low bit +#define LIS2DW_REG_OUT_TEMP_H 0x0E ///< Temperature data high bit + +#define LIS2DW_REG_WHO_AM_I 0x0F ///< Device identification, will read 0x44 +#define LIS2DW_WHO_AM_I_VAL 0x44 ///< Expected value of the WHO_AM_I register + +#define LIS2DW_REG_CTRL1 0x20 ///< CTRL_REG1 in the data sheet. +#define LIS2DW_CTRL1_VAL_ODR_POWERDOWN 0 +#define LIS2DW_CTRL1_VAL_ODR_LOWEST (LIS2DW_DATA_RATE_LOWEST << 4) +#define LIS2DW_CTRL1_VAL_ODR_12_5HZ (LIS2DW_DATA_RATE_12_5_HZ << 4) +#define LIS2DW_CTRL1_VAL_ODR_25HZ (LIS2DW_DATA_RATE_25_HZ << 4) +#define LIS2DW_CTRL1_VAL_ODR_50HZ (LIS2DW_DATA_RATE_50_HZ << 4) +#define LIS2DW_CTRL1_VAL_ODR_100HZ (LIS2DW_DATA_RATE_100_HZ << 4) +#define LIS2DW_CTRL1_VAL_ODR_200HZ (LIS2DW_DATA_RATE_200_HZ << 4) +#define LIS2DW_CTRL1_VAL_ODR_HP_400_HZ (LIS2DW_DATA_RATE_HP_400_HZ << 4) +#define LIS2DW_CTRL1_VAL_ODR_HP_800_HZ (LIS2DW_DATA_RATE_HP_800_HZ << 4) +#define LIS2DW_CTRL1_VAL_ODR_HP_1600_HZ (LIS2DW_DATA_RATE_HP_1600_HZ << 4) +#define LIS2DW_CTRL1_VAL_MODE_LOW_POWER (LIS2DW_MODE_LOW_POWER << 2) +#define LIS2DW_CTRL1_VAL_MODE_HIGH_PERFORMANCE (LIS2DW_MODE_HIGH_PERFORMANCE << 2) +#define LIS2DW_CTRL1_VAL_MODE_ON_DEMAND (LIS2DW_MODE_ON_DEMAND << 2) +#define LIS2DW_CTRL1_VAL_LPMODE_1 (LIS2DW_LP_MODE_1 << 0) +#define LIS2DW_CTRL1_VAL_LPMODE_2 (LIS2DW_LP_MODE_2 << 0) +#define LIS2DW_CTRL1_VAL_LPMODE_3 (LIS2DW_LP_MODE_3 << 0) +#define LIS2DW_CTRL1_VAL_LPMODE_4 (LIS2DW_LP_MODE_4 << 0) + +#define LIS2DW_REG_CTRL2 0x21 +#define LIS2DW_CTRL2_VAL_BOOT 0b10000000 +#define LIS2DW_CTRL2_VAL_SOFT_RESET 0b01000000 +#define LIS2DW_CTRL2_VAL_CS_PU_DISC 0b00010000 +#define LIS2DW_CTRL2_VAL_BDU 0b00001000 +#define LIS2DW_CTRL2_VAL_IF_ADD_INC 0b00000100 + +#define LIS2DW_REG_CTRL3 0x22 +#define LIS2DW_CTRL4_VAL_SELF_TEST_POS 0b10000000 +#define LIS2DW_CTRL4_VAL_SELF_TEST_NEG 0b01000000 +#define LIS2DW_CTRL3_VAL_PP_OD 0b00100000 +#define LIS2DW_CTRL3_VAL_LIR 0b00010000 +#define LIS2DW_CTRL3_VAL_H_L_ACTIVE 0b00001000 +#define LIS2DW_CTRL3_VAL_SLP_MODE_SEL 0b00000010 +#define LIS2DW_CTRL3_VAL_SLP_MODE_1 0b00000001 + +#define LIS2DW_REG_CTRL4 0x23 +#define LIS2DW_CTRL4_INT1_6D 0b10000000 +#define LIS2DW_CTRL4_INT1_SINGLE_TAP 0b01000000 +#define LIS2DW_CTRL4_INT1_WU 0b00100000 +#define LIS2DW_CTRL4_INT1_FF 0b00010000 +#define LIS2DW_CTRL4_INT1_TAP 0b00001000 +#define LIS2DW_CTRL4_INT1_DIFF5 0b00000100 +#define LIS2DW_CTRL4_INT1_FTH 0b00000010 +#define LIS2DW_CTRL4_INT1_DRDY 0b00000001 + +#define LIS2DW_REG_CTRL5 0x24 +#define LIS2DW_CTRL5_INT2_SLEEP_STATE 0b10000000 +#define LIS2DW_CTRL5_INT2_SLEEP_CHG 0b01000000 +#define LIS2DW_CTRL5_INT2_BOOT 0b00100000 +#define LIS2DW_CTRL5_INT2_DRDY_T 0b00010000 +#define LIS2DW_CTRL5_INT2_OVR 0b00001000 +#define LIS2DW_CTRL5_INT2_DIFF5 0b00000100 +#define LIS2DW_CTRL5_INT2_FTH 0b00000010 +#define LIS2DW_CTRL5_INT2_DRDY 0b00000001 + +#define LIS2DW_REG_CTRL6 0x25 +#define LIS2DW_CTRL6_VAL_BANDWIDTH_DIV2 (LIS2DW_BANDWIDTH_FILTER_DIV2 << 6) +#define LIS2DW_CTRL6_VAL_BANDWIDTH_DIV4 (LIS2DW_BANDWIDTH_FILTER_DIV4 << 6) +#define LIS2DW_CTRL6_VAL_BANDWIDTH_DIV10 (LIS2DW_BANDWIDTH_FILTER_DIV10 << 6) +#define LIS2DW_CTRL6_VAL_BANDWIDTH_DIV20 (LIS2DW_BANDWIDTH_FILTER_DIV20 << 6) +#define LIS2DW_CTRL6_VAL_RANGE_2G (LIS2DW_RANGE_2_G << 4) +#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_LOW_NOISE 0b00000100 + +#define LIS2DW_REG_OUT_TEMP 0x26 + +#define LIS2DW_REG_STATUS 0x27 +#define LIS2DW_STATUS_VAL_FIFO_THS 0b10000000 +#define LIS2DW_STATUS_VAL_WU_IA 0b01000000 +#define LIS2DW_STATUS_VAL_SLEEP_STATE 0b00100000 +#define LIS2DW_STATUS_VAL_DOUBLE_TAP 0b00010000 +#define LIS2DW_STATUS_VAL_SINGLE_TAP 0b00001000 +#define LIS2DW_STATUS_VAL_6D_IA 0b00000100 +#define LIS2DW_STATUS_VAL_FF_IA 0b00000010 +#define LIS2DW_STATUS_VAL_DRDY 0b00000001 + +#define LIS2DW_REG_OUT_X_L 0x28 +#define LIS2DW_REG_OUT_X_H 0x29 +#define LIS2DW_REG_OUT_Y_L 0x2A +#define LIS2DW_REG_OUT_Y_H 0x2B +#define LIS2DW_REG_OUT_Z_L 0x2C +#define LIS2DW_REG_OUT_Z_H 0x2D + +#define LIS2DW_REG_FIFO_CTRL 0x2E +#define LIS2DW_REG_FIFO_SRC 0x2F + +#define LIS2DW_REG_TAP_THS_X 0x30 +#define LIS2DW_REG_TAP_THS_Y 0x31 +#define LIS2DW_REG_TAP_THS_Z 0x32 +#define LIS2DW_REG_INT1_DUR 0x33 +#define LIS2DW_REG_WAKE_UP_THS 0x34 +#define LIS2DW_REG_WAKE_UP_DUR 0x35 +#define LIS2DW_REG_FREE_FALL 0x36 +#define LIS2DW_REG_STATUS_DUP 0x37 + +#define LIS2DW_REG_WAKE_UP_SRC 0x38 +#define LIS2DW_WAKE_UP_SRC_VAL_FF_IA 0b00100000 +#define LIS2DW_WAKE_UP_SRC_VAL_SLEEP_STATE_IA 0b00010000 +#define LIS2DW_WAKE_UP_SRC_VAL_WU_IA 0b00001000 +#define LIS2DW_WAKE_UP_SRC_VAL_X_WU 0b00000100 +#define LIS2DW_WAKE_UP_SRC_VAL_Y_WU 0b00000010 +#define LIS2DW_WAKE_UP_SRC_VAL_Z_WU 0b00000001 + +#define LIS2DW_REG_TAP_SRC 0x39 +#define LIS2DW_TAP_SRC_VAL_TAP_IA 0b01000000 +#define LIS2DW_TAP_SRC_VAL_SINGLE_TAP 0b00100000 +#define LIS2DW_TAP_SRC_VAL_DOUBLE_TAP 0b00010000 +#define LIS2DW_TAP_SRC_VAL_TAP_SIGN 0b00001000 +#define LIS2DW_TAP_SRC_VAL_X_TAP 0b00000100 +#define LIS2DW_TAP_SRC_VAL_Y_TAP 0b00000010 +#define LIS2DW_TAP_SRC_VAL_Z_TAP 0b00000001 + +#define LIS2DW_REG_SIXD_SRC 0x3A +#define LIS2DW_WAKE_UP_SRC_VAL_6D_IA 0b01000000 +#define LIS2DW_WAKE_UP_SRC_VAL_ZH 0b00100000 +#define LIS2DW_WAKE_UP_SRC_VAL_ZL 0b00010000 +#define LIS2DW_WAKE_UP_SRC_VAL_YH 0b00001000 +#define LIS2DW_WAKE_UP_SRC_VAL_YL 0b00000100 +#define LIS2DW_WAKE_UP_SRC_VAL_XH 0b00000010 +#define LIS2DW_WAKE_UP_SRC_VAL_XL 0b00000001 + +#define LIS2DW_REG_ALL_INT_SRC 0x3B +#define LIS2DW_REG_ALL_INT_SRC_SLEEP_CHANGE_IA 0b00100000 +#define LIS2DW_REG_ALL_INT_SRC_6D_IA 0b00010000 +#define LIS2DW_REG_ALL_INT_SRC_DOUBLE_TAP 0b00001000 +#define LIS2DW_REG_ALL_INT_SRC_SINGLE_TAP 0b00000100 +#define LIS2DW_REG_ALL_INT_SRC_WU_IA 0b00000010 +#define LIS2DW_REG_ALL_INT_SRC_FF_IA 0b00000001 + +#define LIS2DW_REG_X_OFS_USR 0x3C +#define LIS2DW_REG_Y_OFS_USR 0x3D +#define LIS2DW_REG_Z_OFS_USR 0x3E + +#define LIS2DW_REG_CTRL7 0x3F +#define LIS2DW_CTRL7_VAL_DRDY_PULSED 0b10000000 +#define LIS2DW_CTRL7_VAL_INT2_ON_INT1 0b01000000 +#define LIS2DW_CTRL7_VAL_INTERRUPTS_ENABLE 0b00100000 +#define LIS2DW_CTRL7_VAL_USR_OFF_ON_OUT 0b00010000 +#define LIS2DW_CTRL7_VAL_USR_OFF_ON_WU 0b00001000 +#define LIS2DW_CTRL7_VAL_USR_OFF_W 0b00000100 +#define LIS2DW_CTRL7_VAL_HP_REF_MODE 0b00000010 +#define LIS2DW_CTRL7_VAL_LPASS_ON6D 0b00000001 + +bool lis2dw_begin(void); + +uint8_t lis2dw_get_device_id(void); + +bool lis2dw_have_new_data(void); + +lis2dw_reading lis2dw_get_raw_reading(void); + +lis2dw_acceleration_measurement lis2dw_get_acceleration_measurement(lis2dw_reading *out_reading); + +void lis2dw_set_range(lis2dw_range_t range); + +lis2dw_range_t lis2dw_get_range(void); + +void lis2dw_set_data_rate(lis2dw_data_rate_t dataRate); + +lis2dw_data_rate_t lis2dw_get_data_rate(void); + +void lis2dw_set_low_power_mode(lis2dw_low_power_mode_t mode); + +lis2dw_low_power_mode_t lis2dw_get_low_power_mode(void); + +void lis2dw_set_low_noise_mode(bool on); + +bool lis2dw_get_low_noise_mode(void); + +#endif // LIS2DW_H |