Package: pyA20
DescriptionThe package provide methods for controlling GPIO pins, I2C and SPI
buses. It’s written forA20-OLinuXino-MICRO,
but it can be used with other boards. In this case proper operation is
not guaranteed. You can checkwikifor more
information.Notes:When using GPIO make sure that the desired gpio is not used by another periphery.Using this library requires root access.GPIO methodsThe following methods are available:init()- Make initialization of the module. Must be called first.getcfg()- Read current configuration of gpio.setcfg()- Write configuration to gpio.input()- Return current value of gpio.output()- Set output value.pullup()- Set pull-up/pull-down.The available constants are:HIGH- 1LOW- 0INPUT- 0OUPTUT- 1PULLUP- 1PULLDOWN- 2The gpio are named two ways:By port name: PH0, PG2, PE10, etc. These can be imported from port module:>>>frompyA20.gpioimportport>>>dir(port)By connector name and pin number: gpio2p12, gpio3p8, lcdp18, uext1p3 and etc:>>>frompyA20.gpioimportconnector>>>dir(connector)Generally these constants are just an offset in the memory from the base
GPIO address, so they can be assigned to a number type variable.>>>led=port.PH2>>>printled226I2C methodsinit()- Make initialization of the moduleopen()- Begin communication with slave deviceread()- Read from slave devicewrite()- Write data to slave deviceclose()- End communication with slave deviceSPI methodsopen()- Open SPI bus with given configurationread()- Read data from slave device without writewrite()- Write data to slave device without readxfer()- Do write and after that readclose()- Close SPI busExamplesGPIOThe example consist of: * Initialize gpio module * Initialize one gpio
as output and another one as input * Polling input state and write
corresponding output value#!/usr/bin/env pythonfrompyA20.gpioimportgpiofrompyA20.gpioimportportfrompyA20.gpioimportconnectorgpio.init()#Initialize module. Always called firstgpio.setcfg(port.PG9,gpio.OUTPUT)#Configure LED1 as outputgpio.setcfg(port.PG9,1)#This is the same as abovegpio.setcfg(port.PE11,gpio.INPUT)#Configure PE11 as inputgpio.setcfg(port.PE11,0)#Same as abovegpio.pullup(port.PE11,0)#Clear pullupsgpio.pullup(port.PE11,gpio.PULLDOWN)#Enable pull-downgpio.pullup(port.PE11,gpio.PULLUP)#Enable pull-upwhileTrue:ifgpio.input(port.PE11)==1:gpio.output(port.PG9,gpio.LOW)gpio.output(port.PG9,0)else:gpio.output(port.PG9,gpio.HIGH)gpio.output(port.PG9,1)I2CIn this example: * I2C module is imported * Bus number 2 is opened *
Some data is written, then verified#!/usr/bin/env pythonfrompyA20.i2cimporti2ci2c.init("/dev/i2c-2")#Initialize module to use /dev/i2c-2i2c.open(0x55)#The slave device address is 0x55#If we want to write to some registeri2c.write([0xAA,0x20])#Write 0x20 to register 0xAAi2c.write([0xAA,0x10,0x11,0x12])#Do continuous write with start address 0xAA#If we want to do write and readi2c.write([0xAA])#Set address at 0xAA registervalue=i2c.read(1)#Read 1 byte with start address 0xAAi2c.close()#End communication with slave deviceSPIIn ths example: * SPI module is imported * Bus 2 with chip-select 0 is
opened * Some data is transfered to slave device#!/usr/bin/env pythonfrompyA20.spiimportspispi.open("/dev/spidev2.0")#Open SPI device with default settings# mode : 0# speed : 100000kHz# delay : 0# bits-per-word: 8#Different ways to open devicespi.open("/dev/spidev2.0",mode=1)spi.open("/dev/spidev2.0",mode=2,delay=0)spi.open("/dev/spidev2.0",mode=3,delay=0,bits_per_word=8)spi.open("/dev/spidev2.0",mode=0,delay=0,bits_per_word=8,speed=100000)spi.write([0x01,0x02])#Write 2 bytes to slave devicespi.read(2)#Read 2 bytes from slave devicespi.xfer([0x01,0x02],2)#Write 2 byte and then read 2 bytes.spi.close()#Close SPI busChangelogpyA20 0.2.12 (8 DEC 2017)Fixed extensions importUpdate examplespyA20 0.2.11 (21 NOV 2017)Fixed mapping on portGUpdated READMERemoved processor checking to allow build scriptsUpdate licensepyA20 0.2.0 (02 SEP 2014)Updated to enable SPI and I2C controlGPIO constant in separate modulesAdded example filesAdded support for Python3