Skip to content

Commit

Permalink
fw0.32: toggle raw_enable before destroying iio_buffer (cyclic)
Browse files Browse the repository at this point in the history
- Toggling the raw enable flag when a cyclic buffer is destroyed has the
effect of sending the raw value to the ouput untill a new buffer is created.
- This solves a bug where a spike appeared in between pushes to cyclic buffers
because the new one would start with the last sample from the previously deleted buffer.

Signed-off-by: Adrian Stanea <[email protected]>
  • Loading branch information
Adrian-Stanea committed Dec 8, 2023
1 parent 7b31a3d commit 78035d0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/analog/m2kanalogout_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void M2kAnalogOutImpl::pushBytes(unsigned int chnIdx, double *data, unsigned int
for (unsigned int i = 0; i < nb_samples; i++) {
raw_data_buffer.push_back(convertVoltsToRaw(chnIdx, data[i]));
}
m_dac_devices.at(chnIdx)->push(raw_data_buffer, 0, getCyclic(chnIdx));
m_dac_devices.at(chnIdx)->push(raw_data_buffer, 0, getCyclic(chnIdx), false, true);
}


Expand Down
43 changes: 29 additions & 14 deletions src/utils/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,23 @@ Buffer::~Buffer() {
m_data_short.clear();
}

void Buffer::initializeBuffer(unsigned int size, bool cyclic, bool output)
void Buffer::initializeBuffer(unsigned int size, bool cyclic, bool output, bool enableFlag)
{
/* In non-cyclic mode pushing samples will fill the internal buffers, creating the possibility of continuous
* data transferring; the buffer must be destroy when its size is changed
*
* In cyclic mode the very first buffer pushed will be repeated; in order to push any other buffer the
* old buffer must be destroyed and a new one must be created*/
if (size != m_last_nb_samples || cyclic) {
if (enableFlag && cyclic) {
for(auto channel : m_channel_list) {
if (channel->hasAttribute("raw_enable")) {
channel->setStringValue("raw_enable", "enabled");
}
}
}
destroy();

m_last_nb_samples = size;
m_buffer = iio_device_create_buffer(m_dev, size, cyclic);
if (!m_buffer) {
Expand All @@ -80,6 +88,13 @@ void Buffer::initializeBuffer(unsigned int size, bool cyclic, bool output)
THROW_M2K_EXCEPTION("Buffer: Cannot create the RX buffer", libm2k::EXC_RUNTIME_ERROR, errno);
}
}
if (enableFlag) {
for(auto channel : m_channel_list) {
if (channel->hasAttribute("raw_enable")) {
channel->setStringValue("raw_enable", "disabled");
}
}
}
LIBM2K_LOG(INFO, libm2k::buildLoggingMessage({m_dev_name}, std::string((output ? "TX" : "RX")) + " buffer created (" + std::to_string(size) +
" samples)"));
LIBM2K_LOG_IF(WARNING, size % 4 != 0 || size < 16,
Expand All @@ -100,7 +115,7 @@ void Buffer::setChannels(std::vector<Channel*> channels)
}

void Buffer::push(unsigned short *data, unsigned int channel, unsigned int nb_samples,
bool cyclic, bool multiplex)
bool cyclic, bool multiplex, bool enableFlag)
{
if (Utils::getIioDeviceDirection(m_dev) != OUTPUT) {
THROW_M2K_EXCEPTION("Device not output buffer capable, so no buffer was created", libm2k::EXC_INVALID_PARAMETER);
Expand All @@ -113,7 +128,7 @@ void Buffer::push(unsigned short *data, unsigned int channel, unsigned int nb_sa
return;
}

initializeBuffer(nb_samples, cyclic, true);
initializeBuffer(nb_samples, cyclic, true, enableFlag);

if (channel < m_channel_list.size() ) {
if (!multiplex) {
Expand Down Expand Up @@ -145,9 +160,9 @@ void Buffer::push(unsigned short *data, unsigned int channel, unsigned int nb_sa

//push on a certain channel
void Buffer::push(std::vector<short> const &data, unsigned int channel,
bool cyclic, bool multiplex)
bool cyclic, bool multiplex, bool enableFlag)
{
size_t size = data.size();
size_t size = data.size();
if (Utils::getIioDeviceDirection(m_dev) != OUTPUT) {
THROW_M2K_EXCEPTION("Device not output buffer capable, so no buffer was created", libm2k::EXC_INVALID_PARAMETER);
}
Expand All @@ -159,7 +174,7 @@ void Buffer::push(std::vector<short> const &data, unsigned int channel,
return;
}

initializeBuffer(size, cyclic, true);
initializeBuffer(size, cyclic, true, enableFlag);

if (channel < m_channel_list.size() ) {
if (!multiplex) {
Expand Down Expand Up @@ -190,7 +205,7 @@ void Buffer::push(std::vector<short> const &data, unsigned int channel,
}

void Buffer::push(std::vector<unsigned short> const &data, unsigned int channel,
bool cyclic, bool multiplex)
bool cyclic, bool multiplex, bool enableFlag)
{
size_t size = data.size();
if (Utils::getIioDeviceDirection(m_dev) != OUTPUT) {
Expand All @@ -204,7 +219,7 @@ void Buffer::push(std::vector<unsigned short> const &data, unsigned int channel,
return;
}

initializeBuffer(size, cyclic, true);
initializeBuffer(size, cyclic, true, enableFlag);

if (channel < m_channel_list.size() ) {
if (!multiplex) {
Expand Down Expand Up @@ -235,7 +250,7 @@ void Buffer::push(std::vector<unsigned short> const &data, unsigned int channel,
}
}

void Buffer::push(std::vector<double> const &data, unsigned int channel, bool cyclic)
void Buffer::push(std::vector<double> const &data, unsigned int channel, bool cyclic, bool enableFlag)
{
size_t size = data.size();
if (Utils::getIioDeviceDirection(m_dev) == INPUT) {
Expand All @@ -249,7 +264,7 @@ void Buffer::push(std::vector<double> const &data, unsigned int channel, bool cy
return;
}

initializeBuffer(size, cyclic, true);
initializeBuffer(size, cyclic, true, enableFlag);

if (channel < m_channel_list.size() ) {
m_channel_list.at(channel)->write(m_buffer, data);
Expand Down Expand Up @@ -281,7 +296,7 @@ void Buffer::push(std::vector<std::vector<short>> const &data)
}
}

void Buffer::push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic)
void Buffer::push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic, bool enableFlag)
{
if (Utils::getIioDeviceDirection(m_dev) == INPUT) {
THROW_M2K_EXCEPTION("Device not output buffer capable, so no buffer was created", libm2k::EXC_INVALID_PARAMETER);
Expand All @@ -294,7 +309,7 @@ void Buffer::push(double *data, unsigned int channel, unsigned int nb_samples, b
return;
}

initializeBuffer(nb_samples, cyclic, true);
initializeBuffer(nb_samples, cyclic, true, enableFlag);

if (channel < m_channel_list.size() ) {
m_channel_list.at(channel)->write(m_buffer, data, nb_samples);
Expand All @@ -313,7 +328,7 @@ void Buffer::push(double *data, unsigned int channel, unsigned int nb_samples, b
}
}

void Buffer::push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic)
void Buffer::push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic, bool enableFlag)
{
if (Utils::getIioDeviceDirection(m_dev) == INPUT) {
THROW_M2K_EXCEPTION("Device not output buffer capable, so no buffer was created", libm2k::EXC_INVALID_PARAMETER);
Expand All @@ -326,7 +341,7 @@ void Buffer::push(short *data, unsigned int channel, unsigned int nb_samples, bo
return;
}

initializeBuffer(nb_samples, cyclic, true);
initializeBuffer(nb_samples, cyclic, true, enableFlag);

if (channel < m_channel_list.size() ) {
m_channel_list.at(channel)->write(m_buffer, data, nb_samples);
Expand Down
14 changes: 7 additions & 7 deletions src/utils/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ class Buffer
Buffer(struct iio_device *dev);
~Buffer();

void initializeBuffer(unsigned int size, bool cyclic, bool output);
void initializeBuffer(unsigned int size, bool cyclic, bool output, bool enableFlag = false);
void push(std::vector<short> const &data, unsigned int channel = 0,
bool cyclic = true, bool multiplex = false);
bool cyclic = true, bool multiplex = false, bool enableFlag = false);
void push(std::vector<unsigned short> const &data, unsigned int channel = 0,
bool cyclic = true, bool multiplex = false);
bool cyclic = true, bool multiplex = false, bool enableFlag = false);
void push(unsigned short *data, unsigned int channel, unsigned int nb_samples,
bool cyclic = true, bool multiplex = false);
void push(std::vector<double> const &data, unsigned int channel = 0, bool cyclic = true);
bool cyclic = true, bool multiplex = false, bool enableFlag = false);
void push(std::vector<double> const &data, unsigned int channel = 0, bool cyclic = true, bool enableFlag = false);
void push(std::vector<std::vector<short>> const &data);

void push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true);
void push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true);
void push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true, bool enableFlag = false);
void push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true, bool enableFlag = false);

void setChannels(std::vector<Channel*> channels);
std::vector<unsigned short> getSamples(unsigned int nb_samples);
Expand Down
24 changes: 12 additions & 12 deletions src/utils/deviceout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ void DeviceOut::initializeBuffer(unsigned int size, bool cyclic)
}

void DeviceOut::push(std::vector<short> const &data, unsigned int channel,
bool cyclic, bool multiplex)
bool cyclic, bool multiplex, bool enableFlag)
{
if (!m_buffer) {
THROW_M2K_EXCEPTION("Device: Cannot push; device not buffer capable", libm2k::EXC_RUNTIME_ERROR);
}
m_buffer->setChannels(m_channel_list);
m_buffer->push(data, channel, cyclic, multiplex);
m_buffer->push(data, channel, cyclic, multiplex, enableFlag);
}


Expand All @@ -76,51 +76,51 @@ void DeviceOut::cancelBuffer()


void DeviceOut::push(std::vector<unsigned short> const &data, unsigned int channel,
bool cyclic, bool multiplex)
bool cyclic, bool multiplex, bool enableFlag)
{
if (!m_buffer) {
THROW_M2K_EXCEPTION("Device: Cannot push; device not buffer capable", libm2k::EXC_RUNTIME_ERROR);
}
m_buffer->setChannels(m_channel_list);
m_buffer->push(data, channel, cyclic, multiplex);
m_buffer->push(data, channel, cyclic, multiplex, enableFlag);
}


void DeviceOut::push(unsigned short *data, unsigned int channel, unsigned int nb_samples,
bool cyclic, bool multiplex)
bool cyclic, bool multiplex, bool enableFlag)
{
if (!m_buffer) {
THROW_M2K_EXCEPTION("Device: Cannot push; device not buffer capable", libm2k::EXC_RUNTIME_ERROR);
}
m_buffer->setChannels(m_channel_list);
m_buffer->push(data, channel, nb_samples, cyclic, multiplex);
m_buffer->push(data, channel, nb_samples, cyclic, multiplex, enableFlag);
}

void DeviceOut::push(std::vector<double> const &data, unsigned int channel, bool cyclic)
void DeviceOut::push(std::vector<double> const &data, unsigned int channel, bool cyclic, bool enableFlag)
{
if (!m_buffer) {
THROW_M2K_EXCEPTION("Device: Cannot push; device not buffer capable", libm2k::EXC_RUNTIME_ERROR);
}
m_buffer->setChannels(m_channel_list);
m_buffer->push(data, channel, cyclic);
m_buffer->push(data, channel, cyclic, enableFlag);
}

void DeviceOut::push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic)
void DeviceOut::push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic, bool enableFlag)
{
if (!m_buffer) {
THROW_M2K_EXCEPTION("Device: Cannot push; device not buffer capable", libm2k::EXC_RUNTIME_ERROR);
}
m_buffer->setChannels(m_channel_list);
m_buffer->push(data, channel, nb_samples, cyclic);
m_buffer->push(data, channel, nb_samples, cyclic, enableFlag);
}

void DeviceOut::push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic)
void DeviceOut::push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic, bool enableFlag)
{
if (!m_buffer) {
THROW_M2K_EXCEPTION("Device: Cannot push; device not buffer capable", libm2k::EXC_RUNTIME_ERROR);
}
m_buffer->setChannels(m_channel_list);
m_buffer->push(data, channel, nb_samples, cyclic);
m_buffer->push(data, channel, nb_samples, cyclic, enableFlag);
}

void DeviceOut::stop()
Expand Down
12 changes: 6 additions & 6 deletions src/utils/deviceout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ class DeviceOut : public DeviceGeneric

void initializeBuffer(unsigned int size, bool cyclic);
void push(std::vector<short> const &data, unsigned int channel,
bool cyclic = true, bool multiplex = false);
bool cyclic = true, bool multiplex = false, bool enableFlag = false);
void push(std::vector<unsigned short> const &data, unsigned int channel,
bool cyclic = true, bool multiplex = false);
bool cyclic = true, bool multiplex = false, bool enableFlag = false);
void push(unsigned short *data, unsigned int channel, unsigned int nb_samples,
bool cyclic = true, bool multiplex = false);
void push(std::vector<double> const &data, unsigned int channel, bool cyclic = true);
void push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true);
void push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true);
bool cyclic = true, bool multiplex = false, bool enableFlag = false);
void push(std::vector<double> const &data, unsigned int channel, bool cyclic = true, bool enableFlag = false);
void push(double *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true, bool enableFlag = false);
void push(short *data, unsigned int channel, unsigned int nb_samples, bool cyclic = true, bool enableFlag = false);
void stop();
void cancelBuffer();
struct IIO_OBJECTS getIioObjects();
Expand Down

0 comments on commit 78035d0

Please sign in to comment.