diff --git a/include/libm2k/analog/m2kanalogout.hpp b/include/libm2k/analog/m2kanalogout.hpp index 05452829..84859559 100644 --- a/include/libm2k/analog/m2kanalogout.hpp +++ b/include/libm2k/analog/m2kanalogout.hpp @@ -472,20 +472,20 @@ class LIBM2K_API M2kAnalogOut /** - * @brief Sets the voltage output of the DAC channel - * @param chn_idx - unsigned int representing the index of the channel - * @param volts - actual value to be set - * @return unsigned short - the corresponding raw value for the given voltage - */ - virtual unsigned short setVoltage(unsigned int chn_idx, double volts) = 0; - - /** - * @brief Sets the raw output of the DAC channel - * @param chn_idx - unsigned int representing the index of the channel - * @param raw - actual value to be set - * @return unsigned short - the value set in the raw attribute - */ - virtual unsigned short setVoltageRaw(unsigned int chn_idx, unsigned short raw) = 0; + * @brief Sets the voltage output of the DAC channel + * @param chn_idx - unsigned int representing the index of the channel + * @param volts - actual value to be set + * @return unsigned short - the corresponding raw value for the given voltage + */ + virtual unsigned short setVoltage(unsigned int chn_idx, double volts) = 0; + + /** + * @brief Sets the raw output of the DAC channel + * @param chn_idx - unsigned int representing the index of the channel + * @param raw - actual value to be set + * @return unsigned short - the value set in the raw attribute + */ + virtual unsigned short setVoltageRaw(unsigned int chn_idx, unsigned short raw) = 0; }; } } diff --git a/src/analog/m2kanalogout_impl.cpp b/src/analog/m2kanalogout_impl.cpp index 1ad0600c..a945513e 100644 --- a/src/analog/m2kanalogout_impl.cpp +++ b/src/analog/m2kanalogout_impl.cpp @@ -73,6 +73,11 @@ M2kAnalogOutImpl::M2kAnalogOutImpl(iio_context *ctx, std::vector da // data_available attribute exists only in firmware versions newer than 0.23 m_dma_data_available = getDacDevice(0)->hasBufferAttribute("data_available"); + + m_raw_enable_available.push_back(getDacDevice(0)->getChannel(0, true)->hasAttribute("raw_enable")); + m_raw_enable_available.push_back(getDacDevice(1)->getChannel(0, true)->hasAttribute("raw_enable")); + m_raw_available.push_back(getDacDevice(0)->getChannel(0, true)->hasAttribute("raw")); + m_raw_available.push_back(getDacDevice(1)->getChannel(0, true)->hasAttribute("raw")); LIBM2K_LOG(INFO, "[END] Initialize M2kAnalogOut"); } @@ -132,26 +137,38 @@ void M2kAnalogOutImpl::loadNbKernelBuffers() } } } + unsigned short M2kAnalogOutImpl::setVoltage(unsigned int chn_idx, double volts) { - auto chn = getDacDevice(chn_idx)->getChannel(0, true); + auto chn = getDacDevice(chn_idx)->getChannel(0, true); auto raw = static_cast(convertVoltsToRaw(chn_idx, volts)); - // having the raw_enable attribute set to enable shout give at the dac output the corresponding raw value - chn->setStringValue("raw_enable", "enabled"); - chn->setLongValue("raw", raw); - return chn->getLongValue("raw"); + long long ret = 0; + // having the raw_enable attribute set to enable should give at the dac output the corresponding raw value + if (m_raw_available.at(chn_idx) && m_raw_enable_available.at(chn_idx)) { + chn->setStringValue("raw_enable", "enabled"); + chn->setLongValue("raw", raw); + ret = chn->getLongValue("raw"); + } else { + THROW_M2K_EXCEPTION("Invalid firmware version: 0.32 or greater is required.", libm2k::EXC_INVALID_FIRMWARE_VERSION); + } + return ret; } unsigned short M2kAnalogOutImpl::setVoltageRaw(unsigned int chn_idx, unsigned short raw) { - auto chn = getDacDevice(chn_idx)->getChannel(0, true); - // having the raw_enable attribute set to enable shout give at the dac output the corresponding raw value - chn->setStringValue("raw_enable", "enabled"); - chn->setLongValue("raw", static_cast(raw)); - return chn->getLongValue("raw"); + auto chn = getDacDevice(chn_idx)->getChannel(0, true); + long long ret = 0; + // having the raw_enable attribute set to enable should give at the dac output the corresponding raw value + if (m_raw_available.at(chn_idx) && m_raw_enable_available.at(chn_idx)) { + chn->setStringValue("raw_enable", "enabled"); + chn->setLongValue("raw", static_cast(raw)); + ret = chn->getLongValue("raw"); + } else { + THROW_M2K_EXCEPTION("Invalid firmware version: 0.32 or greater is required.", libm2k::EXC_INVALID_FIRMWARE_VERSION); + } + return ret; } - double M2kAnalogOutImpl::getCalibscale(unsigned int index) { return getDacDevice(index)->getDoubleValue("calibscale"); diff --git a/src/analog/m2kanalogout_impl.hpp b/src/analog/m2kanalogout_impl.hpp index 2e5bc23b..9fa36c7d 100644 --- a/src/analog/m2kanalogout_impl.hpp +++ b/src/analog/m2kanalogout_impl.hpp @@ -92,8 +92,8 @@ class M2kAnalogOutImpl : public M2kAnalogOut void deinitialize(); void loadNbKernelBuffers(); - unsigned short setVoltage(unsigned int chn_idx, double volts) override; - unsigned short setVoltageRaw(unsigned int chn_idx, unsigned short raw) override; + unsigned short setVoltage(unsigned int chn_idx, double volts) override; + unsigned short setVoltageRaw(unsigned int chn_idx, unsigned short raw) override; private: std::shared_ptr m_m2k_fabric; @@ -108,6 +108,8 @@ class M2kAnalogOutImpl : public M2kAnalogOut bool m_dma_start_sync_available; bool m_dma_data_available; std::vector m_nb_kernel_buffers; + std::vector m_raw_enable_available; + std::vector m_raw_available; DeviceOut* getDacDevice(unsigned int chnIdx) const; void syncDevice();