CRC example to compute 32-bit CRC .

main.cpp

Committer:
deepikabhavnani
Date:
2017-12-18
Revision:
0:4ea3a9a52bb6
Child:
1:6f792125397a

File content as of revision 0:4ea3a9a52bb6:

#include "mbed.h"
#include "mbed_crc.h"
#include "SlowCRC.h"
#include "FastCRC.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 main() {
    crc_ibm_16bit();
    crc_ccitt_16bit();
    crc_32_bit();
    return 0;
}