linux 测试 SPI 测试应用程序代码

mail:bookworepeng@Hotmail.com

qq:196568501

author:DriverMonkey

phone:13410905075


测试硬件平台:TI  AM335X


测试代码:

[cpp]  view plain copy
  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/spidev1.1";  
  33. static uint8_t mode;  
  34. static uint8_t bits = 8;  
  35. static uint32_t speed = 500000;  
  36. static uint16_t delay;  
  37. uint8_t tx[4096] = {0};  
  38. uint8_t rx[ARRAY_SIZE(tx)] = {0, };  
  39. static void transfer(int fd)  
  40. {  
  41.     int ret;  
  42.       
  43.     struct spi_ioc_transfer tr = {  
  44.         .tx_buf = (unsigned long)tx,  
  45.         .rx_buf = (unsigned long)rx,  
  46.         .len = ARRAY_SIZE(tx),  
  47.         .delay_usecs = delay,  
  48.         .speed_hz = speed,  
  49.         .bits_per_word = bits,  
  50.     };  
  51.   
  52.     ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);  
  53.     if (ret < 1)  
  54.         pabort("can't send spi message");  
  55.   
  56.     //for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {  
  57.     //  if (!(ret % 6))  
  58.     //      puts("");  
  59.     //  printf("%.2X ", rx[ret]);  
  60.     //}  
  61.     puts("");  
  62. }  
  63.   
  64. static void print_usage(const char *prog)  
  65. {  
  66.     printf("Usage: %s [-DsbdlHOLC3]\n", prog);  
  67.     puts("  -D --device   device to use (default /dev/spidev1.1)\n"  
  68.          "  -s --speed    max speed (Hz)\n"  
  69.          "  -d --delay    delay (usec)\n"  
  70.          "  -b --bpw      bits per word \n"  
  71.          "  -l --loop     loopback\n"  
  72.          "  -H --cpha     clock phase\n"  
  73.          "  -O --cpol     clock polarity\n"  
  74.          "  -L --lsb      least significant bit first\n"  
  75.          "  -C --cs-high  chip select active high\n"  
  76.          "  -3 --3wire    SI/SO signals shared\n");  
  77.     exit(1);  
  78. }  
  79.   
  80. static void parse_opts(int argc, char *argv[])  
  81. {  
  82.     while (1) {  
  83.         static const struct option lopts[] = {  
  84.             { "device",  1, 0, 'D' },  
  85.             { "speed",   1, 0, 's' },  
  86.             { "delay",   1, 0, 'd' },  
  87.             { "bpw",     1, 0, 'b' },  
  88.             { "loop",    0, 0, 'l' },  
  89.             { "cpha",    0, 0, 'H' },  
  90.             { "cpol",    0, 0, 'O' },  
  91.             { "lsb",     0, 0, 'L' },  
  92.             { "cs-high", 0, 0, 'C' },  
  93.             { "3wire",   0, 0, '3' },  
  94.             { "no-cs",   0, 0, 'N' },  
  95.             { "ready",   0, 0, 'R' },  
  96.             { NULL, 0, 0, 0 },  
  97.         };  
  98.         int c;  
  99.   
  100.         c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);  
  101.   
  102.         if (c == -1)  
  103.             break;  
  104.   
  105.         switch (c) {  
  106.         case 'D':  
  107.             device = optarg;  
  108.             break;  
  109.         case 's':  
  110.             speed = atoi(optarg);  
  111.             break;  
  112.         case 'd':  
  113.             delay = atoi(optarg);  
  114.             break;  
  115.         case 'b':  
  116.             bits = atoi(optarg);  
  117.             break;  
  118.         case 'l':  
  119.             mode |= SPI_LOOP;  
  120.             break;  
  121.         case 'H':  
  122.             mode |= SPI_CPHA;  
  123.             break;  
  124.         case 'O':  
  125.             mode |= SPI_CPOL;  
  126.             break;  
  127.         case 'L':  
  128.             mode |= SPI_LSB_FIRST;  
  129.             break;  
  130.         case 'C':  
  131.             mode |= SPI_CS_HIGH;  
  132.             break;  
  133.         case '3':  
  134.             mode |= SPI_3WIRE;  
  135.             break;  
  136.         case 'N':  
  137.             mode |= SPI_NO_CS;  
  138.             break;  
  139.         case 'R':  
  140.             mode |= SPI_READY;  
  141.             break;  
  142.         default:  
  143.             print_usage(argv[0]);  
  144.             break;  
  145.         }  
  146.     }  
  147. }  
  148.   
  149. int main(int argc, char *argv[])  
  150. {  
  151.     int err = 0;  
  152.     unsigned char cmd = 32;  
  153.     unsigned int mode = 0;  
  154.     int handle_spi1dev = 0;  
  155.     int ret = 0;  
  156.   
  157.     printf("init_SPI1()++\n");  
  158.   
  159.     printf("handle_spi1dev = %x \n", handle_spi1dev);  
  160.     handle_spi1dev = open("/dev/spidev1.0",O_RDWR);  
  161.   
  162.     if(handle_spi1dev < 0)  
  163.     {     
  164.         printf("/dev/spidev1.0 error \n");  
  165.     }     
  166.     mode = SPI_CPHA;  
  167.     err = ioctl(handle_spi1dev, SPI_IOC_WR_BITS_PER_WORD,&cmd);//change spi to 32bits mode  
  168.     if (err == -1)  
  169.         printf("can't set spi mode\n");  
  170.   
  171.     err = ioctl(handle_spi1dev, SPI_IOC_WR_MODE, &mode);  
  172.     if (err == -1)  
  173.         printf("can't set spi mode\n");  
  174.     printf("handle_spi1dev = %x \n", handle_spi1dev);  
  175.     printf("init_SPI1()--\n");  
  176.   
  177.   
  178.     transfer(handle_spi1dev);  
  179.   
  180.     close(handle_spi1dev);  
  181.   
  182.     return ret;  
  183. }  

测试结果:

当前SPI 驱动接口最多一次转送 4K Bytes 大小的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值