Skip to content

Instantly share code, notes, and snippets.

@deepikabhavnani
Last active January 24, 2018 19:31
Show Gist options
  • Save deepikabhavnani/44737819d512c86de49ceeabf8867ef7 to your computer and use it in GitHub Desktop.
Save deepikabhavnani/44737819d512c86de49ceeabf8867ef7 to your computer and use it in GitHub Desktop.
New CRC class example
#include "mbed.h"
#include "MbedCRC.h"
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
int crc_sd_7bit() {
MbedCRC<POLY_7BIT_SD, 7> ct;
char test[6];
uint32_t crc;
ct.init();
test[0] = 0x40;
test[1] = 0x00;
test[2] = 0x00;
test[3] = 0x00;
test[4] = 0x00;
ct.compute((void *)test, 5, &crc);
// CRC 7-bit as 8-bit data
crc = crc + 1;
printf("The CRC of 0x%lx \"CMD0\" is \"0x95\" Result: 0x%lx\n",
ct.get_polynomial(), crc);
test[0] = 0x48;
test[1] = 0x00;
test[2] = 0x00;
test[3] = 0x01;
test[4] = 0xAA;
ct.compute((void *)test, 5, &crc);
// CRC 7-bit as 8-bit data
crc = crc + 1;
printf("The CRC of 0x%lx \"CMD8\" is \"0x87\" Result: 0x%lx\n",
ct.get_polynomial(), crc);
test[0] = 0x51;
test[1] = 0x00;
test[2] = 0x00;
test[3] = 0x00;
test[4] = 0x00;
ct.compute((void *)test, 5, &crc);
// CRC 7-bit as 8-bit data
crc = crc + 1;
printf("The CRC of 0x%lx \"CMD17\" is \"0x55\" Result: 0x%lx\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int crc_sd_16bit() {
char test[512];
uint32_t crc;
MbedCRC<POLY_16BIT_CCITT, 16> sd(0, 0, false, false);
memset(test, 0xFF, 512);
// 512 bytes with 0xFF data --> CRC16 = 0x7FA1
sd.init();
sd.compute((void *)test, 512, &crc);
printf("The 16BIT SD CRC of 512 bytes with 0xFF data is \"0x7FA1\" Result: 0x%lx\n", crc);
sd.deinit();
return 0;
}
int crc32_sample() {
MbedCRC<POLY_32BIT_ANSI, 32> ct;
char test[] = "123456789";
uint32_t crc = 0;
ct.init();
ct.compute((void *)test, strlen((const char*)test), &crc);
printf("\nPolynomial = 0x%lx Width = %d \n", ct.get_polynomial(), ct.get_width());
printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
ct.compute_partial_start(&crc);
ct.compute_partial((void *)&test, 4, &crc);
ct.compute_partial((void *)&test[4], 5, &crc);
ct.compute_partial_stop(&crc);
printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
ct.deinit();
return 0;
}
int main() {
crc_sd_7bit();
crc_sd_16bit();
crc32_sample();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment