Bonjour à tous ceux que je suis en train de convertir en code python (Accès au framboise pi 1Mhz timer), je suis bloqué quand mob nous devons + TIMER_OFFSET (timer = (long long int *)((char *)st_base + TIMER_OFFSET);
) quand j'essaye de le faire en Python j'ai obtenu SystemError: mmaps don't support concatenation
. Je cherchais un objet convertir mmap mais je ne trouve rien, Pouvons-nous nous battre contre ça? Il est possible de convertir tout ce code en python? Je veux dire maintenant j'ai des doutes à ce sujet?Convertir l'objet mmap (mmaps ne supporte pas la concaténation)/convertir le code c en python
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define ST_BASE (0x3F003000)
#define TIMER_OFFSET (4)
int main(int argc, char *argv[]) {
long long int t, prev, *timer; // 64 bit timer
int fd;
void *st_base; // byte ptr to simplify offset math
// get access to system core memory
if (-1 == (fd = open("/dev/mem", O_RDONLY))) {
fprintf(stderr, "open() failed.\n");
return 255;
}
// map a specific page into process's address space
if (MAP_FAILED == (st_base = mmap(NULL, 4096,
PROT_READ, MAP_SHARED, fd, ST_BASE))) {
fprintf(stderr, "mmap() failed.\n");
return 254;
}
// set up pointer, based on mapped page
timer = (long long int *)((char *)st_base + TIMER_OFFSET); //<- here is problem
// read initial timer
prev = *timer;
// and wait
sleep(1);
while (1==1) { // forever
// read new timer
t = *timer;
printf("Timer diff = %lld \r", prev);
fflush(stdout);
// save current timer
prev = t;
// and wait
sleep(1);
}
// will never get here
return 0;
}
code Python (pour l'instant je sauter cette ifs):
ST_BASE = 0x3F003000
TIMER_OFFSET = 4
import struct
sizeof_long_long = struct.calcsize('q')
def timer():
while True:
fd = os.open("/dev/mem",os.O_RDWR | os.O_SYNC)
stBase = mmap.mmap(fileno=fd,length=4096,offset=ST_BASE)
timer_bytes = stBase[TIMER_OFFSET:TIMER_OFFSET + sizeof_long_long]
#print(timer_bytes)
timer_value, = struct.unpack('q', timer_bytes)
print(timer_value)
os.close(fd)
@AnttiHaapala Ok j'ajouter un code de python, mais il n'y a pas beaucoup – BengBeng
Merci. Cela est passé de complètement hors-sujet à plutôt OK. Aussi quelle version de Python c'était? –
@AnttiHaapala 3.4.2 – BengBeng