diff --git a/hal/inc/adc_hal.h b/hal/inc/adc_hal.h index 862407651b..b6feb44beb 100644 --- a/hal/inc/adc_hal.h +++ b/hal/inc/adc_hal.h @@ -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 -----------------------------------------------------------*/ @@ -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 diff --git a/hal/inc/hal_dynalib_gpio.h b/hal/inc/hal_dynalib_gpio.h index 0bb3f76af6..ff8245bd4b 100644 --- a/hal/inc/hal_dynalib_gpio.h +++ b/hal/inc/hal_dynalib_gpio.h @@ -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 */ - diff --git a/hal/src/nRF52840/adc_hal.cpp b/hal/src/nRF52840/adc_hal.cpp index c9d182d61a..9f3181ff4b 100644 --- a/hal/src/nRF52840/adc_hal.cpp +++ b/hal/src/nRF52840/adc_hal.cpp @@ -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; @@ -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) @@ -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, \ @@ -117,6 +137,8 @@ int32_t HAL_ADC_Read(uint16_t pin) return 0; } + + /* * @brief Initialize the ADC peripheral. */ diff --git a/hal/src/template/adc_hal.cpp b/hal/src/template/adc_hal.cpp index ea202a7282..23d9685f03 100644 --- a/hal/src/template/adc_hal.cpp +++ b/hal/src/template/adc_hal.cpp @@ -39,6 +39,7 @@ int32_t HAL_ADC_Read(uint16_t pin) return 0; } + /* * @brief Initialize the ADC peripheral. */ diff --git a/user/tests/hal/adc/test_adc.cpp b/user/tests/hal/adc/test_adc.cpp index 115027d2c0..226bb7183a 100644 --- a/user/tests/hal/adc/test_adc.cpp +++ b/user/tests/hal/adc/test_adc.cpp @@ -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), diff --git a/wiring/inc/spark_wiring.h b/wiring/inc/spark_wiring.h index efd14d5422..fc7a4a5870 100644 --- a/wiring/inc/spark_wiring.h +++ b/wiring/inc/spark_wiring.h @@ -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 @@ -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 @@ -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 */ diff --git a/wiring_globals/src/spark_wiring_gpio.cpp b/wiring_globals/src/spark_wiring_gpio.cpp index 6169703619..3cb2a4547f 100644 --- a/wiring_globals/src/spark_wiring_gpio.cpp +++ b/wiring_globals/src/spark_wiring_gpio.cpp @@ -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) @@ -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 @@ -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; }