I am trying to convert c++ code into python.
int fd_vdma = open("/dev/mem", O_RDWR|O_SYNC); // open uiox device for vdma access
if (fd_vdma < 1) {
printf("Invalid mem device file\n");
}
// mmap the vdma device for vdma access
unsigned int *ptr_vdma;
ptr_vdma = (unsigned int*)mmap(NULL, VDMA_MAP_SIZE,PROT_READ|PROT_WRITE,
MAP_SHARED, fd_vdma, VDMA_ADDR);
printf("DMA 1 virtual address: 0x%08x \n",ptr_vdma);
*(ptr_vdma+5) = FRBUF_ADDR_0;
*(ptr_vdma+7) = 2; // use internal fifos to trigger xfer
*(ptr_vdma+8) = 20480;
*(ptr_vdma+6) = (75 << 16) + (HORIZ_PIXELS_SMALL+75);
*(ptr_vdma+0x0D) = 200; // no. FIFO threshhold .. max.. 240
I have done so far,
# vdm memory check
try:
fd_vdm_path = "/dev/mem"
mode = "rb+"
fd_vdm = open(fd_vdm_path, mode)
# print(fd_vdm.fileno())
print("[INFO] " + fd_vdm_path + " checked")
except Exception as error:
print("{}".format(error))
# mmap the VDMA device for VDM access
vdma_buf = mmap.mmap(fd_vdm.fileno(), int(VDMA_MAP_SIZE),
mmap.MAP_SHARED, mmap.PROT_READ|mmap.PROT_WRITE, 0)
ptr_vdm = ctypes.c_uint.from_buffer(vdma_buf)
print(type(ctypes.addressof(ptr_vdm)))
print("[INFO] " + fd_vdm_path + " has allocated virtual address : " +
hex(ctypes.addressof(ptr_vdm)))
but I have stuck here, I do not know how to perform below operations in python.
*(ptr_vdma+5) = FRBUF_ADDR_0;
*(ptr_vdma+7) = 2; // use internal fifos to trigger xfer
*(ptr_vdma+8) = 20480;
*(ptr_vdma+6) = (75 << 16) + (HORIZ_PIXELS_SMALL+75);
*(ptr_vdma+0x0D) = 200; // no. FIFO threshhold .. max.. 240
I have tried to vdma_buf.write(FRBUF_ADDR_0)
but it is not working and I could not find anything on the internet (possibly I did not search it well)
can you please help with this problem or else suggest me some links or tutorials?
You can use memoryview
and struct.pack
:
...
mm = memoryview(vdma_buf)
mm[5*4:6*4] = struct.pack("I", FRBUF_ADDR_0)
mm[7*4:8*4] = struct.pack("I", 2)
mm[8*4:9*4] = struct.pack("I", 20480)
mm[6*4:7*4] = struct.pack("I", (75 << 16) + (HORIZ_PIXELS_SMALL+75))
mm[0x0D*4:0x0E*4] = struct.pack("I", 200)
memoryview represents sequence of bytes, and if you want to write some other type to memory, you must convert it to bytes using struct.pack
.
So, let's say you want to to write an unsigned int at index N.
First, you call struct.pack("I", ...)
to get int's byte representation. Then you must calculate destination memory address. Because unsigned int's size is 4 bytes, the address is equal to N*4.
memoryview supports slicing so you can just write:
mm[StartAddress:EndAddress] = BytesYouWantToWrite
where StartAddress is equal to N*4 and EndAddress is (N+1) * 4