Skip to content

Commit d55cd13

Browse files
authored
Merge pull request #4 from j-marjanovic/feat/write-bulk
Add write_bulk method
2 parents 2928fc3 + 6f01c80 commit d55cd13

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

inc/udmaio/HwAccessor.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class HwAccessor : public Logger, private boost::noncopyable {
6969
return {};
7070
}
7171

72+
virtual void write_bulk(uint32_t offs, std::vector<uint8_t> data) {}
73+
7274
/**
7375
* @brief Read register from UIO interface
7476
*
@@ -237,6 +239,11 @@ class HwAccessorMmap : public HwAccessor {
237239
return result;
238240
}
239241

242+
virtual void write_bulk(uint32_t offs, std::vector<uint8_t> data) final override {
243+
auto dst = _mem_ptr<uint8_t>(offs);
244+
memcpy(const_cast<uint8_t*>(dst), &data[0], std::size(data));
245+
}
246+
240247
public:
241248
UioRegion get_phys_region() const final override {
242249
const size_t access_offs =

inc/udmaio/UioIf.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class UioIf : private boost::noncopyable {
7979
return _hw->read_bulk(offs, size);
8080
}
8181

82+
void write_bulk(uint32_t offs, std::vector<uint8_t> data) {
83+
return _hw->write_bulk(offs, data);
84+
}
85+
8286
template <typename C>
8387
C _rd_reg(uint32_t offs) const {
8488
return _hw->template _rd_reg<C>(offs);

pyudmaio/pyudmaio/binding.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ class UioIf:
445445
...
446446
def read_bulk(self, arg0: typing.SupportsInt, arg1: typing.SupportsInt) -> numpy.typing.NDArray[numpy.uint8]:
447447
...
448+
def write_bulk(self, arg0: typing.SupportsInt, arg1: bytes) -> None:
449+
...
448450
def wait_for_interrupt(self) -> int:
449451
...
450452
class UioMemSgdma(UioIf):

pyudmaio/src/PythonBinding.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class UioIf_PyPublishHelper : public udmaio::UioIf {
3232
using udmaio::UioIf::_wr32;
3333
using udmaio::UioIf::arm_interrupt;
3434
using udmaio::UioIf::read_bulk;
35+
using udmaio::UioIf::write_bulk;
3536
using udmaio::UioIf::UioIf;
3637
using udmaio::UioIf::wait_for_interrupt;
3738
};
@@ -119,7 +120,19 @@ PYBIND11_MODULE(binding, m) {
119120
{sizeof(uint8_t)}, // stride
120121
reinterpret_cast<uint8_t*>(vec->data()), // data pointer
121122
gc_callback);
122-
});
123+
})
124+
.def("write_bulk",
125+
[](UioIf_PyPublishHelper& self, uint32_t offs, py::buffer b) {
126+
py::buffer_info info = b.request();
127+
// Accept only contiguous 1-D buffers
128+
if (info.ndim != 1) {
129+
throw std::runtime_error("write_bulk expects a 1-D bytes-like object");
130+
}
131+
auto ptr = static_cast<uint8_t*>(info.ptr);
132+
std::vector<uint8_t> data(ptr, ptr + info.size * info.itemsize);
133+
self.write_bulk(offs, std::move(data));
134+
},
135+
"Write a contiguous bytes-like object to the mapped device memory at the given offset");
123136

124137
py::class_<udmaio::DmaBufferAbstract, std::shared_ptr<udmaio::DmaBufferAbstract>>(
125138
m,

0 commit comments

Comments
 (0)