The transmit (from Nano to device) is working well and I can see (on a logic analyzer) that the device replies correctly: see attached
The pb is that the byte read in the SPI rx_buffer are always 0…like if only the SPI MOSI was enable but not MISO …!
I’ve tried also several things to fix that (changing the SPI_MODE, a jumper between pins 19/21 of J41 to get same data received than emitted, …)
here is the short C code I use: To init the SPI bus /dev/spidev0.0
int SPI::initSPI(char *device, unsigned int speed, unsigned int delay_usecs, unsigned char bits_per_word, unsigned char cs_change)
{
memset(&config, 0, sizeof config);
memset(spi_rxBuf, 0, sizeof spi_rxBuf);
memset(spi_txBuf, 0, sizeof spi_txBuf);
config.rx_buf = (unsigned long) &spi_rxBuf;
config.tx_buf = (unsigned long) &spi_txBuf;
config.delay_usecs = delay_usecs; // delay after the last bit transfer before optionally deselecting the device before the next transfer
config.speed_hz = speed; // Bus speed in Hz
config.bits_per_word = bits_per_word;
config.cs_change = cs_change; // True to deselect device before starting the next transfer
fd = open(device, O_RDWR); // fd is a private file descriptor
usleep(1000); // Wait a bit after the opening
if (fd < 0)
return -1;
return 0; // return OK
}
To perform a synchronous transfer to/from device on /dev/spidev0.0
spi_dsPIC.spi_txBuf[0] = 0x55; // Fill buffer to be sent
spi_dsPIC.spi_txBuf[1] = 0xC0;
spi_dsPIC.spi_txBuf[2] = 4;
spi_dsPIC.spi_txBuf[3] = 0;
spi_dsPIC.spi_txBuf[4] = 0;
spi_dsPIC.spi_txBuf[5] = 0;
spi_dsPIC.spi_txBuf[6] = 0;
spi_dsPIC.spi_txBuf[7] = 0;
config.len = 8; // complete the len in the structure
ioctl(fd, SPI_IOC_MESSAGE(1), &config); // normally spi_rxBuf contains reply
We are stuck on this pb since 2 days.
Thanks for help.
No The loop back is not working. Only the MOSI pin send the correct bytes but MISO do not read anything.
I ve tried all the speeds from 100KHz to 5MHz same pb remains.
It seems if only the SPI MOSI was enable but not MISO …! see the logic analyser chart:
the protocol is perfect (bytes send par Nano and reply done by device) but no character read
R32 (release), REVISION: 4.4, GCID: 23942405, BOARD: t210ref, EABI: aarch64, DATE: Fri Oct 16 19:44:43 UTC 2020
loop back: a jumper on J41 pin 19 and 21 with logic analyzer on this jumper (to see what is passing thru)
Then running my C++ code (sending a specific byte sequence of 8 bytes and receiving them synchronously):
result : I can see on logic analyser that the specific byte sequence of 8 bytes is sent correctly (and abviuosly on MISO because of the jumper) but never receive by my code (using the spidev library)
In fact the reply from loopBackTest (spidev_test) was correct. So I digged a bit in its source to compare with mine and I found the problem:
The “config” structure sent in ioctl(fd, SPI_IOC_MESSAGE(1), &config); must be totally initialized.
I simply added memset(&config, 0, sizeof config); before the init of this structure. Now it works well.