|
[fireflyP] SPI使用
发表于 2016-7-28 11:53:36
浏览:36492
|
回复:16
打印
只看该作者
[复制链接]
楼主
本帖最后由 zhansb 于 2016-8-25 09:23 编辑
模块安装请参考:http://developer.t-firefly.com/thread-10503-1-2.html
fireflyP的SPI使用spidev接口,所以使用前,必须确保内核已经支持spidev(可参考补丁或者下载现成的固件)。
在python中使用help(Spi)可以打印Spi使用的方法,主要的接口有:
| 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.
SPI使用的流程如下:
1.初始化SPI设备,如spi = Spi('/dev/spidev0.0')
2.设置SPI的参数,比如speed,bits_per_word,mode等,也可以在初始化SPI时作为关键字参数传入
3.然后就可以开始读写SPI了,如spi.write([0x12, 0x34, 0xAB, 0xCD])、received = spi.read(10)
目前Firefly-RK3288目前支持的SPI配置参数有:mode、speed、bits_per_word、cs_high、loop,不支持lsb_first、no_cs等参数的设置
下面用介绍一下怎么使用fireflyP中的SPI点亮OLED屏:
笔者手上刚好有个0.96寸的OLED显示屏:
OLED与开发板的接线如下:
OLED | Firefly-RK3288 | GND=电源地 | GND | VCC=电源 | 5V | D0=时钟 | SPI0 CLK | D1=数据 | SPI0 TXD | RST=复位 | GPIO0A7 | DC=数据/命令 | GPIO7B1 | CS=片选 | SPI0 CSN0 |
OLED的demo代码在github上的demo/spi_test.py,我们来分析一下Oled的代码:
__init__(self, dev, pin_rst, pin_dc)函数主要是做SPI设备、RST和DC的初始化并通过SPI写入OLED初始化的指令。其中SPI设备初始化相关的有:
- self._spi = Spi(dev)
- self._spi.mode = Spi.MODE_3
- self._spi.bits_per_word = 8
- self._spi.speed = 1000*1000
复制代码 这里设置了SPI传输的模式、格式和速度,当初笔者就在mode这里掉坑了,后面参考了wiki中“SPI 使用”,使用示波器才看出原来是波形传输的极性和相位有问题。
初始完SPI和GPIO后,接下来就可以使用SPI写入指令,写了个封装的接口_write,由于Firefly-RK3288目前不支持lsb_first,需要软件转换LSB和MSB(相关代码:tbuf=int(bin(tbuf)[2:].zfill(8)[::-1], 2))
OLED写指令用下面的接口:
- self._write(reg,Oled.WR_CMD)
复制代码 写数据:
- self._write(data,Oled.WR_DATA)
复制代码
Oled也封装了一些显示接口:
| draw_bitmap(self, bitmap, x, y, w, h)
|
| draw_char(self, x, y, ch)
|
| draw_line(self, x0, y0, x1, y1)
|
| draw_point(self, x, y)
|
| draw_str(self, x, y, s)
因此我们可以在Oled上直接使用以上接口显示:
- $ cd pyFireflyP/demo/
- $ sudo python
- >>> from spi_test import Oled,BM_FIREFLY
- >>> oled = Oled("/dev/spidev0.0", 'GPIO0A7', 'GPIO7B1')
- >>> oled.draw_str(2,1, 'Hello,Firefly!')
- >>> oled.draw_bitmap(BM_FIREFLY, 44,16, 40, 48)
- >>> oled.draw_line(1,1,128,1)
- >>> oled.draw_line(1,1,1,64)
- >>> oled.draw_line(128,1,128,64)
- >>> oled.draw_line(1,64,128,64)
复制代码 也可以直接执行:
- $ sudo python spi_test.py
复制代码 下面看看显示效果:
|
|