Problems with SPI slave mode code - Page 1
Forum section: electronics / general

author: perdrix
author_rank: Frequent Contributor
timestamp: August 25, 2021, 05:40:18 pm
content: 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 DWhat I see in the LA trace is:Code:Time ,Packet ID,MOSI,MISO0.034181000000000,0,0xEAEA,0xDFC50.034562000000000,0,0xEAEA,0xDFC50.096511000000000,0,0xEAEA,0xC1ED0.096893000000000,0,0xEAEA,0xC1ED0.159001000000000,0,0xEAEA,0x80060.159382000000000,0,0xEAEA,0x80060.221522000000000,0,0xEAEA,0x8A090.221903000000000,0,0xEAEA,0x8A090.284139000000000,0,0xEAEA,0xC1230.284520000000000,0,0xEAEA,0xC1230.346660000000000,0,0xEAEA,0x80160.347041000000000,0,0xEAEA,0x80160.409213000000000,0,0xEAEA,0x8A220.409594000000000,0,0xEAEA,0x8A22Which is not quite as I expect!My code looks like:Code:bool T1MessageSet = false;in main loop:if (_SPIRBF) SPI_Received = SPI1BUF; // Read SPI input into bufferif (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 welcomeThanksDavid

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 25, 2021, 06:55:31 pm
content: It's hard to guess with so little info, but I think switch can only take literals:Code:case Request_Info:case Request_Ack:SoRequest_Infomust 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:if (Request_Info && !T1MessageSent)Which would translate as:Code:if (true && !T1MessageSent)orif (false && !T1MessageSent)It looks like you'd want to do this:Code:if (SPI_Received==Request_Info && !T1MessageSent)

author: perdrix
author_rank: Frequent Contributor
timestamp: August 25, 2021, 08:17:03 pm
content: 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 messageI should have typed if (Request_Info == request && !T1MessageSent)and//if (0x8001 == SPI_Received ||(Request_Info == request && T1MessageSent))SorryDavid