CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 7097|回复: 3

CubieTruck的SPI还是不会配置

[复制链接]
发表于 2015-4-12 22:47:18 | 显示全部楼层 |阅读模式
我用了很多种配置,/dev下也有spidev*.*这样的文件,但是用kernel给的那个spidev_test的时候总是报错:
can't set spi mode: Inappropriate ioctl for device

附上我的内核配置和script.fex.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

发表于 2015-4-13 09:09:40 | 显示全部楼层
本帖最后由 jiangdou 于 2015-4-13 09:22 编辑
  1. //modify by jiangdou  2013-0612
  2. #include <assert.h>
  3. #include <errno.h>     
  4. #include <stdint.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <getopt.h>
  10. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/types.h>
  13. #include <linux/spi/spidev.h>

  14. char buf[10];
  15. char buf2[10];
  16. int com_serial;
  17. int failcount;

  18. struct spi_ioc_transfer xfer[2];
  19. ////////////////////////
  20. // Init SPIdev
  21. ///////////////////////
  22. int spi_init(char filename[40])
  23. {
  24.         int file;
  25.         __u8    mode, lsb, bits;
  26.         __u32 speed=25000;

  27.         if ((file = open(filename,O_RDWR)) < 0)
  28.                 {
  29.                 printf("Failed to open the bus.");
  30.                 /* ERROR HANDLING; you can check errno to see what went wrong */
  31.                 com_serial=0;
  32.                 exit(1);
  33.                 }

  34.                 ///////////////
  35.                 // Verifications
  36.                 ///////////////
  37.                 //possible modes: mode |= SPI_LOOP; mode |= SPI_CPHA; mode |= SPI_CPOL; mode |= SPI_LSB_FIRST; mode |= SPI_CS_HIGH; mode |= SPI_3WIRE; mode |= SPI_NO_CS; mode |= SPI_READY;
  38.                 //multiple possibilities using |
  39.                 /*
  40.                         if (ioctl(file, SPI_IOC_WR_MODE, &mode)<0)       {
  41.                                 perror("can't set spi mode");
  42.                                 return;
  43.                                 }
  44.                 */

  45.                         if (ioctl(file, SPI_IOC_RD_MODE, &mode) < 0)
  46.                                 {
  47.                                 perror("SPI rd_mode");
  48.                                 return;
  49.                                 }
  50.                         if (ioctl(file, SPI_IOC_RD_LSB_FIRST, &lsb) < 0)
  51.                                 {
  52.                                 perror("SPI rd_lsb_fist");
  53.                                 return;
  54.                                 }
  55.                 //sunxi supports only 8 bits
  56.                 /*
  57.                         if (ioctl(file, SPI_IOC_WR_BITS_PER_WORD, 8)<0)      
  58.                                 {
  59.                                 perror("can't set bits per word");
  60.                                 return;
  61.                                 }
  62.                 */
  63.                         if (ioctl(file, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
  64.                                 {
  65.                                 perror("SPI bits_per_word");
  66.                                 return;
  67.                                 }
  68.                 /*
  69.                         if (ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0)      
  70.                                 {
  71.                                 perror("can't set max speed hz");
  72.                                 return;
  73.                                 }
  74.                 */
  75.                         if (ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
  76.                                 {
  77.                                 perror("SPI max_speed_hz");
  78.                                 return;
  79.                                 }
  80.          

  81.         printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n",filename, mode, bits, lsb ? "(lsb first) " : "", speed);

  82.         //xfer[0].tx_buf = (unsigned long)buf;
  83.         xfer[0].len = 2; /* Length of  command to write*/
  84.         xfer[0].cs_change = 0; /* Keep CS activated */
  85.         xfer[0].delay_usecs = 1000, //delay in us
  86.         xfer[0].speed_hz = 2500000, //speed
  87.         xfer[0].bits_per_word = 2, // bites per word 8

  88.         //xfer[1].rx_buf = (unsigned long) buf2;////modify by jiangdou  2013-0612
  89.         xfer[1].len = 1; /* Length of Data to read */
  90.         xfer[1].cs_change = 0; /* Keep CS activated */
  91.         xfer[0].delay_usecs = 0;
  92.         xfer[0].speed_hz = 2500000;
  93.         xfer[0].bits_per_word = 2;

  94.         return file;
  95. }
  96. ////////////////////////////////////////
  97. // Read n bytes from the 1 bytes add
  98. ///////////////////////////////////////
  99. int spi_read(int add,int file)
  100. {         
  101.         int status;
  102.         char temp;
  103.         memset(buf, 0, sizeof(buf));
  104.         memset(buf2, 0, sizeof(buf2));
  105.         buf[0] = 0x03;        //03 ->读命令
  106.         buf[1] = add;
  107.         //buf[2] = add2; //modify by jiangdou  2013-0612
  108.         xfer[0].tx_buf = (unsigned long)buf;
  109.         xfer[0].len = 2; /* Length of  command to write*/
  110.         xfer[1].rx_buf = (unsigned long) buf2;
  111.         xfer[1].len = 1; /* Length of Data to read */
  112.         status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
  113.         if (status < 0)
  114.         {
  115.                 perror("SPI_IOC_MESSAGE");
  116.                 return;
  117.         }
  118.         //printf("env: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
  119.         //printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);
  120.         com_serial=1;
  121.         failcount=0;
  122.         
  123.         temp = buf2[0];
  124.         return temp;
  125. }
  126. ////////////////////////////////////////////////////////
  127. // Write n bytes int the 1 bytes address add
  128. ///////////////////////////////////////////////////////
  129. //void spi_write(int cmd,int add,int nbytes,char value[10],int file)
  130. void spi_write(int cmd,int add,int nbytes,char value[10],int file)
  131. {         
  132.         unsigned char   buf[32], buf2[32];
  133.         int status;
  134.         memset(buf, 0, sizeof(buf));
  135.         memset(buf2, 0, sizeof(buf2));
  136.         buf[0] = cmd;        //02 ->写命令
  137.         buf[1] = add;
  138.         //buf[2] = add2;
  139.         if (nbytes>=1) buf[2] = value[0];
  140.         if (nbytes>=2) buf[3] = value[1];
  141.         if (nbytes>=3) buf[4] = value[2];
  142.         if (nbytes>=4) buf[5] = value[3];
  143.         xfer[0].tx_buf = (unsigned long)buf;
  144.         xfer[0].len = nbytes+2; /* Length of  command to write*/
  145.         status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);
  146.         if (status < 0)
  147.         {
  148.                 perror("SPI_IOC_MESSAGE");
  149.                 return;
  150.         }         
  151.         //printf("env: %02x %02x %02x\n", buf[0], buf[1], buf[2]);
  152.         //printf("ret: %02x %02x %02x %02x\n", buf2[0], buf2[1], buf2[2], buf2[3]);
  153.         com_serial=1;
  154.         failcount=0;
  155. }

  156. int main()
  157. {
  158.         
  159.         unsigned int i;
  160.         char tmp;
  161.         char bufw[10];
  162.         static char temp2[20];
  163.         static char *p = temp2
  164.         int fd = spi_init("/dev/spidev0.0"); //dev        spi_init初始化
  165.         tmp = spi_read(0x2C,file);        /* 读中断标志寄存器 */
  166.         
  167.         memset(bufw, 0, sizeof(bufw));
  168.         memset(temp2, 0, sizeof(temp2));
  169.         spi_write(0x02,0x2C,1,bufw,file);    /* 清除所有中断标志位 */        
  170. }
复制代码
script.bin
  1. [spi3_para]  
  2. spi_used            = 0  
  3. spi_cs_bitmap       = 1  
  4. spi_cs0             = port:PA05<3><default><default><default>  
  5. spi_cs1             = port:PA09<3><default><default><default>  
  6. spi_sclk            = port:PA06<3><default><default><default>  
  7. spi_mosi            = port:PA07<3><default><default><default>  
  8. spi_miso            = port:PA08<3><default><default><default>  
  9.   
  10. [spi_devices]  
  11. spi_dev_num = 1  
  12. [spi_board0]  
  13. modalias = "spidev"  
  14. max_speed_hz = 12000000  
  15. bus_num = 0  
  16. chip_select = 0  
  17. mode = 3  
  18. full_duplex = 0  
  19. manual_cs = 0  
复制代码

回复 支持 反对

使用道具 举报

发表于 2015-6-3 00:10:49 | 显示全部楼层



我看了你的 config.gz 压缩包,打开之后,搜索 SPI 项目,初步发现:

CONFIG_SPI_SUN7I=m (这个是编译成模块呀!)

感觉应该要配置成:
CONFIG_SPI_SUN7I=y  呀!(这样就会被编译进内核呀!)

也许就是你出错的原因呀!

进 menuconfig 里面选择一下,或者直接修改 .config 文件了!

我用的是 cubieboard2,/dev/spi0.0出不来呀!能够提供一些指导吗?

回复 支持 反对

使用道具 举报

发表于 2015-7-2 11:58:12 | 显示全部楼层
请问我用这个程序 调用ioctl函数的时候说参数非法Invalid  argument是怎么回事?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-5-7 23:58 , Processed in 0.023593 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部