mmap and bit wise twiddling - Raspberry Pi

P

Petr Jakes

Hi,
I am trying to work with HW peripherals on Raspberry Pi
To achieve this, it is necessary read/write some values from/to the
memory directly.

I am looking for some wise way how to organize the bit twiddling.

To set some specific bit, for example, it is necessary:
- read 4 bytes string representation (I am using mmap)
- transform it to the corresponding integer (I am using numpy)
- do some bit masking over this integer
- transport integer to the string representation (numpy again)
- write it back to the memory

In other words I mean: is there wise way to create an instrument/
machinery to define Class and then simply define all necessary objects
and set the bit values over object attributes so the whole bit-
twiddling remains under the hood.

say:
LED01 = GPIO(4) # gpio PIN number 4 is assigned to the LED01 name
(attribute)
LED01.on()
LED01.off()
or

gpio = GPIO()
LED01 = gpio.pin04
LED01 = 1 # led diode is shining (or gpio pin 4 is set)
LED01 = 0 # led diode is off

General suggestions, how to organise this work are more then welcome.

Petr Jakes
 
T

Thomas Heller

Am 02.05.2012 19:40, schrieb Petr Jakes:
Hi,
I am trying to work with HW peripherals on Raspberry Pi
To achieve this, it is necessary read/write some values from/to the
memory directly.

I am looking for some wise way how to organize the bit twiddling.

To set some specific bit, for example, it is necessary:
- read 4 bytes string representation (I am using mmap)
- transform it to the corresponding integer (I am using numpy)
- do some bit masking over this integer
- transport integer to the string representation (numpy again)
- write it back to the memory

In other words I mean: is there wise way to create an instrument/
machinery to define Class and then simply define all necessary objects
and set the bit values over object attributes so the whole bit-
twiddling remains under the hood.

say:
LED01 = GPIO(4) # gpio PIN number 4 is assigned to the LED01 name
(attribute)
LED01.on()
LED01.off()
or

gpio = GPIO()
LED01 = gpio.pin04
LED01 = 1 # led diode is shining (or gpio pin 4 is set)
LED01 = 0 # led diode is off

I have an abstract BitVector base-class that allows to get/set single
bits or several bits in a convenient way. You must define concrete
subclasses which define a _value get/set property that actually
updates the byte or word in the hardware. I use it to access bits
or groups of bits of I2C devices.

You would basically code like this, assuming an 8-bit GPIO port:

class GPIO(BitVector):
def __init__(self, address, value=0xFF, nbits=8):
self.address = address
super(GPIO, self).__init__(value, nbits)
def _get_value(self):
"read an 8-bit value from the hardware as 8-bit integer"
...
def _set_value(self, v):
"write the 8-bit value 'v' to the hardware"
...

then you can do:

gpio = GPIO(0x12345678)

led0 = gpio[4] # bit 4
led0.value = 1 # switch led on
print led0.value # get led status

For multiple bits use this (note that different from standard Python
practices, indexing works inclusive and uses [high_bitnum:low_bitnum]:

port = GPIO(0x12345678)
high_nibble = port[7:4]
print high_nibble.value
low_nibble = port[3:0]
low_nibble.value = 0xF

Thomas
 
T

Thomas Heller

Am 02.05.2012 22:05, schrieb Thomas Heller:
class GPIO(BitVector):
def __init__(self, address, value=0xFF, nbits=8):
self.address = address
super(GPIO, self).__init__(value, nbits)
def _get_value(self):
"read an 8-bit value from the hardware as 8-bit integer"
...
def _set_value(self, v):
"write the 8-bit value 'v' to the hardware"
...

Sorry, forgot to create the property; so please add this
to the class definition:

_value = property(_get_value, _set_value)


Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top