diff --git a/cores/nRF5/wiring.c b/cores/nRF5/wiring.c index 134ee539..8ae5770e 100644 --- a/cores/nRF5/wiring.c +++ b/cores/nRF5/wiring.c @@ -143,3 +143,27 @@ void systemOff(uint32_t pin, uint8_t wake_logic) NRF_POWER->SYSTEMOFF = 1; } } + + +float readCPUTemperature( void ) +{ + uint8_t en; + int32_t temp; + (void) sd_softdevice_is_enabled(&en); + if (en) + { + sd_temp_get(&temp); + } + else + { + NRF_TEMP->EVENTS_DATARDY = 0x00; // Only needed in case another function is also looking at this event flag + NRF_TEMP->TASKS_START = 0x01; + + while (!NRF_TEMP->EVENTS_DATARDY); + temp = NRF_TEMP->TEMP; // Per anomaly 29 (unclear whether still applicable), TASKS_STOP will clear the TEMP register. + + NRF_TEMP->TASKS_STOP = 0x01; // Per anomaly 30 (unclear whether still applicable), the temp peripheral needs to be shut down + NRF_TEMP->EVENTS_DATARDY = 0x00; + } + return temp / 4.0F; +} diff --git a/cores/nRF5/wiring.h b/cores/nRF5/wiring.h index a557f547..e1388aed 100644 --- a/cores/nRF5/wiring.h +++ b/cores/nRF5/wiring.h @@ -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( void ); + #ifdef __cplusplus } #endif diff --git a/cores/nRF5/wiring_analog.h b/cores/nRF5/wiring_analog.h index 7d8e2275..e4e3e325 100644 --- a/cores/nRF5/wiring_analog.h +++ b/cores/nRF5/wiring_analog.h @@ -116,6 +116,14 @@ extern void analogWriteResolution(uint8_t res); */ extern void analogSampleTime(uint8_t sTime); +/* + * \brief Calibrate the ADC offset. This is recommended occasionally, or any time the + * chip temperature changes by more than 10 C. See the SAADC section of the Nordic + * nrf52 product pecification. + * + */ +extern void analogCalibrateOffset( void ); + extern void analogOutputInit( void ) ; #ifdef __cplusplus diff --git a/cores/nRF5/wiring_analog_nRF52.c b/cores/nRF5/wiring_analog_nRF52.c index ee6b94de..e7dde3f3 100644 --- a/cores/nRF5/wiring_analog_nRF52.c +++ b/cores/nRF5/wiring_analog_nRF52.c @@ -284,6 +284,25 @@ uint32_t analogReadVDD( void ) return analogRead_internal(SAADC_CH_PSELP_PSELP_VDD); } +void analogCalibrateOffset( void ) +{ + // Enable the SAADC + NRF_SAADC->ENABLE = 0x01; + + // Be sure the done flag is cleared, then trigger offset calibration + NRF_SAADC->EVENTS_CALIBRATEDONE = 0x00; + NRF_SAADC->TASKS_CALIBRATEOFFSET = 0x01; + + // Wait for completion + while (!NRF_SAADC->EVENTS_CALIBRATEDONE); + + // Clear the done flag (really shouldn't have to do this both times) + NRF_SAADC->EVENTS_CALIBRATEDONE = 0x00; + + // Disable the SAADC + NRF_SAADC->ENABLE = 0x00; +} + #ifdef __cplusplus } #endif