I've posted a more generic version of this question on Stack Overflow, but since I've had no response I'm hoping someone over this side might have an idea.
I've been working on a project in PHP which requires mmap'ing /dev/mem to gain access to the hardware peripheral registers. As there's no native mmap, I have implemented a python subprocess, which communicates with the PHP app via
Strangely, everything works as expected for writing, but when it comes to reading, I have a weird issue where the data coming back is
for as many bytes as I read. It seems to me like it's no coincidence that this is in the
GPIO register address space, since I have seen different repeating patterns in other address spaces.
The unusual thing is that this only occurs with
mem.read(). The workaround I have is by using
struct.unpack_from(), then packing it again with
struct.pack(). It's slow and ugly.
sys.stdout.write(str(struct.pack('L', struct.unpack_from('L', mem, mem.tell()))))
From what I understood, both of those functions should be treating the stream in the same way - apparently they don't!
It's quite possible that I'm doing something completely stupid, but if someone could point it out that'd be great!
To see the issue, I've got some test code like this:
fd = os.open("/dev/mem", os.O_RDWR | os.O_SYNC) mem = mmap.mmap(fd, 4096, offset=0x3f200000) os.close(fd) mem.seek(0x034) bytes_read = mem.read(4) print bytes_read mem.seek(0x034) bytes_unpack_pack = struct.pack('L', struct.unpack_from('L', mem, mem.tell())) print bytes_unpack_pack
Should they not be the same?