| 
      1
     | 
    
      #include <stdint.h>
 
     | 
  
  
    | 
      2
     | 
    
      #include <unistd.h>
 
     | 
  
  
    | 
      3
     | 
    
      #include <stdio.h>
 
     | 
  
  
    | 
      4
     | 
    
      #include <stdlib.h>
 
     | 
  
  
    | 
      5
     | 
    
      #include <getopt.h>
 
     | 
  
  
    | 
      6
     | 
    
      #include <fcntl.h>
 
     | 
  
  
    | 
      7
     | 
    
      #include <sys/ioctl.h>
 
     | 
  
  
    | 
      8
     | 
    
      #include <linux/types.h>
 
     | 
  
  
    | 
      9
     | 
    
      #include <linux/spi/spidev.h>
 
     | 
  
  
    | 
      10
     | 
    
      #include <iostream>
 
     | 
  
  
    | 
      11
     | 
    
      #include <chrono>
 
     | 
  
  
    | 
      12
     | 
    
      
 
     | 
  
  
    | 
      13
     | 
    
      // ... test framework, etc.
 
     | 
  
  
    | 
      14
     | 
    
      
 
     | 
  
  
    | 
      15
     | 
    
      const size_t SPI_BUFFER_SIZE = 4;
 
     | 
  
  
    | 
      16
     | 
    
      uint16_t spi_tx_buffer[SPI_BUFFER_SIZE];
 
     | 
  
  
    | 
      17
     | 
    
      uint16_t spi_rx_buffer[SPI_BUFFER_SIZE];
 
     | 
  
  
    | 
      18
     | 
    
      
 
     | 
  
  
    | 
      19
     | 
    
      struct spi_ioc_transfer xfer[1];
 
     | 
  
  
    | 
      20
     | 
    
      
 
     | 
  
  
    | 
      21
     | 
    
      int spi_init()
 
     | 
  
  
    | 
      22
     | 
    
      {
     | 
  
  
    | 
      23
     | 
    
          int file;
 
     | 
  
  
    | 
      24
     | 
    
          uint8_t mode, lsb, bits;
 
     | 
  
  
    | 
      25
     | 
    
          uint32_t speed;
 
     | 
  
  
    | 
      26
     | 
    
      
 
     | 
  
  
    | 
      27
     | 
    
          if ((file = open("/dev/spidev32766.0", O_RDWR)) < 0)
     | 
  
  
    | 
      28
     | 
    
          {
     | 
  
  
    | 
      29
     | 
    
              printf("Failed to open the bus.");
     | 
  
  
    | 
      30
     | 
    
              return -1;
 
     | 
  
  
    | 
      31
     | 
    
          }
 
     | 
  
  
    | 
      32
     | 
    
      
 
     | 
  
  
    | 
      33
     | 
    
      // ... mode reading and writing
 
     | 
  
  
    | 
      34
     | 
    
      
 
     | 
  
  
    | 
      35
     | 
    
          printf("file: spi mode %d, %d bits %sper word, %d Hz max\n", mode, bits, lsb ? "(lsb first) " : "", speed);
     | 
  
  
    | 
      36
     | 
    
      
 
     | 
  
  
    | 
      37
     | 
    
          memset(xfer, 0, sizeof(spi_ioc_transfer));
 
     | 
  
  
    | 
      38
     | 
    
      
 
     | 
  
  
    | 
      39
     | 
    
          xfer[0].tx_buf = (unsigned long)spi_tx_buffer;
 
     | 
  
  
    | 
      40
     | 
    
          xfer[0].len = 20;       // Length of command to write
 
     | 
  
  
    | 
      41
     | 
    
          xfer[0].cs_change = 1;  // Do NOT Keep CS activated
 
     | 
  
  
    | 
      42
     | 
    
          xfer[0].delay_usecs = 0;
 
     | 
  
  
    | 
      43
     | 
    
          xfer[0].speed_hz = 100000;
 
     | 
  
  
    | 
      44
     | 
    
          xfer[0].bits_per_word = 16;
 
     | 
  
  
    | 
      45
     | 
    
      
 
     | 
  
  
    | 
      46
     | 
    
          return file;
 
     | 
  
  
    | 
      47
     | 
    
      }
 
     | 
  
  
    | 
      48
     | 
    
      
 
     | 
  
  
    | 
      49
     | 
    
      uint16_t* spi_read(int file)
 
     | 
  
  
    | 
      50
     | 
    
      {
     | 
  
  
    | 
      51
     | 
    
          int status;
 
     | 
  
  
    | 
      52
     | 
    
      
 
     | 
  
  
    | 
      53
     | 
    
          memset(spi_tx_buffer, 0, sizeof spi_tx_buffer);
 
     | 
  
  
    | 
      54
     | 
    
          memset(spi_rx_buffer, 0, sizeof spi_rx_buffer);
 
     | 
  
  
    | 
      55
     | 
    
      
 
     | 
  
  
    | 
      56
     | 
    
          spi_tx_buffer[0] = 0x8330;
 
     | 
  
  
    | 
      57
     | 
    
      
 
     | 
  
  
    | 
      58
     | 
    
          xfer[0].tx_buf = (unsigned long)spi_tx_buffer;
 
     | 
  
  
    | 
      59
     | 
    
          xfer[0].len = 2; // Length of command to write/read
 
     | 
  
  
    | 
      60
     | 
    
          xfer[0].rx_buf = (unsigned long)spi_rx_buffer;
 
     | 
  
  
    | 
      61
     | 
    
      
 
     | 
  
  
    | 
      62
     | 
    
          status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);
 
     | 
  
  
    | 
      63
     | 
    
          if (status < 0)
 
     | 
  
  
    | 
      64
     | 
    
          {
     | 
  
  
    | 
      65
     | 
    
              perror("SPI_IOC_MESSAGE");
     | 
  
  
    | 
      66
     | 
    
          }
 
     | 
  
  
    | 
      67
     | 
    
      
 
     | 
  
  
    | 
      68
     | 
    
          // Allow way more than enough time for transfer to occur
 
     | 
  
  
    | 
      69
     | 
    
          sleep(1);
 
     | 
  
  
    | 
      70
     | 
    
      
 
     | 
  
  
    | 
      71
     | 
    
          printf("rx: ");
     | 
  
  
    | 
      72
     | 
    
          for (uint16_t i = 0; i < SPI_BUFFER_SIZE; i++)
 
     | 
  
  
    | 
      73
     | 
    
          {
     | 
  
  
    | 
      74
     | 
    
              printf("%04X ", spi_rx_buffer[i]);
     | 
  
  
    | 
      75
     | 
    
          }
 
     | 
  
  
    | 
      76
     | 
    
          printf("\n");
     | 
  
  
    | 
      77
     | 
    
      
 
     | 
  
  
    | 
      78
     | 
    
          return spi_rx_buffer;
 
     | 
  
  
    | 
      79
     | 
    
      }
 
     | 
  
  
    | 
      80
     | 
    
      
 
     | 
  
  
    | 
      81
     | 
    
      
 
     | 
  
  
    | 
      82
     | 
    
      void main()
 
     | 
  
  
    | 
      83
     | 
    
      {
     | 
  
  
    | 
      84
     | 
    
          int spidev_fd = spi_init();
 
     | 
  
  
    | 
      85
     | 
    
          if (spidev_fd < 0)
 
     | 
  
  
    | 
      86
     | 
    
          {
     | 
  
  
    | 
      87
     | 
    
              printf("Failed to open!");
     | 
  
  
    | 
      88
     | 
    
              return;
 
     | 
  
  
    | 
      89
     | 
    
          }
 
     | 
  
  
    | 
      90
     | 
    
      
 
     | 
  
  
    | 
      91
     | 
    
          // Timeout after 10 seconds
 
     | 
  
  
    | 
      92
     | 
    
          std::cout << "STARTING SPI" << std::endl;
 
     | 
  
  
    | 
      93
     | 
    
          auto timeout_time = std::chrono::high_resolution_clock::now() + std::chrono::seconds(10);
 
     | 
  
  
    | 
      94
     | 
    
          while (std::chrono::high_resolution_clock::now() < timeout_time)
 
     | 
  
  
    | 
      95
     | 
    
          {
     | 
  
  
    | 
      96
     | 
    
              spi_read(spidev_fd); //reading the address 0xE60E
 
     | 
  
  
    | 
      97
     | 
    
              std::cout << "BUFFER: " << spi_rx_buffer << std::endl;
 
     | 
  
  
    | 
      98
     | 
    
      
 
     | 
  
  
    | 
      99
     | 
    
              usleep(100000);
 
     | 
  
  
    | 
      100
     | 
    
          }
 
     | 
  
  
    | 
      101
     | 
    
      
 
     | 
  
  
    | 
      102
     | 
    
          close(spidev_fd);
 
     | 
  
  
    | 
      103
     | 
    
      }
 
     |