cancel
Showing results for 
Search instead for 
Did you mean: 

Cube STM32F411RE Demo - SPI CRC Calculation?

tonofsteel
Associate II
Posted on November 04, 2015 at 05:27

Started using the Cube/HAL libraries and working with the F411RE demonstration with the Adafruit 1.8'' TFT shield.  It has a SD card slot on it and uses it to load images onto the display.  Not sure if ST doesn't realize that it is 2015 and finding SD cards (vs SDHC SDXC) is nearly impossible.  So of course all of my cards are actually SDHC which do not work with the included libs.  So saying that the Cube libs are the newest and greatest is a bit of a stretch here.

Anyways while going through the code I see the command CMD1 which is 0x40 0x00 0x00 0x00 0x00 and it sends the CRC value of 0x95

I have used every online calculator that I can find and I get 0xC8 for the CRC of the 5 command bytes.

How is 0x95 arrived at?  I have searched for SPI CRC and I cannot find anything that would shed any light on this.  I can go through the descriptions for CRC calculations and calculate it but 0x95 is never the value that comes out.

How the heck does this work?

And since the included SD/FAT library blows is there any library out there that can work with SD/SDHC/SDXC FAT/FAT16/FAT32?  I found a couple of home brewed ones but they have not been updated in quite awhile.
2 REPLIES 2
tonofsteel
Associate II
Posted on November 04, 2015 at 14:00

I think I may have found my answer.  According to this site:

http://wiki.seabright.co.nz/wiki/SdCardProtocol.html

The key pieces of information:

The CRC is a 7 bit CRC with polynomial x^7 + x^3 + 1

Note that the final CRC must be ANDed with 0x7F

There appears to be a default CRC polynomial that is commonly used (of course not the same as what SPI uses), with quite a few online calculators giving the same results.

I know ideally I could place a ''knowledge of the world'' book under my pillow at night and by osmosis ''just know'' all these details, but you tools posting blogs/forum posts/info about this stuff should really point out the above two facts when trying to explain SPI commands and CRC's.  I think this was about page 100 that I read about this before finding out the details. 

And yes I probably could have just dug into the SPI standard but I have more pages of standards here for everything I deal with than I do pages/day reading capacity before I die.  ''Wise old guy / been there done that'' summaries and tips are what I go to the net for.

Although on second thought I don't recall anyone ever discussing this, they just know the command and have something along the lines of the following:

Send 6-byte CMD0 command “40 00 00 00 00 95� to put the card in SPI mode

So it is quite plausible that many posting about this don't even know how this works either....... (Would be even more so if the other tip I read about only SPI commands needing a CRC is true, most of these CMD sequences seem to be hard coded into the libraries that I have seen)

Posted on November 04, 2015 at 15:42

//****************************************************************************
// MMC/SD 7-bit CRC test - sourcer32@gmail.com
//****************************************************************************
#include <
windows.h
>
#include <
stdio.h
>
#include <
stdlib.h
>
// MMC/SD polynomial x^7 + x^3 + 1 -> 1<<3+1 = 9
unsigned char crcbyte(unsigned char crc, unsigned char byte)
{
int i;
crc ^= byte;
for(i=0; i<8; i++)
if (crc & 0x80)
crc = (crc << 1) ^ 0x12; // (0x09 << 1) 7-bit poly, in high order bits
else
crc <<= 1; // left shifting
return(crc);
}
int main(int argc, char **argv)
{
int i, j;
unsigned char crc;
unsigned char test[] = { 0x40, 0x00, 0x00, 0x00, 0x00 }; // 0x95
// unsigned char test[] = { 0x77, 0x00, 0x00, 0x00, 0x00 }; // 0x65
crc = 0;
for(i=0; i<5; i++)
crc = crcbyte(crc, test[i]);
printf(''%02
X'', crc | 1); // 7-bit crc in high-order, low order bit stop
return(1);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..