tools/remote/comm_pcie: use ctypes.c_uint32 to do 32-bit accesses and avoid double writes/reads.

This commit is contained in:
Florent Kermarrec 2020-06-30 14:12:35 +02:00
parent 68297fce9e
commit 5713c21017
1 changed files with 12 additions and 14 deletions

View File

@ -1,35 +1,33 @@
# This file is Copyright (c) 2015-2019 Florent Kermarrec <florent@enjoy-digital.fr> # This file is Copyright (c) 2015-2020 Florent Kermarrec <florent@enjoy-digital.fr>
# License: BSD # License: BSD
import os
import ctypes
import mmap import mmap
class CommPCIe: class CommPCIe:
def __init__(self, bar, debug=False): def __init__(self, bar, debug=False):
self.bar = bar self.bar = bar
self.debug = debug self.debug = debug
def open(self): def open(self):
if hasattr(self, "sysfs"): if hasattr(self, "file"):
return return
self.sysfs = open(self.bar, "r+b") self.file = os.open(self.bar, os.O_RDWR | os.O_SYNC)
self.sysfs.flush() self.mmap = mmap.mmap(self.file, 0)
self.mmap = mmap.mmap(self.sysfs.fileno(), 0)
def close(self): def close(self):
if not hasattr(self, "sysfs"): if not hasattr(self, "file"):
return return
self.file.close()
del self.file
self.mmap.close() self.mmap.close()
del self.mmap
self.sysfs.close()
del self.sysfs
def read(self, addr, length=None): def read(self, addr, length=None):
data = [] data = []
length_int = 1 if length is None else length length_int = 1 if length is None else length
for i in range(length_int): for i in range(length_int):
self.mmap.seek(addr + 4*i) value = ctypes.c_uint32.from_buffer(self.mmap, addr + 4*i).value
value = int.from_bytes(self.mmap.read(4), byteorder="little")
if self.debug: if self.debug:
print("read {:08x} @ {:08x}".format(value, addr + 4*i)) print("read {:08x} @ {:08x}".format(value, addr + 4*i))
if length is None: if length is None:
@ -41,6 +39,6 @@ class CommPCIe:
data = data if isinstance(data, list) else [data] data = data if isinstance(data, list) else [data]
length = len(data) length = len(data)
for i, value in enumerate(data): for i, value in enumerate(data):
self.mmap[addr + 4*i:addr + 4*(i + 1)] = value.to_bytes(4, byteorder="little") ctypes.c_uint32.from_buffer(self.mmap, addr + 4*i).value = value
if self.debug: if self.debug:
print("write {:08x} @ {:08x}".format(value, addr + 4*i)) print("write {:08x} @ {:08x}".format(value, addr + 4*i))