Skip to content

Commit d2271c1

Browse files
committed
nicla-system: Simplify the watchdog reset machanism.
1 parent f5e6718 commit d2271c1

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

+16-15
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

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

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

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

0 commit comments

Comments
 (0)