Skip to content

Commit 6f02d53

Browse files
committed
nicla-system: Simplify the watchdog reset machanism.
1 parent 710a7f3 commit 6f02d53

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ uint8_t nicla::_fastChargeRegisterData = 0;
1818
/// Enabled is the default value also represented in the TS Control Register (Bit 7 = 1).
1919
bool nicla::_ntcEnabled = true;
2020

21-
void nicla::pingI2C() {
22-
while(1) {
23-
// already protected by a mutex on Wire operations
24-
synchronizeFastChargeSettings();
25-
delay(10000);
21+
void nicla::pingI2C(bool useWriteOperation) {
22+
// PMIC commands already protected by a mutex on Wire operations.
23+
if(useWriteOperation){
24+
// Write the current charging settings to the register to reset the watchdog timer.
25+
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _fastChargeRegisterData);
26+
} else {
27+
_pmic.getStatusRegister();
2628
}
2729
}
2830

@@ -36,9 +38,16 @@ bool nicla::begin(bool mountedOnMkr)
3638
}
3739
Wire1.begin();
3840
_fastChargeRegisterData = _pmic.getFastChargeControlRegister();
41+
3942
#ifndef NO_NEED_FOR_WATCHDOG_THREAD
43+
// If not using the BHY2 library, we need to start a thread to ping the PMIC every 10 seconds.
4044
static rtos::Thread th(osPriorityHigh, 768, nullptr, "ping_thread");
41-
th.start(&nicla::pingI2C);
45+
th.start([]() {
46+
while(1) {
47+
pingI2C();
48+
delay(10000);
49+
}
50+
});
4251
#endif
4352
started = true;
4453

@@ -390,16 +399,8 @@ OperatingStatus nicla::getOperatingStatus() {
390399
return static_cast<OperatingStatus>(status);
391400
}
392401

393-
394-
void nicla::synchronizeFastChargeSettings()
395-
{
396-
if (_fastChargeRegisterData != _pmic.getFastChargeControlRegister()) {
397-
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _fastChargeRegisterData);
398-
}
399-
}
400-
401402
void nicla::checkChgReg(){
402-
synchronizeFastChargeSettings();
403+
pingI2C();
403404
}
404405

405406

libraries/Nicla_System/src/Nicla_System.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,17 @@ class nicla {
224224
* @brief Pings the I2C interface by querying the PMIC's fast charge register every 10 seconds.
225225
* This is invoked by a thread and is meant to kick the watchdog timer to prevent the PMIC from entering a low power state.
226226
* The I2C interface reset timer for the host is 50 seconds.
227+
* @param useWriteOperation If true, a write operation to a register is performed to reset the watchdog timer.
228+
* If false, a read operation is performed. The default is false.
227229
*/
228-
static void pingI2C();
230+
static void pingI2C(bool useWriteOperation = false);
229231

230-
/**
231-
* @brief Synchronizes the fast charge settings with the PMIC.
232-
* This ensures that the fast charge settings as specified via enableCharge() are applied again the register got wiped.
233-
*/
234-
static void synchronizeFastChargeSettings();
235-
236-
[[deprecated("Use synchronizeFastChargeSettings() instead.")]]
232+
[[deprecated("Use pingI2C() instead.")]]
237233
static void checkChgReg();
238234

239235
/**
240236
* A cached version of the fast charge settings for the PMIC.
241-
* This is used to reapply the settings if the register got wiped.
237+
* This is used to avoid unnecessary I2C communication.
242238
**/
243239
static uint8_t _fastChargeRegisterData;
244240

0 commit comments

Comments
 (0)