static int SetReferenceVoltage()

in CodeSnippets/Peripherals/ADC/AdvancedFunctions/advanced_functions.c [318:342]


static int SetReferenceVoltage(int adcControllerFd, ADC_ChannelId channelId,
                               struct iio_ioctl_chan_spec_buffer *channelSpecBuffer,
                               float referenceVoltage)
{
    // Buffer to hold single precision floating point and null terminator.
    char dataBuffer[12];

    int length = snprintf(dataBuffer, sizeof(dataBuffer), "%.3f", referenceVoltage);
    if (length < 0 || length >= sizeof(dataBuffer)) {
        Log_Debug("ERROR: Cannot write reference voltage to buffer.\n");
        return -1;
    }

    int res =
        SetExtInfo(adcControllerFd, channelId,
                   (unsigned int)GetPropertyIndex(channelSpecBuffer->channel, "reference_voltage"),
                   dataBuffer, (size_t)(length + 1));
    if (res < 0) {
        Log_Debug("ERROR: Failed to set extended channel information for channelId = %u.\n",
                  channelId);
        return -1;
    }

    return 0;
}