12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import numpy as np
- from _pciffi import ffi, lib
- class Error(Exception):
- pass
- class Types(object):
- # This enum also maps directly to the type size for now
- UINT8 = 1
- UINT32 = 4
- TYPE_NAMES = {
- Types.UINT8: 'uint8_t',
- Types.UINT32: 'uint32_t',
- }
- class Device(object):
- def __init__(self, model, device='/dev/fpga0'):
- self.handle = lib.pcilib_open(device, model)
- self.dma_engine = lib.pcilib_find_dma_by_addr(self.handle, lib.PCILIB_DMA_BIDIRECTIONAL, 0xFF)
- def __enter__(self):
- return self
- def __exit__(self, *args):
- self.close()
- def read(self, addr, num, t=Types.UINT32):
- data = ffi.new("{}[]".format(TYPE_NAMES[t]), num)
- if lib.pcilib_read(self.handle, 0, addr, num, t, data) != 0:
- raise Error("Could not read")
- return list(data[0:len(data)])
- def write(self, addr, value, t=Types.UINT32):
- data = ffi.new("{}[]".format(TYPE_NAMES[t]), [value])
- if lib.pcilib_write(self.handle, 0, addr, t, 1, data) != 0:
- raise Error("Could not write")
- def start_dma(self):
- lib.pcilib_start_dma(self.handle, self.dma_engine, lib.PCILIB_DMA_FLAGS_DEFAULT)
- def close(self):
- lib.pcilib_close(self.handle)
|