From 68bdaee1cc301a2aca1849f38d2596debe7d67d1 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Wed, 31 Mar 2021 19:47:27 +0200 Subject: First integration of the motion sensor (bma 421) : step counting + wake on wrist rotation + app to see the value of the 3 axis in "real time". --- src/drivers/Bma421.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/drivers/Bma421.cpp (limited to 'src/drivers/Bma421.cpp') diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp new file mode 100644 index 00000000..f1dd934a --- /dev/null +++ b/src/drivers/Bma421.cpp @@ -0,0 +1,122 @@ +#include +#include +#include "Bma421.h" +#include "TwiMaster.h" +#include + +using namespace Pinetime::Drivers; + +int8_t user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr) { + auto bma421 = static_cast(intf_ptr); + bma421->Read(reg_addr, reg_data, length); + return 0; +} + +int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr) { + auto bma421 = static_cast(intf_ptr); + bma421->Write(reg_addr, reg_data, length); + return 0; +} + +void user_delay(uint32_t period_us, void *intf_ptr) { + nrf_delay_us(period_us); +} + +Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, twiAddress{twiAddress} { + bma.intf = BMA4_I2C_INTF; + bma.bus_read = user_i2c_read; + bma.bus_write = user_i2c_write; + bma.variant = BMA42X_VARIANT; + bma.intf_ptr = this; + bma.delay_us = user_delay; + bma.read_write_len = 8; + + accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ; + accel_conf.range = BMA4_ACCEL_RANGE_2G; + accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4; + accel_conf.perf_mode = BMA4_CIC_AVG_MODE; + + +} + +void Bma421::Init() { + auto ret = bma4_soft_reset(&bma); + ASSERT(ret == BMA4_OK); + + nrf_delay_ms(1); + + ret = bma423_init(&bma); + NRF_LOG_INFO("RESET : %d", ret); + + //ret = bma423_init(&bma); + //NRF_LOG_INFO("ID : %d", bma.chip_id); + ASSERT(ret == BMA4_OK); + + ret = bma423_write_config_file(&bma); + ASSERT(ret == BMA4_OK); + + bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma); + struct bma4_int_pin_config int_pin_config; + int_pin_config.edge_ctrl = BMA4_LEVEL_TRIGGER; + int_pin_config.lvl = BMA4_ACTIVE_LOW; + int_pin_config.od = BMA4_PUSH_PULL; + int_pin_config.output_en = BMA4_OUTPUT_ENABLE; + int_pin_config.input_en = BMA4_INPUT_DISABLE; + bma4_set_int_pin_config(&int_pin_config, BMA4_INTR1_MAP, &bma); + + //ret = bma423_feature_enable(BMA423_STEP_CNTR | BMA423_STEP_ACT | BMA423_WRIST_WEAR | BMA423_SINGLE_TAP | BMA423_DOUBLE_TAP, 1, &bma); + ret = bma423_feature_enable(0xff, 1, &bma); + ASSERT(ret == BMA4_OK); + + //ret = bma423_map_interrupt(BMA4_INTR1_MAP, BMA423_SINGLE_TAP_INT | BMA423_STEP_CNTR_INT | BMA423_ACTIVITY_INT | BMA423_WRIST_WEAR_INT | BMA423_DOUBLE_TAP_INT | BMA423_ANY_MOT_INT | BMA423_NO_MOT_INT| BMA423_ERROR_INT, 1,&bma); + ret = bma423_map_interrupt(BMA4_INTR1_MAP, BMA423_STEP_CNTR_INT, 1,&bma); + ASSERT(ret == BMA4_OK); + + bma423_step_detector_enable(0, &bma); + + bma423_any_no_mot_config motConfig; + motConfig.threshold = 0xaa; + motConfig.axes_en = 3; + motConfig.duration = 1; + bma423_set_any_mot_config(&motConfig, &bma); + + ret = bma4_set_accel_enable(1, &bma); + ASSERT(ret == BMA4_OK); + + ret = bma4_set_accel_config(&accel_conf, &bma); + ASSERT(ret == BMA4_OK); +} + +void Bma421::Reset() { + uint8_t data = 0xb6; + twiMaster.Write(deviceAddress, 0x7E, &data, 1); +} + +void Bma421::Read(uint8_t registerAddress, uint8_t *buffer, size_t size) { + twiMaster.Read(deviceAddress, registerAddress, buffer, size); +} + +void Bma421::Write(uint8_t registerAddress, const uint8_t *data, size_t size) { + twiMaster.Write(deviceAddress, registerAddress, data, size); +} + +Bma421::Values Bma421::Process() { + struct bma4_accel data; + bma4_read_accel_xyz(&data, &bma); + + uint32_t steps = 0; + bma423_step_counter_output(&steps, &bma); + + int32_t temperature; + bma4_get_temperature(&temperature, BMA4_DEG, &bma); + temperature = temperature / 1000; + + uint8_t activity = 0; + bma423_activity_output(&activity, &bma); + + NRF_LOG_INFO("MOTION : %d - %d/%d/%d", steps, data.x, data.y, data.z); + + + return {steps, data.x, data.y, data.z}; +} + -- cgit v1.2.3 From c7cc47ae306b8012c196587f156519b0773aef93 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Thu, 1 Apr 2021 21:18:59 +0200 Subject: Code cleaning in BMA421 driver. Do the axis inversion in the driver and not in the application. NOTE: Axis remapping from the SDK do not apply to the "raw" X/Y/Z values returned to the sensor. According to the doc, the remapping is only applied to features, but I cannot check if it has any effect on step counting (I'm not sure I use it correctly, doc is not complete enough about this feature). --- src/drivers/Bma421.cpp | 76 ++++++++++++++++--------------------------- src/drivers/Bma421.h | 11 ++----- src/systemtask/SystemTask.cpp | 4 +-- 3 files changed, 32 insertions(+), 59 deletions(-) (limited to 'src/drivers/Bma421.cpp') diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index f1dd934a..9e98f1ca 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -6,23 +6,25 @@ using namespace Pinetime::Drivers; -int8_t user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr) { - auto bma421 = static_cast(intf_ptr); - bma421->Read(reg_addr, reg_data, length); - return 0; +namespace { + int8_t user_i2c_read(uint8_t reg_addr, uint8_t* reg_data, uint32_t length, void* intf_ptr) { + auto bma421 = static_cast(intf_ptr); + bma421->Read(reg_addr, reg_data, length); + return 0; + } + + int8_t user_i2c_write(uint8_t reg_addr, const uint8_t* reg_data, uint32_t length, void* intf_ptr) { + auto bma421 = static_cast(intf_ptr); + bma421->Write(reg_addr, reg_data, length); + return 0; + } + + void user_delay(uint32_t period_us, void* intf_ptr) { + nrf_delay_us(period_us); + } } -int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr) { - auto bma421 = static_cast(intf_ptr); - bma421->Write(reg_addr, reg_data, length); - return 0; -} - -void user_delay(uint32_t period_us, void *intf_ptr) { - nrf_delay_us(period_us); -} - -Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, twiAddress{twiAddress} { +Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, deviceAddress{twiAddress} { bma.intf = BMA4_I2C_INTF; bma.bus_read = user_i2c_read; bma.bus_write = user_i2c_write; @@ -30,13 +32,6 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, bma.intf_ptr = this; bma.delay_us = user_delay; bma.read_write_len = 8; - - accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ; - accel_conf.range = BMA4_ACCEL_RANGE_2G; - accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4; - accel_conf.perf_mode = BMA4_CIC_AVG_MODE; - - } void Bma421::Init() { @@ -46,43 +41,28 @@ void Bma421::Init() { nrf_delay_ms(1); ret = bma423_init(&bma); - NRF_LOG_INFO("RESET : %d", ret); - - //ret = bma423_init(&bma); - //NRF_LOG_INFO("ID : %d", bma.chip_id); ASSERT(ret == BMA4_OK); ret = bma423_write_config_file(&bma); ASSERT(ret == BMA4_OK); - bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma); - struct bma4_int_pin_config int_pin_config; - int_pin_config.edge_ctrl = BMA4_LEVEL_TRIGGER; - int_pin_config.lvl = BMA4_ACTIVE_LOW; - int_pin_config.od = BMA4_PUSH_PULL; - int_pin_config.output_en = BMA4_OUTPUT_ENABLE; - int_pin_config.input_en = BMA4_INPUT_DISABLE; - bma4_set_int_pin_config(&int_pin_config, BMA4_INTR1_MAP, &bma); - - //ret = bma423_feature_enable(BMA423_STEP_CNTR | BMA423_STEP_ACT | BMA423_WRIST_WEAR | BMA423_SINGLE_TAP | BMA423_DOUBLE_TAP, 1, &bma); - ret = bma423_feature_enable(0xff, 1, &bma); + ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma); ASSERT(ret == BMA4_OK); - //ret = bma423_map_interrupt(BMA4_INTR1_MAP, BMA423_SINGLE_TAP_INT | BMA423_STEP_CNTR_INT | BMA423_ACTIVITY_INT | BMA423_WRIST_WEAR_INT | BMA423_DOUBLE_TAP_INT | BMA423_ANY_MOT_INT | BMA423_NO_MOT_INT| BMA423_ERROR_INT, 1,&bma); - ret = bma423_map_interrupt(BMA4_INTR1_MAP, BMA423_STEP_CNTR_INT, 1,&bma); + ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma); ASSERT(ret == BMA4_OK); - bma423_step_detector_enable(0, &bma); - - bma423_any_no_mot_config motConfig; - motConfig.threshold = 0xaa; - motConfig.axes_en = 3; - motConfig.duration = 1; - bma423_set_any_mot_config(&motConfig, &bma); + ret = bma423_step_detector_enable(0, &bma); + ASSERT(ret == BMA4_OK); ret = bma4_set_accel_enable(1, &bma); ASSERT(ret == BMA4_OK); + struct bma4_accel_config accel_conf; + accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ; + accel_conf.range = BMA4_ACCEL_RANGE_2G; + accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4; + accel_conf.perf_mode = BMA4_CIC_AVG_MODE; ret = bma4_set_accel_config(&accel_conf, &bma); ASSERT(ret == BMA4_OK); } @@ -116,7 +96,7 @@ Bma421::Values Bma421::Process() { NRF_LOG_INFO("MOTION : %d - %d/%d/%d", steps, data.x, data.y, data.z); - - return {steps, data.x, data.y, data.z}; + // X and Y axis are swapped because of the way the sensor is mounted in the PineTime + return {steps, data.y, data.x, data.z}; } diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index bcc06b1c..fbb190ae 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -25,19 +25,12 @@ namespace Pinetime { void Read(uint8_t registerAddress, uint8_t *buffer, size_t size); void Write(uint8_t registerAddress, const uint8_t *data, size_t size); - void OnIrq(); - - uint32_t GetNbInterrupts() const {return nbInterrupts;} - private: TwiMaster& twiMaster; - uint8_t twiAddress; + uint8_t deviceAddress = 0x18; - struct bma4_dev bma; - struct bma4_accel_config accel_conf; - static constexpr uint8_t deviceAddress = 0x18; - uint32_t nbInterrupts = 0; + struct bma4_dev bma; }; } } \ No newline at end of file diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index bf1839c7..74c2c46e 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -243,8 +243,8 @@ void SystemTask::UpdateMotion() { if(isSleeping) twiMaster.Sleep(); - motionController.Update(motionValues.y, - motionValues.x, + motionController.Update(motionValues.x, + motionValues.y, motionValues.z, motionValues.steps); if (motionController.ShouldWakeUp(isSleeping)) { -- cgit v1.2.3 From 52a90288fd2b744b68d584db316fcf3fede84262 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Fri, 2 Apr 2021 16:56:14 +0200 Subject: Handle return code from BMA driver, and set a flag is the initialization fails. This allows to boot InfiniTime even if the device cannot initialize. --- src/components/motion/MotionController.cpp | 3 +++ src/components/motion/MotionController.h | 7 +++++-- src/displayapp/screens/WatchFaceDigital.cpp | 8 ++++++-- src/displayapp/screens/WatchFaceDigital.h | 1 + src/drivers/Bma421.cpp | 23 ++++++++++++++--------- src/drivers/Bma421.h | 5 +++-- src/systemtask/SystemTask.cpp | 1 + 7 files changed, 33 insertions(+), 15 deletions(-) (limited to 'src/drivers/Bma421.cpp') diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index bad9d45f..e9ee314b 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -31,3 +31,6 @@ bool MotionController::ShouldWakeUp(bool isSleeping) { } return false; } +void MotionController::IsSensorOk(bool isOk) { + isSensorOk = isOk; +} diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index dbbbf910..5881997f 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -14,13 +14,16 @@ namespace Pinetime { uint32_t NbSteps() const { return nbSteps; } bool ShouldWakeUp(bool isSleeping); - private: + void IsSensorOk(bool isOk); + bool IsSensorOk() const { return isSensorOk; } + + private: uint32_t nbSteps; int16_t x; int16_t y; int16_t z; int16_t lastYForWakeUp = 0; - + bool isSensorOk = false; }; } } \ No newline at end of file diff --git a/src/displayapp/screens/WatchFaceDigital.cpp b/src/displayapp/screens/WatchFaceDigital.cpp index 2204e177..2dd89bb4 100644 --- a/src/displayapp/screens/WatchFaceDigital.cpp +++ b/src/displayapp/screens/WatchFaceDigital.cpp @@ -240,9 +240,13 @@ bool WatchFaceDigital::Refresh() { } stepCount = motionController.NbSteps(); - if(stepCount.IsUpdated()) { + motionSensorOk = motionController.IsSensorOk(); + if(stepCount.IsUpdated() || motionSensorOk.IsUpdated()) { char stepBuffer[5]; - sprintf(stepBuffer, "%lu", stepCount.Get()); + if(motionSensorOk.Get()) + sprintf(stepBuffer, "%lu", stepCount.Get()); + else + sprintf(stepBuffer, "---", stepCount.Get()); lv_label_set_text(stepValue, stepBuffer); lv_obj_align(stepValue, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, -5, -2); lv_obj_align(stepIcon, stepValue, LV_ALIGN_OUT_LEFT_MID, -5, 0); diff --git a/src/displayapp/screens/WatchFaceDigital.h b/src/displayapp/screens/WatchFaceDigital.h index e6514f1e..0f6a2f0c 100644 --- a/src/displayapp/screens/WatchFaceDigital.h +++ b/src/displayapp/screens/WatchFaceDigital.h @@ -50,6 +50,7 @@ namespace Pinetime { DirtyValue batteryPercentRemaining {}; DirtyValue bleState {}; DirtyValue> currentDateTime{}; + DirtyValue motionSensorOk {}; DirtyValue stepCount {}; DirtyValue heartbeat {}; DirtyValue heartbeatRunning {}; diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index 9e98f1ca..4e33ef8b 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -36,27 +36,27 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, void Bma421::Init() { auto ret = bma4_soft_reset(&bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; nrf_delay_ms(1); ret = bma423_init(&bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma423_write_config_file(&bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma423_step_detector_enable(0, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma4_set_accel_enable(1, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; struct bma4_accel_config accel_conf; accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ; @@ -64,7 +64,9 @@ void Bma421::Init() { accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4; accel_conf.perf_mode = BMA4_CIC_AVG_MODE; ret = bma4_set_accel_config(&accel_conf, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; + + isOk = true; } void Bma421::Reset() { @@ -81,6 +83,7 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t *data, size_t size) { } Bma421::Values Bma421::Process() { + if(not isOk) return {}; struct bma4_accel data; bma4_read_accel_xyz(&data, &bma); @@ -99,4 +102,6 @@ Bma421::Values Bma421::Process() { // X and Y axis are swapped because of the way the sensor is mounted in the PineTime return {steps, data.y, data.x, data.z}; } - +bool Bma421::IsOk() const { + return isOk; +} diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index fbb190ae..e2e24f0d 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -25,12 +25,13 @@ namespace Pinetime { void Read(uint8_t registerAddress, uint8_t *buffer, size_t size); void Write(uint8_t registerAddress, const uint8_t *data, size_t size); + bool IsOk() const; + private: TwiMaster& twiMaster; uint8_t deviceAddress = 0x18; - - struct bma4_dev bma; + bool isOk = false; }; } } \ No newline at end of file diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 74c2c46e..d2f38adb 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -243,6 +243,7 @@ void SystemTask::UpdateMotion() { if(isSleeping) twiMaster.Sleep(); + motionController.IsSensorOk(motionSensor.IsOk()); motionController.Update(motionValues.x, motionValues.y, motionValues.z, -- cgit v1.2.3 From 1d7576de64a33837434e6f414a74ae7dbe929196 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Fri, 2 Apr 2021 17:33:49 +0200 Subject: Reset the step count every day at midnight. --- src/components/datetime/DateTimeController.cpp | 13 +++++++++++++ src/components/datetime/DateTimeController.h | 8 ++++++++ src/drivers/Bma421.cpp | 4 ++++ src/drivers/Bma421.h | 4 +++- src/main.cpp | 3 +-- src/systemtask/SystemTask.cpp | 14 ++++++++++++-- src/systemtask/SystemTask.h | 7 ++++--- 7 files changed, 45 insertions(+), 8 deletions(-) (limited to 'src/drivers/Bma421.cpp') diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 59982477..9c1f2684 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -1,9 +1,14 @@ #include "DateTimeController.h" #include #include +#include using namespace Pinetime::Controllers; +DateTime::DateTime(System::SystemTask& systemTask) : systemTask{systemTask} { + +} + void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter) { @@ -62,6 +67,14 @@ void DateTime::UpdateTime(uint32_t systickCounter) { hour = time.hours().count(); minute = time.minutes().count(); second = time.seconds().count(); + + // Notify new day to SystemTask + if(hour == 0 and not isMidnightAlreadyNotified) { + isMidnightAlreadyNotified = true; + systemTask.PushMessage(System::SystemTask::Messages::OnNewDay); + } else if (hour != 0) { + isMidnightAlreadyNotified = false; + } } const char *DateTime::MonthShortToString() { diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index 16bb59c9..fb0969d1 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -4,12 +4,17 @@ #include namespace Pinetime { + namespace System { + class SystemTask; + } namespace Controllers { class DateTime { public: enum class Days : uint8_t {Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum class Months : uint8_t {Unknown, January, February, March, April, May, June, July, August, September, October, November, December}; + DateTime(System::SystemTask& systemTask); + void SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter); void UpdateTime(uint32_t systickCounter); uint16_t Year() const { return year; } @@ -31,6 +36,7 @@ namespace Pinetime { std::chrono::time_point CurrentDateTime() const { return currentDateTime; } std::chrono::seconds Uptime() const { return uptime; } private: + System::SystemTask& systemTask; uint16_t year = 0; Months month = Months::Unknown; uint8_t day = 0; @@ -43,6 +49,8 @@ namespace Pinetime { std::chrono::time_point currentDateTime; std::chrono::seconds uptime {0}; + bool isMidnightAlreadyNotified = false; + static char const *DaysString[]; static char const *DaysStringShort[]; static char const *DaysStringLow[]; diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index 4e33ef8b..10d3e5e8 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -105,3 +105,7 @@ Bma421::Values Bma421::Process() { bool Bma421::IsOk() const { return isOk; } + +void Bma421::ResetStepCounter() { + bma423_reset_step_counter(&bma); +} diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index e2e24f0d..d36d1db5 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -19,8 +19,8 @@ namespace Pinetime { Bma421& operator=(Bma421&&) = delete; void Init(); - void Reset(); Values Process(); + void ResetStepCounter(); void Read(uint8_t registerAddress, uint8_t *buffer, size_t size); void Write(uint8_t registerAddress, const uint8_t *data, size_t size); @@ -28,6 +28,8 @@ namespace Pinetime { bool IsOk() const; private: + void Reset(); + TwiMaster& twiMaster; uint8_t deviceAddress = 0x18; struct bma4_dev bma; diff --git a/src/main.cpp b/src/main.cpp index f1d1bcf7..6cca9c8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,7 +107,6 @@ Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress TimerHandle_t debounceTimer; Pinetime::Controllers::Battery batteryController; Pinetime::Controllers::Ble bleController; -Pinetime::Controllers::DateTime dateTimeController; void ble_manager_set_ble_connection_callback(void (*connection)()); void ble_manager_set_ble_disconnection_callback(void (*disconnection)()); static constexpr uint8_t pinTouchIrq = 28; @@ -259,7 +258,7 @@ int main(void) { debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback); systemTask = std::make_unique(spi, lcd, spiNorFlash, twiMaster, touchPanel, lvgl, batteryController, bleController, - dateTimeController, motorController, heartRateSensor, motionSensor, settingsController); + motorController, heartRateSensor, motionSensor, settingsController); systemTask->Start(); nimble_port_init(); diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index d2f38adb..4605ba95 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -40,7 +40,6 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::TwiMaster& twiMaster, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, - Controllers::DateTime &dateTimeController, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, Pinetime::Drivers::Bma421& motionSensor, @@ -48,7 +47,7 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, twiMaster{twiMaster}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, heartRateController{*this}, - bleController{bleController}, dateTimeController{dateTimeController}, + bleController{bleController}, dateTimeController{*this}, watchdog{}, watchdogView{watchdog}, motorController{motorController}, heartRateSensor{heartRateSensor}, motionSensor{motionSensor}, settingsController{settingsController}, @@ -210,6 +209,11 @@ void SystemTask::Work() { isSleeping = true; isGoingToSleep = false; break; + case Messages::OnNewDay: + // We might be sleeping (with TWI device disabled. + // Remember we'll have to reset the counter next time we're awake + stepCounterMustBeReset = true; + break; default: break; } } @@ -239,6 +243,12 @@ void SystemTask::UpdateMotion() { if(isSleeping) twiMaster.Wakeup(); + + if(stepCounterMustBeReset) { + motionSensor.ResetStepCounter(); + stepCounterMustBeReset = false; + } + auto motionValues = motionSensor.Process(); if(isSleeping) twiMaster.Sleep(); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 2b70d19a..bb2a2868 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -40,7 +40,8 @@ namespace Pinetime { class SystemTask { public: enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, OnNewCall, BleConnected, - BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping + BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping, + OnNewDay }; SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, @@ -48,7 +49,6 @@ namespace Pinetime { Drivers::TwiMaster& twiMaster, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, - Controllers::DateTime &dateTimeController, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, Pinetime::Drivers::Bma421& motionSensor, @@ -80,7 +80,7 @@ namespace Pinetime { std::unique_ptr heartRateApp; Pinetime::Controllers::Ble& bleController; - Pinetime::Controllers::DateTime& dateTimeController; + Pinetime::Controllers::DateTime dateTimeController; QueueHandle_t systemTasksMsgQueue; std::atomic isSleeping{false}; std::atomic isGoingToSleep{false}; @@ -115,6 +115,7 @@ namespace Pinetime { void GoToRunning(); void UpdateMotion(); + bool stepCounterMustBeReset = false; #if configUSE_TRACE_FACILITY == 1 SystemMonitor monitor; -- cgit v1.2.3 From 9ac4be8b759bb2cedeb999ce5e87d983261beded Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Thu, 8 Apr 2021 20:07:24 +0200 Subject: TwiMaster is now based on the NRFX TWI driver, as it handles more edge cases and workarounds for errors on the bus. Reset the TWI bus after the soft-reset of the motion sensor to workaround issues on the TWI bus. --- src/CMakeLists.txt | 2 +- src/drivers/Bma421.cpp | 15 ++- src/drivers/Bma421.h | 4 + src/drivers/TwiMaster.cpp | 225 ++++++++---------------------------------- src/drivers/TwiMaster.h | 14 +-- src/sdk_config.h | 4 +- src/systemtask/SystemTask.cpp | 8 +- 7 files changed, 66 insertions(+), 206 deletions(-) (limited to 'src/drivers/Bma421.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 89b1bc5a..3e199442 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -83,7 +83,7 @@ set(SDK_SOURCE_FILES "${NRF5_SDK_PATH}/external/fprintf/nrf_fprintf_format.c" # TWI - "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_twi.c" + "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_twim.c" # GPIOTE "${NRF5_SDK_PATH}/components/libraries/gpiote/app_gpiote.c" diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index 10d3e5e8..ea705d8e 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -35,12 +35,9 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, } void Bma421::Init() { - auto ret = bma4_soft_reset(&bma); - if(ret != BMA4_OK) return; + if(not isResetOk) return; // Call SoftReset (and reset TWI device) first! - nrf_delay_ms(1); - - ret = bma423_init(&bma); + auto ret = bma423_init(&bma); if(ret != BMA4_OK) return; ret = bma423_write_config_file(&bma); @@ -109,3 +106,11 @@ bool Bma421::IsOk() const { void Bma421::ResetStepCounter() { bma423_reset_step_counter(&bma); } + +void Bma421::SoftReset() { + auto ret = bma4_soft_reset(&bma); + if(ret == BMA4_OK) { + isResetOk = true; + nrf_delay_ms(1); + } +} diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index d36d1db5..da021cbf 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -18,6 +18,9 @@ namespace Pinetime { Bma421(Bma421&&) = delete; Bma421& operator=(Bma421&&) = delete; + /// The chip freezes the TWI bus after the softreset operation. Softreset is separated from the + /// Init() method to allow the caller to uninit and then reinit the TWI device after the softreset. + void SoftReset(); void Init(); Values Process(); void ResetStepCounter(); @@ -34,6 +37,7 @@ namespace Pinetime { uint8_t deviceAddress = 0x18; struct bma4_dev bma; bool isOk = false; + bool isResetOk = false; }; } } \ No newline at end of file diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp index 6a063ecf..646823d0 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/TwiMaster.cpp @@ -2,195 +2,77 @@ #include #include #include - +#include +#include using namespace Pinetime::Drivers; // TODO use shortcut to automatically send STOP when receive LastTX, for example // TODO use DMA/IRQ -TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module{module}, params{params} { - mutex = xSemaphoreCreateBinary(); -} - -void TwiMaster::Init() { - NRF_GPIO->PIN_CNF[params.pinScl] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - - NRF_GPIO->PIN_CNF[params.pinSda] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - +TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module{module}, params{params}, mutex{xSemaphoreCreateBinary()} { + ASSERT(mutex != nullptr); switch(module) { - case Modules::TWIM1: twiBaseAddress = NRF_TWIM1; break; + case Modules::TWIM1: default: - return; - } - - switch(static_cast(params.frequency)) { - case Frequencies::Khz100 : twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100; break; - case Frequencies::Khz250 : twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K250; break; - case Frequencies::Khz400 : twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400; break; + twim = NRFX_TWIM_INSTANCE(1); + break; } +} - twiBaseAddress->PSEL.SCL = params.pinScl; - twiBaseAddress->PSEL.SDA = params.pinSda; - twiBaseAddress->EVENTS_LASTRX = 0; - twiBaseAddress->EVENTS_STOPPED = 0; - twiBaseAddress->EVENTS_LASTTX = 0; - twiBaseAddress->EVENTS_ERROR = 0; - twiBaseAddress->EVENTS_RXSTARTED = 0; - twiBaseAddress->EVENTS_SUSPENDED = 0; - twiBaseAddress->EVENTS_TXSTARTED = 0; - - twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos); - - - /* // IRQ - NVIC_ClearPendingIRQ(_IRQn); - NVIC_SetPriority(_IRQn, 2); - NVIC_EnableIRQ(_IRQn); - */ +void TwiMaster::Init() { + nrfx_twim_config_t config; + config.frequency = static_cast(params.frequency); + config.hold_bus_uninit = false; + config.interrupt_priority = 0; + config.scl = params.pinScl; + config.sda = params.pinSda; + nrfx_twim_init(&twim, + &config, + nullptr, + nullptr); + nrfx_twim_enable(&twim); xSemaphoreGive(mutex); - } TwiMaster::ErrorCodes TwiMaster::Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t *data, size_t size) { xSemaphoreTake(mutex, portMAX_DELAY); - auto ret = ReadWithRetry(deviceAddress, registerAddress, data, size); + TwiMaster::ErrorCodes ret; + + auto err = nrfx_twim_tx(&twim, deviceAddress, ®isterAddress, 1, false); + if(err != 0) { + return TwiMaster::ErrorCodes::TransactionFailed; + } + + err = nrfx_twim_rx(&twim, deviceAddress, data, size); + if(err != 0) { + return TwiMaster::ErrorCodes::TransactionFailed; + } xSemaphoreGive(mutex); - return ret; + return TwiMaster::ErrorCodes::NoError; } TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t *data, size_t size) { ASSERT(size <= maxDataSize); xSemaphoreTake(mutex, portMAX_DELAY); - - auto ret = WriteWithRetry(deviceAddress, registerAddress, data, size); - xSemaphoreGive(mutex); - return ret; -} - -/* Execute a read transaction (composed of a write and a read operation). If one of these opeartion fails, - * it's retried once. If it fails again, an error is returned */ -TwiMaster::ErrorCodes TwiMaster::ReadWithRetry(uint8_t deviceAddress, uint8_t registerAddress, uint8_t *data, size_t size) { TwiMaster::ErrorCodes ret; - ret = Write(deviceAddress, ®isterAddress, 1, false); - if(ret != ErrorCodes::NoError) - ret = Write(deviceAddress, ®isterAddress, 1, false); - - if(ret != ErrorCodes::NoError) return ret; - ret = Read(deviceAddress, data, size, true); - if(ret != ErrorCodes::NoError) - ret = Read(deviceAddress, data, size, true); - - return ret; -} - -/* Execute a write transaction. If it fails, it is retried once. If it fails again, an error is returned. */ -TwiMaster::ErrorCodes TwiMaster::WriteWithRetry(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t *data, size_t size) { internalBuffer[0] = registerAddress; std::memcpy(internalBuffer+1, data, size); - auto ret = Write(deviceAddress, internalBuffer, size+1, true); - if(ret != ErrorCodes::NoError) - ret = Write(deviceAddress, internalBuffer, size+1, true); - - return ret; -} - - -TwiMaster::ErrorCodes TwiMaster::Read(uint8_t deviceAddress, uint8_t *buffer, size_t size, bool stop) { - twiBaseAddress->ADDRESS = deviceAddress; - twiBaseAddress->TASKS_RESUME = 0x1UL; - twiBaseAddress->RXD.PTR = (uint32_t)buffer; - twiBaseAddress->RXD.MAXCNT = size; - - twiBaseAddress->TASKS_STARTRX = 1; - - while(!twiBaseAddress->EVENTS_RXSTARTED && !twiBaseAddress->EVENTS_ERROR); - twiBaseAddress->EVENTS_RXSTARTED = 0x0UL; - - txStartedCycleCount = DWT->CYCCNT; - uint32_t currentCycleCount; - while(!twiBaseAddress->EVENTS_LASTRX && !twiBaseAddress->EVENTS_ERROR) { - currentCycleCount = DWT->CYCCNT; - if ((currentCycleCount-txStartedCycleCount) > HwFreezedDelay) { - FixHwFreezed(); - return ErrorCodes::TransactionFailed; - } - } - twiBaseAddress->EVENTS_LASTRX = 0x0UL; - - if (stop || twiBaseAddress->EVENTS_ERROR) { - twiBaseAddress->TASKS_STOP = 0x1UL; - while(!twiBaseAddress->EVENTS_STOPPED); - twiBaseAddress->EVENTS_STOPPED = 0x0UL; - } - else { - twiBaseAddress->TASKS_SUSPEND = 0x1UL; - while(!twiBaseAddress->EVENTS_SUSPENDED); - twiBaseAddress->EVENTS_SUSPENDED = 0x0UL; + auto err = nrfx_twim_tx(&twim, deviceAddress, internalBuffer , size+1, false); + if(err != 0){ + return TwiMaster::ErrorCodes::TransactionFailed; } - if (twiBaseAddress->EVENTS_ERROR) { - twiBaseAddress->EVENTS_ERROR = 0x0UL; - } - return ErrorCodes::NoError; -} - -TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, const uint8_t *data, size_t size, bool stop) { - twiBaseAddress->ADDRESS = deviceAddress; - twiBaseAddress->TASKS_RESUME = 0x1UL; - twiBaseAddress->TXD.PTR = (uint32_t)data; - twiBaseAddress->TXD.MAXCNT = size; - - twiBaseAddress->TASKS_STARTTX = 1; - - while(!twiBaseAddress->EVENTS_TXSTARTED && !twiBaseAddress->EVENTS_ERROR); - twiBaseAddress->EVENTS_TXSTARTED = 0x0UL; - - txStartedCycleCount = DWT->CYCCNT; - uint32_t currentCycleCount; - while(!twiBaseAddress->EVENTS_LASTTX && !twiBaseAddress->EVENTS_ERROR) { - currentCycleCount = DWT->CYCCNT; - if ((currentCycleCount-txStartedCycleCount) > HwFreezedDelay) { - FixHwFreezed(); - return ErrorCodes::TransactionFailed; - } - } - twiBaseAddress->EVENTS_LASTTX = 0x0UL; - - if (stop || twiBaseAddress->EVENTS_ERROR) { - twiBaseAddress->TASKS_STOP = 0x1UL; - while(!twiBaseAddress->EVENTS_STOPPED); - twiBaseAddress->EVENTS_STOPPED = 0x0UL; - } - else { - twiBaseAddress->TASKS_SUSPEND = 0x1UL; - while(!twiBaseAddress->EVENTS_SUSPENDED); - twiBaseAddress->EVENTS_SUSPENDED = 0x0UL; - } - - if (twiBaseAddress->EVENTS_ERROR) { - twiBaseAddress->EVENTS_ERROR = 0x0UL; - uint32_t error = twiBaseAddress->ERRORSRC; - twiBaseAddress->ERRORSRC = error; - } - - return ErrorCodes::NoError; + xSemaphoreGive(mutex); + return TwiMaster::ErrorCodes::NoError; } void TwiMaster::Sleep() { - while(twiBaseAddress->ENABLE != 0) { - twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos); - } + nrfx_twim_disable(&twim); + nrfx_twim_uninit(&twim); + nrf_gpio_cfg_default(6); nrf_gpio_cfg_default(7); NRF_LOG_INFO("[TWIMASTER] Sleep"); @@ -200,30 +82,3 @@ void TwiMaster::Wakeup() { Init(); NRF_LOG_INFO("[TWIMASTER] Wakeup"); } - -/* Sometimes, the TWIM device just freeze and never set the event EVENTS_LASTTX. - * This method disable and re-enable the peripheral so that it works again. - * This is just a workaround, and it would be better if we could find a way to prevent - * this issue from happening. - * */ -void TwiMaster::FixHwFreezed() { - NRF_LOG_INFO("I2C device frozen, reinitializing it!"); - // Disable I²C - uint32_t twi_state = NRF_TWI1->ENABLE; - twiBaseAddress->ENABLE = TWIM_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; - - NRF_GPIO->PIN_CNF[params.pinScl] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - - NRF_GPIO->PIN_CNF[params.pinSda] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - - // Re-enable I²C - twiBaseAddress->ENABLE = twi_state; -} diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h index 399e3d0f..6e3ff721 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -3,13 +3,13 @@ #include #include // NRF_TWIM_Type #include +#include namespace Pinetime { namespace Drivers { class TwiMaster { public: enum class Modules { TWIM1 }; - enum class Frequencies {Khz100, Khz250, Khz400}; enum class ErrorCodes {NoError, TransactionFailed}; struct Parameters { uint32_t frequency; @@ -27,21 +27,13 @@ namespace Pinetime { void Wakeup(); private: - ErrorCodes ReadWithRetry(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size); - ErrorCodes WriteWithRetry(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size); - - ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop); - ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop); - void FixHwFreezed(); - NRF_TWIM_Type* twiBaseAddress; - SemaphoreHandle_t mutex; + nrfx_twim_t twim; const Modules module; const Parameters params; + SemaphoreHandle_t mutex; static constexpr uint8_t maxDataSize{8}; static constexpr uint8_t registerSize{1}; uint8_t internalBuffer[maxDataSize + registerSize]; - uint32_t txStartedCycleCount = 0; - static constexpr uint32_t HwFreezedDelay{161000}; }; } } \ No newline at end of file diff --git a/src/sdk_config.h b/src/sdk_config.h index aa1bbb3a..bc97ef14 100644 --- a/src/sdk_config.h +++ b/src/sdk_config.h @@ -4992,7 +4992,7 @@ // NRFX_TWIM_ENABLED - nrfx_twim - TWIM peripheral driver //========================================================== #ifndef NRFX_TWIM_ENABLED -#define NRFX_TWIM_ENABLED 0 +#define NRFX_TWIM_ENABLED 1 #endif // NRFX_TWIM0_ENABLED - Enable TWIM0 instance @@ -5005,7 +5005,7 @@ #ifndef NRFX_TWIM1_ENABLED -#define NRFX_TWIM1_ENABLED 0 +#define NRFX_TWIM1_ENABLED 1 #endif // NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY - Frequency diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 4605ba95..6163c105 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -84,10 +84,14 @@ void SystemTask::Work() { touchPanel.Init(); batteryController.Init(); motorController.Init(); - motionSensor.Init(); + motionSensor.SoftReset(); - settingsController.Init(); + // Reset the TWI device because the motion sensor chip most probably crashed it... + twiMaster.Sleep(); + twiMaster.Init(); + motionSensor.Init(); + settingsController.Init(); displayApp = std::make_unique(lcd, lvgl, touchPanel, batteryController, bleController, dateTimeController, watchdogView, *this, notificationManager, -- cgit v1.2.3