mytbk 发表于 2015-4-12 22:47:18

CubieTruck的SPI还是不会配置

我用了很多种配置,/dev下也有spidev*.*这样的文件,但是用kernel给的那个spidev_test的时候总是报错:
can't set spi mode: Inappropriate ioctl for device

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

jiangdou 发表于 2015-4-13 09:09:40

本帖最后由 jiangdou 于 2015-4-13 09:22 编辑

//modify by jiangdou2013-0612
#include <assert.h>
#include <errno.h>   
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>

char buf;
char buf2;
int com_serial;
int failcount;

struct spi_ioc_transfer xfer;
////////////////////////
// Init SPIdev
///////////////////////
int spi_init(char filename)
{
      int file;
      __u8    mode, lsb, bits;
      __u32 speed=25000;

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

                ///////////////
                // Verifications
                ///////////////
                //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;
                //multiple possibilities using |
                /*
                        if (ioctl(file, SPI_IOC_WR_MODE, &mode)<0)       {
                              perror("can't set spi mode");
                              return;
                              }
                */

                        if (ioctl(file, SPI_IOC_RD_MODE, &mode) < 0)
                              {
                              perror("SPI rd_mode");
                              return;
                              }
                        if (ioctl(file, SPI_IOC_RD_LSB_FIRST, &lsb) < 0)
                              {
                              perror("SPI rd_lsb_fist");
                              return;
                              }
                //sunxi supports only 8 bits
                /*
                        if (ioctl(file, SPI_IOC_WR_BITS_PER_WORD, 8)<0)      
                              {
                              perror("can't set bits per word");
                              return;
                              }
                */
                        if (ioctl(file, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
                              {
                              perror("SPI bits_per_word");
                              return;
                              }
                /*
                        if (ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0)      
                              {
                              perror("can't set max speed hz");
                              return;
                              }
                */
                        if (ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
                              {
                              perror("SPI max_speed_hz");
                              return;
                              }
         

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

      //xfer.tx_buf = (unsigned long)buf;
      xfer.len = 2; /* Length ofcommand to write*/
      xfer.cs_change = 0; /* Keep CS activated */
      xfer.delay_usecs = 1000, //delay in us
      xfer.speed_hz = 2500000, //speed
      xfer.bits_per_word = 2, // bites per word 8

      //xfer.rx_buf = (unsigned long) buf2;////modify by jiangdou2013-0612
      xfer.len = 1; /* Length of Data to read */
      xfer.cs_change = 0; /* Keep CS activated */
      xfer.delay_usecs = 0;
      xfer.speed_hz = 2500000;
      xfer.bits_per_word = 2;

      return file;
}
////////////////////////////////////////
// Read n bytes from the 1 bytes add
///////////////////////////////////////
int spi_read(int add,int file)
{         
      int status;
      char temp;
      memset(buf, 0, sizeof(buf));
      memset(buf2, 0, sizeof(buf2));
      buf = 0x03;      //03 ->读命令
      buf = add;
      //buf = add2; //modify by jiangdou2013-0612
      xfer.tx_buf = (unsigned long)buf;
      xfer.len = 2; /* Length ofcommand to write*/
      xfer.rx_buf = (unsigned long) buf2;
      xfer.len = 1; /* Length of Data to read */
      status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
      if (status < 0)
      {
                perror("SPI_IOC_MESSAGE");
                return;
      }
      //printf("env: %02x %02x %02x\n", buf, buf, buf);
      //printf("ret: %02x %02x %02x %02x\n", buf2, buf2, buf2, buf2);
      com_serial=1;
      failcount=0;
      
      temp = buf2;
      return temp;
}
////////////////////////////////////////////////////////
// Write n bytes int the 1 bytes address add
///////////////////////////////////////////////////////
//void spi_write(int cmd,int add,int nbytes,char value,int file)
void spi_write(int cmd,int add,int nbytes,char value,int file)
{         
      unsigned char   buf, buf2;
      int status;
      memset(buf, 0, sizeof(buf));
      memset(buf2, 0, sizeof(buf2));
      buf = cmd;      //02 ->写命令
      buf = add;
      //buf = add2;
      if (nbytes>=1) buf = value;
      if (nbytes>=2) buf = value;
      if (nbytes>=3) buf = value;
      if (nbytes>=4) buf = value;
      xfer.tx_buf = (unsigned long)buf;
      xfer.len = nbytes+2; /* Length ofcommand to write*/
      status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);
      if (status < 0)
      {
                perror("SPI_IOC_MESSAGE");
                return;
      }         
      //printf("env: %02x %02x %02x\n", buf, buf, buf);
      //printf("ret: %02x %02x %02x %02x\n", buf2, buf2, buf2, buf2);
      com_serial=1;
      failcount=0;
}

int main()
{
      
      unsigned int i;
      char tmp;
      char bufw;
      static char temp2;
      static char *p = temp2
      int fd = spi_init("/dev/spidev0.0"); //dev      spi_init初始化
      tmp = spi_read(0x2C,file);      /* 读中断标志寄存器 */
      
      memset(bufw, 0, sizeof(bufw));
      memset(temp2, 0, sizeof(temp2));
      spi_write(0x02,0x2C,1,bufw,file);    /* 清除所有中断标志位 */      
}
script.bin
spi_used            = 0
spi_cs_bitmap       = 1
spi_cs0             = port:PA05<3><default><default><default>
spi_cs1             = port:PA09<3><default><default><default>
spi_sclk            = port:PA06<3><default><default><default>
spi_mosi            = port:PA07<3><default><default><default>
spi_miso            = port:PA08<3><default><default><default>


spi_dev_num = 1

modalias = "spidev"
max_speed_hz = 12000000
bus_num = 0
chip_select = 0
mode = 3
full_duplex = 0
manual_cs = 0

wb4916 发表于 2015-6-3 00:10:49




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

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

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

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

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

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

cike 发表于 2015-7-2 11:58:12

请问我用这个程序 调用ioctl函数的时候说参数非法Invalidargument是怎么回事?
页: [1]
查看完整版本: CubieTruck的SPI还是不会配置