Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analogReference function to switch between default VDD/4 reference and internal reference for ADC. #1876

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion hal/inc/adc_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
#include "pinmap_hal.h"

/* Exported types ------------------------------------------------------------*/

#if (PLATFORM_ID == PLATFORM_ARGON) || (PLATFORM_ID == PLATFORM_BORON) || (PLATFORM_ID == PLATFORM_XENON)
typedef enum
{
AR_DEFAULT,
INTERNAL,
} vref_e;
#endif
/* Exported constants --------------------------------------------------------*/

/* Exported macros -----------------------------------------------------------*/
Expand All @@ -46,6 +52,10 @@ void HAL_ADC_Set_Sample_Time(uint8_t ADC_SampleTime);
int32_t HAL_ADC_Read(pin_t pin);
void HAL_ADC_DMA_Init();

#if (PLATFORM_ID == PLATFORM_ARGON) || (PLATFORM_ID == PLATFORM_BORON) || (PLATFORM_ID == PLATFORM_XENON)
void HAL_ADC_Set_VREF(vref_e v_e);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion hal/inc/hal_dynalib_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ DYNALIB_FN(34, hal_gpio, HAL_PWM_Get_Max_Frequency, uint32_t(uint16_t))
DYNALIB_FN(35, hal_gpio, HAL_Interrupts_Detach_Ext, void(uint16_t, uint8_t, void*))
DYNALIB_FN(36, hal_gpio, HAL_Set_Direct_Interrupt_Handler, int(IRQn_Type irqn, HAL_Direct_Interrupt_Handler handler, uint32_t flags, void* reserved))

DYNALIB_FN(37, hal_gpio, HAL_ADC_Set_VREF, void(vref_e))

DYNALIB_END(hal_gpio)

#endif /* HAL_DYNALIB_GPIO_H */

28 changes: 25 additions & 3 deletions hal/src/nRF52840/adc_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
#include "adc_hal.h"
#include "pinmap_impl.h"


static volatile bool m_adc_initiated = false;

static const nrfx_saadc_config_t saadc_config =
static const nrfx_saadc_config_t saadc_config =
{
.resolution = NRF_SAADC_RESOLUTION_12BIT,
.oversample = NRF_SAADC_OVERSAMPLE_DISABLED,
.interrupt_priority = NRFX_SAADC_CONFIG_IRQ_PRIORITY
};

nrf_saadc_reference_t VREF = NRF_SAADC_REFERENCE_VDD4;


static void analog_in_event_handler(nrfx_saadc_evt_t const *p_event)
{
(void) p_event;
Expand All @@ -39,6 +43,22 @@ void HAL_ADC_Set_Sample_Time(uint8_t ADC_SampleTime)
// deprecated
}

/*
* @brief @brief Set the ADC reference to either VDD / 4 (AR_DEFAULT) or the internal 0.6v (INTERNAL)
*/
#if (PLATFORM_ID == PLATFORM_ARGON) || (PLATFORM_ID == PLATFORM_BORON) || (PLATFORM_ID == PLATFORM_XENON)
void HAL_ADC_Set_VREF(vref_e v_e){
switch (v_e) {
case AR_DEFAULT: VREF = NRF_SAADC_REFERENCE_VDD4; break;

case INTERNAL: VREF = NRF_SAADC_REFERENCE_INTERNAL; break;

default: VREF = NRF_SAADC_REFERENCE_VDD4; break;
}

}
#endif

/*
* @brief Read the analog value of a pin.
* Should return a 16-bit value, 0-65536 (0 = LOW, 65536 = HIGH)
Expand Down Expand Up @@ -77,11 +97,11 @@ int32_t HAL_ADC_Read(uint16_t pin)
}

//Single ended, negative input to ADC shorted to GND.
nrf_saadc_channel_config_t channel_config = {
nrf_saadc_channel_config_t channel_config = {
.resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
.gain = NRF_SAADC_GAIN1_4, \
.reference = NRF_SAADC_REFERENCE_VDD4, \
.reference = VREF, \
.acq_time = NRF_SAADC_ACQTIME_10US, \
.mode = NRF_SAADC_MODE_SINGLE_ENDED, \
.burst = NRF_SAADC_BURST_DISABLED, \
Expand Down Expand Up @@ -117,6 +137,8 @@ int32_t HAL_ADC_Read(uint16_t pin)
return 0;
}



/*
* @brief Initialize the ADC peripheral.
*/
Expand Down
1 change: 1 addition & 0 deletions hal/src/template/adc_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int32_t HAL_ADC_Read(uint16_t pin)
return 0;
}


/*
* @brief Initialize the ADC peripheral.
*/
Expand Down
6 changes: 5 additions & 1 deletion user/tests/hal/adc/test_adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ Serial1LogHandler logHandler(115200);
/* executes once at startup */
void setup() {
HAL_ADC_DMA_Init();
#if (PLATFORM_ID == PLATFORM_ARGON) || (PLATFORM_ID == PLATFORM_BORON) || (PLATFORM_ID == PLATFORM_XENON)
HAL_ADC_Set_VREF(INTERNAL);
HAL_ADC_Set_VREF(AR_DEFAULT);
#endif
}

/* executes continuously after setup() runs */
void loop() {
Log.info("A0: %d, A1: %d, A2: %d, A3: %d, A4: %d, A5: %d",
Log.info("A0: %d, A1: %d, A2: %d, A3: %d, A4: %d, A5: %d",
HAL_ADC_Read(A0),
HAL_ADC_Read(A1),
HAL_ADC_Read(A2),
Expand Down
7 changes: 5 additions & 2 deletions wiring/inc/spark_wiring.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
******************************************************************************
* @file spark_wiring.h
* @author Satish Nair, Zachary Crockett, Zach Supalla and Mohit Bhoite
* @author Satish Nair, Zachary Crockett, Zach Supalla, Mohit Bhoite and Stuart Feichtinger
* @version V1.0.0
* @date 13-March-2013
* @brief Header for spark_wiring.c module
Expand Down Expand Up @@ -51,7 +51,6 @@
#include "spark_wiring_cloud.h"
#include "spark_wiring_rgb.h"
#include "spark_wiring_ticks.h"
#include "spark_wiring_nfc.h"

/* To prevent build error, we are undefining and redefining DAC here */
#undef DAC
Expand All @@ -67,6 +66,10 @@ extern "C" {
void setADCSampleTime(uint8_t ADC_SampleTime);
int32_t analogRead(uint16_t pin);

#if (PLATFORM_ID == PLATFORM_ARGON) || (PLATFORM_ID == PLATFORM_BORON) || (PLATFORM_ID == PLATFORM_XENON)
void analogReference(vref_e v);
#endif

/*
* GPIO
*/
Expand Down
11 changes: 10 additions & 1 deletion wiring_globals/src/spark_wiring_gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ int32_t digitalRead(pin_t pin)

return HAL_GPIO_Read(pin);
}
#if (PLATFORM_ID == PLATFORM_ARGON) || (PLATFORM_ID == PLATFORM_BORON) || (PLATFORM_ID == PLATFORM_XENON)
/*
* @brief Set the ADC reference to either VDD / 4 (AR_DEFAULT) or the internal 0.6v (INTERNAL)
*/

void analogReference(vref_e v){
HAL_ADC_Set_VREF(v);
}
#endif
/*
* @brief Read the analog value of a pin.
* Should return a 16-bit value, 0-65536 (0 = LOW, 65536 = HIGH)
Expand All @@ -178,6 +186,7 @@ int32_t analogRead(pin_t pin)
return HAL_ADC_Read(pin);
}


/*
* @brief Should take an integer 0-255 and create a 500Hz PWM signal with a duty cycle from 0-100%.
* On Photon, DAC1 and DAC2 act as true analog outputs(values: 0 to 4095) using onchip DAC peripheral
Expand Down Expand Up @@ -251,7 +260,7 @@ uint8_t analogWriteResolution(pin_t pin, uint8_t value)
HAL_PWM_Set_Resolution(pin, value);
return HAL_PWM_Get_Resolution(pin);
}


return 0;
}
Expand Down