SPI Read Not Working

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI Read Not Working

Jump to solution
6,645 Views
jiehunt
Contributor II

I use the i.mx sabreauto board to communicate to the other board with SPI interface.

But, I cannot get the data from SPI Interface. 

I use the oscilloscope to check the CLK signal, and the write operation can generate the CLK signal,

but the read one cannot.

Linux version is Linux version 4.1.15-1.1.1

CPU: ARMv7 Processor [412fc09a] revision 10 (ARMv7)

Please, give some hint about this issue. Thank you.

The test source and log is attached.

Original Attachment has been moved to: teraterm20190929182.log.zip

Original Attachment has been moved to: spitester.c.zip

Labels (3)
0 Kudos
Reply
1 Solution
5,225 Views
Ray_V
Contributor V

In 3-wire (clk, tx, rx) spi protocol a read is done during the write.

The read function (Chip_SSP_ReceiveFrame() or Chip_SPI_ReceiveFrame())call just returns the data that went in the fifo during the last write.

If you need to read after sending a command (reading spi flash for example ), you will need to write "dummy" data to clock out the data from the slave. Look at function (Chip_SSP_ReadFrames_Blocking() or Chip_SPI_ReadFrames_Blocking()) for reference.

View solution in original post

0 Kudos
Reply
7 Replies
5,206 Views
Galeom12
Contributor I

Fifo during the last write it's good way in this topic and the crucial thing how spi works

0 Kudos
Reply
5,226 Views
Ray_V
Contributor V

In 3-wire (clk, tx, rx) spi protocol a read is done during the write.

The read function (Chip_SSP_ReceiveFrame() or Chip_SPI_ReceiveFrame())call just returns the data that went in the fifo during the last write.

If you need to read after sending a command (reading spi flash for example ), you will need to write "dummy" data to clock out the data from the slave. Look at function (Chip_SSP_ReadFrames_Blocking() or Chip_SPI_ReadFrames_Blocking()) for reference.

0 Kudos
Reply
5,225 Views
jiehunt
Contributor II

Hi, Raymundo Velarde

Thank you for your answer.

I think your right. To Read the data from slave, I need sent the dummy data to slave.

I use the SPI_IOC_MESSAGE(1) , and fill the tx_buf with "0", then the clock is generated.

0 Kudos
Reply
5,225 Views
igorpadykov
NXP Employee
NXP Employee

Hi Jie

this is correct, in master mode processor generates clock during write,

please check Chapter 21 Enhanced Configurable SPI (ECSPI)

i.MX6DQ Reference Manual

http://cache.freescale.com/files/32bit/doc/ref_manual/IMX6DQRM.pdf

Spi test can be found in imx-test package (folder mxc_spi_test)

www.nxp.com/lgfiles/NMG/MAD/YOCTO/imx-test-5.4.tar.gz

Best regards
igor
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply
5,225 Views
jiehunt
Contributor II

Hi igorpadykov,

Thank you for your answer.

I checked the document IMX6DQRM.pdf

21.4.4.1 Typical Master Mode

****
When the ECSPI is in Master mode the SS, SCLK, and MOSI are output signals, and the MISO signal is an input.

****

It means in Master mode When i use the READ command , the SCLK signals should be generated.

But in my case , the SCLK has no signals when i use the 

  retSize = read (fd, &data, sizeof(data));

I also run the imx-test package mxc_spi_test, and i get the error below:

./spitester_mxc -D 0 -s 1000000 -b 8 

Execute data transfer test: 0 1 8
spi mode: 0
bits per word: 8
max speed: 1000000 Hz (1000 KHz)
can't send spi messageCorrupted data at 0 wbuf = 56 rbuf = 0
Data sent : 8
Data received :
Test FAILED.

./spitester_mxc -D 0 -s 1000000 -b 8 0BFD01000000
Execute data transfer test: 0 12 0BFD01000000
spi mode: 0
bits per word: 8
max speed: 1000000 Hz (1000 KHz)
Corrupted data at 0 wbuf = 48 rbuf = 0
Corrupted data at 1 wbuf = 66 rbuf = 0
Corrupted data at 2 wbuf = 70 rbuf = 0
Corrupted data at 3 wbuf = 68 rbuf = 0
Corrupted data at 4 wbuf = 48 rbuf = 0
Corrupted data at 5 wbuf = 49 rbuf = 0
Corrupted data at 6 wbuf = 48 rbuf = 0
Corrupted data at 7 wbuf = 48 rbuf = 0
Corrupted data at 8 wbuf = 48 rbuf = 0
Corrupted data at 9 wbuf = 48 rbuf = 0
Corrupted data at 10 wbuf = 48 rbuf = 0
Corrupted data at 11 wbuf = 48 rbuf = 0
Data sent : 0BFD01000000
Data received :
Test FAILED.

I just want to send 6byte to the slave and there is 12Byte.

and the wbuf is corrupted.

pls help me to resolve this problem.

Thank you!

0 Kudos
Reply
5,225 Views
igorpadykov
NXP Employee
NXP Employee

Hi Jie

please check for example
http://www.corelis.com/education/SPI_Tutorial.htm
"Simple SPI Read Transaction" :

This sequence is really a single-byte write followed by a two byte read;

~igor

0 Kudos
Reply
5,225 Views
jiehunt
Contributor II

Hi igorpadykov,

Thank you for your answer.

I checked the doc 

http://www.corelis.com/education/SPI_Tutorial.htm
"Simple SPI Read Transaction" 

But, I still think the driver(or hardware) is weird.

Because i use the same example spitester.c.zip i attached before,It works on raspberrypi.

Both of write/read operation and spi_ioc_massage(2)(read after write) operation can give the right response from slave device.

0 Kudos
Reply