at91sam9261的板子,在调试SPI,但是老是不对,感觉根本没读书据,内核中我已经添加了spi1,添加的方法如下: 在 linux-2.6.24/arch/arm/mach-at91下面的board-sam9261ek.c 中的ek_spi_devices[]中添加如下代码 { /* add a spi1 device */ .modalias ="spidev", .bus_num =1, .chip_select =0, .max_speed_hz =15 * 1000 * 1000, }, { /* add a spi1 device */ .modalias ="spidev", .bus_num =1, .chip_select =1, .max_speed_hz =15 * 1000 * 1000, }, 在cat /dev/spi1的时候,时钟上有反应 上面添加代码有没有对阿,请指教(内核里面已经添加spi支持) 但是在我测试的时候不对,首先用了内核里面的spidev_test.c输出结果全部是0,然后我根据网上的资料写了一个测试程序,但是还是输出不对,都是0,我的输出显示如下: open spi port=1 spi mode: 1 bits per word: 8 max speed: 500000 Hz (500 KHz) from__main_buf: AA BB CC DD send_buf_test AA BB CC DD addr_frommain AA BB CC DD tx_write2read AA BB CC DD read_[0].tx_buf: AA BB CC DD read_xfer[1].rx_buf: 00 00 00 00 buf_return2main: 00 00 00 00 MAIN_data; 00 00 00 00
源代码如下: #include <stdint.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <getopt.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/types.h> #include <linux/spi/spidev.h>
#define UCHAR unsigned char #define MAXNUM_SPI 3 static int fd_spi[MAXNUM_SPI] = {-1, -1}; static uint8_t mode=SPI_MODE_1; static uint8_t bits = 8; static uint32_t speed = 500000;
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #define SIZE_APPSPI_RCVBUF 1024
struct spi_ioc_transfer tfer[1]; struct spi_ioc_transfer xfer[2];
struct appspi_rcvst { char buf[SIZE_APPSPI_RCVBUF]; int len; int head; };
static struct appspi_rcvst appspi_rcvbuf[MAXNUM_SPI];
int appspi_open(unsigned int port) { int fd; char dev[32]; int ret; if(-1 != fd_spi[port]) close(fd_spi[port]);
if(port==0) sprintf(dev, "/dev/spi0"); else if(port==1) sprintf(dev, "/dev/spi1"); fd = open(dev, O_RDWR); if(fd<0) { printf("can't open spi device"); return 1; } printf("open spi port=%d\n",port); ret = ioctl(fd, SPI_IOC_WR_MODE,&mode); if (ret == -1) printf("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE,&mode); if (ret == -1) printf("can't get spi mode");
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) printf("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) printf("can't get bits per word");
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) printf("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); if (ret == -1) printf("can't get max speed hz");
printf("spi mode: %d\n", mode); printf("bits per word: %d\n", bits); printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); fd_spi[port]=fd; appspi_rcvbuf[port].len=0; return 0; }
void appspi_close(unsigned int port) { if((port >= MAXNUM_SPI) || (-1 == fd_spi[port])) return;
close(fd_spi[port]); //这样关闭是什么意思。关闭设备不应该是close(fd);其中fd是打开时的返回值;??
fd_spi[port] = -1; }
int appspi_send(unsigned int port, UCHAR *buf, unsigned int len) { int i; int ret; int fd;
//add by Gan // 20120227 UCHAR *buftest; //add end if(port >= MAXNUM_SPI) return -1; fd = fd_spi[port]; if(fd < 0) { appspi_open(port); fd = fd_spi[port]; if(fd<0) return -1; }
// struct spi_ioc_transfer tfer[1];
memset(tfer, 0, sizeof(tfer));
// tfer[0].tx_buf = (__u64)(int) buf; tfer[0].len = len; //add by Gan //20120227 tfer[0].tx_buf = (__u64)buf; buftest=tfer[0].tx_buf;
//add end
printf("from__main_buf:\n"); for(i=0;i<4;i++) printf("%.2X\t",*buf++); printf("\n");
ret = ioctl(fd, SPI_IOC_MESSAGE(1), tfer); if (ret < 0) { printf("Error: SPI_IOC_MESSAGEn"); return -1; } printf("send_buf_test\n"); for(i=0;i<4;i++) printf("%.2X\t",*buftest++); printf("\n");
return ret; } //int appspi_recv(unsigned int port,UCHAR addr, UCHAR *buf, unsigned int len) //reverse by Gan //20120027 int appspi_recv(unsigned int port,UCHAR *addr, UCHAR *buf, unsigned int len) //reverse end { int i; int ret; int fd; UCHAR tx[4]; UCHAR *bp; UCHAR *addr_save=addr; UCHAR *tx_test; if(port>= MAXNUM_SPI) return -1; fd = fd_spi[port]; if(fd < 0) { appspi_open(port); fd = fd_spi[port]; if(fd<0) return -1; } // struct spi_ioc_transfer xfer[2]; memset(xfer, 0, sizeof(xfer)); // tx[0]=addr; // xfer[0].tx_buf =(__u64)(int)tx; //reverse by Gan //20120027 for(i=0;i<4;i++) tx=*addr++; addr=addr_save;
printf("addr_frommain\n"); for(i=0;i<4;i++) printf("%.2X\t",*addr++); printf("\n");
printf("tx_write2read\n"); for(i=0;i<4;i++) printf("%.2X\t",tx); printf("\n"); xfer[0].cs_change=1; xfer[0].tx_buf =(__u64)tx; xfer[1].rx_buf =(__u64)buf; tx_test=xfer[0].tx_buf; //reverse end xfer[0].len = 1;
// xfer[1].rx_buf =(__u64)(int) buf; xfer[1].len =len; //add by Gan //20120227
ret = ioctl(fd, SPI_IOC_MESSAGE(2), xfer); printf("read_[0].tx_buf:\n"); for(i=0;i<4;i++) printf("%.2X\t",*tx_test++); printf("\n");
bp=xfer[1].rx_buf; printf("read_xfer[1].rx_buf:\n"); for(i=0;i<4;i++) printf("%.2X\t",*bp++); printf("\n");
printf("buf_return2main:\n"); for(i=0;i<4;i++) printf("%.2X\t",*buf++); printf("\n"); if (ret < 0) { printf("Error: SPI_IOC_MESSAGEn"); return -1; } return ret; }
void main() { int i; UCHAR buf[4]={0xaa,0xbb,0xcc,0xdd}; UCHAR rec[4]={}; unsigned int len= ARRAY_SIZE(buf); appspi_open(1);
appspi_send(1,buf,len);
appspi_recv(1,buf,rec,len);
printf("MAIN_data;\n"); for(i=0;i<4;i++) printf("%.2X\t",rec); printf("\n"); appspi_close(1); }
|