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.

TMS570LC4357: SPI

Part Number: TMS570LC4357

王工:

我想用spiTransmitData()和spiReceiveData()替换掉spiTransmitAndReceiveData()为什么接收的数据不同?

代码附上:

#if 0

uint16_t command[1]={RBPR};
uint16_t RxData[19];
uint16_t i=18,j=1;
spiTransmitAndReceiveData(spiREG4,&g_spi4Config,19,command,RxData);

while(i--){
*pBuffer=RxData[j++];
pBuffer++;
}

#endif
#if 1

uint16_t command[1];
command[0]=RBPR;

spiTransmitData(spiREG4,&g_spi4Config,1,command);

spiReceiveData(spiREG4,&g_spi4Config,18,pBuffer);

#endif

  • English:

    Hi Wang Gong,

    Why the received data is different?

    When you call the "spiTransmitAndReceiveData" function, then transmission and reception happens parallelly. That means if you send 'N" number of frames and you will receive equal number of frames from slave.

    But if you use "spiTransmitData" and "spiReceiveData" separately, then when you are sending data using "spiTransmitData" then the received frames from slave will be ignored(In your case the first frame corresponding to the command will be ignored). And when you are trying to receive data using "spiReceiveData" function then dummy frames "0x00000" will be transmitted to receive data from slave.

    I noticed one issue with your code, in your code you declared command size with only array of 1 but you are calling "spiTransmitAndReceiveData" function with block size of 19 right, so for first frame it will send your command in array[0] and receives one frame from slave that is fine but after that it will just increments the source buffer by one to send next frame and in this case you just end up with array overflow(because you declared array size as 1), so some unknown frame will be send to the slave. To eliminate this array overflow try to declare your "command" array as below while using  "spiTransmitAndReceiveData".

    uint16_t command[19];

    for(uint8_t k=0;k<19;k++)

    {

       if(k==0)

          command[k] =  RBPR;

       else

          command[k] =  0;

    }

    中国人:
    你好王工,
    
    为什么接收到的数据不一样?
    
    当您调用“spiTransmitAndReceiveData”函数时,传输和接收会并行进行。这意味着如果您发送“N”个帧,您将从从站接收相同数量的帧。
    
    但是如果你分别使用“spiTransmitData”和“spiReceiveData”,那么当你使用“spiTransmitData”发送数据时,从slave接收到的帧将被忽略(在你的情况下,对应于命令的第一帧将被忽略)。当您尝试使用“spiReceiveData”函数接收数据时,将传输虚拟帧“0x00000”以接收来自从站的数据。
    我注意到您的代码存在一个问题,在您的代码中,您声明的命令大小只有数组 1,但是您正在调用块大小为 19 的“spiTransmitAndReceiveData”函数,因此对于第一帧,它将在数组 [0] 中发送您的命令和从从站接收一帧很好,但之后它只会将源缓冲区增加一以发送下一帧,在这种情况下,您最终会出现数组溢出(因为您将数组大小声明为 1),所以一些未知帧将被发送到从机。要消除此数组溢出,请在使用“spiTransmitAndReceiveData”时尝试如下声明您的“命令”数组。

    uint16_t command[19];

    for(uint8_t k=0;k<19;k++)

    {

       if(k==0)

          command[k] =  RBPR;

       else

          command[k] =  0;

    }

     

    --

    Thanks,
    Jagadish.

  • Thanks for your detailed answer.

    But I think my problem description is not detailed enough.

    I want to read data from flash through SPI. 

    Firstly, I need to transfer a read command to flash, and then the flash will reply me the data.

    The first method is to use the function of spiTransmitAndReceiveData(). it works but in consideration of other things, I need to replace the spiTransmitAndReceiveData() with spiTransmitData() and spiReceiveData(). Because the first frame of replied by flash is useless.

    The question is that I called spiReceiveData() after I called spiTransmitData() and couldn't receive the data.

  • Hi,

    I understood why it is not working when you are using spiTransmitData() and spiReceiveData() methods instead of single spiTransmitAndReceiveData() method.

    If you see your attached timing diagram, command and receive data everything should be done in single chip select active, that will exactly happening with spiTransmitAndReceiveData() method.

    But if you use spiTransmitData() and spiReceiveData() methods then chip select will toggle in between the methods because 

    If you see the spiTransmitData() method then at the end of the last frame transfer the CSHOLD bit will gets clear to deactivate the Chip select

    So in your case after you sent command using spiTransmitData(), the chip select will be deactivate after the command frame. And again it will get activate after calling spiReceiveData(). But this should not happen right, once you deactivate the chip select in between the transfer then the slave may go to idle state again right. But this is not the case with spiTransmitAndReceiveData(), here Chip select get activate before transmitting command and it will be continue to be in active still last byte from slave has been received.

    I hope this clarifies your doubt.

    --

    Thanks,
    Jagadish.