Skip to content

Commit

Permalink
M2kAnalogOut: Check whether the attr exists in the current fw version.
Browse files Browse the repository at this point in the history
For older fw versions, throw an exception if the user tries to set the
raw attribute value.

Signed-off-by: AlexandraTrifan <[email protected]>
  • Loading branch information
AlexandraTrifan committed Jan 29, 2024
1 parent 39bf62c commit e4a8865
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
28 changes: 14 additions & 14 deletions include/libm2k/analog/m2kanalogout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
}
Expand Down
39 changes: 28 additions & 11 deletions src/analog/m2kanalogout_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ M2kAnalogOutImpl::M2kAnalogOutImpl(iio_context *ctx, std::vector<std::string> 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");
}

Expand Down Expand Up @@ -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<long long>(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<long long>(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<long long>(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");
Expand Down
6 changes: 4 additions & 2 deletions src/analog/m2kanalogout_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<libm2k::utils::DeviceGeneric> m_m2k_fabric;
Expand All @@ -108,6 +108,8 @@ class M2kAnalogOutImpl : public M2kAnalogOut
bool m_dma_start_sync_available;
bool m_dma_data_available;
std::vector<unsigned int> m_nb_kernel_buffers;
std::vector<bool> m_raw_enable_available;
std::vector<bool> m_raw_available;

DeviceOut* getDacDevice(unsigned int chnIdx) const;
void syncDevice();
Expand Down

0 comments on commit e4a8865

Please sign in to comment.