Skip to content

Commit 3dda52f

Browse files
authored
Merge pull request #683 from ajs123/master
Add SAADC offset calibration and reading of CPU temperature
2 parents c19516d + df35645 commit 3dda52f

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

cores/nRF5/wiring.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,27 @@ void systemOff(uint32_t pin, uint8_t wake_logic)
143143
NRF_POWER->SYSTEMOFF = 1;
144144
}
145145
}
146+
147+
148+
float readCPUTemperature( void )
149+
{
150+
uint8_t en;
151+
int32_t temp;
152+
(void) sd_softdevice_is_enabled(&en);
153+
if (en)
154+
{
155+
sd_temp_get(&temp);
156+
}
157+
else
158+
{
159+
NRF_TEMP->EVENTS_DATARDY = 0x00; // Only needed in case another function is also looking at this event flag
160+
NRF_TEMP->TASKS_START = 0x01;
161+
162+
while (!NRF_TEMP->EVENTS_DATARDY);
163+
temp = NRF_TEMP->TEMP; // Per anomaly 29 (unclear whether still applicable), TASKS_STOP will clear the TEMP register.
164+
165+
NRF_TEMP->TASKS_STOP = 0x01; // Per anomaly 30 (unclear whether still applicable), the temp peripheral needs to be shut down
166+
NRF_TEMP->EVENTS_DATARDY = 0x00;
167+
}
168+
return temp / 4.0F;
169+
}

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( void );
50+
4551
#ifdef __cplusplus
4652
}
4753
#endif

cores/nRF5/wiring_analog.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ extern void analogWriteResolution(uint8_t res);
116116
*/
117117
extern void analogSampleTime(uint8_t sTime);
118118

119+
/*
120+
* \brief Calibrate the ADC offset. This is recommended occasionally, or any time the
121+
* chip temperature changes by more than 10 C. See the SAADC section of the Nordic
122+
* nrf52 product pecification.
123+
*
124+
*/
125+
extern void analogCalibrateOffset( void );
126+
119127
extern void analogOutputInit( void ) ;
120128

121129
#ifdef __cplusplus

cores/nRF5/wiring_analog_nRF52.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,25 @@ uint32_t analogReadVDD( void )
284284
return analogRead_internal(SAADC_CH_PSELP_PSELP_VDD);
285285
}
286286

287+
void analogCalibrateOffset( void )
288+
{
289+
// Enable the SAADC
290+
NRF_SAADC->ENABLE = 0x01;
291+
292+
// Be sure the done flag is cleared, then trigger offset calibration
293+
NRF_SAADC->EVENTS_CALIBRATEDONE = 0x00;
294+
NRF_SAADC->TASKS_CALIBRATEOFFSET = 0x01;
295+
296+
// Wait for completion
297+
while (!NRF_SAADC->EVENTS_CALIBRATEDONE);
298+
299+
// Clear the done flag (really shouldn't have to do this both times)
300+
NRF_SAADC->EVENTS_CALIBRATEDONE = 0x00;
301+
302+
// Disable the SAADC
303+
NRF_SAADC->ENABLE = 0x00;
304+
}
305+
287306
#ifdef __cplusplus
288307
}
289308
#endif

0 commit comments

Comments
 (0)