Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of RAJANGAM_REVIEW_BAE_CODE by
crc.h@20:949d13045431, 2016-07-01 (annotated)
- Committer:
 - lakshya
 - Date:
 - Fri Jul 01 17:55:30 2016 +0000
 - Revision:
 - 20:949d13045431
 - Parent:
 - 19:79e69017c855
 
BAE final 1.0 (1st july); ; BCN and EPS code to be upgraded; +; testing and troubleshooting to be done ; +; watchdog to be implemented; ; comparing with flowcharts.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| sakthipriya | 3:07e15677a75c | 1 | //EDITS | 
| sakthipriya | 3:07e15677a75c | 2 | //changed the initial remainder from 0x0000 to 0xffff according to the standards | 
| sakthipriya | 3:07e15677a75c | 3 | //made two seperate functions crc16_gen and crc8_gen | 
| sakthipriya | 3:07e15677a75c | 4 | |
| sakthipriya | 3:07e15677a75c | 5 | #define TOPBIT16 (1 << 15) | 
| sakthipriya | 3:07e15677a75c | 6 | #define TOPBIT8 (1 << 7) | 
| sakthipriya | 3:07e15677a75c | 7 | #define POLYNOMIAL16 0x1021 | 
| sakthipriya | 3:07e15677a75c | 8 | #define POLYNOMIAL8 0xEA | 
| sakthipriya | 3:07e15677a75c | 9 | |
| lakshya | 19:79e69017c855 | 10 | namespace CRC | 
| lakshya | 19:79e69017c855 | 11 | { | 
| lakshya | 20:949d13045431 | 12 | typedef uint16_t crctype16; | 
| lakshya | 19:79e69017c855 | 13 | crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes) | 
| lakshya | 19:79e69017c855 | 14 | { | 
| lakshya | 19:79e69017c855 | 15 | crctype16 remainder = 0xffff; | 
| lakshya | 19:79e69017c855 | 16 | int byte; | 
| lakshya | 19:79e69017c855 | 17 | char bit; | 
| sakthipriya | 3:07e15677a75c | 18 | |
| lakshya | 19:79e69017c855 | 19 | for( byte = 0 ; byte < nBytes ; byte++ ) | 
| lakshya | 19:79e69017c855 | 20 | { | 
| lakshya | 19:79e69017c855 | 21 | /* | 
| lakshya | 19:79e69017c855 | 22 | Bring the data byte by byte | 
| lakshya | 19:79e69017c855 | 23 | each time only one byte is brought | 
| lakshya | 19:79e69017c855 | 24 | 0 xor x = x | 
| lakshya | 19:79e69017c855 | 25 | */ | 
| lakshya | 19:79e69017c855 | 26 | remainder = remainder ^ ( message[byte] << 8 ); | 
| sakthipriya | 3:07e15677a75c | 27 | |
| lakshya | 19:79e69017c855 | 28 | for( bit = 8 ; bit > 0 ; bit--) | 
| lakshya | 19:79e69017c855 | 29 | { | 
| lakshya | 19:79e69017c855 | 30 | /* | 
| lakshya | 19:79e69017c855 | 31 | for each bit, xor the remainder with polynomial | 
| lakshya | 19:79e69017c855 | 32 | if the MSB is 1 | 
| lakshya | 19:79e69017c855 | 33 | */ | 
| lakshya | 19:79e69017c855 | 34 | if(remainder & TOPBIT16) | 
| lakshya | 19:79e69017c855 | 35 | { | 
| lakshya | 19:79e69017c855 | 36 | remainder = (remainder << 1) ^ POLYNOMIAL16; | 
| lakshya | 19:79e69017c855 | 37 | /* | 
| lakshya | 19:79e69017c855 | 38 | each time the remainder is xor-ed with polynomial, the MSB is made zero | 
| lakshya | 19:79e69017c855 | 39 | hence the first digit of the remainder is ignored in the loop | 
| lakshya | 19:79e69017c855 | 40 | */ | 
| lakshya | 19:79e69017c855 | 41 | } | 
| lakshya | 19:79e69017c855 | 42 | else | 
| lakshya | 19:79e69017c855 | 43 | { | 
| lakshya | 19:79e69017c855 | 44 | remainder = (remainder << 1); | 
| lakshya | 19:79e69017c855 | 45 | } | 
| lakshya | 19:79e69017c855 | 46 | } | 
| sakthipriya | 3:07e15677a75c | 47 | } | 
| lakshya | 19:79e69017c855 | 48 | |
| lakshya | 19:79e69017c855 | 49 | return remainder; | 
| sakthipriya | 3:07e15677a75c | 50 | } | 
| lakshya | 20:949d13045431 | 51 | |
| lakshya | 20:949d13045431 | 52 | |
| sakthipriya | 3:07e15677a75c | 53 | typedef uint8_t crctype8; | 
| lakshya | 19:79e69017c855 | 54 | crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes) | 
| lakshya | 19:79e69017c855 | 55 | { | 
| lakshya | 19:79e69017c855 | 56 | crctype8 remainder = 0xff; | 
| sakthipriya | 3:07e15677a75c | 57 | |
| lakshya | 19:79e69017c855 | 58 | for(int byte = 0 ; byte < nBytes ; byte++ ) | 
| lakshya | 19:79e69017c855 | 59 | { | 
| lakshya | 19:79e69017c855 | 60 | /* | 
| lakshya | 19:79e69017c855 | 61 | Bring the data byte by byte | 
| lakshya | 19:79e69017c855 | 62 | each time only one byte is brought | 
| lakshya | 19:79e69017c855 | 63 | 0 xor x = x | 
| lakshya | 19:79e69017c855 | 64 | */ | 
| lakshya | 19:79e69017c855 | 65 | remainder = remainder ^ ( message[byte] ); | 
| sakthipriya | 3:07e15677a75c | 66 | |
| lakshya | 19:79e69017c855 | 67 | for(int bit = 8 ; bit > 0 ; bit--) | 
| lakshya | 19:79e69017c855 | 68 | { | 
| lakshya | 19:79e69017c855 | 69 | /* | 
| lakshya | 19:79e69017c855 | 70 | for each bit, xor the remainder with polynomial | 
| lakshya | 19:79e69017c855 | 71 | if the MSB is 1 | 
| lakshya | 19:79e69017c855 | 72 | */ | 
| lakshya | 19:79e69017c855 | 73 | if(remainder & TOPBIT8) | 
| lakshya | 19:79e69017c855 | 74 | { | 
| lakshya | 19:79e69017c855 | 75 | remainder = (remainder << 1) ^ POLYNOMIAL8; | 
| lakshya | 19:79e69017c855 | 76 | /* | 
| lakshya | 19:79e69017c855 | 77 | each time the remainder is xor-ed with polynomial, the MSB is made zero | 
| lakshya | 19:79e69017c855 | 78 | hence the first digit of the remainder is ignored in the loop | 
| lakshya | 19:79e69017c855 | 79 | */ | 
| lakshya | 19:79e69017c855 | 80 | } | 
| lakshya | 19:79e69017c855 | 81 | else | 
| lakshya | 19:79e69017c855 | 82 | { | 
| lakshya | 19:79e69017c855 | 83 | remainder = (remainder << 1); | 
| lakshya | 19:79e69017c855 | 84 | } | 
| lakshya | 19:79e69017c855 | 85 | } | 
| sakthipriya | 3:07e15677a75c | 86 | } | 
| lakshya | 19:79e69017c855 | 87 | |
| lakshya | 19:79e69017c855 | 88 | return remainder; | 
| sakthipriya | 3:07e15677a75c | 89 | } | 
| sakthipriya | 3:07e15677a75c | 90 | } | 
