Skip to content

Commit aa3cbfc

Browse files
committed
feat: add UioIf.read_bulk() to python binding
1 parent 1e6fc09 commit aa3cbfc

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

pyudmaio/src/PythonBinding.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <pybind11/pybind11.h>
1515
#include <pybind11/stl.h>
16+
#include <udmaio/UioIf.hpp>
1617

1718
#include "DataHandlerPython.hpp"
1819
#include "docstrings.hpp"
@@ -25,16 +26,12 @@
2526

2627
namespace py = pybind11;
2728

28-
class UioIf_PyOverrideHelper : public udmaio::UioIf {
29-
public:
30-
using udmaio::UioIf::UioIf;
31-
};
32-
3329
class UioIf_PyPublishHelper : public udmaio::UioIf {
3430
public:
3531
using udmaio::UioIf::_rd32;
3632
using udmaio::UioIf::_wr32;
3733
using udmaio::UioIf::arm_interrupt;
34+
using udmaio::UioIf::read_bulk;
3835
using udmaio::UioIf::UioIf;
3936
using udmaio::UioIf::wait_for_interrupt;
4037
};
@@ -96,7 +93,7 @@ PYBIND11_MODULE(binding, m) {
9693
py::arg("x7_series_mode") = bool(false),
9794
DOC(udmaio, UioConfigXdma, UioConfigXdma));
9895

99-
py::class_<udmaio::UioIf, UioIf_PyOverrideHelper, std::shared_ptr<udmaio::UioIf>>(
96+
py::class_<udmaio::UioIf, UioIf_PyPublishHelper, std::shared_ptr<udmaio::UioIf>>(
10097
m,
10198
"UioIf",
10299
DOC(udmaio, UioIf))
@@ -108,7 +105,21 @@ PYBIND11_MODULE(binding, m) {
108105
DOC(udmaio, UioIf, arm_interrupt))
109106
.def("wait_for_interrupt",
110107
&UioIf_PyPublishHelper::wait_for_interrupt,
111-
DOC(udmaio, UioIf, wait_for_interrupt));
108+
DOC(udmaio, UioIf, wait_for_interrupt))
109+
.def("read_bulk", [](UioIf_PyPublishHelper& self, uint32_t offs, uint32_t size) {
110+
// Create vector on the heap, holding the data
111+
auto vec = new std::vector<uint8_t>(self.read_bulk(offs, size));
112+
// Callback for Python garbage collector
113+
py::capsule gc_callback(vec, [](void* f) {
114+
auto ptr = reinterpret_cast<std::vector<uint8_t>*>(f);
115+
delete ptr;
116+
});
117+
// Return Numpy array, transferring ownership to Python
118+
return py::array_t<uint8_t>({vec->size() / sizeof(uint8_t)}, // shape
119+
{sizeof(uint8_t)}, // stride
120+
reinterpret_cast<uint8_t*>(vec->data()), // data pointer
121+
gc_callback);
122+
});
112123

113124
py::class_<udmaio::DmaBufferAbstract, std::shared_ptr<udmaio::DmaBufferAbstract>>(
114125
m,

0 commit comments

Comments
 (0)