Author Topic: Problems with SPI slave mode code  (Read 1416 times)

0 Members and 1 Guest are viewing this topic.

Offline perdrixTopic starter

  • Frequent Contributor
  • **
  • Posts: 856
  • Country: gb
Problems with SPI slave mode code
« on: August 25, 2021, 05:40:18 pm »
I'm trying to implement a protocol defined like this:

The Reply T1 and Reply T2 messages are sent one after the other to the master processor in response to a pair of poll messages (0xEAEA, 0xEAEA), OR a single poll followed by an acknowledgement (0xEAEA, 0x8001).   The reply to the first poll message will the T1 reply.    If the T1 reply indicates a specific condition (bit 10 on), then the second message from the master will be an acknowledgement instead of a poll.   The response to the second message of the pair sent by the master will a T2 type of reply.

The T1 message hi nibble is 0x8 while the T2 message hi nibble is C or D

What I see in the LA trace is:

Code: [Select]
Time [s],Packet ID,MOSI,MISO
0.034181000000000,0,0xEAEA,0xDFC5
0.034562000000000,0,0xEAEA,0xDFC5
0.096511000000000,0,0xEAEA,0xC1ED
0.096893000000000,0,0xEAEA,0xC1ED
0.159001000000000,0,0xEAEA,0x8006
0.159382000000000,0,0xEAEA,0x8006
0.221522000000000,0,0xEAEA,0x8A09
0.221903000000000,0,0xEAEA,0x8A09
0.284139000000000,0,0xEAEA,0xC123
0.284520000000000,0,0xEAEA,0xC123
0.346660000000000,0,0xEAEA,0x8016
0.347041000000000,0,0xEAEA,0x8016
0.409213000000000,0,0xEAEA,0x8A22
0.409594000000000,0,0xEAEA,0x8A22

Which is not quite as I expect!

My code looks like:

Code: [Select]
bool T1MessageSet = false;

in main loop:

        if (_SPIRBF) SPI_Received = SPI1BUF;     // Read SPI input into buffer


        if (0 != SPI_Received && !_SPITBF)
        {
            request = SPI_Received & 0xF000;
            value = SPI_Received & Request_Data_Mask;
            switch (request)
            {
                :
                : many cases not shown
                :
                //
                // A poll request (0xEAEA) or a Stall acknowledgement
                //
                case Request_Info:
                case Request_Ack:
                    if (Request_Info == request && !T1MessageSent)
                    {
                        //
                        // Always send the Type 1 message in reply to 1st poll
                        //
                        reply = T1Data;
                        T1MessageSent = true;
                    }
                    else
                    {
                        //
                        // Check if we have a stall acknowledgement
                        //
                        if (0x8001 == SPI_Received) Stall_Ack = true;

                        //
                        // Message should be a poll or a stall acknowledgement
                        //
                        if (0x8001 == SPI_Received ||
                            (Request_Info == request && T1MessageSent))
                        {
                            //
                            reply = T2Data;
                        }
                        else reply = 0;
                        T1MessageSent = false;
                    }
                    break;

                default:
                    break;
            }
            SPI1BUF = reply;
        }

Clearly there's a an error in there somewhere, but for the life of me I cannot see what it is!

Any help most welcome

Thanks
David
« Last Edit: August 25, 2021, 08:18:18 pm by perdrix »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6936
  • Country: es
Re: Problems with SPI slave mode code
« Reply #1 on: August 25, 2021, 06:55:31 pm »
It's hard to guess with so little info, but I think switch can only take literals:
Code: [Select]
case Request_Info:
case Request_Ack:

So Request_Info  must be a literal defined somewhere.
And here you're using it in the conditional without any logic function, it would alway be true or false depending on the defined value.
Code: [Select]
if (Request_Info && !T1MessageSent)
Which would translate as:
Code: [Select]
if (true && !T1MessageSent)
or
if (false && !T1MessageSent)

It looks like you'd want to do this:
Code: [Select]
if (SPI_Received==Request_Info && !T1MessageSent)
« Last Edit: August 25, 2021, 07:06:54 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline perdrixTopic starter

  • Frequent Contributor
  • **
  • Posts: 856
  • Country: gb
Re: Problems with SPI slave mode code
« Reply #2 on: August 25, 2021, 08:17:03 pm »
Sorry a transcription error I was taking the original code and simplifying it minimising what I showed and made an error - I'll correct the original message

I should have typed if (Request_Info == request && !T1MessageSent)

and
                        //
                        if (0x8001 == SPI_Received ||
                            (Request_Info == request && T1MessageSent))
         

Sorry
David
« Last Edit: August 25, 2021, 08:20:00 pm by perdrix »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->