Skip to content

Commit f1d78e4

Browse files
committed
Added readCPUTemperature()
1 parent 401eb29 commit f1d78e4

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

cores/nRF5/wiring.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,44 @@ void systemOff(uint32_t pin, uint8_t wake_logic)
143143
NRF_POWER->SYSTEMOFF = 1;
144144
}
145145
}
146+
147+
148+
float readCPUTemperature()
149+
{
150+
// These are defined simply to make the ensuing code easier to read. Ready and triggers are 0x01, and not ready is 0x00.
151+
// Many other library functions use a literal 0x00 to reset flags and a literal 0x01 for triggers, and test "done"
152+
// flags for nonzero. In this function, we used the defined constants. Using 0x00 and 0x01 would probably be fine
153+
// because they're unlikely to ever change.
154+
const uint32_t temp_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Generated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos)
155+
&& TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk );
156+
157+
const uint32_t temp_not_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_NotGenerated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos)
158+
&& TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk );
159+
160+
const uint32_t temp_trigger = ( (TEMP_TASKS_START_TASKS_START_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos)
161+
&& TEMP_TASKS_START_TASKS_START_Msk );
162+
163+
const uint32_t temp_stop = ( (TEMP_TASKS_STOP_TASKS_STOP_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos)
164+
&& TEMP_TASKS_STOP_TASKS_STOP_Msk );
165+
166+
uint8_t en;
167+
int32_t temp;
168+
(void) sd_softdevice_is_enabled(&en);
169+
if (en)
170+
{
171+
sd_temp_get(&temp);
172+
}
173+
else
174+
{
175+
NRF_TEMP->EVENTS_DATARDY = temp_not_ready; // Only needed in case another function is also looking at this event flag
176+
NRF_TEMP->TASKS_START = temp_trigger;
177+
178+
bool done = false;
179+
while (NRF_TEMP->EVENTS_DATARDY != temp_ready);
180+
temp = NRF_TEMP->TEMP; // Per anomaly 29 (unclear whether still applicable), TASKS_STOP will clear the TEMP register.
181+
182+
NRF_TEMP->TASKS_STOP = temp_stop; // Per anomaly 30 (unclear whether still applicable), the temp peripheral needs to be shut down
183+
NRF_TEMP->EVENTS_DATARDY = temp_not_ready;
184+
}
185+
return temp / 4.0F;
186+
}

cores/nRF5/wiring.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ static inline bool isInISR(void)
4242
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ;
4343
}
4444

45+
/*
46+
* \brief Reads the on-chip temperature sensor, returning the temperature in degrees C
47+
* with a resolution of 0.25 degrees.
48+
*/
49+
float readCPUTemperature();
50+
4551
#ifdef __cplusplus
4652
}
4753
#endif

0 commit comments

Comments
 (0)