Station B haas development tutorial Teaching video link of station B
1. Hardware diagram
2. Read the register value of spi device
Case description:
- This case is a reading and writing test of spi equipment, which uses different functions to complete two reading and writing methods.
- When reading and writing spi, you need to operate the cs pin, first set it to the low level, and then set it to the high level to complete the reading and writing operation.
1. Method 1
- Using SPI write(writeBuf, len) ,spi. The read() function writes and reads separately.
main.py
# coding=utf-8 # This is a sample Python script. #0x9f is the address of a register #bytearray(b'\xef@\x16') is the read value. If necessary, you can look at ASCII, that is 0xef,0x40,0x16, a total of three bytes 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" }
Log output
-------------------spi test-------------------- bytearray(b'\x9f') bytearray(b'\x00\x00\x00') 1 3 bytearray(b'\x9f') bytearray(b'\xef@\x16') -------------------spi test--------------------
2. Method 2
Using SPI Writeread (writebuf, readbuf) is used for reading and writing, but intermediate values need to be used for storage during reading.
# 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" }
Log output
-------------------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 function library
open | write | read | writeRead | close |
---|---|---|---|---|
Open spi | spi write operation to write data to the specified register | spi read operation, which reads data from the specified register | spi write and read operations, write data to and get data from registers | Close spi |
- SPI
- instantiation
- spi=SPI()
- spi.open(params)
- Function: open spi
- Parameter: params is a string, which needs to be specified in board Defined in JSON
- Return: 0 successful
- spi.write(writeBuf, len)
- Function: spi write operation, write data to the specified register
- Parameter: spi write operation needs to pass in a writeBuf byte array, which contains the address of the register and the data to be written; You also need to pass in the length len of the writeBuf array
- Return: length of written data, i.e. len
- spi.read()
- Function: spi read operation to read data from the specified register
- Parameter: the spi read operation needs to pass in a readbuf byte array, which contains the address of the specified register, that is, which register to get the data from. After the read operation is completed, the data in the obtained register is stored in readbuf; You also need to pass in the length len of the readbuf array
- Return: length of read data, i.e. len
- spi.writeRead(writeBuf, readBuf)
- Function: spi write and read operations, write data to and get data from registers
- Parameter: writeBuf byte array, which contains the address of the register; readBuf byte array, which contains the address of the specified register, that is, which register to get data from
- Return: length of read data
- spi.close()
- Function: close spi
- Return: 0 successful
4. Summary
This section describes how to use the SPI module in the driver library of haas506. It should be noted that:
- During the reading and writing operation of spi, it is necessary to process the cs pin, that is, set the cs pin to low level first, and set the cs pin to high level after data reading / writing.