mmap and bit wise twiddling - Raspberry Pi

Discussion in 'Python' started by Petr Jakes, May 2, 2012.

  1. Petr Jakes

    Petr Jakes Guest

    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
     
    Petr Jakes, May 2, 2012
    #1
    1. Advertising

  2. 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
     
    Thomas Heller, May 2, 2012
    #2
    1. Advertising

  3. 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
     
    Thomas Heller, May 2, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duncan Lyall

    bit twiddling in Java

    Duncan Lyall, Jan 16, 2005, in forum: Java
    Replies:
    2
    Views:
    1,370
    Duncan Lyall
    Jan 18, 2005
  2. Enrique Cruiz

    Row-wise vs. column-wise image processing

    Enrique Cruiz, Jan 25, 2007, in forum: C Programming
    Replies:
    10
    Views:
    724
    christian.bau
    Jan 26, 2007
  3. Enrique Cruiz
    Replies:
    5
    Views:
    420
    Jim Langston
    Jan 25, 2007
  4. Sal
    Replies:
    16
    Views:
    196
    Ilya Zakharevich
    May 25, 2010
  5. Jean Dubois
    Replies:
    5
    Views:
    781
    Jean Dubois
    Dec 23, 2013
Loading...

Share This Page