From 6731b47d5aee2c90b4eb15cfd3dc283427dc0ef8 Mon Sep 17 00:00:00 2001 From: arturo182 Date: Fri, 2 Feb 2018 20:33:21 +0100 Subject: [PATCH] nrf: Implement MCU temperature reading If softdevice is available and enabled the SD function will be used, otherwise use MCU registers. --- .../common-hal/microcontroller/Processor.c | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/ports/nrf/common-hal/microcontroller/Processor.c b/ports/nrf/common-hal/microcontroller/Processor.c index ba5aa09327..cf808cdf4c 100644 --- a/ports/nrf/common-hal/microcontroller/Processor.c +++ b/ports/nrf/common-hal/microcontroller/Processor.c @@ -25,10 +25,45 @@ */ #include "common-hal/microcontroller/Processor.h" +#include "py/runtime.h" + +#ifdef BLUETOOTH_SD +#include "nrf_sdm.h" +#endif + +#include "nrf.h" -// TODO port common_hal_mcu_processor float common_hal_mcu_processor_get_temperature(void) { - return 0; + int32_t temp = 0; + +#ifdef BLUETOOTH_SD + uint8_t sd_en = 0; + + (void) sd_softdevice_is_enabled(&sd_en); + + if (sd_en) { + uint32_t err_code = sd_temp_get(&temp); + if (err_code != NRF_SUCCESS) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, + "Can not get temperature. status: 0x" HEX2_FMT, (uint16_t)err_code)); + + return 0; + } + } +#endif + + NRF_TEMP->TASKS_START = 1; + + while (NRF_TEMP->EVENTS_DATARDY == 0) + ; + + NRF_TEMP->EVENTS_DATARDY = 0; + + temp = NRF_TEMP->TEMP; + + NRF_TEMP->TASKS_STOP = 1; + + return temp / 4.0f; } uint32_t common_hal_mcu_processor_get_frequency(void) {