![](/media/cache/group/mbed-logo-square-512_3.png.50x50_q85.png)
CRC example to compute 32-bit CRC .
main.cpp@0:4ea3a9a52bb6, 2017-12-18 (annotated)
- Committer:
- deepikabhavnani
- Date:
- Mon Dec 18 22:53:07 2017 +0000
- Revision:
- 0:4ea3a9a52bb6
- Child:
- 1:6f792125397a
Fast and Slow CRC example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
deepikabhavnani | 0:4ea3a9a52bb6 | 1 | #include "mbed.h" |
deepikabhavnani | 0:4ea3a9a52bb6 | 2 | #include "mbed_crc.h" |
deepikabhavnani | 0:4ea3a9a52bb6 | 3 | #include "SlowCRC.h" |
deepikabhavnani | 0:4ea3a9a52bb6 | 4 | #include "FastCRC.h" |
deepikabhavnani | 0:4ea3a9a52bb6 | 5 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 6 | #define FAST_COMPUTE 1 |
deepikabhavnani | 0:4ea3a9a52bb6 | 7 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 8 | #if defined(FAST_COMPUTE) |
deepikabhavnani | 0:4ea3a9a52bb6 | 9 | #define CRC_TYPE FastCRC |
deepikabhavnani | 0:4ea3a9a52bb6 | 10 | #else |
deepikabhavnani | 0:4ea3a9a52bb6 | 11 | #define CRC_TYPE SlowCRC |
deepikabhavnani | 0:4ea3a9a52bb6 | 12 | #endif |
deepikabhavnani | 0:4ea3a9a52bb6 | 13 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 14 | char test[] = "123456789"; |
deepikabhavnani | 0:4ea3a9a52bb6 | 15 | uint32_t crc; |
deepikabhavnani | 0:4ea3a9a52bb6 | 16 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 17 | int crc_ibm_16bit() { |
deepikabhavnani | 0:4ea3a9a52bb6 | 18 | CRC_TYPE<uint16_t> ct(CRC_16BIT_IBM); |
deepikabhavnani | 0:4ea3a9a52bb6 | 19 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 20 | ct.init(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 21 | ct.compute((void *)test, strlen((const char*)test), &crc); |
deepikabhavnani | 0:4ea3a9a52bb6 | 22 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 23 | printf("The CRC of 0x%x \"123456789\" is \"0xBB3D\" Result: 0x%x\n", |
deepikabhavnani | 0:4ea3a9a52bb6 | 24 | ct.get_polynomial(), crc); |
deepikabhavnani | 0:4ea3a9a52bb6 | 25 | ct.deinit(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 26 | return 0; |
deepikabhavnani | 0:4ea3a9a52bb6 | 27 | } |
deepikabhavnani | 0:4ea3a9a52bb6 | 28 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 29 | int crc_ccitt_16bit() { |
deepikabhavnani | 0:4ea3a9a52bb6 | 30 | CRC_TYPE<uint16_t> ct(CRC_16BIT_CCITT); |
deepikabhavnani | 0:4ea3a9a52bb6 | 31 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 32 | ct.init(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 33 | ct.compute((void *)test, strlen((const char*)test), &crc); |
deepikabhavnani | 0:4ea3a9a52bb6 | 34 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 35 | printf("The CRC of 0x%x \"123456789\" is \"0x29B1\" Result: 0x%x\n", |
deepikabhavnani | 0:4ea3a9a52bb6 | 36 | ct.get_polynomial(), crc); |
deepikabhavnani | 0:4ea3a9a52bb6 | 37 | ct.deinit(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 38 | return 0; |
deepikabhavnani | 0:4ea3a9a52bb6 | 39 | } |
deepikabhavnani | 0:4ea3a9a52bb6 | 40 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 41 | int crc_32_bit() { |
deepikabhavnani | 0:4ea3a9a52bb6 | 42 | CRC_TYPE<uint32_t> ct(CRC_32BIT); |
deepikabhavnani | 0:4ea3a9a52bb6 | 43 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 44 | ct.init(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 45 | ct.compute((void *)test, strlen((const char*)test), &crc); |
deepikabhavnani | 0:4ea3a9a52bb6 | 46 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 47 | printf("The CRC of 0x%x \"123456789\" is \"0xCBF43926\" Result: 0x%x\n", |
deepikabhavnani | 0:4ea3a9a52bb6 | 48 | ct.get_polynomial(), crc); |
deepikabhavnani | 0:4ea3a9a52bb6 | 49 | ct.deinit(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 50 | return 0; |
deepikabhavnani | 0:4ea3a9a52bb6 | 51 | } |
deepikabhavnani | 0:4ea3a9a52bb6 | 52 | |
deepikabhavnani | 0:4ea3a9a52bb6 | 53 | int main() { |
deepikabhavnani | 0:4ea3a9a52bb6 | 54 | crc_ibm_16bit(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 55 | crc_ccitt_16bit(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 56 | crc_32_bit(); |
deepikabhavnani | 0:4ea3a9a52bb6 | 57 | return 0; |
deepikabhavnani | 0:4ea3a9a52bb6 | 58 | } |