Skip to content

Commit 1f9fce6

Browse files
committed
nicla-system: Simplify the watchdog reset machanism.
1 parent 7c463bf commit 1f9fce6

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
@@ -215,21 +215,17 @@ class nicla {
215215
* @brief Pings the I2C interface by querying the PMIC's fast charge register every 10 seconds.
216216
* This is invoked by a thread and is meant to kick the watchdog timer to prevent the PMIC from entering a low power state.
217217
* The I2C interface reset timer for the host is 50 seconds.
218+
* @param useWriteOperation If true, a write operation to a register is performed to reset the watchdog timer.
219+
* If false, a read operation is performed. The default is false.
218220
*/
219-
static void pingI2C();
221+
static void pingI2C(bool useWriteOperation = false);
220222

221-
/**
222-
* @brief Synchronizes the fast charge settings with the PMIC.
223-
* This ensures that the fast charge settings as specified via enableCharge() are applied again the register got wiped.
224-
*/
225-
static void synchronizeFastChargeSettings();
226-
227-
[[deprecated("Use synchronizeFastChargeSettings() instead.")]]
223+
[[deprecated("Use pingI2C() instead.")]]
228224
static void checkChgReg();
229225

230226
/**
231227
* A cached version of the fast charge settings for the PMIC.
232-
* This is used to reapply the settings if the register got wiped.
228+
* This is used to avoid unnecessary I2C communication.
233229
**/
234230
static uint8_t _fastChargeRegisterData;
235231

0 commit comments

Comments
 (0)