SPI and FRDM-K64F - strange behavior

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

SPI and FRDM-K64F - strange behavior

992 Views
frarugi87
Contributor I

Hi all

I have a little problem with my FRDM-K64F board. I'm trying to communicate with an SPI peripheral (touchscreen contoller), but it behaves in a strange way.

I connected it to the SPI0 pins, then all I have to do is to send him the configuration byte and he gives me the data. Since it needs 24 clock cycles to give me the results, I send it the configuration byte and two dummy bytes.

 

So I have to send him three bytes. The function I made to do this job is attached (I can't find any "code" block here).

 

So... If I call this function this way:

 

byte recvbuff[3] = { 0, 0, 0};

byte sendbuff[3] = { 0x90, 0, 0 };

SpiManager_SendAndReceive(sendbuff, 3, recvbuff, 3);


(i.e. with both buffers 3 bytes wide) I get this on the SPI wires:


17610_17610.pngFRDMK64F_SPI_SingleWrite.png


So the bytes are spaced by a 1us time delay. This is good and is exactly what I wanted.


BUT if I call it this way:


byte recvbuff[3] = { 0, 0, 0};

byte sendbyte = 0x90;

SpiManager_SendAndReceive(&sendbyte, 1, recvbuff, 3);


(i.e. just one byte in the send buffer buffer) what I get is this:

17611_17611.pngFRDMK64F_SPI_MultipleWrite.png

The bytes are separated by a 20us delay. This is not good, since the other device doesn't like it (and gives me strange data).


What is the problem?

I also attached the SPI_LDD confguration.


Thank yuo for your help

Original Attachment has been moved to: FRDMK64F_SPI_Function.txt.zip

Tags (4)
0 Kudos
1 Reply

470 Views
adriancano
NXP Employee
NXP Employee

Hi,

The SPI send and receive functions are implemented on the SPI_ISR therefore the SM1_SendBlock performs a send and receive operation. The SM1_SendBlock function is enabling the interrupt then the interrupt is called every time a character is sent or is received; this operation is performed based on the buffer_size you specified as parameter in the SM1_SendBlock function.

If you specify a send_buffer_size of 3: byte sendbuff[3] = { 0x90, 0, 0 }; then the SPI_LDD driver will handle the transmit and received characters with the specified time between frames. And will return ERR_OK when the 3 bytes were successfully sent.

Then you can check if the bytes were successfully received when the OnBlockReceived is called. This event is called when the requested number of data is moved to the input buffer. The requested number of data is set when SM1_ReceiveBlock is called and the size of the receive buffer is specified.

Please refer to the attached document for more information about the typical usage of this component.

What is happening on your code (second image) is that the SM1_SendBlock is that just one byte is being sent with 0x90 and after that program reaches the for statement and perform another SendBlock with one byte and after that another one. What, I think, you see as the time between frames is the time the code is taking to execute again a call to the SM1_SendBlock function.

In conclusion, the better way to send dummy bytes is just calling the SM1_SendBlock like in the first image without the for statement. In this case the dummy writes will be performed by the LDD driver sending first: 0x90 and after that the dummy byte 0 and the dummy byte 0 with the time between frames you specified in the Component configurations.

I hope this information is clear enough, if you have any question please feel free to ask.


Hope this information can help you.

Best Regards,
Adrian Sanchez Cano
Technical Support Engineer
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos