Skip to content

Commit c2ce9d4

Browse files
committed
Implement BufferParams in the python binding
8291539 ("task: Implement iio_buffer_params struct and make CMA selectable at runtime") implemented iio_buffer_params, replacing idx with a struct. In the python binding, the change broke usage since dropped optional idx with required params. Define the struct in python, defaulting .idx to 0 and .dma_allocator = IIO_DMA_ALLOCATOR_SYSTEM, and make optional, to restore compatibility with legacy code (idx was hardly set). In case #1344 does not revert iio_buffer_params. Signed-off-by: Jorge Marques <[email protected]>
1 parent 6d64df5 commit c2ce9d4

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

bindings/python/iio.py

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ class ContextParams(Structure):
133133
pass # TODO
134134

135135
class BufferParams(Structure):
136-
pass # TODO
136+
"""Represents the low-level control over DMA buffer allocation."""
137+
138+
_fields_ = [
139+
("idx", c_uint),
140+
("dma_allocator", c_int),
141+
("__rsrv", c_char * 64),
142+
]
137143

138144
class DataFormat(Structure):
139145
"""Represents the data format of an IIO channel."""
@@ -304,6 +310,13 @@ class ChannelType(Enum):
304310
IIO_CHAN_TYPE_UNKNOWN = 0x7FFFFFFF
305311

306312

313+
class BufferDMAAllocator(Enum):
314+
"""Contains DMA allocator options."""
315+
316+
IIO_DMA_ALLOCATOR_SYSTEM = 0
317+
IIO_DMA_ALLOCATOR_CMA_LINUX = 1
318+
319+
307320
# pylint: disable=invalid-name
308321
_ScanPtr = _POINTER(_Scan)
309322
_ContextPtr = _POINTER(_Context)
@@ -814,6 +827,36 @@ def _write(self, value):
814827
"Current value of this attribute.\n\ttype=str",
815828
)
816829

830+
class ChannelsBufferParams(_IIO_Object):
831+
"""Represents buffer creation parameters."""
832+
833+
def __init__(self, idx=0, dma_allocator=BufferDMAAllocator.IIO_DMA_ALLOCATOR_SYSTEM):
834+
"""
835+
Initialize a new instance of the ChannelsBufferParams class.
836+
837+
:param idx: type=int
838+
The index of the hardware buffer. Should be 0 in most cases.
839+
:param dma_allocator: type=BufferDMAAllocator
840+
841+
returns: type=iio.ChannelsBufferParams
842+
A new instance of this class
843+
"""
844+
self._params = BufferParams()
845+
self._params.idx = idx
846+
self._params.dma_allocator = dma_allocator.value
847+
848+
super(ChannelsBufferParams, self).__init__(self._params, None)
849+
850+
@property
851+
def idx(self):
852+
"""Get the buffer index."""
853+
return self._params.idx
854+
855+
@property
856+
def dma_allocator(self):
857+
"""Get the DMA allocator type."""
858+
return self._params.dma_allocator
859+
817860
class ChannelsMask(_IIO_Object):
818861
"""A bitmask where each bit corresponds to an enabled channel."""
819862

@@ -1093,7 +1136,7 @@ def buffer(self):
10931136
class Buffer(_IIO_Object):
10941137
"""Represents a hardware I/O buffer."""
10951138

1096-
def __init__(self, device, mask, params):
1139+
def __init__(self, device, mask, params=None):
10971140
"""
10981141
Initialize a new instance of the Buffer class.
10991142
@@ -1102,21 +1145,23 @@ def __init__(self, device, mask, params):
11021145
operations will be performed
11031146
:param mask: type=ChannelsMask
11041147
The mask of enabled channels
1105-
:param idx: type=int
1106-
The hardware index of the buffer to use. If unsure, leave it to 0
1148+
:param params: type=ChannelsBufferParams
1149+
The parameters of the buffer to use.
11071150
11081151
returns: type=iio.Buffer
11091152
An new instance of this class
11101153
"""
1154+
if params is None:
1155+
params = ChannelsBufferParams()
11111156
try:
1112-
self._buffer = _create_buffer(device._device, params._params, mask._mask)
1157+
self._buffer = _create_buffer(device._device, _byref(params._params), mask._mask)
11131158
except Exception:
11141159
self._buffer = None
11151160
raise
11161161

11171162
super(Buffer, self).__init__(self._buffer, device)
11181163

1119-
self._idx = idx
1164+
self._params = params
11201165
self._enabled = False
11211166

11221167
def __del__(self):
@@ -1159,6 +1204,14 @@ def _set_enabled(self, enabled):
11591204
"List of attributes for this buffer.\n\ttype=dict of iio.Attr",
11601205
)
11611206

1207+
@property
1208+
def idx(self):
1209+
"""
1210+
Get the buffer index from the buffer parameters.
1211+
type: int
1212+
"""
1213+
return self._params.idx
1214+
11621215

11631216
class Stream(_IIO_Object):
11641217
def __init__(self, buffer, samples_count, nb_blocks = 4):

0 commit comments

Comments
 (0)