Skip to content

Instantly share code, notes, and snippets.

@deepikabhavnani
Last active December 19, 2017 22:38
Show Gist options
  • Save deepikabhavnani/0dacf5572c0b4d87b8fbfbcfd9246a95 to your computer and use it in GitHub Desktop.
Save deepikabhavnani/0dacf5572c0b4d87b8fbfbcfd9246a95 to your computer and use it in GitHub Desktop.
CRC _ Test
#include "mbed.h"
#include "mbed_crc.h"
#include "SlowCRC.h"
#include "FastCRC.h"
#include "HalfByteCRC.h"
#define FAST_COMPUTE 1
#if defined(FAST_COMPUTE)
#define CRC_TYPE FastCRC
#else
#define CRC_TYPE SlowCRC
#endif
char test[] = "123456789";
uint32_t crc;
int crc_ibm_16bit() {
CRC_TYPE<uint16_t> ct(CRC_16BIT_IBM);
ct.init();
ct.compute((void *)test, strlen((const char*)test), &crc);
printf("The CRC of 0x%x \"123456789\" is \"0xBB3D\" Result: 0x%x\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int crc_ccitt_16bit() {
CRC_TYPE<uint16_t> ct(CRC_16BIT_CCITT);
ct.init();
ct.compute((void *)test, strlen((const char*)test), &crc);
printf("The CRC of 0x%x \"123456789\" is \"0x29B1\" Result: 0x%x\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int crc_32_bit() {
CRC_TYPE<uint32_t> ct(CRC_32BIT);
ct.init();
ct.compute((void *)test, strlen((const char*)test), &crc);
printf("The CRC of 0x%x \"123456789\" is \"0xCBF43926\" Result: 0x%x\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int crc_sd_7bit() {
CRC_TYPE<uint8_t> ct(CRC_7BIT_SD);
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 ) + 1;
printf("The CRC of 0x%x \"CMD0\" is \"0x95\" Result: 0x%x\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 ) + 1;
printf("The CRC of 0x%x \"CMD8\" is \"0x87\" Result: 0x%x\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 ) + 1;
printf("The CRC of 0x%x \"CMD17\" is \"0x55\" Result: 0x%x\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int crc_sd_16bit() {
char test[512];
uint32_t crc;
CRC_TYPE<uint16_t> sd(CRC_16BIT_SD);
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%x\n", crc);
sd.deinit();
return 0;
}
int crc_32_half_byte() {
HalfByteCRC ct;
ct.init();
ct.compute((void *)test, strlen((const char*)test), &crc);
printf("The Half-byte CRC of 0x%x \"123456789\" is \"0xCBF43926\" Result: 0x%x\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int partial_crc_16() {
CRC_TYPE<uint16_t> ct(CRC_16BIT_CCITT);
ct.init();
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 Partial CRC16 of 0x%x \"123456789\" is \"0x29B1\" Result: 0x%x\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int partial_crc_32() {
HalfByteCRC ct;
ct.init();
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 Partial CRC32 of 0x%x \"123456789\" is \"0xCBF43926\" Result: 0x%x\n",
ct.get_polynomial(), crc);
ct.deinit();
return 0;
}
int main() {
crc_ibm_16bit();
crc_ccitt_16bit();
crc_32_bit();
crc_32_half_byte();
partial_crc_16();
partial_crc_32();
crc_sd_16bit();
crc_sd_7bit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment