Example Program for EVAL-AD7606
Dependencies: platform_drivers
Diff: app/iio_ad7606.c
- Revision:
- 7:054dbd5e1f45
- Parent:
- 6:32de160dce43
--- a/app/iio_ad7606.c Wed Jul 21 11:16:56 2021 +0100 +++ b/app/iio_ad7606.c Tue Aug 03 11:54:49 2021 +0100 @@ -29,7 +29,7 @@ #include "error.h" #include "ad7606.h" -#include "ad7606_data_capture.h" +#include "adc_data_capture.h" #include "ad7606_support.h" #include "ad7606_user_config.h" @@ -68,6 +68,22 @@ #define OFFSET_REG_RESOLUTION 1 #endif +/* Bytes per sample. This count should divide the total 256 bytes into 'n' equivalent + * ADC samples as IIO library requests only 256bytes of data at a time in a given + * data read query. + * For 1 to 8-bit ADC, bytes per sample = 1 (2^0) + * For 9 to 16-bit ADC, bytes per sample = 2 (2^1) + * For 17 to 32-bit ADC, bytes per sample = 4 (2^2) + **/ +#if (AD7606X_ADC_RESOLUTION == 18) +#define BYTES_PER_SAMPLE sizeof(uint32_t) // For ADC resolution of 18-bits +#else +#define BYTES_PER_SAMPLE sizeof(uint16_t) // For ADC resolution of 16-bits +#endif + +/* Number of data storage bits (needed for IIO client to plot ADC data) */ +#define CHN_STORAGE_BITS (BYTES_PER_SAMPLE * 8) + /******************************************************************************/ /*************************** Types Declarations *******************************/ /******************************************************************************/ @@ -83,7 +99,7 @@ /** * Pointer to the struct representing the AD7606 IIO device */ -static struct ad7606_dev *p_ad7606_dev_inst = NULL; +struct ad7606_dev *p_ad7606_dev_inst = NULL; /* Device attributes with default values */ @@ -282,8 +298,9 @@ int32_t adc_data_raw; /* Capture the raw adc data */ - adc_data_raw = single_data_read(device, channel->ch_num, - attr_polarity_val[channel->ch_num]); + if (read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw) != SUCCESS) { + return -EINVAL; + } return (ssize_t) sprintf(buf, "%d", adc_data_raw); } @@ -834,7 +851,7 @@ const struct iio_ch_info *channel, intptr_t id) { - int32_t adc_chn_data = 0; + int32_t adc_data_raw = 0; float temperature; float voltage; @@ -849,11 +866,10 @@ udelay(100); /* Sample the channel and read conversion result */ - adc_chn_data = single_data_read(device, channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw); /* Convert ADC data into equivalent voltage */ - voltage = convert_adc_raw_to_voltage(adc_chn_data, + voltage = convert_adc_raw_to_voltage(adc_data_raw, attr_scale_val[channel->ch_num]); /* Obtain the temperature using equation specified in device datasheet */ @@ -899,7 +915,7 @@ intptr_t id) { float vref_voltge; - int32_t adc_chn_data; + int32_t adc_data_raw; /* Configure the channel multiplexer to select Vref read */ if (ad7606_spi_write_mask(device, @@ -912,11 +928,10 @@ udelay(100); /* Sample the channel and read conversion result */ - adc_chn_data = single_data_read(device, channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw); /* Convert ADC data into equivalent voltage */ - vref_voltge = convert_adc_raw_to_voltage(adc_chn_data, + vref_voltge = convert_adc_raw_to_voltage(adc_data_raw, attr_scale_val[channel->ch_num]); /* Divide by 4 since Vref Mux has 4x multiplier on it */ @@ -962,7 +977,7 @@ intptr_t id) { float vdrive_voltge; - int32_t adc_chn_data; + int32_t adc_data_raw; /* Configure the channel multiplexer to select Vdrive read */ if (ad7606_spi_write_mask(device, @@ -975,11 +990,10 @@ udelay(100); /* Sample the channel and read conversion result */ - adc_chn_data = single_data_read(device, channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw); /* Convert ADC data into equivalent voltage */ - vdrive_voltge = convert_adc_raw_to_voltage(adc_chn_data, + vdrive_voltge = convert_adc_raw_to_voltage(adc_data_raw, attr_scale_val[channel->ch_num]); /* Change channel mux back to analog input */ @@ -1022,7 +1036,7 @@ intptr_t id) { float aldo_voltge; - int32_t adc_chn_data; + int32_t adc_data_raw; /* Configure the channel multiplexer to select ALDO read */ if (ad7606_spi_write_mask(device, @@ -1035,11 +1049,10 @@ udelay(100); /* Sample the channel and read conversion result */ - adc_chn_data = single_data_read(device, channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw); /* Convert ADC data into equivalent voltage */ - aldo_voltge = convert_adc_raw_to_voltage(adc_chn_data, + aldo_voltge = convert_adc_raw_to_voltage(adc_data_raw, attr_scale_val[channel->ch_num]); /* Divide by 4 since ALDO Mux has 4x multiplier on it */ @@ -1085,7 +1098,7 @@ intptr_t id) { float dldo_voltge; - int32_t adc_chn_data; + int32_t adc_data_raw; /* Configure the channel multiplexer to select DLDO read */ if (ad7606_spi_write_mask(device, @@ -1098,12 +1111,10 @@ udelay(100); /* Sample the channel and read conversion result */ - adc_chn_data = single_data_read(device, - channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&adc_data_raw); /* Convert ADC data into equivalent voltage */ - dldo_voltge = convert_adc_raw_to_voltage(adc_chn_data, + dldo_voltge = convert_adc_raw_to_voltage(adc_data_raw, attr_scale_val[channel->ch_num]); /* Divide by 4 since ALDO Mux has 4x multiplier on it */ @@ -1156,18 +1167,14 @@ do { if (ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_QUEUE, 1) == SUCCESS) { /* Read the ADC on selected chnnel (first reading post open circuit detection start) */ - prev_adc_code = single_data_read(device, - channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&prev_adc_code); /* Perform N conversions and monitor the code delta */ for (cnt = 0; cnt < MANUAL_OPEN_DETECT_CONV_CNTS; cnt++) { /* Check if code is within 350LSB (nearest ZS code) */ if (prev_adc_code >= 0 && prev_adc_code < MANUAL_OPEN_DETECT_ENTRY_TRHLD) { /* Perform next conversion and read the result */ - curr_adc_code = single_data_read(device, - channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&curr_adc_code); /* Check if delta b/w current and previus reading is within 10 LSB code */ if (abs(curr_adc_code - prev_adc_code) > MANUAL_OPEN_DETECT_CONV_TRSHLD) { @@ -1195,9 +1202,7 @@ /* Perform next conversions (~2-3) and read the result (with common mode set high) */ for (cnt = 0; cnt < MANUAL_OPEN_DETECT_CM_CNV_CNT; cnt++) { udelay(100); - curr_adc_code = single_data_read(device, - channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&curr_adc_code); } /* Check if delta b/w common mode high code and previous N conversion code is > threshold */ @@ -1218,9 +1223,7 @@ AD7606_REG_OPEN_DETECT_ENABLE, 0) == SUCCESS) { /* Perform next conversion and read the result (with common mode set low) */ - curr_adc_code = single_data_read(device, - channel->ch_num, - attr_polarity_val[channel->ch_num]); + read_single_sample(channel->ch_num, (uint32_t *)&curr_adc_code); /* Check if delta b/w common mode low code and previous N conversion code is < threshold */ if (abs(curr_adc_code - prev_adc_code) < MANUAL_OPEN_DETECT_THRESHOLD_RPD50K) { @@ -1395,7 +1398,7 @@ float lsb_voltage; float adc_voltage; polarity_e polarity = attr_polarity_val[channel->ch_num]; - int32_t adc_data; + int32_t adc_raw_data; int8_t chn_offset; /* Perform the system offset calibration */ @@ -1407,11 +1410,10 @@ } /* Sample and read the ADC channel */ - adc_data = single_data_read(device, channel->ch_num, - polarity); + read_single_sample(channel->ch_num, (uint32_t *)&adc_raw_data); /* Get an equivalent ADC voltage */ - adc_voltage = convert_adc_raw_to_voltage(adc_data, + adc_voltage = convert_adc_raw_to_voltage(adc_raw_data, attr_scale_val[channel->ch_num]); /* Calculate the channel offset and write it to offset register */ @@ -1553,12 +1555,16 @@ uint32_t ch_mask) { if (adc_data_capture_started == false) { - start_background_data_capture(ch_mask, bytes_count); + start_data_capture(ch_mask, AD7606X_ADC_CHANNELS); adc_data_capture_started = true; } /* Read the buffered data */ - return (ssize_t)buffered_data_read(pbuf, bytes_count, offset, ch_mask); + return (ssize_t)read_buffered_data(pbuf, + bytes_count, + offset, + ch_mask, + BYTES_PER_SAMPLE); } @@ -1579,7 +1585,7 @@ * iio_ad7606_read_data() function */ /* Store the requested samples count value for data capture */ - store_requested_samples_count(bytes_count); + store_requested_samples_count(bytes_count, BYTES_PER_SAMPLE); return SUCCESS; } @@ -1605,7 +1611,7 @@ static int32_t iio_ad7606_stop_transfer(void *dev) { adc_data_capture_started = false; - stop_background_data_capture(); + stop_data_capture(); return SUCCESS; } @@ -1688,7 +1694,6 @@ .store = set_chn_calibrate_adc_gain, }, #if defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16) - &iio_attr_bandwidth { .name = "bandwidth", .show = get_bandwidth, @@ -1750,13 +1755,8 @@ /* IIOD channels configurations */ struct scan_type chn_scan = { .sign = 's', -#if (AD7606X_ADC_RESOLUTION == 18) - .realbits = 18, - .storagebits = 18, -#else - .realbits = 16, - .storagebits = 16, -#endif + .realbits = CHN_STORAGE_BITS, + .storagebits = CHN_STORAGE_BITS, .shift = 0, .is_big_endian = false }; @@ -2008,12 +2008,6 @@ return init_status; } - /* Init the data capture for AD7606 IIO app */ - init_status = iio_data_capture_init(&p_ad7606_dev_inst); - if (init_status != SUCCESS) { - return init_status; - } - return init_status; }