1.7 haas506 2.0开发教程-driver-SPI

本文详细介绍了如何在Haas5062平台上使用driver库进行SPI通信,包括硬件连接、方法1(独立操作)、方法2(writeRead函数)、Class-SPI函数库的运用,并特别强调了CS引脚处理的重要性。
摘要由CSDN通过智能技术生成


B站haas开发教程 B站教学视频链接

最新案例教程点击下方链接跳转,CSDN已停止更新

点击跳转HaaS506官方最新案例







1.硬件图

在这里插入图片描述

2.读取spi设备的寄存器值

案例说明:

  1. 本案例是对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函数库

openwritereadreadAfterWriteclose
打开spispi写操作,往指定寄存器内写数据spi读操作,从指定寄存器中读取数据spi写和读操作,往寄存器写数据、从寄存器中取数据关闭spi

实例化
spi=SPI()

spi.open(params)

作用:打开spi
参数:params是一个字符串,需要预先在board.json中定义
返回:0成功

spi.write(writeBuf)

作用:spi写操作,往指定寄存器内写数据
参数:发送数据的存储数组,期待发送的字节数为buf的长度。
返回:写入的数据长度,即len

spi.read()

作用:spi读操作,从指定寄存器中读取数据
返回:读取的数据长度,即len
参数说明:
属性类型必填描述
bufbytearray接收数据的存储数组,期待接收的字节数为buf的长度。
databytearray发送数据的存储数组,期待发送的字节数为data的长度。

SPI.readAfterWrite(buf, data)

作用:spi写和读操作,往寄存器写数据、从寄存器中取数据
参数:writeBuf字节数组,数组中包含寄存器的地址;readBuf字节数组,数组中包含指定寄存器的地址,
即从哪个寄存器取数据
返回:读取到的数据长度

spi.close()

作用:关闭spi
返回:0成功

4.总结

本节介绍了如何使用haas506的driver库中的SPI模块。需要注意的有:

  • 在进行spi的读写操作时,需要对cs引脚进行一些处理,即先置cs脚为低电平,待数据读取/写入完毕后置cs脚为高电平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值