Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1246457
  • 博文数量: 105
  • 博客积分: 127
  • 博客等级: 入伍新兵
  • 技术积分: 962
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-29 15:22
文章分类

全部博文(105)

文章存档

2021年(1)

2019年(3)

2018年(1)

2017年(11)

2016年(47)

2015年(32)

2014年(4)

2012年(6)

我的朋友

分类: LINUX

2015-06-23 10:11:02


点击(此处)折叠或打开

  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. #include <stdint.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <getopt.h>
  18. #include <fcntl.h>
  19. #include <sys/ioctl.h>
  20. #include <linux/types.h>
  21. #include <linux/spi/spidev.h>

  22. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

  23. static void pabort(const char *s)
  24. {
  25.     perror(s);
  26.     abort();
  27. }

  28. static const char *device = "/dev/spidev0.0";
  29. static uint8_t mode;
  30. static uint8_t bits = 8;
  31. static uint32_t speed = 500000;
  32. static uint16_t delay;

  33. static void transfer(int fd)
  34. {
  35.     int ret;
  36.     //发送数据区域
  37.     uint8_t tx[] = {
  38.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  39.         0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
  40.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  41.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  42.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  43.         0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
  44.         0xF0, 0x0D,
  45.     };

  46.     //接收数据区域
  47.     uint8_t rx[ARRAY_SIZE(tx)] = {0, };
  48.     struct spi_ioc_transfer tr = {
  49.         .tx_buf = (unsigned long)tx,
  50.         .rx_buf = (unsigned long)rx,
  51.         .len = ARRAY_SIZE(tx),
  52.         .delay_usecs = delay,
  53.         .speed_hz = speed,
  54.         .bits_per_word = bits,
  55.     };

  56.     ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  57.     if (ret < 1)
  58.         pabort("can't send spi message");

  59.     for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
  60.         if (!(ret % 6))
  61.             puts("");
  62.         printf("%.2X ", rx[ret]);
  63.     }
  64.     puts("");
  65. }

  66. static void print_usage(const char *prog)
  67. {
  68.     printf("Usage: %s [-DsbdlHOLC3]\n", prog);
  69.     puts(" -D --device device to use (default /dev/spidev1.1)\n"
  70.      " -s --speed max speed (Hz)\n"
  71.      " -d --delay delay (usec)\n"
  72.      " -b --bpw bits per word \n"
  73.      " -l --loop loopback\n"
  74.      " -H --cpha clock phase\n"
  75.      " -O --cpol clock polarity\n"
  76.      " -L --lsb least significant bit first\n"
  77.      " -C --cs-high chip select active high\n"
  78.      " -3 --3wire SI/SO signals shared\n");
  79.     exit(1);
  80. }

  81. static void parse_opts(int argc, char *argv[])
  82. {
  83.     while (1) {
  84.         static const struct option lopts[] = {
  85.             { "device", 1, 0, 'D' },
  86.             { "speed", 1, 0, 's' },
  87.             { "delay", 1, 0, 'd' },
  88.             { "bpw", 1, 0, 'b' },
  89.             { "loop", 0, 0, 'l' },
  90.             { "cpha", 0, 0, 'H' },
  91.             { "cpol", 0, 0, 'O' },
  92.             { "lsb", 0, 0, 'L' },
  93.             { "cs-high", 0, 0, 'C' },
  94.             { "3wire", 0, 0, '3' },
  95.             { "no-cs", 0, 0, 'N' },
  96.             { "ready", 0, 0, 'R' },
  97.             { NULL, 0, 0, 0 },
  98.         };
  99.         int c;

  100.         c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);

  101.         if (c == -1)
  102.             break;

  103.         switch (c) {
  104.         case 'D':
  105.             device = optarg;
  106.             break;
  107.         case 's':
  108.             speed = atoi(optarg);
  109.             break;
  110.         case 'd':
  111.             delay = atoi(optarg);
  112.             break;
  113.         case 'b':
  114.             bits = atoi(optarg);
  115.             break;
  116.         case 'l':
  117.             mode |= SPI_LOOP;
  118.             break;
  119.         case 'H':
  120.             mode |= SPI_CPHA;
  121.             break;
  122.         case 'O':
  123.             mode |= SPI_CPOL;
  124.             break;
  125.         case 'L':
  126.             mode |= SPI_LSB_FIRST;
  127.             break;
  128.         case 'C':
  129.             mode |= SPI_CS_HIGH;
  130.             break;
  131.         case '3':
  132.             mode |= SPI_3WIRE;
  133.             break;
  134.         case 'N':
  135.             mode |= SPI_NO_CS;
  136.             break;
  137.         case 'R':
  138.             mode |= SPI_READY;
  139.             break;
  140.         default:
  141.             print_usage(argv[0]);
  142.             break;
  143.         }
  144.     }
  145. }

  146. int main(int argc, char *argv[])
  147. {
  148.     int ret = 0;
  149.     int fd;

  150.     parse_opts(argc, argv);

  151.     fd = open(device, O_RDWR);
  152.     if (fd < 0)
  153.         pabort("can't open device");

  154.     /*
  155.      * spi mode
  156.      */
  157.     ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  158.     if (ret == -1)
  159.         pabort("can't set spi mode");

  160.     ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  161.     if (ret == -1)
  162.         pabort("can't get spi mode");

  163.     /*
  164.      * bits per word
  165.      */
  166.     ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  167.     if (ret == -1)
  168.         pabort("can't set bits per word");

  169.     ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  170.     if (ret == -1)
  171.         pabort("can't get bits per word");

  172.     /*
  173.      * max speed hz
  174.      */
  175.     ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  176.     if (ret == -1)
  177.         pabort("can't set max speed hz");

  178.     ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  179.     if (ret == -1)
  180.         pabort("can't get max speed hz");

  181.     printf("spi mode: %d\n", mode);
  182.     printf("bits per word: %d\n", bits);
  183.     printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);

  184.     transfer(fd);

  185.     close(fd);

  186.     return ret;
  187. }

阅读(8180) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~