控制GPIO、I2C和SPI为A20-Olinuxino-Micro
pyA20的Python项目详细描述
说明
该软件包提供了控制GPIO管脚、I2C和SPI的方法 公共汽车。它是为 A20-OLinuXino-MICRO, 但它也可以和其他电路板一起使用。在这种情况下,正确的操作是 不能保证。你可以查一下 wiki了解更多 信息。
注释:
- When using GPIO make sure that the desired gpio is not used by another periphery.
- Using this library requires root access.
GPIO方法
可用的方法如下:
- init()-初始化模块。必须先打电话。
- getcfg()-读取GPIO的当前配置。
- setcfg()-将配置写入gpio。
- input()-返回GPIO的当前值。
- output()-设置输出值。
- pull up()-设置上拉/下拉。
可用常数为:
- 高-1
- 低-0
- 输入-0
- ouptut-1
- pullup-1
- 下拉菜单-2
GPIO有两种命名方式:
- 按端口名称:ph0、pg2、pe10等。这些可以从端口模块导入:
>>>frompyA20.gpioimportport>>>dir(port)
- 按连接器名称和管脚编号:gpio2p12、gpio3p8、lcdp18、uext1p3等:
>>>frompyA20.gpioimportconnector>>>dir(connector)
通常,这些常数只是内存中与基值的偏移量 GPIO地址,因此可以将它们分配给数字类型变量。
>>>led=port.PH2>>>printled226
I2C方法
- init()-初始化模块
- open()-开始与从设备通信
- read()-从设备读取
- write()-将数据写入从设备
- close()-结束与从属设备的通信
SPI方法
- open()-使用给定配置打开SPI总线
- read()-从从属设备读取数据而不进行写入
- write()-在不读取的情况下将数据写入从设备
- xfer()-写,然后读
- close()-关闭SPI总线
示例
gpio
示例包括:*初始化GPIO模块*初始化一个GPIO 作为输出,另一个作为输入*轮询输入状态并写入 相应的输出值
#!/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)
I2c在此示例中:*I2C模块已导入*总线2已打开*
写入一些数据,然后进行验证
#!/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 device
在此示例中:*I2C模块已导入*总线2已打开* 写入一些数据,然后进行验证
#!/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 device
spi
在这个例子中:*SPI模块被导入*总线2和芯片选择0是 打开*一些数据传输到从属设备
#!/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 bus
更改日志
PYA20 0.2.12(2017年12月8日)
- Fixed extensions import
- Update examples
PYA20 0.2.11(2017年11月21日)
- Fixed mapping on portG
- Updated README
- Removed processor checking to allow build scripts
- Update license
PYA20 0.2.0(2014年9月2日)
- Updated to enable SPI and I2C control
- GPIO constant in separate modules
- Added example files
- Added support for Python3