
theller at ctypes
May 2, 2012, 1:05 PM
Post #2 of 3
(112 views)
Permalink
|
|
Re: mmap and bit wise twiddling - Raspberry Pi
[In reply to]
|
|
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
|