本文共 1631 字,大约阅读时间需要 5 分钟。
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.
转载地址:http://muzji.baihongyu.com/