Added readCPUTemperature()

This commit is contained in:
ajs123 committed 2021-08-03 20:09:34 -04:00
1 parent 401eb29ca5
commit f1d78e488e
2 files changed
+47

No files matched your search

+41
View File
@@ -143,3 +143,44 @@ void systemOff(uint32_t pin, uint8_t wake_logic)
NRF_POWER->SYSTEMOFF = 1;
}
}
float readCPUTemperature()
{
// These are defined simply to make the ensuing code easier to read. Ready and triggers are 0x01, and not ready is 0x00.
// Many other library functions use a literal 0x00 to reset flags and a literal 0x01 for triggers, and test "done"
// flags for nonzero. In this function, we used the defined constants. Using 0x00 and 0x01 would probably be fine
// because they're unlikely to ever change.
const uint32_t temp_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Generated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos)
&& TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk );
const uint32_t temp_not_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_NotGenerated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos)
&& TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk );
const uint32_t temp_trigger = ( (TEMP_TASKS_START_TASKS_START_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos)
&& TEMP_TASKS_START_TASKS_START_Msk );
const uint32_t temp_stop = ( (TEMP_TASKS_STOP_TASKS_STOP_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos)
&& TEMP_TASKS_STOP_TASKS_STOP_Msk );
uint8_t en;
int32_t temp;
(void) sd_softdevice_is_enabled(&en);
if (en)
{
sd_temp_get(&temp);
}
else
{
NRF_TEMP->EVENTS_DATARDY = temp_not_ready; // Only needed in case another function is also looking at this event flag
NRF_TEMP->TASKS_START = temp_trigger;
bool done = false;
while (NRF_TEMP->EVENTS_DATARDY != temp_ready);
temp = NRF_TEMP->TEMP; // Per anomaly 29 (unclear whether still applicable), TASKS_STOP will clear the TEMP register.
NRF_TEMP->TASKS_STOP = temp_stop; // Per anomaly 30 (unclear whether still applicable), the temp peripheral needs to be shut down
NRF_TEMP->EVENTS_DATARDY = temp_not_ready;
}
return temp / 4.0F;
}
+6
View File
@@ -42,6 +42,12 @@ static inline bool isInISR(void)
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ;
}
/*
* \brief Reads the on-chip temperature sensor, returning the temperature in degrees C
* with a resolution of 0.25 degrees.
*/
float readCPUTemperature();
#ifdef __cplusplus
}
#endif