Firefly Open Source Community

   Login   |   Register   |
New_Topic
Print Previous Topic Next Topic

[fireflyP] Python For Firefly Port release

2442

Credits

0

Prestige

0

Contribution

vip

Rank: 6Rank: 6

Credits
2442

[fireflyP] Python For Firefly Port release

Posted at 7/23/2016 11:59:58      View:3558 | Replies:0        Print      Only Author   [Copy Link] 1#
Last edited by zhansb In 7/23/2016 11:59 Editor

fireflyP is designed for using devices port on firefly or other similar platforms.It can support RK3288 now.
Like GPIO, PWM, SPI and so on.

NOTE: fireflyP is still in development. if you find something wrong, please let me know, and if you fix bugs, it is nice to give me 'Pull requests'


Install
fetch source code:
  1. $ git clone https://github.com/T-Firefly/pyFireflyP.git
  2. $ cd pyFireflyP
Copy the code


install python modules(python can be python2 or python3):
  1. $ sudo python setup.py install
Copy the code



Usage
GPIO and PWM control regs directly by devmem in fireflyP, and independent of kernel space.

The devmem source is modifed from pydevmem(Thanks to Kyle).

You need root privilege to execute it!
Gpio
Interface:
    init()
        Init GPIO function
        implement it before using Gpio
    get_level(self)
        Returns the level of the pin for input direction
        or return setting of the DR register for output gpios.

    set_dir(self, dir)
        set GPIO direction
        :dir: refer to GpioDir

    set_drv(self, drv)
        set GPIO drv
        :drv: refer to GpioDrv

    set_level(self, level)
        set GPIO output signal
        :level: refer to GpioLevel

    set_mux(self, mux)
        set GPIO mux
        :mux: refer to GpioMux

    set_pull(self, pull)
        set GPIO pull
        :pull: refer to GpioPull

Example for turn on/off the yellow led on firefly-rk3288:
  1.     $sudo python
  2.     >>> from fireflyP import Gpio
  3.     >>> Gpio.init()
  4.     >>> LED_YELLOW="GPIO8A2"
  5.     >>> led_yellow=Gpio(LED_YELLOW)
  6.     >>> led_yellow.set_dir(Gpio.OUTPUT) #set_dir have contained set_mux(GpioMux.MUX_GPIO)
  7.     >>> led_yellow.set_level(Gpio.LOW)  #turn on the yellow led
  8.     >>> led_yellow.set_level(Gpio.HIGH) #turn off the yellow led
Copy the code

or you can refer to demo/gpio_test.py

Pwm
Interface:
    init()
        Init PWM function
        implement it before using Pwm
    get_counter(self)
        Get PWM counter

    set_config(self, period, duty, config=10)
        set PWM period and duty.
        :period: ns
        :duty: ns
        :config: set PWMx_CTRL value except PWMx_CTRL.scale

    set_counter(self, counter)
        set PWM counter

    start(self)
        Start PWM

    stop(self)
        Stop PWM

Example for config pwm1(freq=1MHz,duty=50%):
  1.     $sudo python
  2.     >>> from fireflyP import Gpio
  3.     >>> from fireflyP import Pwm
  4.     >>> Gpio.init()
  5.     >>> g7a1=Gpio('GPIO7A1')
  6.     >>> g7a1.set_mux(1)         #set GPIO7A1 mux to pwm1
  7.     >>> Pwm.init()
  8.     >>> pwm=Pwm('PWM1')
  9.     >>> pwm.set_config(1000,500)
  10.     >>> pwm.start()
  11.     >>> pwm.stop()
Copy the code
or you can refer to demo/pwm_test.py

Spi
Spi in fireflyP is depend on spidev,make sure your kernel support spidev(like this patch) or you can down the ready-made firmware([Google drive],[Baidu]).

The Spi source is modifed from python-spi (Thanks to Thomas).


Interface:
    read(self, length, speed=0, bits_per_word=0, delay=0)
        Perform half-duplex Spi read as a binary string

        Args:
            length: Integer count of words to read
            speed: Optional temporary bitrate override in Hz. 0 (default)
                uses existing spidev speed setting.
            bits_per_word: Optional temporary bits_per_word override. 0
                (default) will use the current bits_per_word setting.
            delay: Optional delay in usecs between sending the last bit and
                deselecting the chip select line. 0 (default) for no delay.

        Returns:
            List of words read from device

    transfer(self, data, speed=0, bits_per_word=0, delay=0)
        Perform full-duplex Spi transfer

        Args:
            data: List of words to transmit
            speed: Optional temporary bitrate override in Hz. 0 (default)
                uses existing spidev speed setting.
            bits_per_word: Optional temporary bits_per_word override. 0
                (default) will use the current bits_per_word setting.
            delay: Optional delay in usecs between sending the last bit and
                deselecting the chip select line. 0 (default) for no delay.

        Returns:
            List of words read from Spi bus during transfer

    write(self, data, speed=0, bits_per_word=0, delay=0)
        Perform half-duplex Spi write.

        Args:
            data: List of words to write
            speed: Optional temporary bitrate override in Hz. 0 (default)
                uses existing spidev speed setting.
            bits_per_word: Optional temporary bits_per_word override. 0
                (default) will use the current bits_per_word setting.
            delay: Optional delay in usecs between sending the last bit and
                deselecting the chip select line. 0 (default) for no delay.

Example for config spi0:
  1.     $sudo python
  2.     >>> from fireflyP import Spi
  3.     >>> spi = Spi('/dev/spidev0.0')
  4.     >>> spi.mode = Spi.MODE_3
  5.     >>> spi.bits_per_word = 8
  6.     >>> spi.speed = 1000*1000
  7.     >>> received = spi.transfer([0x11, 0x22, 0xFF])
  8.     >>> spi.write([0x12, 0x34, 0xAB, 0xCD])
  9.     >>> received = spi.read(10)
Copy the code

or you can refer to demo/spi_test.py, it is a example of lighting up a oled.

More project information refer to github.
Reply

Use props Report

You need to log in before you can reply Login | Register

This forum Credits Rules

Quick Reply Back to top Back to list