A simple CRC library used to verify configuration data
Fork of CRC by
crc.cpp@3:599d283e5a44, 2016-10-03 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Mon Oct 03 09:45:59 2016 +0000
- Revision:
- 3:599d283e5a44
- Parent:
- 2:53b7db1f47ea
First commit for OSIO desk sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 3:599d283e5a44 | 1 | /** crc.cpp |
SomeRandomBloke | 3:599d283e5a44 | 2 | * |
SomeRandomBloke | 3:599d283e5a44 | 3 | * @license |
SomeRandomBloke | 3:599d283e5a44 | 4 | * MIT License |
SomeRandomBloke | 3:599d283e5a44 | 5 | * |
SomeRandomBloke | 3:599d283e5a44 | 6 | * Copyright (c) 2016 Andrew D Lindsay/Thing Innovations https://thinginnovations.uk |
SomeRandomBloke | 3:599d283e5a44 | 7 | * |
SomeRandomBloke | 3:599d283e5a44 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
SomeRandomBloke | 3:599d283e5a44 | 9 | * of this software and associated documentation files (the "Software"), to deal |
SomeRandomBloke | 3:599d283e5a44 | 10 | * in the Software without restriction, including without limitation the rights |
SomeRandomBloke | 3:599d283e5a44 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
SomeRandomBloke | 3:599d283e5a44 | 12 | * copies of the Software, and to permit persons to whom the Software is |
SomeRandomBloke | 3:599d283e5a44 | 13 | * furnished to do so, subject to the following conditions: |
SomeRandomBloke | 3:599d283e5a44 | 14 | * |
SomeRandomBloke | 3:599d283e5a44 | 15 | * The above copyright notice and this permission notice shall be included in all |
SomeRandomBloke | 3:599d283e5a44 | 16 | * copies or substantial portions of the Software. |
SomeRandomBloke | 3:599d283e5a44 | 17 | * |
SomeRandomBloke | 3:599d283e5a44 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
SomeRandomBloke | 3:599d283e5a44 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
SomeRandomBloke | 3:599d283e5a44 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
SomeRandomBloke | 3:599d283e5a44 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
SomeRandomBloke | 3:599d283e5a44 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
SomeRandomBloke | 3:599d283e5a44 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
SomeRandomBloke | 3:599d283e5a44 | 24 | * SOFTWARE. |
SomeRandomBloke | 3:599d283e5a44 | 25 | * |
SomeRandomBloke | 3:599d283e5a44 | 26 | */ |
sam_grove | 0:28e16d33040f | 27 | #define CRC32_POLYNOMIAL 0xEDB88320L |
sam_grove | 0:28e16d33040f | 28 | |
SomeRandomBloke | 2:53b7db1f47ea | 29 | void CRC32Value(unsigned long &CRCval, unsigned char c) |
sam_grove | 0:28e16d33040f | 30 | { |
sam_grove | 0:28e16d33040f | 31 | ///////////////////////////////////////////////////////////////////////////////////// |
sam_grove | 0:28e16d33040f | 32 | //CRC must be initialized as zero |
sam_grove | 0:28e16d33040f | 33 | //c is a character from the sequence that is used to form the CRC |
sam_grove | 0:28e16d33040f | 34 | //this code is a modification of the code from the Novatel OEM615 specification |
sam_grove | 0:28e16d33040f | 35 | ///////////////////////////////////////////////////////////////////////////////////// |
SomeRandomBloke | 2:53b7db1f47ea | 36 | unsigned long ulTemp1 = ( CRCval >> 8 ) & 0x00FFFFFFL; |
SomeRandomBloke | 2:53b7db1f47ea | 37 | unsigned long ulCRC = ((int) CRCval ^ c ) & 0xff ; |
sam_grove | 0:28e16d33040f | 38 | for (int j = 8 ; j > 0; j-- ) |
sam_grove | 0:28e16d33040f | 39 | { |
sam_grove | 0:28e16d33040f | 40 | if ( ulCRC & 1 ) |
sam_grove | 1:ec8513f94d23 | 41 | { |
sam_grove | 0:28e16d33040f | 42 | ulCRC = ( ulCRC >> 1 ) ^ CRC32_POLYNOMIAL; |
sam_grove | 1:ec8513f94d23 | 43 | } |
sam_grove | 0:28e16d33040f | 44 | else |
sam_grove | 1:ec8513f94d23 | 45 | { |
sam_grove | 0:28e16d33040f | 46 | ulCRC >>= 1; |
sam_grove | 1:ec8513f94d23 | 47 | } |
sam_grove | 0:28e16d33040f | 48 | } |
SomeRandomBloke | 2:53b7db1f47ea | 49 | CRCval = ulTemp1 ^ ulCRC; |
sam_grove | 0:28e16d33040f | 50 | } |
sam_grove | 0:28e16d33040f | 51 | |
sam_grove | 0:28e16d33040f | 52 | /* -------------------------------------------------------------------------- |
sam_grove | 0:28e16d33040f | 53 | Calculates the CRC-32 of a block of data all at once |
sam_grove | 0:28e16d33040f | 54 | //the CRC is from the complete message (header plus data) |
sam_grove | 0:28e16d33040f | 55 | //but excluding (of course) the CRC at the end |
sam_grove | 0:28e16d33040f | 56 | -------------------------------------------------------------------------- */ |
sam_grove | 0:28e16d33040f | 57 | unsigned long CalculateBlockCRC32( |
sam_grove | 0:28e16d33040f | 58 | unsigned long ulCount, /* Number of bytes in the data block */ |
sam_grove | 0:28e16d33040f | 59 | unsigned char *ucBuffer ) /* Data block */ |
sam_grove | 0:28e16d33040f | 60 | { |
sam_grove | 0:28e16d33040f | 61 | ////////////////////////////////////////////////////////////////////// |
sam_grove | 0:28e16d33040f | 62 | //the below code tests the CRC32Value procedure used in a markov form |
sam_grove | 0:28e16d33040f | 63 | ////////////////////////////////////////////////////////////////////// |
sam_grove | 0:28e16d33040f | 64 | unsigned long CRC = 0; |
sam_grove | 1:ec8513f94d23 | 65 | for (int i = 0; i<ulCount; i++) |
sam_grove | 1:ec8513f94d23 | 66 | { |
sam_grove | 1:ec8513f94d23 | 67 | CRC32Value( CRC, *ucBuffer++ ); |
sam_grove | 1:ec8513f94d23 | 68 | } |
sam_grove | 0:28e16d33040f | 69 | return CRC; |
sam_grove | 0:28e16d33040f | 70 | } |
sam_grove | 0:28e16d33040f | 71 | |
sam_grove | 0:28e16d33040f | 72 | /* |
sam_grove | 0:28e16d33040f | 73 | unsigned long CalculateBlockCRC32( |
sam_grove | 0:28e16d33040f | 74 | unsigned long ulCount, |
sam_grove | 0:28e16d33040f | 75 | unsigned char *ucBuffer ) |
sam_grove | 0:28e16d33040f | 76 | { |
sam_grove | 0:28e16d33040f | 77 | //////////////////////////////////////////// |
sam_grove | 0:28e16d33040f | 78 | //original code from the OEM615 manual |
sam_grove | 0:28e16d33040f | 79 | //////////////////////////////////////////// |
sam_grove | 0:28e16d33040f | 80 | unsigned long ulTemp1; |
sam_grove | 0:28e16d33040f | 81 | unsigned long ulTemp2; |
sam_grove | 0:28e16d33040f | 82 | unsigned long ulCRC = 0; |
sam_grove | 0:28e16d33040f | 83 | while ( ulCount-- != 0 ) |
sam_grove | 0:28e16d33040f | 84 | { |
sam_grove | 0:28e16d33040f | 85 | ulTemp1 = ( ulCRC >> 8 ) & 0x00FFFFFFL; |
sam_grove | 0:28e16d33040f | 86 | ulTemp2 = CRC32Value( ((int) ulCRC ^ *ucBuffer++ ) & 0xff ); |
sam_grove | 0:28e16d33040f | 87 | ulCRC = ulTemp1 ^ ulTemp2; |
sam_grove | 0:28e16d33040f | 88 | } |
sam_grove | 0:28e16d33040f | 89 | return( ulCRC ); |
sam_grove | 0:28e16d33040f | 90 | } |
sam_grove | 0:28e16d33040f | 91 | */ |