Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

mmap and bit wise twiddling - Raspberry Pi

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


petr.jakes.tpc at gmail

May 2, 2012, 10:40 AM

Post #1 of 3 (115 views)
Permalink
mmap and bit wise twiddling - Raspberry Pi

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
--
http://mail.python.org/mailman/listinfo/python-list


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
Attachments: bitvector.py (3.58 KB)


theller at ctypes

May 2, 2012, 1:07 PM

Post #3 of 3 (112 views)
Permalink
Re: mmap and bit wise twiddling - Raspberry Pi [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.