Skip to content

Commit

Permalink
mmio: fix duplicate transactions in integral read/write methods
Browse files Browse the repository at this point in the history
the integral write methods (e.g. write32, write16, write8) were causing
duplicate writes on the memory bus, apparently due to the underlying
implementation of packing / unpacking a memory mapping slice. this
commit modifies the integral read and write methods to dereference a
ctypes pointer of the appropriate width instead, which was tested and
shown to only yield one transaction on the bus. special thanks to
@paul-demo for raising this issue, investigating solutions, and testing.

resolves #50.
  • Loading branch information
vsergeev committed Feb 14, 2021
1 parent 7ff3403 commit b318a6a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions periphery/mmio.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def read32(self, offset):

offset = self._adjust_offset(offset)
self._validate_offset(offset, 4)
return struct.unpack("=L", self.mapping[offset:offset + 4])[0]
return ctypes.c_uint32.from_buffer(self.mapping, offset).value

def read16(self, offset):
"""Read 16-bits from the specified `offset` in bytes, relative to the
Expand All @@ -127,7 +127,7 @@ def read16(self, offset):

offset = self._adjust_offset(offset)
self._validate_offset(offset, 2)
return struct.unpack("=H", self.mapping[offset:offset + 2])[0]
return ctypes.c_uint16.from_buffer(self.mapping, offset).value

def read8(self, offset):
"""Read 8-bits from the specified `offset` in bytes, relative to the
Expand All @@ -149,7 +149,7 @@ def read8(self, offset):

offset = self._adjust_offset(offset)
self._validate_offset(offset, 1)
return struct.unpack("B", self.mapping[offset:offset + 1])[0]
return ctypes.c_uint8.from_buffer(self.mapping, offset).value

def read(self, offset, length):
"""Read a string of bytes from the specified `offset` in bytes,
Expand Down Expand Up @@ -196,7 +196,7 @@ def write32(self, offset, value):

offset = self._adjust_offset(offset)
self._validate_offset(offset, 4)
self.mapping[offset:offset + 4] = struct.pack("=L", value)
ctypes.c_uint32.from_buffer(self.mapping, offset).value = value

def write16(self, offset, value):
"""Write 16-bits to the specified `offset` in bytes, relative to the
Expand All @@ -220,7 +220,7 @@ def write16(self, offset, value):

offset = self._adjust_offset(offset)
self._validate_offset(offset, 2)
self.mapping[offset:offset + 2] = struct.pack("=H", value)
ctypes.c_uint16.from_buffer(self.mapping, offset).value = value

def write8(self, offset, value):
"""Write 8-bits to the specified `offset` in bytes, relative to the
Expand All @@ -244,7 +244,7 @@ def write8(self, offset, value):

offset = self._adjust_offset(offset)
self._validate_offset(offset, 1)
self.mapping[offset:offset + 1] = struct.pack("B", value)
ctypes.c_uint8.from_buffer(self.mapping, offset).value = value

def write(self, offset, data):
"""Write a string of bytes to the specified `offset` in bytes, relative
Expand Down

0 comments on commit b318a6a

Please sign in to comment.