Rockchip SPI (serial peripheral interface) has the following features
The driver has been adapted to the standard SPI driver framework under Linux.
The 30-pin connector on the board leads to an SPI that is multiplexed. To use this SPI, you need to modify the kernel to multiplex these pins into SPI.
The pins are as follows
SPI as userspace use usually load spidev driver, the generated node is spidevx.y (x is the controller serial number, y is the chip select number). The node of this SPI group is /dev/spidev0.0. The following is a transceiver test by shorting the MISO and MOSI of SPI0 in hardware, wired as follows
The nodes in "/dev/spidev0.0" require root privileges to operate, and the operation of the program compiled from the command line or C language requires root privileges. If you are using ssh or LX terminal, execute the following command to get root privileges.
sudo su
By linking to the libperipheral_api.a static library, the following interfaces can be called in C to operate SPI
/**
* @name: user_spi_open
* @description: open spidev device
* @param i2c_num: spi number
* @param spi_cs: spi slice 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/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: pointer to spi write data
* @param read_buf: spi read data pointer
* @param addr_len: register address length
* @param len: read/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 synchronization signal, clocked by the master device, which must send data (which can be all 0x00) in order to send out the clock, and the slave device receives the clock from the master before it can send data.
The test demo is as follows, for example, operate spidev0.0, 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 peripheral_api.a static library, peripheral_api.h and test demo source code test.c into the same path, compile the command as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o spitest
The result is as follows