1

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?

vivek patel
  • 95
  • 1
  • 15
  • My Python is tragically weak, but you may be able to take advantage of `*(ptr_vdma+5)` being identical to `ptr_vdma[5]`. – user4581301 Oct 12 '18 at 17:43
  • I tried it earlier, it throws an error called `TypeError: 'c_ulong' object does not support item assignment` – vivek patel Oct 13 '18 at 07:25

1 Answers1

3

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

Igor
  • 1,307
  • 1
  • 10
  • 19
  • when I use memoryview, it throws an error called `TypeError: cannot make memory view because the object does not have the buffer interface` Am I doing mmap wrong? or I need to do some step before using memoryview? – vivek patel Oct 13 '18 at 07:27
  • Do you use python 3? This error shows up in python 2.7 : https://bugs.python.org/issue9229 – Igor Oct 13 '18 at 07:40
  • I am sorry, I am using a virtual environment and I was not in the virtual environment. that why I had that error. but even in python3, I am getting this error ValueError: memoryview: invalid value for formate 'B' , when i am using mm[5]= FRBUF_ADDR_0 (FRBUF_ADDR_0 = 0x1E000000) , seems like i can write only limited values (0xff) in the buffer. – vivek patel Oct 13 '18 at 08:45
  • Actually it was my original answer, which was not corecct. See updated version. – Igor Oct 13 '18 at 09:01
  • thank you very much, it works like a charm, but would you mind explain it a little? – vivek patel Oct 13 '18 at 09:09
  • 1
    I've added some explanation, hope it;s more clear now. – Igor Oct 13 '18 at 09:48
  • 1
    thank you very much, it is perfect, I have been trying to solve this problem for 3 days and you have done so easily. – vivek patel Oct 13 '18 at 10:01