0
\$\begingroup\$

I want to do SPI communication using the MAX31856.

I am using TMS320F28335 as MASTER.

I think there is no problem with the connection between MASTER and SLAVE.

0x0c is transmitted in the first byte and 0x00 is transmitted in the remaining 3 bytes.

enter image description here The problem is that the received value is not steady, but is displayed as a number 128 or 255.

Is there anything wrong with my code?

I think I send (Tx) 4 bytes.

Below is the code I wrote.

    while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
    TxData[0]=0x0c;
    SpiaRegs.SPITXBUF = (TxData[0]<<8);

    while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
    TxData[1]=0x00; //dummy byte
    SpiaRegs.SPITXBUF = TxData[1];

    while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
    TxData[2]=0x00;
    SpiaRegs.SPITXBUF = TxData[2];

    while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
    TxData[3]=0x00;
    SpiaRegs.SPITXBUF = TxData[3];

    for(i=0;i<4;i++)
    {
        RxData[i] = SpiaRegs.SPIRXBUF;
    }

Any help would be appreciated.

\$\endgroup\$
5
  • \$\begingroup\$ semicolon at the end of while() results in a null statement, assuming this is C. \$\endgroup\$
    – Syed
    Nov 30, 2021 at 2:30
  • 2
    \$\begingroup\$ @Syed, That does not sound right. I do not remember anywhere in K&R about ";" nullifies a statement. \$\endgroup\$
    – jay
    Nov 30, 2021 at 3:58
  • \$\begingroup\$ I see, you are checking a flag in an empty(null) loop. Thanks for the feedback. \$\endgroup\$
    – Syed
    Nov 30, 2021 at 4:34
  • \$\begingroup\$ @Syed so.. "while(a_flag);" is empty as in "while(a_flag){null};" ? Dang, I wrote uncountable lines of codes with that... Lucky, no one noticed it,, and kept my job,.. still doing... and will continue. \$\endgroup\$
    – jay
    Nov 30, 2021 at 4:42
  • \$\begingroup\$ @Syed , I figured out what you are talking about. So, your code "while(something);" was OPTIMIZED OUT. You need to use "while(volatiled-anything);", or set it to " pragma __NO_OPTIMIZATION", and similar. \$\endgroup\$
    – jay
    Nov 30, 2021 at 16:03

1 Answer 1

1
\$\begingroup\$

I need to read the TMS320F28335 data sheet, and the library API, in order to get the answer right.


Added per @Codo 's suggestion

SPI RX is received and buffered by the same clock as the TX, which is driven by the Master. There are multiple factors that decides how soon the last RX bit comes in the buffer. And that supposed to be very close to the TX-empty.
Since, you are checking the TX-gone flag, I assume the SPI channel is not running in queued/buffered/DMA mode. Thus, checking RX-received flag assures new data. Meantime, RX data has to be fetched before overridden by the following cycle.

End add


Try this:

#define __RX_EMPTY_FLAG FIND_THE_FLAG_BIT_IN_THE_DTASHEET

int i = 0;

while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
TxData[0]=0x0c;
SpiaRegs.SPITXBUF = (TxData[0]<<8);
while(SpiaRegs.SPISTS.bit.__RX_EMPTY_FLAG);
RxData[i++] = SpiaRegs.SPIRXBUF;

while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
TxData[1]=0x00; //dummy byte
SpiaRegs.SPITXBUF = TxData[1];
while(SpiaRegs.SPISTS.bit.__RX_EMPTY_FLAG);
RxData[i++] = SpiaRegs.SPIRXBUF;


while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
TxData[2]=0x00;
SpiaRegs.SPITXBUF = TxData[2];
while(SpiaRegs.SPISTS.bit.__RX_EMPTY_FLAG);
RxData[i++] = SpiaRegs.SPIRXBUF;


while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG);
TxData[3]=0x00;
SpiaRegs.SPITXBUF = TxData[3];
while(SpiaRegs.SPISTS.bit.__RX_EMPTY_FLAG);
RxData[i] = SpiaRegs.SPIRXBUF;
\$\endgroup\$
4
  • \$\begingroup\$ Hello thank you for the reply. Are you saying that the RX flag should be empty? Thank you again. \$\endgroup\$
    – shin
    Nov 30, 2021 at 9:03
  • \$\begingroup\$ @jay I think your answer addresses the cause. But it would profit considerably from an good explanation. \$\endgroup\$
    – Codo
    Nov 30, 2021 at 12:01
  • 1
    \$\begingroup\$ @Codo Good suggestion! Though I try to have OP think, ask, and learn by own-self, your suggestion would help revealing my mistakes, too. :-) \$\endgroup\$
    – jay
    Nov 30, 2021 at 15:43
  • \$\begingroup\$ @shin , RX flag should tell not-empty at the time you are reading it out, right? The code is assuming many other parts of the code is in working order. What I suggest only addresses the mistakes in the code you posted. It may not make everything work, since you likely have more than one problem. Read the microprocessor reference manual carefully, then read the ST micro library API documentation, then debug through. \$\endgroup\$
    – jay
    Nov 30, 2021 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.