Skip to content
Closed
6 changes: 4 additions & 2 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ ssize_t iio_buffer_foreach_sample(struct iio_buffer *buffer,
processed += ret;
}

ptr += length;
ptr += length *
(chn->format.repeat ? chn->format.repeat : 1);
}
}
return processed;
Expand All @@ -259,7 +260,8 @@ void * iio_buffer_first(const struct iio_buffer *buffer,

for (i = 0; i < buffer->dev->nb_channels; i++) {
struct iio_channel *cur = buffer->dev->channels[i];
len = cur->format.length / 8;
len = cur->format.length / 8 *
(cur->format.repeat ? cur->format.repeat : 1);

/* NOTE: dev->channels are ordered by index */
if (cur->index < 0 || cur->index == chn->index)
Expand Down
77 changes: 50 additions & 27 deletions channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,17 @@ static char *get_attr_xml(struct iio_channel_attr *attr, size_t *length)

static char * get_scan_element(const struct iio_channel *chn, size_t *length)
{
char buf[1024], *str;
char buf[1024], repeat[8] = "", *str;
char processed = (chn->format.is_fully_defined ? 'A' - 'a' : 0);

if (chn->format.repeat)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare to repeat > 1 for consistency since a value of 1 could be used.

snprintf(repeat, sizeof(repeat), "X%u", chn->format.repeat);

snprintf(buf, sizeof(buf), "<scan-element index=\"%li\" "
"format=\"%ce:%c%u/%u&gt;&gt;%u\" />",
"format=\"%ce:%c%u/%u%s&gt;&gt;%u\" />",
chn->index, chn->format.is_be ? 'b' : 'l',
chn->format.is_signed ? 's' + processed : 'u' + processed,
chn->format.bits, chn->format.length,
chn->format.bits, chn->format.length, repeat,
chn->format.shift);

if (chn->format.with_scale) {
Expand Down Expand Up @@ -519,33 +522,46 @@ static void mask_upper_bits(uint8_t *dst, size_t bits, size_t len)
void iio_channel_convert(const struct iio_channel *chn,
void *dst, const void *src)
{
uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst;
unsigned int len = chn->format.length / 8;
ptrdiff_t end = len * (chn->format.repeat ? chn->format.repeat : 1);
uintptr_t end_ptr = src_ptr + end;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
bool swap = chn->format.is_be;
#else
bool swap = !chn->format.is_be;
#endif

if (len == 1 || !swap)
memcpy(dst, src, len);
else
byte_swap(dst, src, len);

if (chn->format.shift)
shift_bits(dst, chn->format.shift, len, false);

if (!chn->format.is_fully_defined) {
if (chn->format.is_signed)
sign_extend(dst, chn->format.bits, len);
for (src_ptr = (uintptr_t) src; src_ptr < end_ptr;
src_ptr += len, dst_ptr += len) {
if (len == 1 || !swap)
memcpy((void *) dst_ptr, (const void *) src_ptr, len);
else
mask_upper_bits(dst, chn->format.bits, len);
byte_swap((void *) dst_ptr, (const void *) src_ptr,
len);

if (chn->format.shift)
shift_bits((void *) dst_ptr, chn->format.shift, len,
false);

if (!chn->format.is_fully_defined) {
if (chn->format.is_signed)
sign_extend((void *) dst_ptr,
chn->format.bits, len);
else
mask_upper_bits((void *) dst_ptr,
chn->format.bits, len);
}
}
}

void iio_channel_convert_inverse(const struct iio_channel *chn,
void *dst, const void *src)
{
uintptr_t src_ptr = (uintptr_t) src, dst_ptr = (uintptr_t) dst;
unsigned int len = chn->format.length / 8;
ptrdiff_t end = len * (chn->format.repeat ? chn->format.repeat : 1);
uintptr_t end_ptr = dst_ptr + end;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
bool swap = chn->format.is_be;
#else
Expand All @@ -557,23 +573,27 @@ void iio_channel_convert_inverse(const struct iio_channel *chn,
if (len > sizeof(buf))
return;

memcpy(buf, src, len);
mask_upper_bits(buf, chn->format.bits, len);
for (dst_ptr = (uintptr_t) dst; dst_ptr < end_ptr;
src_ptr += len, dst_ptr += len) {
memcpy(buf, (const void *) src_ptr, len);
mask_upper_bits(buf, chn->format.bits, len);

if (chn->format.shift)
shift_bits(buf, chn->format.shift, len, true);
if (chn->format.shift)
shift_bits(buf, chn->format.shift, len, true);

if (len == 1 || !swap)
memcpy(dst, buf, len);
else
byte_swap(dst, buf, len);
if (len == 1 || !swap)
memcpy((void *) dst_ptr, buf, len);
else
byte_swap((void *) dst_ptr, buf, len);
}
}

size_t iio_channel_read_raw(const struct iio_channel *chn,
struct iio_buffer *buf, void *dst, size_t len)
{
uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len;
unsigned int length = chn->format.length / 8;
unsigned int length = chn->format.length / 8 *
(chn->format.repeat ? chn->format.repeat : 1);
uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
ptrdiff_t buf_step = iio_buffer_step(buf);

Expand All @@ -588,7 +608,8 @@ size_t iio_channel_read(const struct iio_channel *chn,
struct iio_buffer *buf, void *dst, size_t len)
{
uintptr_t src_ptr, dst_ptr = (uintptr_t) dst, end = dst_ptr + len;
unsigned int length = chn->format.length / 8;
unsigned int length = chn->format.length / 8 *
(chn->format.repeat ? chn->format.repeat : 1);
uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
ptrdiff_t buf_step = iio_buffer_step(buf);

Expand All @@ -604,7 +625,8 @@ size_t iio_channel_write_raw(const struct iio_channel *chn,
struct iio_buffer *buf, const void *src, size_t len)
{
uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len;
unsigned int length = chn->format.length / 8;
unsigned int length = chn->format.length / 8 *
(chn->format.repeat ? chn->format.repeat : 1);
uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
ptrdiff_t buf_step = iio_buffer_step(buf);

Expand All @@ -619,7 +641,8 @@ size_t iio_channel_write(const struct iio_channel *chn,
struct iio_buffer *buf, const void *src, size_t len)
{
uintptr_t dst_ptr, src_ptr = (uintptr_t) src, end = src_ptr + len;
unsigned int length = chn->format.length / 8;
unsigned int length = chn->format.length / 8 *
(chn->format.repeat ? chn->format.repeat : 1);
uintptr_t buf_end = (uintptr_t) iio_buffer_end(buf);
ptrdiff_t buf_step = iio_buffer_step(buf);

Expand Down
3 changes: 2 additions & 1 deletion device.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ ssize_t iio_device_get_sample_size_mask(const struct iio_device *dev,

for (i = 0; i < dev->nb_channels; i++) {
const struct iio_channel *chn = dev->channels[i];
unsigned int length = chn->format.length / 8;
unsigned int length = chn->format.length / 8 *
(chn->format.repeat ? chn->format.repeat : 1);

if (chn->index < 0)
break;
Expand Down
5 changes: 4 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Lesser General Public License for more details.


TARGETS := ad9361-iiostream iio-monitor
TARGETS := ad9361-iiostream dummy-iiostream iio-monitor

CFLAGS = -Wall

Expand All @@ -28,5 +28,8 @@ iio-monitor: iio-monitor.o
ad9361-iiostream : ad9361-iiostream.o
$(CC) -o $@ $^ $(LDFLAGS) -liio

dummy-iiostream : dummy-iiostream.o
$(CC) -o $@ $^ $(LDFLAGS) -liio

clean:
rm -f $(TARGETS) $(TARGETS:%=%.o)
Loading