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.
Dependents: blinky_max32630fthr
features/storage/FEATURE_STORAGE/flash-journal/flash-journal-strategy-sequential/flash_journal_crc.c@3:1198227e6421, 2016-12-16 (annotated)
- Committer:
- switches
- Date:
- Fri Dec 16 16:27:57 2016 +0000
- Revision:
- 3:1198227e6421
- Parent:
- 0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| switches | 0:5c4d7b2438d3 | 1 | /********************************************************************** |
| switches | 0:5c4d7b2438d3 | 2 | * |
| switches | 0:5c4d7b2438d3 | 3 | * Filename: flash_journal_crc.c |
| switches | 0:5c4d7b2438d3 | 4 | * |
| switches | 0:5c4d7b2438d3 | 5 | * Description: Slow and fast implementations of the CRC standards. |
| switches | 0:5c4d7b2438d3 | 6 | * |
| switches | 0:5c4d7b2438d3 | 7 | * Notes: The parameters for each supported CRC standard are |
| switches | 0:5c4d7b2438d3 | 8 | * defined in the header file crc.h. The implementations |
| switches | 0:5c4d7b2438d3 | 9 | * here should stand up to further additions to that list. |
| switches | 0:5c4d7b2438d3 | 10 | * |
| switches | 0:5c4d7b2438d3 | 11 | * |
| switches | 0:5c4d7b2438d3 | 12 | * Copyright (c) 2000 by Michael Barr. This software is placed into |
| switches | 0:5c4d7b2438d3 | 13 | * the public domain and may be used for any purpose. However, this |
| switches | 0:5c4d7b2438d3 | 14 | * notice must not be changed or removed and no warranty is either |
| switches | 0:5c4d7b2438d3 | 15 | * expressed or implied by its publication or distribution. |
| switches | 0:5c4d7b2438d3 | 16 | **********************************************************************/ |
| switches | 0:5c4d7b2438d3 | 17 | |
| switches | 0:5c4d7b2438d3 | 18 | #include "flash-journal-strategy-sequential/flash_journal_crc.h" |
| switches | 0:5c4d7b2438d3 | 19 | |
| switches | 0:5c4d7b2438d3 | 20 | #define FALSE 0 |
| switches | 0:5c4d7b2438d3 | 21 | #define TRUE !FALSE |
| switches | 0:5c4d7b2438d3 | 22 | |
| switches | 0:5c4d7b2438d3 | 23 | #define CRC_NAME "CRC-32" |
| switches | 0:5c4d7b2438d3 | 24 | #define POLYNOMIAL 0x04C11DB7 |
| switches | 0:5c4d7b2438d3 | 25 | #define INITIAL_REMAINDER 0xFFFFFFFF |
| switches | 0:5c4d7b2438d3 | 26 | #define FINAL_XOR_VALUE 0xFFFFFFFF |
| switches | 0:5c4d7b2438d3 | 27 | #define REFLECT_DATA TRUE |
| switches | 0:5c4d7b2438d3 | 28 | #define REFLECT_REMAINDER TRUE |
| switches | 0:5c4d7b2438d3 | 29 | #define CHECK_VALUE 0xCBF43926 |
| switches | 0:5c4d7b2438d3 | 30 | |
| switches | 0:5c4d7b2438d3 | 31 | |
| switches | 0:5c4d7b2438d3 | 32 | /* |
| switches | 0:5c4d7b2438d3 | 33 | * Derive parameters from the standard-specific parameters in crc.h. |
| switches | 0:5c4d7b2438d3 | 34 | */ |
| switches | 0:5c4d7b2438d3 | 35 | #define WIDTH (8 * sizeof(flash_journal_crc32_t)) |
| switches | 0:5c4d7b2438d3 | 36 | /* U postfix required to suppress the following warning with TOOLCHAIN_ARM: |
| switches | 0:5c4d7b2438d3 | 37 | * #61-D: integer operation result is out of range |
| switches | 0:5c4d7b2438d3 | 38 | */ |
| switches | 0:5c4d7b2438d3 | 39 | #define TOPBIT 0x80000000U |
| switches | 0:5c4d7b2438d3 | 40 | |
| switches | 0:5c4d7b2438d3 | 41 | #if (REFLECT_DATA == TRUE) |
| switches | 0:5c4d7b2438d3 | 42 | #undef REFLECT_DATA |
| switches | 0:5c4d7b2438d3 | 43 | #define REFLECT_DATA(X) ((unsigned char) reflect((X), 8)) |
| switches | 0:5c4d7b2438d3 | 44 | #else |
| switches | 0:5c4d7b2438d3 | 45 | #undef REFLECT_DATA |
| switches | 0:5c4d7b2438d3 | 46 | #define REFLECT_DATA(X) (X) |
| switches | 0:5c4d7b2438d3 | 47 | #endif |
| switches | 0:5c4d7b2438d3 | 48 | |
| switches | 0:5c4d7b2438d3 | 49 | #if (REFLECT_REMAINDER == TRUE) |
| switches | 0:5c4d7b2438d3 | 50 | #undef REFLECT_REMAINDER |
| switches | 0:5c4d7b2438d3 | 51 | #define REFLECT_REMAINDER(X) ((flash_journal_crc32_t) reflect((X), WIDTH)) |
| switches | 0:5c4d7b2438d3 | 52 | #else |
| switches | 0:5c4d7b2438d3 | 53 | #undef REFLECT_REMAINDER |
| switches | 0:5c4d7b2438d3 | 54 | #define REFLECT_REMAINDER(X) (X) |
| switches | 0:5c4d7b2438d3 | 55 | #endif |
| switches | 0:5c4d7b2438d3 | 56 | |
| switches | 0:5c4d7b2438d3 | 57 | |
| switches | 0:5c4d7b2438d3 | 58 | /********************************************************************* |
| switches | 0:5c4d7b2438d3 | 59 | * |
| switches | 0:5c4d7b2438d3 | 60 | * Function: reflect() |
| switches | 0:5c4d7b2438d3 | 61 | * |
| switches | 0:5c4d7b2438d3 | 62 | * Description: Reorder the bits of a binary sequence, by reflecting |
| switches | 0:5c4d7b2438d3 | 63 | * them about the middle position. |
| switches | 0:5c4d7b2438d3 | 64 | * |
| switches | 0:5c4d7b2438d3 | 65 | * Notes: No checking is done that nBits <= 32. |
| switches | 0:5c4d7b2438d3 | 66 | * |
| switches | 0:5c4d7b2438d3 | 67 | * Returns: The reflection of the original data. |
| switches | 0:5c4d7b2438d3 | 68 | * |
| switches | 0:5c4d7b2438d3 | 69 | *********************************************************************/ |
| switches | 0:5c4d7b2438d3 | 70 | static uint32_t |
| switches | 0:5c4d7b2438d3 | 71 | reflect(uint32_t data, unsigned char nBits) |
| switches | 0:5c4d7b2438d3 | 72 | { |
| switches | 0:5c4d7b2438d3 | 73 | uint32_t reflection = 0x00000000; |
| switches | 0:5c4d7b2438d3 | 74 | unsigned char bit; |
| switches | 0:5c4d7b2438d3 | 75 | |
| switches | 0:5c4d7b2438d3 | 76 | /* |
| switches | 0:5c4d7b2438d3 | 77 | * Reflect the data about the center bit. |
| switches | 0:5c4d7b2438d3 | 78 | */ |
| switches | 0:5c4d7b2438d3 | 79 | for (bit = 0; bit < nBits; ++bit) |
| switches | 0:5c4d7b2438d3 | 80 | { |
| switches | 0:5c4d7b2438d3 | 81 | /* |
| switches | 0:5c4d7b2438d3 | 82 | * If the LSB bit is set, set the reflection of it. |
| switches | 0:5c4d7b2438d3 | 83 | */ |
| switches | 0:5c4d7b2438d3 | 84 | if (data & 0x01) |
| switches | 0:5c4d7b2438d3 | 85 | { |
| switches | 0:5c4d7b2438d3 | 86 | reflection |= (1 << ((nBits - 1) - bit)); |
| switches | 0:5c4d7b2438d3 | 87 | } |
| switches | 0:5c4d7b2438d3 | 88 | |
| switches | 0:5c4d7b2438d3 | 89 | data = (data >> 1); |
| switches | 0:5c4d7b2438d3 | 90 | } |
| switches | 0:5c4d7b2438d3 | 91 | |
| switches | 0:5c4d7b2438d3 | 92 | return (reflection); |
| switches | 0:5c4d7b2438d3 | 93 | |
| switches | 0:5c4d7b2438d3 | 94 | } /* reflect() */ |
| switches | 0:5c4d7b2438d3 | 95 | |
| switches | 0:5c4d7b2438d3 | 96 | |
| switches | 0:5c4d7b2438d3 | 97 | static flash_journal_crc32_t crcTable[256]; |
| switches | 0:5c4d7b2438d3 | 98 | static flash_journal_crc32_t crcEngineRemainder = INITIAL_REMAINDER; |
| switches | 0:5c4d7b2438d3 | 99 | |
| switches | 0:5c4d7b2438d3 | 100 | /********************************************************************* |
| switches | 0:5c4d7b2438d3 | 101 | * |
| switches | 0:5c4d7b2438d3 | 102 | * Function: flashJournalCrcInit() |
| switches | 0:5c4d7b2438d3 | 103 | * |
| switches | 0:5c4d7b2438d3 | 104 | * Description: Populate the partial CRC lookup table. |
| switches | 0:5c4d7b2438d3 | 105 | * |
| switches | 0:5c4d7b2438d3 | 106 | * Notes: This function must be rerun any time the CRC standard |
| switches | 0:5c4d7b2438d3 | 107 | * is changed. If desired, it can be run "offline" and |
| switches | 0:5c4d7b2438d3 | 108 | * the table results stored in an embedded system's ROM. |
| switches | 0:5c4d7b2438d3 | 109 | * |
| switches | 0:5c4d7b2438d3 | 110 | * Returns: None defined. |
| switches | 0:5c4d7b2438d3 | 111 | * |
| switches | 0:5c4d7b2438d3 | 112 | *********************************************************************/ |
| switches | 0:5c4d7b2438d3 | 113 | void |
| switches | 0:5c4d7b2438d3 | 114 | flashJournalCrcInit(void) |
| switches | 0:5c4d7b2438d3 | 115 | { |
| switches | 0:5c4d7b2438d3 | 116 | flash_journal_crc32_t remainder; |
| switches | 0:5c4d7b2438d3 | 117 | int dividend; |
| switches | 0:5c4d7b2438d3 | 118 | unsigned char bit; |
| switches | 0:5c4d7b2438d3 | 119 | |
| switches | 0:5c4d7b2438d3 | 120 | |
| switches | 0:5c4d7b2438d3 | 121 | /* |
| switches | 0:5c4d7b2438d3 | 122 | * Compute the remainder of each possible dividend. |
| switches | 0:5c4d7b2438d3 | 123 | */ |
| switches | 0:5c4d7b2438d3 | 124 | for (dividend = 0; dividend < 256; ++dividend) |
| switches | 0:5c4d7b2438d3 | 125 | { |
| switches | 0:5c4d7b2438d3 | 126 | /* |
| switches | 0:5c4d7b2438d3 | 127 | * Start with the dividend followed by zeros. |
| switches | 0:5c4d7b2438d3 | 128 | */ |
| switches | 0:5c4d7b2438d3 | 129 | remainder = dividend << (WIDTH - 8); |
| switches | 0:5c4d7b2438d3 | 130 | |
| switches | 0:5c4d7b2438d3 | 131 | /* |
| switches | 0:5c4d7b2438d3 | 132 | * Perform modulo-2 division, a bit at a time. |
| switches | 0:5c4d7b2438d3 | 133 | */ |
| switches | 0:5c4d7b2438d3 | 134 | for (bit = 8; bit > 0; --bit) |
| switches | 0:5c4d7b2438d3 | 135 | { |
| switches | 0:5c4d7b2438d3 | 136 | /* |
| switches | 0:5c4d7b2438d3 | 137 | * Try to divide the current data bit. |
| switches | 0:5c4d7b2438d3 | 138 | */ |
| switches | 0:5c4d7b2438d3 | 139 | if (remainder & TOPBIT) |
| switches | 0:5c4d7b2438d3 | 140 | { |
| switches | 0:5c4d7b2438d3 | 141 | remainder = (remainder << 1) ^ POLYNOMIAL; |
| switches | 0:5c4d7b2438d3 | 142 | } |
| switches | 0:5c4d7b2438d3 | 143 | else |
| switches | 0:5c4d7b2438d3 | 144 | { |
| switches | 0:5c4d7b2438d3 | 145 | remainder = (remainder << 1); |
| switches | 0:5c4d7b2438d3 | 146 | } |
| switches | 0:5c4d7b2438d3 | 147 | } |
| switches | 0:5c4d7b2438d3 | 148 | |
| switches | 0:5c4d7b2438d3 | 149 | /* |
| switches | 0:5c4d7b2438d3 | 150 | * Store the result into the table. |
| switches | 0:5c4d7b2438d3 | 151 | */ |
| switches | 0:5c4d7b2438d3 | 152 | crcTable[dividend] = remainder; |
| switches | 0:5c4d7b2438d3 | 153 | } |
| switches | 0:5c4d7b2438d3 | 154 | |
| switches | 0:5c4d7b2438d3 | 155 | } /* flashJournalCrcInit() */ |
| switches | 0:5c4d7b2438d3 | 156 | |
| switches | 0:5c4d7b2438d3 | 157 | /********************************************************************* |
| switches | 0:5c4d7b2438d3 | 158 | * |
| switches | 0:5c4d7b2438d3 | 159 | * Function: flashJournalCrcReset() |
| switches | 0:5c4d7b2438d3 | 160 | * |
| switches | 0:5c4d7b2438d3 | 161 | * Description: Resets internal state before calling crcCummulative(). |
| switches | 0:5c4d7b2438d3 | 162 | * |
| switches | 0:5c4d7b2438d3 | 163 | * Notes: See the notes to crcCummulative(). |
| switches | 0:5c4d7b2438d3 | 164 | * |
| switches | 0:5c4d7b2438d3 | 165 | * Returns: None defined. |
| switches | 0:5c4d7b2438d3 | 166 | * |
| switches | 0:5c4d7b2438d3 | 167 | *********************************************************************/ |
| switches | 0:5c4d7b2438d3 | 168 | void |
| switches | 0:5c4d7b2438d3 | 169 | flashJournalCrcReset(void) |
| switches | 0:5c4d7b2438d3 | 170 | { |
| switches | 0:5c4d7b2438d3 | 171 | static unsigned initCalled = 0; |
| switches | 0:5c4d7b2438d3 | 172 | if (!initCalled) { |
| switches | 0:5c4d7b2438d3 | 173 | flashJournalCrcInit(); |
| switches | 0:5c4d7b2438d3 | 174 | initCalled = 1; |
| switches | 0:5c4d7b2438d3 | 175 | } |
| switches | 0:5c4d7b2438d3 | 176 | |
| switches | 0:5c4d7b2438d3 | 177 | crcEngineRemainder = INITIAL_REMAINDER; |
| switches | 0:5c4d7b2438d3 | 178 | |
| switches | 0:5c4d7b2438d3 | 179 | } /* flashJournalCrcReset() */ |
| switches | 0:5c4d7b2438d3 | 180 | |
| switches | 0:5c4d7b2438d3 | 181 | /********************************************************************* |
| switches | 0:5c4d7b2438d3 | 182 | * |
| switches | 0:5c4d7b2438d3 | 183 | * Function: crcCummulative() |
| switches | 0:5c4d7b2438d3 | 184 | * |
| switches | 0:5c4d7b2438d3 | 185 | * Description: Compute the CRC of a group of messages. |
| switches | 0:5c4d7b2438d3 | 186 | * |
| switches | 0:5c4d7b2438d3 | 187 | * Notes: |
| switches | 0:5c4d7b2438d3 | 188 | * This function is intended to be used in the following way: |
| switches | 0:5c4d7b2438d3 | 189 | * - crcReset() is called first to reset internal state before the first |
| switches | 0:5c4d7b2438d3 | 190 | * fragment of a new message is processed with crcCummulative(). |
| switches | 0:5c4d7b2438d3 | 191 | * - crcCummulative() called successfully appending additional message |
| switches | 0:5c4d7b2438d3 | 192 | * fragments to those previously supplied (in order), and returning |
| switches | 0:5c4d7b2438d3 | 193 | * the current crc for the message payload so far. |
| switches | 0:5c4d7b2438d3 | 194 | * |
| switches | 0:5c4d7b2438d3 | 195 | * Returns: The CRC of the message. |
| switches | 0:5c4d7b2438d3 | 196 | * |
| switches | 0:5c4d7b2438d3 | 197 | *********************************************************************/ |
| switches | 0:5c4d7b2438d3 | 198 | flash_journal_crc32_t |
| switches | 0:5c4d7b2438d3 | 199 | flashJournalCrcCummulative(unsigned char const message[], int nBytes) |
| switches | 0:5c4d7b2438d3 | 200 | { |
| switches | 0:5c4d7b2438d3 | 201 | unsigned char data; |
| switches | 0:5c4d7b2438d3 | 202 | int byte; |
| switches | 0:5c4d7b2438d3 | 203 | |
| switches | 0:5c4d7b2438d3 | 204 | |
| switches | 0:5c4d7b2438d3 | 205 | /* |
| switches | 0:5c4d7b2438d3 | 206 | * Divide the message by the polynomial, a byte at a time. |
| switches | 0:5c4d7b2438d3 | 207 | */ |
| switches | 0:5c4d7b2438d3 | 208 | for (byte = 0; byte < nBytes; ++byte) |
| switches | 0:5c4d7b2438d3 | 209 | { |
| switches | 0:5c4d7b2438d3 | 210 | data = REFLECT_DATA(message[byte]) ^ (crcEngineRemainder >> (WIDTH - 8)); |
| switches | 0:5c4d7b2438d3 | 211 | crcEngineRemainder = crcTable[data] ^ (crcEngineRemainder << 8); |
| switches | 0:5c4d7b2438d3 | 212 | } |
| switches | 0:5c4d7b2438d3 | 213 | |
| switches | 0:5c4d7b2438d3 | 214 | /* |
| switches | 0:5c4d7b2438d3 | 215 | * The final remainder is the CRC. |
| switches | 0:5c4d7b2438d3 | 216 | */ |
| switches | 0:5c4d7b2438d3 | 217 | return (REFLECT_REMAINDER(crcEngineRemainder) ^ FINAL_XOR_VALUE); |
| switches | 0:5c4d7b2438d3 | 218 | |
| switches | 0:5c4d7b2438d3 | 219 | } /* crcCummulative() */ |
| switches | 0:5c4d7b2438d3 | 220 |
