Skip to content

Commit 2194a33

Browse files
committed
Only enable the bus when needed
1 parent baa7e1b commit 2194a33

2 files changed

Lines changed: 4 additions & 11 deletions

File tree

src/drivers/TwiMaster.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ void TwiMaster::Init() {
7272

7373
TwiMaster::ErrorCodes TwiMaster::Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* data, size_t size) {
7474
xSemaphoreTake(mutex, portMAX_DELAY);
75+
Wakeup();
7576
auto ret = Write(deviceAddress, &registerAddress, 1, false);
7677
ret = Read(deviceAddress, data, size, true);
78+
Sleep();
7779
xSemaphoreGive(mutex);
7880
return ret;
7981
}
8082

8183
TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size) {
8284
ASSERT(size <= maxDataSize);
8385
xSemaphoreTake(mutex, portMAX_DELAY);
86+
Wakeup();
8487
internalBuffer[0] = registerAddress;
8588
std::memcpy(internalBuffer + 1, data, size);
8689
auto ret = Write(deviceAddress, internalBuffer, size + 1, true);
90+
Sleep();
8791
xSemaphoreGive(mutex);
8892
return ret;
8993
}
@@ -179,13 +183,11 @@ void TwiMaster::Sleep() {
179183
}
180184
nrf_gpio_cfg_default(6);
181185
nrf_gpio_cfg_default(7);
182-
NRF_LOG_INFO("[TWIMASTER] Sleep");
183186
}
184187

185188
void TwiMaster::Wakeup() {
186189
ConfigurePins();
187190
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
188-
NRF_LOG_INFO("[TWIMASTER] Wakeup");
189191
}
190192

191193
/* Sometimes, the TWIM device just freeze and never set the event EVENTS_LASTTX.

src/systemtask/SystemTask.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ void SystemTask::Work() {
219219
break;
220220
case Messages::GoToRunning:
221221
spi.Wakeup();
222-
twiMaster.Wakeup();
223222

224223
// Double Tap needs the touch screen to be in normal mode
225224
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
@@ -240,9 +239,7 @@ void SystemTask::Work() {
240239
isDimmed = false;
241240
break;
242241
case Messages::TouchWakeUp: {
243-
twiMaster.Wakeup();
244242
auto touchInfo = touchPanel.GetTouchInfo();
245-
twiMaster.Sleep();
246243
if (touchInfo.isTouch and ((touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap and
247244
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) or
248245
(touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap and
@@ -315,7 +312,6 @@ void SystemTask::Work() {
315312
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
316313
touchPanel.Sleep();
317314
}
318-
twiMaster.Sleep();
319315

320316
isSleeping = true;
321317
isGoingToSleep = false;
@@ -367,17 +363,12 @@ void SystemTask::UpdateMotion() {
367363
if (isSleeping && !settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist))
368364
return;
369365

370-
if (isSleeping)
371-
twiMaster.Wakeup();
372-
373366
if (stepCounterMustBeReset) {
374367
motionSensor.ResetStepCounter();
375368
stepCounterMustBeReset = false;
376369
}
377370

378371
auto motionValues = motionSensor.Process();
379-
if (isSleeping)
380-
twiMaster.Sleep();
381372

382373
motionController.IsSensorOk(motionSensor.IsOk());
383374
motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps);

0 commit comments

Comments
 (0)