From 401eb29ca56cf3d32621ca0ae68ebb7f9a3d29b2 Mon Sep 17 00:00:00 2001 From: ajs123 Date: Tue, 3 Aug 2021 20:08:49 -0400 Subject: [PATCH 1/5] Added SAADC offset calibration --- cores/nRF5/wiring_analog.h | 8 +++++++ cores/nRF5/wiring_analog_nRF52.c | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/cores/nRF5/wiring_analog.h b/cores/nRF5/wiring_analog.h index 7d8e2275..05981a8d 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(); + extern void analogOutputInit( void ) ; #ifdef __cplusplus diff --git a/cores/nRF5/wiring_analog_nRF52.c b/cores/nRF5/wiring_analog_nRF52.c index ee6b94de..e8fa82fe 100644 --- a/cores/nRF5/wiring_analog_nRF52.c +++ b/cores/nRF5/wiring_analog_nRF52.c @@ -284,6 +284,45 @@ uint32_t analogReadVDD( void ) return analogRead_internal(SAADC_CH_PSELP_PSELP_VDD); } +analogCalibrateOffset() +{ + const uint32_t calibrate_done = ( (SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Generated << + SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Pos ) + && SAADC_EVENTS_CH_LIMITH_LIMITH_Msk ); + + const uint32_t calibrate_not_done = ( (SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_NotGenerated << + SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Pos ) + && SAADC_EVENTS_CH_LIMITH_LIMITH_Msk ); + + const uint32_t saadc_enable = ( (SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos) + && SAADC_ENABLE_ENABLE_Msk ); + + const uint32_t saadc_disable = ( (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos) + && SAADC_ENABLE_ENABLE_Msk ); + + const uint32_t calibrate_trigger = ( (SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Trigger << + SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Pos) + && SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Msk ); + + // Enable the SAADC + NRF_SAADC->ENABLE = saadc_enable; + + // Be sure the done flag is cleared, then trigger offset calibration + NRF_SAADC->EVENTS_CALIBRATEDONE = calibrate_not_done; + NRF_SAADC->TASKS_CALIBRATEOFFSET = calibrate_trigger; + + // Wait for completion + while (NRF_SAADC->EVENTS_CALIBRATEDONE != calibrate_done); + //bool done = false; + //for (int i = 0; (i < 20) && !(done = (NRF_SAADC->EVENTS_CALIBRATEDONE == calibrate_done)); i++ ) delay(1); + + // Clear the done flag (really shouldn't have to do this both times) + NRF_SAADC->EVENTS_CALIBRATEDONE = calibrate_not_done; + + // Disable the SAADC + NRF_SAADC->ENABLE = saadc_disable; +} + #ifdef __cplusplus } #endif From f1d78e488e3e090cb3d05bf6e92afeaf85dc490d Mon Sep 17 00:00:00 2001 From: ajs123 Date: Tue, 3 Aug 2021 20:09:34 -0400 Subject: [PATCH 2/5] Added readCPUTemperature() --- cores/nRF5/wiring.c | 41 +++++++++++++++++++++++++++++++++++++++++ cores/nRF5/wiring.h | 6 ++++++ 2 files changed, 47 insertions(+) diff --git a/cores/nRF5/wiring.c b/cores/nRF5/wiring.c index 134ee539..5fa08608 100644 --- a/cores/nRF5/wiring.c +++ b/cores/nRF5/wiring.c @@ -143,3 +143,44 @@ void systemOff(uint32_t pin, uint8_t wake_logic) NRF_POWER->SYSTEMOFF = 1; } } + + +float readCPUTemperature() +{ + // These are defined simply to make the ensuing code easier to read. Ready and triggers are 0x01, and not ready is 0x00. + // Many other library functions use a literal 0x00 to reset flags and a literal 0x01 for triggers, and test "done" + // flags for nonzero. In this function, we used the defined constants. Using 0x00 and 0x01 would probably be fine + // because they're unlikely to ever change. + const uint32_t temp_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Generated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos) + && TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk ); + + const uint32_t temp_not_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_NotGenerated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos) + && TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk ); + + const uint32_t temp_trigger = ( (TEMP_TASKS_START_TASKS_START_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos) + && TEMP_TASKS_START_TASKS_START_Msk ); + + const uint32_t temp_stop = ( (TEMP_TASKS_STOP_TASKS_STOP_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos) + && TEMP_TASKS_STOP_TASKS_STOP_Msk ); + + uint8_t en; + int32_t temp; + (void) sd_softdevice_is_enabled(&en); + if (en) + { + sd_temp_get(&temp); + } + else + { + NRF_TEMP->EVENTS_DATARDY = temp_not_ready; // Only needed in case another function is also looking at this event flag + NRF_TEMP->TASKS_START = temp_trigger; + + bool done = false; + while (NRF_TEMP->EVENTS_DATARDY != temp_ready); + temp = NRF_TEMP->TEMP; // Per anomaly 29 (unclear whether still applicable), TASKS_STOP will clear the TEMP register. + + NRF_TEMP->TASKS_STOP = temp_stop; // Per anomaly 30 (unclear whether still applicable), the temp peripheral needs to be shut down + NRF_TEMP->EVENTS_DATARDY = temp_not_ready; + } + return temp / 4.0F; +} diff --git a/cores/nRF5/wiring.h b/cores/nRF5/wiring.h index a557f547..279abfcf 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(); + #ifdef __cplusplus } #endif From 511573a5ced7e18c2e88f7e0caa3a06159f6b7ae Mon Sep 17 00:00:00 2001 From: ajs123 Date: Wed, 4 Aug 2021 07:54:48 -0400 Subject: [PATCH 3/5] Add offset calibration --- cores/nRF5/wiring_analog_nRF52.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/cores/nRF5/wiring_analog_nRF52.c b/cores/nRF5/wiring_analog_nRF52.c index e8fa82fe..1b3eaa02 100644 --- a/cores/nRF5/wiring_analog_nRF52.c +++ b/cores/nRF5/wiring_analog_nRF52.c @@ -286,41 +286,21 @@ uint32_t analogReadVDD( void ) analogCalibrateOffset() { - const uint32_t calibrate_done = ( (SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Generated << - SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Pos ) - && SAADC_EVENTS_CH_LIMITH_LIMITH_Msk ); - - const uint32_t calibrate_not_done = ( (SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_NotGenerated << - SAADC_EVENTS_CALIBRATEDONE_EVENTS_CALIBRATEDONE_Pos ) - && SAADC_EVENTS_CH_LIMITH_LIMITH_Msk ); - - const uint32_t saadc_enable = ( (SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos) - && SAADC_ENABLE_ENABLE_Msk ); - - const uint32_t saadc_disable = ( (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos) - && SAADC_ENABLE_ENABLE_Msk ); - - const uint32_t calibrate_trigger = ( (SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Trigger << - SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Pos) - && SAADC_TASKS_CALIBRATEOFFSET_TASKS_CALIBRATEOFFSET_Msk ); - // Enable the SAADC - NRF_SAADC->ENABLE = saadc_enable; + NRF_SAADC->ENABLE = 0x01; // Be sure the done flag is cleared, then trigger offset calibration - NRF_SAADC->EVENTS_CALIBRATEDONE = calibrate_not_done; - NRF_SAADC->TASKS_CALIBRATEOFFSET = calibrate_trigger; + NRF_SAADC->EVENTS_CALIBRATEDONE = 0x00; + NRF_SAADC->TASKS_CALIBRATEOFFSET = 0x01; // Wait for completion - while (NRF_SAADC->EVENTS_CALIBRATEDONE != calibrate_done); - //bool done = false; - //for (int i = 0; (i < 20) && !(done = (NRF_SAADC->EVENTS_CALIBRATEDONE == calibrate_done)); i++ ) delay(1); + while (!NRF_SAADC->EVENTS_CALIBRATEDONE); // Clear the done flag (really shouldn't have to do this both times) - NRF_SAADC->EVENTS_CALIBRATEDONE = calibrate_not_done; + NRF_SAADC->EVENTS_CALIBRATEDONE = 0x00; // Disable the SAADC - NRF_SAADC->ENABLE = saadc_disable; + NRF_SAADC->ENABLE = 0x00; } #ifdef __cplusplus From 6227526699383198a1b88ff0f269fae6334e196e Mon Sep 17 00:00:00 2001 From: ajs123 Date: Wed, 4 Aug 2021 07:55:01 -0400 Subject: [PATCH 4/5] Add reading of CPU temp --- cores/nRF5/wiring.c | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/cores/nRF5/wiring.c b/cores/nRF5/wiring.c index 5fa08608..1e4c465a 100644 --- a/cores/nRF5/wiring.c +++ b/cores/nRF5/wiring.c @@ -147,22 +147,6 @@ void systemOff(uint32_t pin, uint8_t wake_logic) float readCPUTemperature() { - // These are defined simply to make the ensuing code easier to read. Ready and triggers are 0x01, and not ready is 0x00. - // Many other library functions use a literal 0x00 to reset flags and a literal 0x01 for triggers, and test "done" - // flags for nonzero. In this function, we used the defined constants. Using 0x00 and 0x01 would probably be fine - // because they're unlikely to ever change. - const uint32_t temp_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Generated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos) - && TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk ); - - const uint32_t temp_not_ready = ( (TEMP_EVENTS_DATARDY_EVENTS_DATARDY_NotGenerated << TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Pos) - && TEMP_EVENTS_DATARDY_EVENTS_DATARDY_Msk ); - - const uint32_t temp_trigger = ( (TEMP_TASKS_START_TASKS_START_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos) - && TEMP_TASKS_START_TASKS_START_Msk ); - - const uint32_t temp_stop = ( (TEMP_TASKS_STOP_TASKS_STOP_Trigger << TEMP_TASKS_STOP_TASKS_STOP_Pos) - && TEMP_TASKS_STOP_TASKS_STOP_Msk ); - uint8_t en; int32_t temp; (void) sd_softdevice_is_enabled(&en); @@ -172,15 +156,14 @@ float readCPUTemperature() } else { - NRF_TEMP->EVENTS_DATARDY = temp_not_ready; // Only needed in case another function is also looking at this event flag - NRF_TEMP->TASKS_START = temp_trigger; + NRF_TEMP->EVENTS_DATARDY = 0x00; // Only needed in case another function is also looking at this event flag + NRF_TEMP->TASKS_START = 0x01; - bool done = false; - while (NRF_TEMP->EVENTS_DATARDY != temp_ready); + 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 = temp_stop; // Per anomaly 30 (unclear whether still applicable), the temp peripheral needs to be shut down - NRF_TEMP->EVENTS_DATARDY = temp_not_ready; + 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; } From df356456c7527941075ad1768351ee0422f881a0 Mon Sep 17 00:00:00 2001 From: ajs123 Date: Thu, 5 Aug 2021 19:11:22 -0400 Subject: [PATCH 5/5] Add SAADC offset cal and read CPU temp --- cores/nRF5/wiring.c | 2 +- cores/nRF5/wiring.h | 2 +- cores/nRF5/wiring_analog.h | 2 +- cores/nRF5/wiring_analog_nRF52.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/nRF5/wiring.c b/cores/nRF5/wiring.c index 1e4c465a..8ae5770e 100644 --- a/cores/nRF5/wiring.c +++ b/cores/nRF5/wiring.c @@ -145,7 +145,7 @@ void systemOff(uint32_t pin, uint8_t wake_logic) } -float readCPUTemperature() +float readCPUTemperature( void ) { uint8_t en; int32_t temp; diff --git a/cores/nRF5/wiring.h b/cores/nRF5/wiring.h index 279abfcf..e1388aed 100644 --- a/cores/nRF5/wiring.h +++ b/cores/nRF5/wiring.h @@ -46,7 +46,7 @@ static inline bool isInISR(void) * \brief Reads the on-chip temperature sensor, returning the temperature in degrees C * with a resolution of 0.25 degrees. */ -float readCPUTemperature(); +float readCPUTemperature( void ); #ifdef __cplusplus } diff --git a/cores/nRF5/wiring_analog.h b/cores/nRF5/wiring_analog.h index 05981a8d..e4e3e325 100644 --- a/cores/nRF5/wiring_analog.h +++ b/cores/nRF5/wiring_analog.h @@ -122,7 +122,7 @@ extern void analogSampleTime(uint8_t sTime); * nrf52 product pecification. * */ -extern void analogCalibrateOffset(); +extern void analogCalibrateOffset( void ); extern void analogOutputInit( void ) ; diff --git a/cores/nRF5/wiring_analog_nRF52.c b/cores/nRF5/wiring_analog_nRF52.c index 1b3eaa02..e7dde3f3 100644 --- a/cores/nRF5/wiring_analog_nRF52.c +++ b/cores/nRF5/wiring_analog_nRF52.c @@ -284,7 +284,7 @@ uint32_t analogReadVDD( void ) return analogRead_internal(SAADC_CH_PSELP_PSELP_VDD); } -analogCalibrateOffset() +void analogCalibrateOffset( void ) { // Enable the SAADC NRF_SAADC->ENABLE = 0x01;