Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions cnc/hal_raspberry/rpgpio_private.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,27 @@ def CM_DIV_VALUE(x):


class PhysicalMemory(object):
lc = ctypes.cdll.LoadLibrary('libc.so.6')

def __init__(self, phys_address, size=PAGE_SIZE):
""" Create object which maps physical memory to Python's mmap object.
:param phys_address: based address of physical memory
"""
self._size = size
phys_address -= phys_address % PAGE_SIZE
fd = self._open_dev("/dev/mem")
self._rmap = mmap.mmap(fd, size, flags=mmap.MAP_SHARED,
prot=mmap.PROT_READ | mmap.PROT_WRITE,
offset=phys_address)
# Pypy doesn't work correctly with Python's mmap, use native calls
# self._rmap = mmap.mmap(fd, size, flags=mmap.MAP_SHARED,
# prot=mmap.PROT_READ | mmap.PROT_WRITE,
# offset=phys_address)
self._rmap = self.lc.mmap(None, size, mmap.PROT_READ | mmap.PROT_WRITE,
mmap.MAP_SHARED, fd, phys_address)
os.close(fd)
atexit.register(self.cleanup)

def cleanup(self):
self._rmap.close()
# self._rmap.close()
self.lc.munmap(self._rmap, self._size)

@staticmethod
def _open_dev(name):
Expand All @@ -139,19 +146,22 @@ def _close_dev(fd):
os.close(fd)

def write_int(self, address, int_value):
self._rmap[address:address + 4] = struct.pack("I", int_value)
# self._rmap[address:address + 4] = struct.pack("I", int_value)
ctypes.c_uint32.from_address(self._rmap + address).value = int_value

def write(self, address, data):
self._rmap.seek(address)
self._rmap.write(struct.pack(str(len(data)) + "I", *data))
# self._rmap.seek(address)
# self._rmap.write(struct.pack(str(len(data)) + "I", *data))
for i in range(0, len(data)):
self.write_int(address + i * 4, data[i])

def read_int(self, address):
return struct.unpack("I", self._rmap[address:address + 4])[0]
# return struct.unpack("I", self._rmap[address:address + 4])[0]
return ctypes.c_uint32.from_address(self._rmap + address).value

def get_size(self):
return self._size


class CMAPhysicalMemory(PhysicalMemory):
IOCTL_MBOX_PROPERTY = ctypes.c_long(0xc0046400).value
def __init__(self, size):
Expand Down