Thread: DMA and python

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Cool DMA and python


    Hi I am accessing hardware from Python. I am running Linux.
    I have a "C" library compiled as libvv_pts.so that I load using CDLL ...

    self.lib = CDLL('./libvv_pts.so')

    and in that library there is a function as follows ...

    /**
    * =============================
    * @brief read data from the application VME address space
    * @param handle returned from a successfull open
    * @param the 4 byte aligned byte offset where D32 reads will start
    * @param buf points to an array where the read data will be stored
    * @param size is the size in bytes of the read transaction to be performed
    * @return -1 on error (see errno) or 0 if closed OK
    */

    int vv_read(void *handle, int byte_offset, int *ibuf, size_t size)

    I implement vv_pts.py that calls the C library as follows ...


    def vv_read(self, byte_offset):
    """ Read from the application FPGA wishbone bus
    The byte offset will be aligned to D32
    The value will contain the 32 bit integer read
    """
    x = c_int(0)
    cc = self.lib.vv_read(self.handle, byte_offset, byref(x), 4)
    value = x.value
    if cc != 0:
    raise BusException("Failed vv_read: offset:0x%X" % (byte_offset))
    return value & 0xFFFFFFFF

    and call it in my program, and this all works just fine but is horribly slow. So I would like to read an array. I could even do a DMA tarnsfer if I could figure out a way of pointing it at Python memory, but I am trying a more simple array access as follows ...

    def vv_read_array(self, byte_offset, buf, size):
    """ Read size bytes into the string array buf
    The byte_offset and size will be D32 aligned
    """
    cc = self.lib.vv_read(self.handle, byte_offset, id(buf), size)
    if cc != 0:
    raise BusException("Failed vv_read_array: offset:0x%X size:%d" % (byte_offset, size))
    return cc

    So I tried "by_ref" and that didn't work, so I am using the id(buf) to get the address to copy to. From my "big integers" question I discover Python doesn't represent data in raw format and my read array function destroys the interpreter.

    Can anyone make a suggestion as to how to do a hardware array transfer into Python memory ...

    Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Write, python stores everything as an object.
    Python has objects with contiguous arrays.
    You could try the array module
    import array # etceteras
    or scipy
    www.scipy.org

    Carefully read all of the ctypes doc
    http://docs.python.org/py3k/library/ctypes.html
    I'm pretty sure you'll find that it supports large transfers.

    Here's an example I wrote years ago to call the gsl from python using ctypes: http://code.activestate.com/recipes/576550-gsl-real-fft-in-python3/

    Hope some thing in here helps.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Array access


    Hi David,

    You say ...
    Quote: "I've wrapped the high level gsl_interp routines and the outstanding gsl_rng routines for python3k. By request, I'll post these also."

    I would like you to post them

    Thanks
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    OK, here's the complete work.


    Assuming I can attach a .zip archive...

    There were four errors in the tests, which I fixed. All of were caused by python tightening their code since the python3 beta. It's been a while since I used gsl with python. The fft test finishes with a memory error (failure to deallocate fft storage) which doesn't bode well for the advice I gave earlier in this thread.

    I inserted a brief 00README file which accents that although no manual is included (no longer accessible to me, although I know exactly where a hard copy is within 1.1 miles of here) the tests fully demonstrate the functionality, as I recall.


    The initial value ODE solver uses an iterator. The code did use the new python3 features.

    Edit 2: unit tests take 15 seconds with ubuntu linux distribution on a 2009 ThinkPad T500.

    ALSO, PLEASE

    if any of you know how to make an installer so this package can go into site packages, or whatever it's called now in python 3, please make it known.

    It's a critical part of the project.
    Attached Files
    Last edited by b49P23TIvg; September 21st, 2012 at 10:44 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo