Linux spi master dev驱动及测试例程

spidev

设备:

			spidev@0{
				compatible = "xxx,spidev";
				reg = <0>;
				spi-max-frequency = <1000000>;
				spi-cpha;
				spi-cpol;
			};

驱动:

kernel/linux-4.14/drivers/spi/spidev.c(由平台原创)

测试spidev程序

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
unsigned char  default_tx[] = {
	0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
	0xff, 0xfe,
};
unsigned char default_rx[ARRAY_SIZE(default_tx)] = {0, };
int main(int argc, char **argv)
{
	int		fd;
	char		dev_node[16];
	int		mode = 0, bits = 8, speed = 500 * 1000;
	int		ret = 0;
	unsigned char	tx_buf[1000], rx_buf[1000];
	struct spi_ioc_transfer tr;
	int		i, t1, t2;

	sprintf(dev_node, "/dev/spidev%d.0", atoi(argv[1]));
	fd = open(dev_node, O_RDWR);
	if (fd < 0) {
		perror("open");
		return -1;
	}

	/* Mode */
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret < 0) {
		perror("SPI_IOC_WR_MODE");
		return -1;
	}
	ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
	if (ret < 0) {
		perror("SPI_IOC_RD_MODE");
		return -1;
	}

	/* bpw */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret < 0) {
		perror("SPI_IOC_WR_BITS_PER_WORD");
		return -1;
	}
	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
	if (ret < 0) {
		perror("SPI_IOC_RD_BITS_PER_WORD");
		return -1;
	}

	/* speed */
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret < 0) {
		perror("SPI_IOC_WR_MAX_SPEED_HZ");
		return -1;
	}
	ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
	if (ret < 0) {
		perror("SPI_IOC_RD_MAX_SPEED_HZ");
		return -1;
	}

	printf("Mode = %d\n", mode);
	printf("Bpw = %d\n", bits);
	printf("Baudrate = %d\n", speed);

	tr.tx_buf = (unsigned long)default_tx;
	tr.rx_buf = (unsigned long)default_rx;
	tr.len = sizeof(default_tx);
	tr.delay_usecs = 0;
	tr.speed_hz = speed;
	tr.bits_per_word = bits;

while(1){
	ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	sleep(1);
};

	return 0;
}

 

你可能感兴趣的:(安霸amba,spi)