控制GPIO、I2C和SPI
pyA20Lime2的Python项目详细描述
该软件包提供了控制gpio管脚、i2c和spi总线的方法。 这是为A20-Olinuxino-Lime2编写的,但可以与其他板一起使用。如果你这样做了 我们不能保证模块正常工作。在使用这个之前 我们建议您在olimex wiki上阅读本文:
https://www.olimex.com/wiki/A20-OLinuXino-LIME2
使用gpio时,请确保所需的gpio未被其他外围设备使用。
GPIO方法
init() - Make initialization of the module. Always 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.
可用常数为:
NAME - EQUALS TO ==== ========= HIGH -> 1 LOW -> 0 INPUT -> 0 OUPTUT -> 1 PULLUP -> 1 PULLDOWN -> 2
GPIO有两种命名方式:
By port name: PH0, PG2, PE10, etc. These can be imported from port module:
>>> from pyA20Lime2.gpio import port >>> dir(port)By connector name and pin number: gpio2p12, gpio3p8, etc. These can be imported from connector module:
>>> from pyA20Lime2.gpio import connector >>> dir(connector)
通常,这些常量只是内存中与基本GPIO地址的偏移量,因此它们可以 分配给数字类型变量。
>>> led = port.PH2 >>> print led 226
I2C方法
init() - Make initialization of the module open() - Begin communication with slave device read() - Read from slave device write() - Write data to slave device close() - End communication with slave device
SPI方法
open() - Open SPI bus with given configuration read() - Read data from slave device without write write() - Write data to slave device without read xfer() - Do write and after that read close() - Close SPI bus
示例
GPIO:
#!/usr/bin/env python from pyA20Lime2.gpio import gpio from pyA20Lime2.gpio import port from pyA20Lime2.gpio import connector gpio.init() #Initialize module. Always called first gpio.setcfg(port.PG9, gpio.OUTPUT) #Configure LED1 as output gpio.setcfg(port.PG9, 1) #This is the same as above gpio.setcfg(port.PE11, gpio.INPUT) #Configure PE11 as input gpio.setcfg(port.PE11, 0) #Same as above gpio.pullup(port.PE11, 0) #Clear pullups gpio.pullup(port.PE11, gpio.PULLDOWN) #Enable pull-down gpio.pullup(port.PE11, gpio.PULLUP) #Enable pull-up while True: if gpio.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)
I2C:
#!/usr/bin/env python from pyA20Lime2 import i2c i2c.init("/dev/i2c-2") #Initialize module to use /dev/i2c-2 i2c.open(0x55) #The slave device address is 0x55 #If we want to write to some register i2c.write([0xAA, 0x20]) #Write 0x20 to register 0xAA i2c.write([0xAA, 0x10, 0x11, 0x12]) #Do continuous write with start address 0xAA #If we want to do write and read i2c.write([0xAA]) #Set address at 0xAA register value = i2c.read(1) #Read 1 byte with start address 0xAA i2c.close() #End communication with slave device
SPI:
#!/usr/bin/env python from pyA20Lime2 import spi spi.open("/dev/spidev2.0") #Open SPI device with default settings # mode : 0 # speed : 100000kHz # delay : 0 # bits-per-word: 8 #Different ways to open device spi.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 device spi.read(2) #Read 2 bytes from slave device spi.xfer([0x01, 0x02], 2) #Write 2 byte and then read 2 bytes. spi.close() #Close SPI bus
以根用户身份运行python脚本很重要!
更改日志
- pyAA20Lime2 0.2.1 (30 JUN 2015)
- Fixed issue with SPI xfer function
- pyA20Lime2 0.2.0 (3 SEP 2014)
- Initial release