static int GetSampleBitCount()

in CodeSnippets/Peripherals/ADC/AdvancedFunctions/advanced_functions.c [22:110]


static int GetSampleBitCount(int adcControllerFd, ADC_ChannelId channelId,
                             struct iio_ioctl_chan_spec_buffer *channelSpecBuffer);
static int GetPropertyIndex(const struct iio_ioctl_chan_spec *channelSpec,
                            const char *propertyName);
static int GetExtInfo(int adcControllerFd, ADC_ChannelId channelId,
                      unsigned int extendedPropertyIndex, char *data, size_t length);
static int SetReferenceVoltage(int adcControllerFd, ADC_ChannelId channelId,
                               struct iio_ioctl_chan_spec_buffer *channelSpecBuffer,
                               float referenceVoltage);
static int PollAdc(int adcControllerFd, ADC_ChannelId channelId, uint32_t *outSampleValue);
static int SetExtInfo(int adcControllerFd, ADC_ChannelId channelId,
                      unsigned int extendedPropertyIndex, const char *data, size_t length);
static int GetChannelSpecification(int adcControllerFd, ADC_ChannelId channelId,
                                   struct iio_ioctl_chan_spec_buffer *channelSpecBuffer);
static int ReadAdcChannel(void);

// The maximum voltage.
static const float sampleMaxVoltage = 2.5f;
// ADC device path.
static const char adcPath[] = "/dev/adc";
// Channel specification.
static struct iio_ioctl_chan_spec_buffer channelSpecBuffer;

/// <summary>
/// Reads the value from the ADC channel and displays the value in volts.
/// </summary>
/// <returns>0 on success, else -1.</returns>
static int ReadAdcChannel(void)
{
    int adcControllerFd = -1;
    int sampleBitCount = -1;
    int retVal = -1;

    // Open the ADC.
    adcControllerFd = OpenAdc(SAMPLE_POTENTIOMETER_ADC_CONTROLLER);
    if (adcControllerFd == -1) {
        Log_Debug("ERROR: OpenAdc failed.\n");
        goto cleanup;
    }

    // Get the specification for the channel.
    int result = GetChannelSpecification(adcControllerFd, SAMPLE_POTENTIOMETER_ADC_CHANNEL,
                                         &channelSpecBuffer);
    if (result == -1) {
        Log_Debug("ERROR: GetChannelSpecification failed.\n");
        goto cleanup;
    }

    // Get the number of bits in the sample.
    sampleBitCount =
        GetSampleBitCount(adcControllerFd, SAMPLE_POTENTIOMETER_ADC_CHANNEL, &channelSpecBuffer);
    if (sampleBitCount == -1) {
        Log_Debug("ERROR: GetSampleBitCount failed.\n");
        goto cleanup;
    }

    // Set the reference voltage.
    result = SetReferenceVoltage(adcControllerFd, SAMPLE_POTENTIOMETER_ADC_CHANNEL,
                                 &channelSpecBuffer, sampleMaxVoltage);
    if (result == -1) {
        Log_Debug("ERROR: SetReferenceVoltage failed.\n");
        goto cleanup;
    }

    // Poll the ADC to read the voltage.
    uint32_t value;
    result = PollAdc(adcControllerFd, SAMPLE_POTENTIOMETER_ADC_CHANNEL, &value);
    if (result == -1) {
        Log_Debug("ERROR: PollAdc failed.\n");
        goto cleanup;
    }

    // Calculate voltage.
    float maxSample = (float)((1 << sampleBitCount) - 1);
    float voltage = ((float)value * sampleMaxVoltage) / maxSample;
    Log_Debug("The out sample value is %.3f V.\n", voltage);
    retVal = 0;

cleanup:
    // Close the file descriptor.
    if (adcControllerFd != -1) {
        int result = close(adcControllerFd);
        if (result != 0) {
            Log_Debug("ERROR: Could not close ADC fd: %s (%d).\n", strerror(errno), errno);
        }
    }

    return retVal;
}