Rockchip SPI (serial peripheral interface) has the following features
Its driver has been adapted to the standard SPI driver framework under Linux.
The 30-pin socket on the board leads to one SPI, and there is a multiplexing relationship. To use this SPI, you need to modify the kernel and multiplex these pins into SPI. Specific operation reference
The pins are as follows
When SPI is used as userspace, the spidev driver is usually loaded, and the generated node is spidevx.y (x is the controller serial number, y is the chip select number). The node of this group of SPI is /dev/spidev0.0. Next, we will short-circuit the MISO and MOSI of SPI0 in hardware to perform a transceiver test. The wiring is as follows
The node "/dev/spidev0.0" requires root privileges to operate. The command line or the program compiled in C language requires root privileges. If you use ssh or LX terminal, execute the following command to obtain root privileges first.
sudo su
By connecting to the libperipheral_api.a static library, you can use C language to call the following interface to operate SPI
/**
* @name: user_spi_open
* @description: Open spidev device
* @param i2c_num: spi number
* @param spi_cs: spi chip select number
* @return greater than or equal to 0 - success, return value is file descriptor less than 0 - failure
*/
int user_spi_open(int spi_num,int spi_cs);
/**
* @name: user_spi_write_read
* @description: read and write data from spi device spi communication is full-duplex read-only can configure write_buf content to 0 write-only can discard read_buf content
* @param fd: File descriptor, user_spi_open return value
* @param write_buf: spi write data pointer
* @param read_buf: spi read data pointer
* @param addr_len: register address length
* @param len: read and write data length
*
* @return equal to 0 - success less than 0 - failure
*/
int user_spi_write_read(int fd, unsigned char* write_buf,unsigned char* read_buf,unsigned int len);
/**
* @name: user_spi_close
* @description: close spi
* @param fd: file descriptor, user_spi_close return value
* @return always equal to 0, meaningless
*/
int user_spi_close(int fd);
SPI is a synchronous signal, the master device provides the clock, and the master device must send data (can be all 0x00) to send the clock. The slave device can send data only after receiving the clock from the master device.
The test demo is as follows. Take spidev0.0 as an example and short MISO MOSI
#include "peripheral_api.h"
void spi_api_test(void)
{
int ret = 0;
int fd = -1;
unsigned char buf_write[5] = {0};
unsigned char buf_read[5] = {0};
for (unsigned char i = 0; i < sizeof(buf_write); i++) {
buf_write[i] = 0x32 + i;
}
fd = user_spi_open(0, 0);
if (fd < 0) {
printf("user_spi_open fail \n");
return;
}
ret = user_spi_write_read(fd, buf_write, buf_read, sizeof(buf_read));
if (ret < 0) {
printf("user_spi_write_read fail \n");
user_spi_close(fd);
return;
}
user_spi_close(fd);
if (memcmp(buf_write, buf_read, sizeof(buf_write)) == 0) {
printf("buf_write buf_read same\n");
} else {
printf("buf_write buf_read diff %d %d %d %d %d \n", buf_read[0], buf_read[1], buf_read[2], buf_read[3], buf_read[4]);
}
return;
}
int main()
{
spi_api_test();
return 0;
}
Put the peripheral_api.a static library, peripheral_api.h and the test demo source code test.c in the same path, and the compilation command is as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o spitest
The running results are as follows