
#include "spi_api.h"
#include <linux/spi/spidev.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
int spi_api_read(void)
{

 	unsigned char  buf[32], *bp;
 	int   	status;
	int	mode=0;
	int i;
	int fd;
	int ret;
	int bits =8;
	int delay;
	int speed = 10*1000;
	char tx[12],rx[12];
	struct spi_ioc_transfer xfer = {
        .tx_buf =tx,
        .rx_buf =rx,
        .len =sizeof(tx),
        .delay_usecs = delay,
        .speed_hz = speed,
        .bits_per_word = bits
    	};
	tx[0]=0x02;
	tx[1]=0x01;
	fd=open("/dev/spi2",O_RDWR);
	if(fd<0)
		printf("spi2 open fail\n");
	mode=mode|SPI_MODE_0;
 	ret=ioctl(fd,SPI_IOC_WR_MODE,&mode);
	if(ret<0)
		printf("SPI_IOC_WR_MODE fail \n");
	ret=ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
        if(ret<0)
                printf("SPI_IOC_WR_BITS_PER_WORD fail \n");	
	ret=ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
        if(ret<0)
                printf("SPI_IOC_WR_MAX_SPEED_HZ fail \n");	
//	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
//	if(ret<0)
//		printf("SPI_IOC_MESSAGE fail\n");
	ret=write(fd,tx,2);
	if(ret<0)
		printf("write fail\n");
	ret=read(fd,rx,1);
	if(ret<0)
		printf("read fail\n");
	printf("read data is %x\n",rx[0]);	
 	printf("\n");


}
