Andrew.Barton
Posts: 3
Joined: Fri Jun 01, 2018 5:35 pm

Trouble with SPI read

Fri Jun 01, 2018 5:40 pm

I am having trouble reading the SPI bus.
The bus is connected in a loop back configuration (ie MOSI is connected to MISO) and this appears to work.
When I connect MISO to Vcc I can read 0xFF as expected.
This is the code that I am running.
int main()
{
unsigned char writeData[1];
unsigned char readData[1];
int fd, res;

writeData[0] = 0x21;

printf("Initializing\n");
// Configure the interface.
fd = wiringPiSPISetup(0, 250000);
printf("SPI Initialisation result: %d\n",fd);

res = write(fd,writeData,1);
if (res < 1)
fprintf (stderr, "Unable to write data: %s\n", strerror (errno)) ;
else
{
printf("Number of bytes written = %d\n",res);
printf("Data written to the bus is %x\n\n",writeData[0]);
}

res = read(fd,readData,2);
if (res < 1)
fprintf (stderr, "Unable to read data: %s\n", strerror (errno)) ;
else
{
printf("Number of bytes read = %d\n",res);
printf("Data read from bus is %x\n\n",readData[0]);
}
return(0);
}

When I run this I only ever see 0 in readData. Any suggestions as to what I am doing wrong?

Andrew

GuruMeditation
Posts: 31
Joined: Thu May 31, 2018 3:33 pm
Location: UK

Re: Trouble with SPI read

Fri Jun 01, 2018 8:07 pm

The SPI interface is full duplex so I'm suspecting that, as you first write then read, there is no data to be read when you call 'read'. Hence 0.

If so then try using wiringPiSPIDataRW().

Andrew.Barton
Posts: 3
Joined: Fri Jun 01, 2018 5:35 pm

Re: Trouble with SPI read

Sat Jun 02, 2018 9:11 am

Thanks - that confirms my suspicion. The problem with wiringPiSPIDataRW() is that it returns the read data in the same variable as you pass the write data and I wanted to separate the two actions out so I could confirm correct operation. Is this possible or should I try a different library?

GuruMeditation
Posts: 31
Joined: Thu May 31, 2018 3:33 pm
Location: UK

Re: Trouble with SPI read

Sat Jun 02, 2018 9:28 am

I don't think that WiringPI has an API function that separates the two buffers, which is a shame since they are separate internally (you can check the code).

You could copy the data into a second array that you then use for wiringPiSPIDataRW(). After the function call both arrays' contents should still be equal.
Last edited by GuruMeditation on Sat Jun 02, 2018 10:54 am, edited 1 time in total.

Andrew.Barton
Posts: 3
Joined: Fri Jun 01, 2018 5:35 pm

Re: Trouble with SPI read

Sat Jun 02, 2018 10:37 am

OK - I will give it a try.
Thanks for the help

User avatar
DougieLawson
Posts: 33003
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Trouble with SPI read

Sun Jun 03, 2018 7:59 am

You can get the source code for WiringPi, so hacking the function to work the way you want is trivial.

Code: Select all

/*
 * wiringPiSPIDataRW:
 *      Write and Read a block of data over the SPI bus.
 *      Note the data ia being read into the transmit buffer, so will
 *      overwrite it!
 *      This is also a full-duplex operation.
 *********************************************************************************
 */

int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
{
  struct spi_ioc_transfer spi ;

  channel &= 1 ;

// Mentioned in spidev.h but not used in the original kernel documentation
//      test program )-:

  memset (&spi, 0, sizeof (spi)) ;

  spi.tx_buf        = (unsigned long)data ;
  spi.rx_buf        = (unsigned long)data ;
  spi.len           = len ;
  spi.delay_usecs   = spiDelay ;
  spi.speed_hz      = spiSpeeds [channel] ;
  spi.bits_per_word = spiBPW ;

  return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
}
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “C/C++”

Who is online

Users browsing this forum: Google [Bot] and 4 guests