2009-02-23 06:29:48     spidev configuration

Document created by Aaronwu Employee on Aug 13, 2013
Version 1Show Document
  • View in full screen mode

2009-02-23 06:29:48     spidev configuration

leifi leif (SWEDEN)

Message: 69751   

 

Hi!

 

I have a question about how to configure the spidev driver. What I want to do is to communicate with a spi interface sensor. I need to send a 16bit word to the sensor and then read out a 16 bit word from the sensor.

 

The hardware I am using is the bf537-STAMP bord.

 

My stamp.c file look like this:

 

#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)

static struct bfin5xx_spi_chip spidev_chip_info = {

    .enable_dma = 0,

    .bits_per_word = 16,

};

#endif

 

static struct spi_board_info bfin_spi_board_info[] __initdata = {

...

#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)

    {

        .modalias = "spidev",

        .max_speed_hz = 100000,     /* max spi clock (SCK) speed in HZ */

        .bus_num = 0,

        .chip_select = 1,

        .controller_data = &spidev_chip_info,

        .mode = SPI_MODE_3,

    },

#endif

};

 

My question is if this is the right way to configure the spidev driver or am I missing something? I can compile the kernel and the device is showing up in /dev/spidev0.1. Then I trying to write and read a 16 bit word on the spi bus by using the do_msg() function taken from the spi documentation:

 

static void do_msg(int fd, int len)

{

    struct spi_ioc_transfer    xfer[2];

    unsigned char        buf[2], *bp;

    int            status;

 

    memset(xfer, 0, sizeof xfer);

    memset(buf, 0, sizeof buf);

 

    if (len > sizeof buf)

        len = sizeof buf;

 

    buf[0] = 0x04; //Data to set sensor control register

    buf[1] = 0x00;

 

    xfer[0].tx_buf = (__u64) buf;

    xfer[0].len = 1;

    xfer[1].rx_buf = (__u64) buf;

    xfer[1].len = len;

 

    status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);

    if (status < 0) {

        perror("SPI_IOC_MESSAGE");

       return;

    }

 

    printf("response(%2d, %2d): ", len, status);

    for (bp = buf; len; len--)

        printf(" %02x", *bp++);

    printf("\n");

}

 

But the data the sensor gives me is just random(the sensor is configured to give me a specific bit pattern). My question is if the configuration of the spidev driver is right and enought? The .mode should also be right.

QuoteReplyEditDelete

 

 

2009-02-23 12:35:41     Re: spidev configuration

Mike Frysinger (UNITED STATES)

Message: 69762   

 

SPI_IOC_MESSAGE() operates on messages, not buffers, so you probably want SPI_IOC_MESSAGE(1)

 

since you arent doing full duplex transfers, why not use read()/write() as half-duplex transfers as that'll be a lot simpler

 

otherwise, try putting a signal analyzer on the SPI lines to make sure the pattern going out and coming back is as expected

QuoteReplyEditDelete

 

 

2009-02-23 15:19:14     Re: spidev configuration

leifi leif (SWEDEN)

Message: 69769   

 

Thanks for the answer!

 

It was me who had mixed a few things up in the example code, the code was changed as below and now works.

 

static void do_msg(int fd)

{

    struct spi_ioc_transfer    xfer[1];

    __u16 buf[1], *bp;

    int            status;

 

    memset(xfer, 0, sizeof xfer);

    memset(buf, 0, sizeof buf);

    int len = 1;

 

    //Sensor start bytes

    buf[0] = 0x8400;

  

    xfer[0].tx_buf = (__u64) buf;

    xfer[0].rx_buf = (__u64) buf;

    xfer[0].len = 2;

 

    status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);

    if (status < 0) {

        perror("SPI_IOC_MESSAGE");

        return;

    }

 

    printf("response(%2d, %2d): ", len, status);

    for (bp = buf; len; len--)

        printf(" %04x", *bp++);

    printf("\n");

}

QuoteReplyEditDelete

 

 

2009-10-30 02:50:14     Re: spidev configuration

鹏 肖 (CHINA)

Message: 81884   

 

Mike:

 

         you say "SPI_IOC_MESSAGE() operates on messages, not buffers, so you probably want SPI_IOC_MESSAGE(1)".I want to know where is defined location of SPI_IOC_MESSAGE() .what is difference between SPI_IOC_MESSAGE(1) and SPI_IOC_MESSAGE(2)

QuoteReplyEditDelete

 

 

2009-10-30 03:16:46     Re: spidev configuration

Mike Frysinger (UNITED STATES)

Message: 81895   

 

please do not post to old threads.  start your own.

 

otherwise, read the documentation:

docs.blackfin.uclinux.org/doku.php?id=spi#userspace_driver

Attachments

    Outcomes