/*****RAS-32IO-V2 SPI Sample Program*****/ #include #include #include #include #include #include #include #include #include #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) static uint8_t WRSPIMode = 0; static uint8_t RDSPIMode = 0; static uint8_t SPIbit = 8; static uint32_t SPISpeed = 1000000;//1Mhz static char *SPIDevice = "/dev/spidev0.1"; int main(int argc,char *argv[]){ int fd,ret=0; int k,j; uint8_t tx[2]; uint8_t rx[2]; unsigned char buf[1]; fd = open(SPIDevice,O_RDWR); if(fd<0) printf("SPI couldn't open\n"); ret = ioctl(fd,SPI_IOC_WR_MODE,&WRSPIMode); if(ret <0) printf("SPI write Mode error\n"); ret = ioctl(fd,SPI_IOC_RD_MODE,&RDSPIMode); if(ret <0) printf("SPI read Mode error\n"); ret =ioctl(fd,SPI_IOC_WR_BITS_PER_WORD,&SPIbit); if(ret <0) printf("SPI write bit/Word error\n"); ret =ioctl(fd,SPI_IOC_RD_BITS_PER_WORD,&SPIbit); if(ret <0) printf("SPI read bit/Word error \n"); ret =ioctl(fd,SPI_IOC_WR_MAX_SPEED_HZ,&SPISpeed); if(ret <0) printf("SPI write Speed init error\n"); ret =ioctl(fd,SPI_IOC_RD_MAX_SPEED_HZ,&SPISpeed); if(ret <0) printf("SPI read Speed init error\n"); struct spi_ioc_transfer tr={ tr.tx_buf =(unsigned long)tx, tr.rx_buf =(unsigned long)rx, tr.len =ARRAY_SIZE(tx), tr.delay_usecs =0, tr.speed_hz =SPISpeed, tr.bits_per_word=SPIbit, }; ///WRITE/// tx[0]=0x00; //address bit tx[1]=0x55; //data bit ret=ioctl(fd,SPI_IOC_MESSAGE(1),&tr); if(ret <1) printf("SPI transfer error\n"); tx[0]=0x01; //address bit tx[1]=0xaa; //data bit ret=ioctl(fd,SPI_IOC_MESSAGE(1),&tr); if(ret <1) printf("SPI transfer error\n"); ///READ/// tx[0]=0x02; //address bit ret=ioctl(fd,SPI_IOC_MESSAGE(1),&tr); if(ret <1) printf("SPI transfer error\n"); printf("DATA %x \n",rx[1]); tx[0]=0x03; //address bit ret=ioctl(fd,SPI_IOC_MESSAGE(1),&tr); if(ret <1) printf("SPI transfer error\n"); printf("DATA %x \n",rx[1]); return 0; }