Advertisement
Guest User

libsoc SPI Test

a guest
Feb 11th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <sys/time.h>
  8.  
  9. #include "libsoc_spi.h"
  10. #include "libsoc_debug.h"
  11.  
  12. #define SPI_DEVICE   0
  13. #define CHIP_SELECT  0
  14.  
  15. #define LEN 8
  16. static uint8_t tx[LEN], rx[LEN];
  17.  
  18. // For testing SPI with loopback: MISO connected to MOSI
  19.  
  20. int main()
  21. {
  22.   int i;
  23.   libsoc_set_debug(1);
  24.  
  25.   spi* spi_dev = libsoc_spi_init(SPI_DEVICE, CHIP_SELECT);
  26.  
  27.   if (!spi_dev)
  28.   {
  29.     printf("Failed to get spidev device!\n");
  30.     return EXIT_FAILURE;
  31.   }
  32.  
  33.   // SPI Settings
  34.   libsoc_spi_set_mode(spi_dev, MODE_0);
  35.   libsoc_spi_get_mode(spi_dev);
  36.  
  37.   libsoc_spi_set_speed(spi_dev, 1000000);
  38.   libsoc_spi_get_speed(spi_dev);
  39.  
  40.   libsoc_spi_set_bits_per_word(spi_dev, BITS_8);
  41.   libsoc_spi_get_bits_per_word(spi_dev);
  42.  
  43.   // set TX buffer to 0,1,2,3,...
  44.   for (i = 0; i < LEN; i++)
  45.   {
  46.       tx[i] = i;
  47.   }
  48.  
  49.   // clear RX buffer
  50.   memset(rx, 0, LEN);
  51.  
  52.  // Do a transfer
  53.   if (libsoc_spi_rw(spi_dev, tx, rx, LEN) != EXIT_SUCCESS)
  54.   {
  55.       printf("SPI RW Failed.\n");
  56.   }
  57.  
  58.   // Print received bytes
  59.   for (i = 0; i < LEN; i++)
  60.   {
  61.       printf("[0x%02X]", rx[i]);
  62.   }
  63.   printf("\n");
  64.  
  65.   libsoc_spi_free(spi_dev);
  66.  
  67.   return EXIT_SUCCESS;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement