haas506 2.0开发教程-driver-SPI
B站haas开发教程 B站教学视频链接
最新案例教程点击下方链接跳转,CSDN已停止更新
点
击
跳
转
1.硬件图
2.读取spi设备的寄存器值
案例说明:
- 本案例是对spi设备的读写测试,使用不同函数完成2种读写方式。
方法1
- 使用spi.write(writeBuf, len) 、spi.read()函数分别写入读取。
main.py
# coding=utf-8
# This is a sample Python script.
#0x9f是一个寄存器的地址
#bytearray(b'\xef@\x16'),是读取到的值,有需要的话 可以看一下ASCII,即0xef,0x40,0x16,一共三个字节
from driver import SPI
from driver import GPIO
print("-------------------spi test--------------------")
spi = SPI()
spi.open("SPI0")
readBuf = bytearray(3)
writeBuf = bytearray([0x9f])
print(writeBuf)
print(readBuf)
value1 = spi.write(writeBuf)
value2=spi.read(readBuf)
print(value1)
print(value2)
print(writeBuf)
print(readBuf)
spi.close()
print("-------------------spi test--------------------")
board.json
{
"version": "2.0.0",
"io": {
"ADS1115": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 72
},
"cs":{
"type":"GPIO",
"port": 15,
"dir": "output",
"pull":"pullup"
},
"mosi":{
"type":"GPIO",
"port": 16,
"dir": "output",
"pull":"pullup"
},
"miso":{
"type":"GPIO",
"port": 17,
"dir": "output",
"pull":"pullup"
},
"clk":{
"type":"GPIO",
"port": 18,
"dir": "output",
"pull":"pullup"
},
"SPI0": {
"type": "SPI",
"port": 0,
"mode": "master",
"freq": 2000000
}
},
"debugLevel": "ERROR"
}
日志输出
-------------------spi test--------------------
bytearray(b'\x9f')
bytearray(b'\x00\x00\x00')
1
3
bytearray(b'\x9f')
bytearray(b'\xef@\x16')
-------------------spi test--------------------
方法2
使用spi.writeRead(writeBuf, readBuf)进行读写,但读取时需要使用中间值进行储存。
# coding=utf-8
# This is a sample Python script.
from driver import SPI
from driver import GPIO
print("-------------------spi test--------------------")
spi = SPI()
cs = GPIO()
spi.open("SPI0")
cs.open('cs')
readBuf = bytearray(4)
writeBuf = bytearray([0x9f,0x00,0x00,0x00])
temp=bytearray(4)
print(writeBuf)
print(readBuf)
cs.write(0)
value=spi.readAfterWrite(readBuf,writeBuf)
temp[0]=readBuf[0]
temp[1]=readBuf[1]
temp[2]=readBuf[2]
temp[3]=readBuf[3]
cs.write(1)
print(value)
print(temp)
print(temp[1:])
cs.close()
spi.close()
print("-------------------spi test--------------------")
board.json
{
"version": "2.0.0",
"io": {
"ADS1115": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 72
},
"cs":{
"type":"GPIO",
"port": 15,
"dir": "output",
"pull":"pullup"
},
"mosi":{
"type":"GPIO",
"port": 16,
"dir": "output",
"pull":"pullup"
},
"miso":{
"type":"GPIO",
"port": 17,
"dir": "output",
"pull":"pullup"
},
"clk":{
"type":"GPIO",
"port": 18,
"dir": "output",
"pull":"pullup"
},
"SPI0": {
"type": "SPI",
"port": 0,
"mode": "master",
"freq": 2000000
}
},
"debugLevel": "DEBUG"
}
日志输出
-------------------spi test--------------------
bytearray(b'\x9f\x00\x00\x00')
bytearray(b'\x00\x00\x00\x00')
4
bytearray(b'\xff\xef@\x16')
bytearray(b'\xef@\x16')
-------------------spi test--------------------
3.Class-SPI函数库
open | write | read | readAfterWrite | close |
---|---|---|---|---|
打开spi | spi写操作,往指定寄存器内写数据 | spi读操作,从指定寄存器中读取数据 | spi写和读操作,往寄存器写数据、从寄存器中取数据 | 关闭spi |
实例化
spi=SPI()
spi.open(params)
作用:打开spi
参数:params是一个字符串,需要预先在board.json中定义
返回:0成功
spi.write(writeBuf)
作用:spi写操作,往指定寄存器内写数据
参数:发送数据的存储数组,期待发送的字节数为buf的长度。
返回:写入的数据长度,即len
spi.read()
作用:spi读操作,从指定寄存器中读取数据
返回:读取的数据长度,即len
参数说明:
属性 | 类型 | 必填 | 描述 |
---|---|---|---|
buf | bytearray | 是 | 接收数据的存储数组,期待接收的字节数为buf的长度。 |
data | bytearray | 是 | 发送数据的存储数组,期待发送的字节数为data的长度。 |
SPI.readAfterWrite(buf, data)
作用:spi写和读操作,往寄存器写数据、从寄存器中取数据
参数:writeBuf字节数组,数组中包含寄存器的地址;readBuf字节数组,数组中包含指定寄存器的地址,
即从哪个寄存器取数据
返回:读取到的数据长度
spi.close()
作用:关闭spi
返回:0成功
4.总结
本节介绍了如何使用haas506的driver库中的SPI模块。需要注意的有:
- 在进行spi的读写操作时,需要对cs引脚进行一些处理,即先置cs脚为低电平,待数据读取/写入完毕后置cs脚为高电平。