Advertisement
Guest User

Untitled

a guest
Sep 17th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.56 KB | None | 0 0
  1. /*
  2.  * SPI testing utility (using spidev driver)
  3.  *
  4.  * Copyright (c) 2007  MontaVista Software, Inc.
  5.  * Copyright (c) 2007  Anton Vorontsov <avorontsov@ru.mvista.com>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License.
  10.  *
  11.  * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
  12.  */
  13.  
  14. #include <stdint.h>
  15. #include <unistd.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <getopt.h>
  19. #include <fcntl.h>
  20. #include <sys/ioctl.h>
  21. #include <linux/types.h>
  22. #include <linux/spi/spidev.h>
  23.  
  24. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  25.  
  26. static void pabort(const char *s)
  27. {
  28.     perror(s);
  29.     abort();
  30. }
  31.  
  32. static const char *device = "/dev/spidev0.0";
  33. static uint8_t mode;
  34. static uint8_t bits = 8;
  35. static uint32_t speed = 1000000;
  36. static uint16_t delay;
  37.  
  38. static void transfer(int fd)
  39. {
  40.     int ret;
  41.     uint8_t tx[] = {
  42.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  43.         0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
  44.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  45.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  46.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  47.         0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
  48.         0xF0, 0x0D,
  49.     };
  50.     uint8_t rx[ARRAY_SIZE(tx)] = {0, };
  51.     struct spi_ioc_transfer tr = {
  52.         .tx_buf = (unsigned long)tx,
  53.         .rx_buf = (unsigned long)rx,
  54.         .len = ARRAY_SIZE(tx),
  55.         .delay_usecs = delay,
  56.         .speed_hz = speed,
  57.         .bits_per_word = bits,
  58.     };
  59.  
  60.     ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  61.     if (ret < 1)
  62.         pabort("can't send spi message");
  63.  
  64. #if 0
  65.     for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
  66.         if (!(ret % 6))
  67.             puts("");
  68.         printf("%.2X ", rx[ret]);
  69.     }
  70.     puts("");
  71. #endif
  72. }
  73.  
  74. static void print_usage(const char *prog)
  75. {
  76.     printf("Usage: %s [-DsbdlHOLC3]\n", prog);
  77.     puts("  -D --device   device to use (default /dev/spidev1.1)\n"
  78.          "  -s --speed    max speed (Hz)\n"
  79.          "  -d --delay    delay (usec)\n"
  80.          "  -b --bpw      bits per word \n"
  81.          "  -l --loop     loopback\n"
  82.          "  -H --cpha     clock phase\n"
  83.          "  -O --cpol     clock polarity\n"
  84.          "  -L --lsb      least significant bit first\n"
  85.          "  -C --cs-high  chip select active high\n"
  86.          "  -3 --3wire    SI/SO signals shared\n");
  87.     exit(1);
  88. }
  89.  
  90. static void parse_opts(int argc, char *argv[])
  91. {
  92.     while (1) {
  93.         static const struct option lopts[] = {
  94.             { "device",  1, 0, 'D' },
  95.             { "speed",   1, 0, 's' },
  96.             { "delay",   1, 0, 'd' },
  97.             { "bpw",     1, 0, 'b' },
  98.             { "loop",    0, 0, 'l' },
  99.             { "cpha",    0, 0, 'H' },
  100.             { "cpol",    0, 0, 'O' },
  101.             { "lsb",     0, 0, 'L' },
  102.             { "cs-high", 0, 0, 'C' },
  103.             { "3wire",   0, 0, '3' },
  104.             { "no-cs",   0, 0, 'N' },
  105.             { "ready",   0, 0, 'R' },
  106.             { NULL, 0, 0, 0 },
  107.         };
  108.         int c;
  109.  
  110.         c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
  111.  
  112.         if (c == -1)
  113.             break;
  114.  
  115.         switch (c) {
  116.         case 'D':
  117.             device = optarg;
  118.             break;
  119.         case 's':
  120.             speed = atoi(optarg);
  121.             break;
  122.         case 'd':
  123.             delay = atoi(optarg);
  124.             break;
  125.         case 'b':
  126.             bits = atoi(optarg);
  127.             break;
  128.         case 'l':
  129.             mode |= SPI_LOOP;
  130.             break;
  131.         case 'H':
  132.             mode |= SPI_CPHA;
  133.             break;
  134.         case 'O':
  135.             mode |= SPI_CPOL;
  136.             break;
  137.         case 'L':
  138.             mode |= SPI_LSB_FIRST;
  139.             break;
  140.         case 'C':
  141.             mode |= SPI_CS_HIGH;
  142.             break;
  143.         case '3':
  144.             mode |= SPI_3WIRE;
  145.             break;
  146.         case 'N':
  147.             mode |= SPI_NO_CS;
  148.             break;
  149.         case 'R':
  150.             mode |= SPI_READY;
  151.             break;
  152.         default:
  153.             print_usage(argv[0]);
  154.             break;
  155.         }
  156.     }
  157. }
  158.  
  159. int main(int argc, char *argv[])
  160. {
  161.     int ret = 0;
  162.     int fd, i;
  163.  
  164.     parse_opts(argc, argv);
  165.  
  166.     fd = open(device, O_RDWR);
  167.     if (fd < 0)
  168.         pabort("can't open device");
  169.  
  170.     /*
  171.      * spi mode
  172.      */
  173.     ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  174.     if (ret == -1)
  175.         pabort("can't set spi mode");
  176.  
  177.     ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  178.     if (ret == -1)
  179.         pabort("can't get spi mode");
  180.  
  181.     /*
  182.      * bits per word
  183.      */
  184.     ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  185.     if (ret == -1)
  186.         pabort("can't set bits per word");
  187.  
  188.     ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  189.     if (ret == -1)
  190.         pabort("can't get bits per word");
  191.  
  192.     /*
  193.      * max speed hz
  194.      */
  195.     ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  196.     if (ret == -1)
  197.         pabort("can't set max speed hz");
  198.  
  199.     ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  200.     if (ret == -1)
  201.         pabort("can't get max speed hz");
  202.  
  203.     printf("spi mode: %d\n", mode);
  204.     printf("bits per word: %d\n", bits);
  205.     printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
  206.  
  207.     i = 1000;
  208.     while (i--)
  209.         transfer(fd);
  210.  
  211.     close(fd);
  212.  
  213.     return ret;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement