haas506开发教程-driver-SPI
1.硬件图
2.读取spi设备的寄存器值(方法1)
- 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()
cs = GPIO()
spi.open("SPI0")
cs.open('cs')
readBuf = bytearray(3)
writeBuf = bytearray([0x9f])
print(writeBuf)
print(readBuf)
cs.write(0)
value1 = spi.write(writeBuf,1)
value2=spi.read(readBuf,3)
print(value1)
print(value2)
cs.write(1)
print(writeBuf)
print(readBuf)
cs.close()
spi.close()
print("-------------------spi test--------------------")
- board.json
{
"version": "1.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--------------------
3.读取spi设备的寄存器值(方法2)
- main.py
# 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.writeRead(writeBuf,readBuf)
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": "1.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--------------------
4.Class-SPI
- SPI
-
实例化
- spi=SPI()
-
spi.open(params)
- 作用:打开spi
- 参数:params是一个字符串,需要预先在board.json中定义
- 返回:0成功
-
spi.write(writeBuf, len)
- 作用:spi写操作,往指定寄存器内写数据
- 参数:spi写操作需要传入一个writeBuf字节数组,数组中包含寄存器的地址和待写入的数据;还需传入writeBuf数组的长度len
- 返回:写入的数据长度,即len
-
spi.read()
- 作用:spi读操作,从指定寄存器中读取数据
- 参数:spi读操作需要传入一个readBuf字节数组,数组中包含指定寄存器的地址,即从哪个寄存器取数据,完成读操作后,所取得的寄存器中的数据被存放在ReadBuf中;还需传入readBuf数组的长度len
- 返回:读取的数据长度,即len
-
spi.writeRead(writeBuf, readBuf)
- 作用:spi写和读操作,往寄存器写数据、从寄存器中取数据
- 参数:writeBuf字节数组,数组中包含寄存器的地址;readBuf字节数组,数组中包含指定寄存器的地址,即从哪个寄存器取数据
- 返回:读取到的数据长度
-
spi.close()
- 作用:关闭spi
- 返回:0成功
-
5.总结
本节介绍了如何使用haas506的driver库中的SPI模块。需要注意的有:
- 在进行spi的读写操作时,需要对cs引脚进行一些处理,即先置cs脚为低电平,待数据读取/写入完毕后置cs脚为高电平。