|
| 1 | +#include "Arduino.h" |
| 2 | +#include "cyhal_adc.h" |
| 3 | + |
| 4 | +// Constants and Macros |
| 5 | +#define MIN_ACQUISITION_TIME 220 |
| 6 | +#define adc_assert(cy_ret) if (cy_ret != CY_RSLT_SUCCESS) { return 0xff; } |
| 7 | + |
| 8 | +// Type Definitions |
| 9 | +typedef struct { |
| 10 | + pin_size_t pin; |
| 11 | + cyhal_adc_channel_t chan_obj; |
| 12 | + bool initialized; |
| 13 | +} adc_channel_t; |
| 14 | + |
| 15 | +// Static Variables |
| 16 | +static cyhal_adc_t adc_obj = {0}; |
| 17 | +static cyhal_adc_vref_t desiredVRef = CYHAL_ADC_REF_VDDA; |
| 18 | +static bool adc_initialized = false; |
| 19 | +static adc_channel_t adc_channel[ADC_HOWMANY] = {0}; |
| 20 | + |
| 21 | +// Helper Function: Initialize ADC hardware if not already done |
| 22 | +static cy_rslt_t initialize_adc(pin_size_t pinNumber) { |
| 23 | + if (!adc_initialized) { |
| 24 | + cy_rslt_t status = cyhal_adc_init(&adc_obj, mapping_gpio_pin[pinNumber], NULL); |
| 25 | + adc_assert(status); |
| 26 | + adc_initialized = true; |
| 27 | + } |
| 28 | + |
| 29 | + // Configure ADC settings |
| 30 | + cyhal_adc_config_t adc_config = { |
| 31 | + .continuous_scanning = false, |
| 32 | + .resolution = ADC_RESOLUTION, |
| 33 | + .average_count = 1, |
| 34 | + .vneg = CYHAL_ADC_VNEG_VSSA, |
| 35 | + .vref = desiredVRef, |
| 36 | + .ext_vref = NC, |
| 37 | + .bypass_pin = NC |
| 38 | + }; |
| 39 | + |
| 40 | + return cyhal_adc_configure(&adc_obj, &adc_config); |
| 41 | +} |
| 42 | + |
| 43 | +static cy_rslt_t initialize_adc_channel(pin_size_t pinNumber, uint8_t adc_index) { |
| 44 | + // Ensure ADC hardware is initialized |
| 45 | + cy_rslt_t status = initialize_adc(pinNumber); |
| 46 | + adc_assert(status); |
| 47 | + |
| 48 | + // Configure individual ADC channel |
| 49 | + cyhal_adc_channel_config_t chan_config = { |
| 50 | + .enabled = true, |
| 51 | + .enable_averaging = false, |
| 52 | + .min_acquisition_ns = MIN_ACQUISITION_TIME |
| 53 | + }; |
| 54 | + |
| 55 | + status = cyhal_adc_channel_init_diff(&adc_channel[adc_index].chan_obj, &adc_obj, mapping_gpio_pin[pinNumber], CYHAL_ADC_VNEG, &chan_config); |
| 56 | + adc_assert(status); |
| 57 | + |
| 58 | + adc_channel[adc_index].pin = pinNumber; |
| 59 | + adc_channel[adc_index].initialized = true; |
| 60 | + |
| 61 | + return status; |
| 62 | +} |
| 63 | + |
| 64 | +int analogRead(pin_size_t pinNumber) { |
| 65 | + int adc_value = 0; |
| 66 | + uint8_t adc_index = 0; |
| 67 | + |
| 68 | + // Find existing channel or initialize a new one |
| 69 | + for (adc_index = 0; adc_index < ADC_HOWMANY; adc_index++) { |
| 70 | + if (adc_channel[adc_index].initialized) { |
| 71 | + if (adc_channel[adc_index].pin == pinNumber) { |
| 72 | + // Found an existing channel for the pin |
| 73 | + break; |
| 74 | + } |
| 75 | + } else { |
| 76 | + // Found an uninitialized channel, initialize it |
| 77 | + cy_rslt_t result = initialize_adc_channel(pinNumber, adc_index); |
| 78 | + adc_assert(result); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (adc_index < ADC_HOWMANY) { |
| 84 | + adc_value = cyhal_adc_read(&adc_channel[adc_index].chan_obj); |
| 85 | + } |
| 86 | + return adc_value; |
| 87 | +} |
| 88 | + |
| 89 | +void analogReference(uint8_t mode) { |
| 90 | + desiredVRef = CYHAL_ADC_REF_VDDA; |
| 91 | +} |
0 commit comments