#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>

static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay;
const char *device = "/dev/spidev0.0";
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
char buf[10];
char buf2[10];
int com_serial;
int failcount;
struct spi_ioc_transfer xfer[2];
struct spi_ioc_transfer xfer1[2];

void spi_write(int file)
{
    unsigned char   buf[32];
    int status;
    memset(buf, 0, sizeof buf);
    buf[0] = 0x06;
    xfer1[0].tx_buf = (unsigned long)buf;
    xfer1[0].len = 1; /* Length of  command to write*/
    status = ioctl(file, SPI_IOC_MESSAGE(1), xfer1);
    if (status < 0)
    {
        perror("SPI_IOC_MESSAGE");
        return;
    }
    buf[0] = 0x01;
    buf[1] = 0x30;
    xfer1[0].tx_buf = (unsigned long)buf;
    xfer1[0].len = 2; /* Length of  command to write*/
    status = ioctl(file, SPI_IOC_MESSAGE(1), xfer1);
    if (status < 0)
    {
        perror("SPI_IOC_MESSAGE");
        return;
    }
    printf("env: %02x %02x\n", buf[0], buf[1]);
    //    printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);

    com_serial=1;
    failcount=0;
}


char * spi_read(int file)
{
    int status;
    memset(buf, 0, sizeof buf);
    memset(buf2, 0, sizeof buf2);
    buf[0] = 0x05;//0x9E;
    //    buf[1] = 0x00;
    //    buf[2] = 0x00;
    //    buf[3] = 0x00;
    xfer[0].tx_buf = (unsigned long)buf;
    xfer[0].len = 1; /* Length of  command to write*/
    xfer[1].rx_buf = (unsigned long) buf2;
    xfer[1].len = 1; /* Length of Data to read */
    status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
    if (status < 0)
    {
        perror("SPI_IOC_MESSAGE");
        return 0;
    }
    //printf("env: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
    printf("ret: %02x\n", buf2[0]);
}
int main(int argc, char *argv[])
{
    int file;

    if ((file = open(device,O_RDWR)) < 0)
    {
        printf("Failed to open the bus.");
        /* ERROR HANDLING; you can check errno to see what went wrong */
        exit(1);
    }

    printf("success to open the bus.");

    if (ioctl(file, SPI_IOC_RD_MODE, &mode) < 0)
    {
        perror("SPI rd_mode");
        return 0;
    }
    //    if (ioctl(file, SPI_IOC_RD_LSB_FIRST, &lsb) < 0)
    //    {
    //        perror("SPI rd_lsb_fist");
    //        return;
    //    }

    if (ioctl(file, SPI_IOC_WR_BITS_PER_WORD, &bits)<0)
    {
        perror("can't set bits per word");
        return 0;
    }

    if (ioctl(file, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
    {
        perror("SPI bits_per_word");
        return 0;
    }

    if (ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0)
    {
        perror("can't set max speed hz");
        return 0;
    }

    if (ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
    {
        perror("SPI max_speed_hz");
        return 0;
    }

    printf("spi mode %d, %d bits, %d Hz max\n",mode, bits, speed);
    //    transfer(file);
        spi_write(file);
//    spi_read(file);
    return 0;
}
