Skip to content

Commit 71dbd7b

Browse files
committed
Added MCU temp sensor to the companion
Fixes #2896
1 parent 57563ab commit 71dbd7b

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

examples/companion_radio/MyMesh.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,11 @@ uint8_t MyMesh::onContactRequest(const ContactInfo &contact, uint32_t sender_tim
654654
// query other sensors -- target specific
655655
sensors.querySensors(permissions, telemetry);
656656

657+
float temperature = board.getMCUTemperature();
658+
if(!isnan(temperature)) { // Supported boards with built-in temperature sensor. ESP32-C3 may return NAN
659+
telemetry.addTemperature(TELEM_CHANNEL_SELF, temperature); // Built-in MCU Temperature
660+
}
661+
657662
memcpy(reply, &sender_timestamp,
658663
4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
659664

@@ -1636,6 +1641,11 @@ void MyMesh::handleCmdFrame(size_t len) {
16361641
} else if (cmd_frame[0] == CMD_SEND_TELEMETRY_REQ && len == 4) { // 'self' telemetry request
16371642
telemetry.reset();
16381643
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
1644+
float temperature = board.getMCUTemperature();
1645+
if(!isnan(temperature)) { // Supported boards with built-in temperature sensor. ESP32-C3 may return NAN
1646+
telemetry.addTemperature(TELEM_CHANNEL_SELF, temperature); // Built-in MCU Temperature
1647+
}
1648+
16391649
// query other sensors -- target specific
16401650
sensors.querySensors(0xFF, telemetry);
16411651

src/helpers/NRF52Board.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,29 @@ void NRF52Board::sleep(uint32_t secs) {
279279

280280
// Temperature from NRF52 MCU
281281
float NRF52Board::getMCUTemperature() {
282-
NRF_TEMP->TASKS_START = 1; // Start temperature measurement
283-
284-
long startTime = millis();
285-
while (NRF_TEMP->EVENTS_DATARDY == 0) { // Wait for completion. Should complete in 50us
286-
if(millis() - startTime > 5) { // To wait 5ms just in case
287-
NRF_TEMP->TASKS_STOP = 1;
282+
uint8_t sd_enabled = 0;
283+
sd_softdevice_is_enabled(&sd_enabled);
284+
if (sd_enabled) {
285+
uint32_t err_code;
286+
int32_t temp;
287+
err_code = sd_temp_get(&temp);
288+
if (err_code == NRF_SUCCESS) {
289+
return (float)temp * 0.25f;
290+
} else {
288291
return NAN;
289292
}
293+
} else {
294+
NRF_TEMP->TASKS_START = 1; // Start temperature measurement
295+
296+
long startTime = millis();
297+
while (NRF_TEMP->EVENTS_DATARDY == 0) { // Wait for completion. Should complete in 50us
298+
if(millis() - startTime > 5) { // To wait 5ms just in case
299+
NRF_TEMP->TASKS_STOP = 1;
300+
return NAN;
301+
}
302+
}
290303
}
291-
304+
292305
NRF_TEMP->EVENTS_DATARDY = 0; // Clear event flag
293306

294307
int32_t temp = NRF_TEMP->TEMP; // In 0.25 *C units

0 commit comments

Comments
 (0)