From eb9206c8a14e52c54d5c9cc5657d5d052522e083 Mon Sep 17 00:00:00 2001 From: joeycastillo Date: Wed, 9 Oct 2024 01:09:48 -0400 Subject: [PATCH] separate out interrupt config vs enable --- watch-faces/demo/accel_interrupt_count_face.c | 7 ++- watch-library/shared/driver/lis2dw.c | 46 +++++++++++++------ watch-library/shared/driver/lis2dw.h | 16 ++++++- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/watch-faces/demo/accel_interrupt_count_face.c b/watch-faces/demo/accel_interrupt_count_face.c index e7b61ff..5f8d145 100644 --- a/watch-faces/demo/accel_interrupt_count_face.c +++ b/watch-faces/demo/accel_interrupt_count_face.c @@ -54,7 +54,10 @@ static void _accel_interrupt_count_face_update_display(accel_interrupt_count_sta } static void _accel_interrupt_count_face_configure_threshold(uint8_t threshold) { - lis2dw_configure_wakeup_int1(threshold, false, true); + lis2dw_enable_sleep(); + lis2dw_configure_wakeup_threshold(threshold); + lis2dw_configure_int1(LIS2DW_CTRL4_INT1_WU); + lis2dw_enable_interrupts(); } void accel_interrupt_count_face_setup(uint8_t watch_face_index, void ** context_ptr) { @@ -104,7 +107,7 @@ bool accel_interrupt_count_face_loop(movement_event_t event, void *context) { } break; case EVENT_ALARM_BUTTON_UP: - lis2dw_configure_wakeup_int1(state->threshold, false, true); + lis2dw_configure_wakeup_threshold(state->new_threshold); state->threshold = state->new_threshold; state->is_setting = false; break; diff --git a/watch-library/shared/driver/lis2dw.c b/watch-library/shared/driver/lis2dw.c index b392702..7dbe8a4 100644 --- a/watch-library/shared/driver/lis2dw.c +++ b/watch-library/shared/driver/lis2dw.c @@ -208,27 +208,45 @@ void lis2dw_clear_fifo(void) { watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_CTRL, LIS2DW_FIFO_CTRL_MODE_COLLECT_AND_STOP | LIS2DW_FIFO_CTRL_FTH); } -void lis2dw_configure_wakeup_int1(uint8_t threshold, bool latch, bool active_state) { - uint8_t configuration; +void lis2dw_enable_sleep(void) { + uint8_t configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS); + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS, configuration | LIS2DW_WAKE_UP_THS_VAL_SLEEP_ON); +} + +void lis2dw_disable_sleep(void) { + uint8_t configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS); + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS, configuration & ~LIS2DW_WAKE_UP_THS_VAL_SLEEP_ON); +} + +void lis2dw_enable_tap_detection(void) { + uint8_t configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS); + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS, configuration | LIS2DW_WAKE_UP_THS_VAL_TAP_EVENT_ENABLED); +} - // enable wakeup interrupt on INT1 pin - configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL4_INT1); - watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL4_INT1, configuration | LIS2DW_CTRL4_INT1_WU); +void lis2dw_disable_tap_detection(void) { + uint8_t configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS); + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS, configuration & ~LIS2DW_WAKE_UP_THS_VAL_TAP_EVENT_ENABLED); +} - // set threshold - watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS, threshold | LIS2DW_WAKE_UP_THS_VAL_SLEEP_ON); - watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_INT1_DUR, 0b01111111); +void lis2dw_configure_wakeup_threshold(uint8_t threshold) { + uint8_t configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS) & 0b11000000; + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS, configuration | threshold); +} - configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL3) & ~(LIS2DW_CTRL3_VAL_LIR); - if (!active_state) configuration |= LIS2DW_CTRL3_VAL_H_L_ACTIVE; - if (latch) configuration |= LIS2DW_CTRL3_VAL_LIR; - watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL3, configuration); +void lis2dw_configure_int1(uint8_t sources) { + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL4_INT1, sources); +} - // enable interrupts - configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL7); +void lis2dw_enable_interrupts(void) { + uint8_t configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL7); watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL7, configuration | LIS2DW_CTRL7_VAL_INTERRUPTS_ENABLE); } +void lis2dw_disable_interrupts(void) { + uint8_t configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL7); + watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL7, configuration & ~LIS2DW_CTRL7_VAL_INTERRUPTS_ENABLE); +} + lis2dw_wakeup_source_t lis2dw_get_wakeup_source() { return (lis2dw_wakeup_source_t) watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_SRC); } diff --git a/watch-library/shared/driver/lis2dw.h b/watch-library/shared/driver/lis2dw.h index 9815c2c..d6379c0 100644 --- a/watch-library/shared/driver/lis2dw.h +++ b/watch-library/shared/driver/lis2dw.h @@ -335,7 +335,21 @@ bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data); void lis2dw_clear_fifo(void); -void lis2dw_configure_wakeup_int1(uint8_t threshold, bool latch, bool active_state); +void lis2dw_enable_sleep(void); + +void lis2dw_disable_sleep(void); + +void lis2dw_enable_tap_detection(void); + +void lis2dw_disable_tap_detection(void); + +void lis2dw_configure_wakeup_threshold(uint8_t threshold); + +void lis2dw_configure_int1(uint8_t sources); + +void lis2dw_enable_interrupts(void); + +void lis2dw_disable_interrupts(void); lis2dw_interrupt_source_t lis2dw_get_interrupt_source(void); -- 2.39.5