2

Hello every one I'm trying to convert below code to python (Accessing the raspberry pi 1Mhz timer), I'm stuck when to mmap object we need to + TIMER_OFFSET (timer = (long long int *)((char *)st_base + TIMER_OFFSET);) when I try to do it in Python I got SystemError: mmaps don't support concatenation. I was looking for convert mmap object but I find nothing, Can we fight this ? It is possible to convert whole this code to python? I mean now I have second thoughts about it ?

#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;
}

Python code (for now I skip this 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)
6
  • @AnttiHaapala Ok I add python code but there is not a lot Commented Sep 18, 2017 at 10:59
  • Thanks. This turned from completely offtopic to rather OK. Also which python version this was? Commented Sep 18, 2017 at 11:02
  • @AnttiHaapala 3.4.2 Commented Sep 18, 2017 at 11:04
  • Try to use the os.O_RDONLY only + mmap(..., prot=PROT_READ) Commented Sep 18, 2017 at 13:45
  • Can't use prot=PROT_READ because it is not definied, about os.O_RDONLY funny thing before I have os.O_RDWR and code was executing (and still is) when I change on os.O_RDONLY suddenlny I got [Errno 13] permission denied (which is strange because I run code with "sudo" and RDONLY<RDWR) Commented Sep 19, 2017 at 5:25

1 Answer 1

2

Almost good :) Just unpack_from instead off unpack

import os, mmap, sys
import struct

ST_BASE = 0x3F003000
TIMER_OFFSET = 4
sizeof_long_long = struct.calcsize("Q")
LENGTH = TIMER_OFFSET + sizeof_long_long

def timer():
    fd = os.open("/dev/mem", os.O_RDONLY | os.O_SYNC)
    stBase = mmap.mmap(fileno=fd, length=4096, access=mmap.ACCESS_COPY, offset=ST_BASE)
    os.close(fd)

    while True:
        timer_value = struct.unpack_from("Q", stBase, TIMER_OFFSET)[0]
        print("timer_value")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.