Go Down

### Topic: Can someone help me figure out what this loop is doing? (Read 734 times)previous topic - next topic

#### scswift

##### Jan 06, 2013, 06:39 pm
Code: [Select]
`  // send CRC  uint8_t crc = 0XFF;  if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0  if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA  spiSend(crc);    // wait for response  for (uint8_t retry = 0; ((r1 = spiRec()) & 0X80) && retry != 0XFF; retry++);`

The bit there where it waits for a response is confusing me.

If I understand this correctly, a byte is read from the SD card into R1.  R1 is then ANDed with 0x80.  So it appears to be looking to see if a particular bit is set in the reply.  If no bit is set, then that side of the equation evaluates to false.

Then retry is compared to 0xFF.  If it isn't 0xFF, then it evaluates to true, and the loop continues.  This makes sense.  It's trying 255 times to get the response its looking for.

But it's the first part that doesn't make sense to me.  If it's waiting for a response from the card, why does it continue only if a particular bit is not set?  Wouldn't a card that is not responding return 0?  And wouldn't 0 & 0x80 evaluate to false, and cause the loop to exit prematurely?

#### Msquare

#1
##### Jan 06, 2013, 07:27 pm
A loop finishes on a false in the test condition. So it is looping until either 0x80 bit is false or it has tried 255 times. (As the && will be false if either term is false.)

#### marco_c

#2
##### Jan 06, 2013, 08:25 pmLast Edit: Jan 07, 2013, 08:24 pm by marco_c Reason: 1
Quote
Wouldn't a card that is not responding return 0?

Not necessarily. It depends on the specification for the card. It could also be that the programmer wanted to make sure that he was looking for the top bit and not just a non-zero value, which could be noise on the comms line, for example.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### Tom Carpenter

#3
##### Jan 06, 2013, 08:44 pm
If the card did return 0, then you would get:

0x00 & 0x80 = 0 = false, which means the loop would be broken.
~Tom~

#### scswift

#4
##### Jan 07, 2013, 07:31 pm

If the card did return 0, then you would get:

0x00 & 0x80 = 0 = false, which means the loop would be broken.

That's why I'm asking.  I would expect the card to return 0 if it's not responding.  But maybe it returns 0xFF instead, until it responds, and then it responds with that particular bit not being set.

Anyway I guess this code was not what was broken because I found the issue elsewhere.

#### robtillaart

#5
##### Jan 07, 2013, 08:11 pm
0xFF is also -1  a very common error value ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### scswift

#6
##### Jan 07, 2013, 08:18 pm
Well I was thinking more along the lines of the data line just being stuck high while the card isn't responding.

Go Up

Please enter a valid email to subscribe