Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fw0.32/adc_calib: sync calibration state and use new attribute to decouple HW calibration #351

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/analog/m2kanalogin_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ M2kAnalogInImpl::M2kAnalogInImpl(iio_context * ctx, std::string adc_dev, bool sy
m_trigger(trigger)
{
LIBM2K_LOG(INFO, "[BEGIN] Initialize M2kAnalogIn");
firmware_version = Utils::getFirmwareVersion(ctx);

m_m2k_adc = make_shared<DeviceIn>(ctx, adc_dev);
m_m2k_fabric = make_shared<DeviceGeneric>(ctx, "m2k-fabric");
m_ad5625_dev = make_shared<DeviceGeneric>(ctx, "ad5625");

if (firmware_version >= "v0.32") {
setCalibrateHDL("true");
}

/* Filters applied while decimating affect the
/ amplitude of the received data */
m_filter_compensation_table[1E8] = 1.00;
Expand Down Expand Up @@ -135,6 +141,9 @@ void M2kAnalogInImpl::syncDevice()
} else {
m_adc_calib_offset.at(i) = 2048;
}
if (firmware_version >= "v0.32") {
m_adc_calib_gain.at(i) = getCalibscale(i);
}
m_adc_hw_offset_raw.at(i) = m_ad5625_dev->getLongValue(2 + i, "raw", true);
m_adc_hw_vert_offset.at(i) = convertRawToVoltsVerticalOffset(static_cast<ANALOG_IN_CHANNEL>(i), m_adc_hw_offset_raw.at(i) - m_adc_calib_offset.at(i));

Expand Down Expand Up @@ -241,6 +250,10 @@ double M2kAnalogInImpl::setCalibscale(unsigned int index, double calibscale)
if (index >= getNbChannels()) {
THROW_M2K_EXCEPTION("M2kAnalogIn: no such channel", libm2k::EXC_OUT_OF_RANGE);
}
// update class attribute as well as the attribute in the device
if (firmware_version >= "v0.32") {
m_adc_calib_gain.at(index) = calibscale;
}
return m_m2k_adc->setDoubleValue(index, calibscale, "calibscale");
}

Expand Down Expand Up @@ -941,3 +954,12 @@ int M2kAnalogInImpl::getAdcCalibOffset(ANALOG_IN_CHANNEL channel)
return (adc_hw_offset - convertVoltsToRawVerticalOffset(channel, m_adc_hw_vert_offset.at(channel)));
}
}

void M2kAnalogInImpl::setCalibrateHDL(std::string calibrate_val)
{
m_m2k_adc->setStringValue("calibrate", calibrate_val);
}
std::string M2kAnalogInImpl::getCalibrateHDL()
{
return m_m2k_adc->getStringValue("calibrate");
}
16 changes: 16 additions & 0 deletions src/analog/m2kanalogin_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class M2kAnalogInImpl : public M2kAnalogIn
bool hasCalibbias();
void loadNbKernelBuffers();
private:
std::string firmware_version;
std::shared_ptr<libm2k::utils::DeviceGeneric> m_ad5625_dev;
std::shared_ptr<libm2k::utils::DeviceGeneric> m_m2k_fabric;
std::shared_ptr<libm2k::utils::DeviceIn> m_m2k_adc;
Expand Down Expand Up @@ -180,6 +181,21 @@ class M2kAnalogInImpl : public M2kAnalogIn

int convertVoltsToRawVerticalOffset(ANALOG_IN_CHANNEL channel, double vertOffset);
double convertRawToVoltsVerticalOffset(ANALOG_IN_CHANNEL channel, int rawVertOffset);

/**
* This function can stop the calibration process for the ADC at the HW level.
* The possible values can be checked in IIO in the "calibrate_available" device attribute of m2k-adc.
*
* When calibrate is set to true, the calibration won't affect the ADC samples. When set to false
* the calibration is applied. It should be set before doing calibration to avoid sending samples.
*
* Introduced in fw v0.32 to solve a bug where after HW calibration, some samples were missing due to rounding errors.
*
* @param calibrate_val The calibration value to set. Can take values "true" or "false" as std::strings.
*
*/
void setCalibrateHDL(std::string calibrate_val);
Adrian-Stanea marked this conversation as resolved.
Show resolved Hide resolved
std::string getCalibrateHDL();
};
}
}
Expand Down
Loading