Python Mapped Registers

Has anyone come up with a good solution for working with memory device registers in Python? Ideally, something that would allow me to work with the named registers and even name the bitpods in these registers, so that it is not really ad hoc? Perfectly cross-platform, but I can only live with Linux.

It seems like some combination of mmap and ctypes Structs can handle this, but a) Structures do not seem to allow you to specify the locations of the bit fields (and they are not in C, therefore ...) and b) I cannot figure out how would I reset the Struct mapping on top of mmap.

Or am I fundamentally asking how can I use a circular saw as a screwdriver? (i.e. this does not do this and why do you want?)

An example in C (untested, NOT to use) would look something like this:

volatile struct {
    uint32_t mfr_id;
    uint32_t prod_id;
    uint32_t ctl;
    uint32_t dummy[5];
    ...
} * pDev;
hFile = open('/dev/bridge', O_RDWR)
pDev = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, hFile, 0);
assert(pDev->mfr_id == 0x12345678);
assert(pDev->prod_id == 0xDEADBEEF);

uint32_t temp = pDev->ctl;
temp &= CTL_ACCESS_MASK | CTL_SHIFT_MASK;
temp |= CTL_ACCESS_RO | CTL_SHIFT_5;
pDev->ctl = temp;
+5
source share
1 answer

I think the easiest way is to write access functions in C and call them from Python (between ctypes and Cython, calling C code from Python is not that difficult).

0
source

All Articles