This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

spidev and reading

Hello,

I have the following issue when using a spidev with a M25P32 spi flash. Looking at the scope everything seems to work just fine. I get a clock, CS is issued correctly, DQ0 changes, DQ1 outputs data.

However, when I look at my program:

int fd;
fd = open("/dev/spidev1.0", O_RDWR);

if( fd <= 0 )
{
printf("erroor\n");
exit(1);
}

char cmd[4] = { 0x9F,0x00,0x00,0x00 };
char buf[16];

write(fd, cmd,4);
read(fd,buf,16);

close(fd);

printf("Read: %s\n",buf);

says otherwise. De buffer content is always FF....... I don't understand. This spi flash works, since I use it to boot my board's bootloader. Linux itself is loaded from nand flash. I hope someone can help me...

Kind regards

  • Hi Arend,
     
    Just a suggestion for the possible cause - please check your pinmux settings for the SPI clock signal. It should have the "Receiver Enabled" bit set (rxactive=1). This is necessary, because the SPI clock is used for input re-timing inside the SPI module.
  • Turns out I needed to use ioctl transfers to get it to work:

    char *tmp = new char[out_length];

    struct spi_ioc_transfer xfer[2];

    memset( xfer, 0, sizeof( xfer ));

    xfer[0].tx_buf = (unsigned long)buffer;
    xfer[0].len = in_length;

    xfer[1].rx_buf = (unsigned long) tmp;
    xfer[1].len = out_length;

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

    ... this because the CS was toggled between the read and write, making the flash "forget" it's previous command

    Thanks anyway!

  • Dear Sir,


    I am trying to connect SPI based EEPROM with beaglebone white. I have done pinmux settings to use SPI1 of P9 header of Beaglebone board.

    I am operating SPI at 500Khz frequency, 8 bit and Mode 0.

    SPI1_D1 is output Pull Up pin and SPI1_D0 is input pull up pin.

    Below is my spi intializaition routine.

    static const char *device = "/dev/spidev2.0";
    static uint8_t mode;
    static uint8_t bits = 8;
    static uint32_t speed = 500000;

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

        ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
        if (ret == -1)
            pabort("can't set spi mode");

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

        ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
        if (ret == -1)
            pabort("can't set bits per word");

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

        ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
        if (ret == -1)
            pabort("can't set max speed hz");

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

        struct spi_ioc_transfer tr;
        unsigned char tx[8];//={0x84,0x11,0x22};
        
        unsigned char wren[1];//={0x84,0x11,0x22};
        unsigned char rx1[8] = {0, };
        unsigned int i;

        unsigned char data[8];


        data[0]=EEPROM_READ;
        data[1]=0xfa;//spi_address;
        data[2]=0x00;
        data[3]=0x00;
        data[4]=0x00;
        data[5]=0x00;
        data[6]=0x00;
        data[7]=0x00;

        for (i=0;i<8;i++)
        {
            tx[i]=data[i];
        }

            tr.tx_buf = (unsigned long)tx;
            tr.rx_buf = (unsigned long)rx1;
            tr.len = ARRAY_SIZE(tx);
            tr.delay_usecs = 0;//delay,
            tr.speed_hz = speed;
            tr.bits_per_word = bits;

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

        for (ret = 0; ret < 8; ret++)
        {
        printf ("\n0=%d",rx1[ret]);
        }

    Problem: I am reading a address byte of EEPROM and i am able to see the 6 bytes of data on Oscilloscope but i am able to read the data in my user space program.

    Please let me know the problem in my code.

    Regards,

    Ajay Rajput

  • This is an old thread, but it comes up near the top of the list when searching for problems with reading SPI. I was unable to read data from my SPI device as well and the solution was as suggested by Biser above: make sure you set the SCLK pin as PIN_INPUT_PULLUP in pinmux. Thanks Biser!