Skip to content

Commit 401eb29

Browse files
committed
Added SAADC offset calibration
1 parent c19516d commit 401eb29

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

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();
126+
119127
extern void analogOutputInit( void ) ;
120128

121129
#ifdef __cplusplus

cores/nRF5/wiring_analog_nRF52.c

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

287+
analogCalibrateOffset()
288+
{
289+
const uint32_t calibrate_done = ( (SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Generated <<
290+
SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Pos )
291+
&& SAADC_EVENTS_CH_LIMITH_LIMITH_Msk );
292+
293+
const uint32_t calibrate_not_done = ( (SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_NotGenerated <<
294+
SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Pos )
295+
&& SAADC_EVENTS_CH_LIMITH_LIMITH_Msk );
296+
297+
const uint32_t saadc_enable = ( (SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos)
298+
&& SAADC_ENABLE_ENABLE_Msk );
299+
300+
const uint32_t saadc_disable = ( (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos)
301+
&& SAADC_ENABLE_ENABLE_Msk );
302+
303+
const uint32_t calibrate_trigger = ( (SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Trigger <<
304+
SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Pos)
305+
&& SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Msk );
306+
307+
// Enable the SAADC
308+
NRF_SAADC->ENABLE = saadc_enable;
309+
310+
// Be sure the done flag is cleared, then trigger offset calibration
311+
NRF_SAADC->EVENTS_CALIBRATEDONE = calibrate_not_done;
312+
NRF_SAADC->TASKS_CALIBRATEOFFSET = calibrate_trigger;
313+
314+
// Wait for completion
315+
while (NRF_SAADC->EVENTS_CALIBRATEDONE != calibrate_done);
316+
//bool done = false;
317+
//for (int i = 0; (i < 20) && !(done = (NRF_SAADC->EVENTS_CALIBRATEDONE == calibrate_done)); i++ ) delay(1);
318+
319+
// Clear the done flag (really shouldn't have to do this both times)
320+
NRF_SAADC->EVENTS_CALIBRATEDONE = calibrate_not_done;
321+
322+
// Disable the SAADC
323+
NRF_SAADC->ENABLE = saadc_disable;
324+
}
325+
287326
#ifdef __cplusplus
288327
}
289328
#endif

0 commit comments

Comments
 (0)