@@ -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+ }
0 commit comments