Skip to content

Commit 6731b47

Browse files
committed
nrf: Implement MCU temperature reading
If softdevice is available and enabled the SD function will be used, otherwise use MCU registers.
1 parent c6a542d commit 6731b47

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

ports/nrf/common-hal/microcontroller/Processor.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,45 @@
2525
*/
2626

2727
#include "common-hal/microcontroller/Processor.h"
28+
#include "py/runtime.h"
29+
30+
#ifdef BLUETOOTH_SD
31+
#include "nrf_sdm.h"
32+
#endif
33+
34+
#include "nrf.h"
2835

29-
// TODO port common_hal_mcu_processor
3036
float common_hal_mcu_processor_get_temperature(void) {
31-
return 0;
37+
int32_t temp = 0;
38+
39+
#ifdef BLUETOOTH_SD
40+
uint8_t sd_en = 0;
41+
42+
(void) sd_softdevice_is_enabled(&sd_en);
43+
44+
if (sd_en) {
45+
uint32_t err_code = sd_temp_get(&temp);
46+
if (err_code != NRF_SUCCESS) {
47+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
48+
"Can not get temperature. status: 0x" HEX2_FMT, (uint16_t)err_code));
49+
50+
return 0;
51+
}
52+
}
53+
#endif
54+
55+
NRF_TEMP->TASKS_START = 1;
56+
57+
while (NRF_TEMP->EVENTS_DATARDY == 0)
58+
;
59+
60+
NRF_TEMP->EVENTS_DATARDY = 0;
61+
62+
temp = NRF_TEMP->TEMP;
63+
64+
NRF_TEMP->TASKS_STOP = 1;
65+
66+
return temp / 4.0f;
3267
}
3368

3469
uint32_t common_hal_mcu_processor_get_frequency(void) {

0 commit comments

Comments
 (0)