linux spi test application
2015-05-13 16:25
232 查看
Here is a very simple demo application that shows how to read the Flash ID from an SPI Flash device:
/*
* Sample application that makes use of the SPIDEV interface
* to access an SPI slave device. Specifically, this sample
* reads a Device ID of a JEDEC-compliant SPI Flash device.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
/*
* This assumes that a 'mdev -s' has been run to create
* /dev/spidev* devices after the kernel bootstrap.
* First number is the "bus" (SPI contoller id), second number
* is the "chip select" of the specific SPI slave
* ...
* char *name = "/dev/spidev1.1";
*/
char *name;
int fd;
struct spi_ioc_transfer xfer[2];
unsigned char buf[32], *bp;
int len, status;
name = argv[1];
fd = open(name, O_RDWR);
if (fd < 0) {
perror("open");
return 1;
}
memset(xfer, 0, sizeof xfer);
memset(buf, 0, sizeof buf);
len = sizeof buf;
/*
* Send a GetID command
*/
buf[0] = 0x9f;
len = 6;
xfer[0].tx_buf = (unsigned long)buf;
xfer[0].len = 1;
xfer[1].rx_buf = (unsigned long) buf;
xfer[1].len = 6;
status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
if (status < 0) {
perror("SPI_IOC_MESSAGE");
return;
}
printf("response(%d): ", status);
for (bp = buf; len; len--)
printf("%02x ", *bp++);
printf("\n");
}
Here is how the application runs on the STM32F4 target:
~ # /spidev_flash /dev/spidev3.0
response(7): 20 20 16 10 00 00
~ #
SPI输出数据的同时必将读回数据来,因为要读回6个数据来,所以这里使用了缓冲区1的长度为6.
/*
* Sample application that makes use of the SPIDEV interface
* to access an SPI slave device. Specifically, this sample
* reads a Device ID of a JEDEC-compliant SPI Flash device.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
/*
* This assumes that a 'mdev -s' has been run to create
* /dev/spidev* devices after the kernel bootstrap.
* First number is the "bus" (SPI contoller id), second number
* is the "chip select" of the specific SPI slave
* ...
* char *name = "/dev/spidev1.1";
*/
char *name;
int fd;
struct spi_ioc_transfer xfer[2];
unsigned char buf[32], *bp;
int len, status;
name = argv[1];
fd = open(name, O_RDWR);
if (fd < 0) {
perror("open");
return 1;
}
memset(xfer, 0, sizeof xfer);
memset(buf, 0, sizeof buf);
len = sizeof buf;
/*
* Send a GetID command
*/
buf[0] = 0x9f;
len = 6;
xfer[0].tx_buf = (unsigned long)buf;
xfer[0].len = 1;
xfer[1].rx_buf = (unsigned long) buf;
xfer[1].len = 6;
status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
if (status < 0) {
perror("SPI_IOC_MESSAGE");
return;
}
printf("response(%d): ", status);
for (bp = buf; len; len--)
printf("%02x ", *bp++);
printf("\n");
}
Here is how the application runs on the STM32F4 target:
~ # /spidev_flash /dev/spidev3.0
response(7): 20 20 16 10 00 00
~ #
SPI输出数据的同时必将读回数据来,因为要读回6个数据来,所以这里使用了缓冲区1的长度为6.
相关文章推荐
- linux spi驱动开发学习-----spidev.c和spi test app
- linux spi驱动开发学习-----spidev.c和spi test app
- linux spi驱动开发学习-----spidev.c和spi test app
- linux spi驱动开发学习(二)-----spidev.c和spi test app
- linux系统总线SPI总线四之SPI设备驱动程序
- linux的test命令(转)
- linux spi驱动开发学习(三)-----spi_bitbang.c详解
- Linux下SPI从设备驱动的编写
- run application on linux port 80 as non-root user.
- Linux下spi驱动开发(2)
- Linux SPI总线和设备驱动架构之二:SPI通用接口层
- Error:Application and test application id cannot be the same: both are 'com.xxxxxxx' for debugAndroi
- Linux下SPI读写外部寄存器的操作
- Linux test命令
- linux spi子系统驱动分析
- WEB压力测试工具 ACT工具(Microsoft Application Center Test)
- linux test命令
- linux epoll Model Test
- linux3.2下adt7320的spi驱动编写
- Linux下spi驱动开发