mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
drivers/MbedCRC.h@184:08ed48f1de7f, 2018-04-19 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Apr 19 17:12:19 2018 +0100
- Revision:
- 184:08ed48f1de7f
- Child:
- 186:707f6e361f3e
mbed-dev library. Release version 161
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 184:08ed48f1de7f | 1 | /* mbed Microcontroller Library |
AnnaBridge | 184:08ed48f1de7f | 2 | * Copyright (c) 2018 ARM Limited |
AnnaBridge | 184:08ed48f1de7f | 3 | * |
AnnaBridge | 184:08ed48f1de7f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 184:08ed48f1de7f | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 184:08ed48f1de7f | 6 | * You may obtain a copy of the License at |
AnnaBridge | 184:08ed48f1de7f | 7 | * |
AnnaBridge | 184:08ed48f1de7f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 184:08ed48f1de7f | 9 | * |
AnnaBridge | 184:08ed48f1de7f | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 184:08ed48f1de7f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 184:08ed48f1de7f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 184:08ed48f1de7f | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 184:08ed48f1de7f | 14 | * limitations under the License. |
AnnaBridge | 184:08ed48f1de7f | 15 | */ |
AnnaBridge | 184:08ed48f1de7f | 16 | #ifndef MBED_CRC_API_H |
AnnaBridge | 184:08ed48f1de7f | 17 | #define MBED_CRC_API_H |
AnnaBridge | 184:08ed48f1de7f | 18 | |
AnnaBridge | 184:08ed48f1de7f | 19 | #include <stdint.h> |
AnnaBridge | 184:08ed48f1de7f | 20 | #include "drivers/TableCRC.h" |
AnnaBridge | 184:08ed48f1de7f | 21 | #include "platform/mbed_assert.h" |
AnnaBridge | 184:08ed48f1de7f | 22 | |
AnnaBridge | 184:08ed48f1de7f | 23 | /* This is invalid warning from the compiler for below section of code |
AnnaBridge | 184:08ed48f1de7f | 24 | if ((width < 8) && (NULL == _crc_table)) { |
AnnaBridge | 184:08ed48f1de7f | 25 | p_crc = (uint32_t)(p_crc << (8 - width)); |
AnnaBridge | 184:08ed48f1de7f | 26 | } |
AnnaBridge | 184:08ed48f1de7f | 27 | Compiler warns of the shift operation with width as it is width=(std::uint8_t), |
AnnaBridge | 184:08ed48f1de7f | 28 | but we check for ( width < 8) before performing shift, so it should not be an issue. |
AnnaBridge | 184:08ed48f1de7f | 29 | */ |
AnnaBridge | 184:08ed48f1de7f | 30 | #if defined ( __CC_ARM ) |
AnnaBridge | 184:08ed48f1de7f | 31 | #pragma diag_suppress 62 // Shift count is negative |
AnnaBridge | 184:08ed48f1de7f | 32 | #elif defined ( __GNUC__ ) |
AnnaBridge | 184:08ed48f1de7f | 33 | #pragma GCC diagnostic push |
AnnaBridge | 184:08ed48f1de7f | 34 | #pragma GCC diagnostic ignored "-Wshift-count-negative" |
AnnaBridge | 184:08ed48f1de7f | 35 | #endif |
AnnaBridge | 184:08ed48f1de7f | 36 | |
AnnaBridge | 184:08ed48f1de7f | 37 | namespace mbed { |
AnnaBridge | 184:08ed48f1de7f | 38 | /** \addtogroup drivers */ |
AnnaBridge | 184:08ed48f1de7f | 39 | /** @{*/ |
AnnaBridge | 184:08ed48f1de7f | 40 | |
AnnaBridge | 184:08ed48f1de7f | 41 | /** CRC Polynomial value |
AnnaBridge | 184:08ed48f1de7f | 42 | * |
AnnaBridge | 184:08ed48f1de7f | 43 | * Different polynomial values supported |
AnnaBridge | 184:08ed48f1de7f | 44 | */ |
AnnaBridge | 184:08ed48f1de7f | 45 | typedef enum crc_polynomial { |
AnnaBridge | 184:08ed48f1de7f | 46 | POLY_OTHER = 0, |
AnnaBridge | 184:08ed48f1de7f | 47 | POLY_8BIT_CCITT = 0x07, // x8+x2+x+1 |
AnnaBridge | 184:08ed48f1de7f | 48 | POLY_7BIT_SD = 0x9, // x7+x3+1; |
AnnaBridge | 184:08ed48f1de7f | 49 | POLY_16BIT_CCITT = 0x1021, // x16+x12+x5+1 |
AnnaBridge | 184:08ed48f1de7f | 50 | POLY_16BIT_IBM = 0x8005, // x16+x15+x2+1 |
AnnaBridge | 184:08ed48f1de7f | 51 | POLY_32BIT_ANSI = 0x04C11DB7, // x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+1 |
AnnaBridge | 184:08ed48f1de7f | 52 | } crc_polynomial_t; |
AnnaBridge | 184:08ed48f1de7f | 53 | |
AnnaBridge | 184:08ed48f1de7f | 54 | /** CRC object provides CRC generation through hardware/software |
AnnaBridge | 184:08ed48f1de7f | 55 | * |
AnnaBridge | 184:08ed48f1de7f | 56 | * ROM polynomial tables for supported polynomials (:: crc_polynomial_t) will be used for |
AnnaBridge | 184:08ed48f1de7f | 57 | * software CRC computation, if ROM tables are not available then CRC is computed runtime |
AnnaBridge | 184:08ed48f1de7f | 58 | * bit by bit for all data input. |
AnnaBridge | 184:08ed48f1de7f | 59 | * |
AnnaBridge | 184:08ed48f1de7f | 60 | * @tparam polynomial CRC polynomial value in hex |
AnnaBridge | 184:08ed48f1de7f | 61 | * @tparam width CRC polynomial width |
AnnaBridge | 184:08ed48f1de7f | 62 | * |
AnnaBridge | 184:08ed48f1de7f | 63 | * Example: Compute CRC data |
AnnaBridge | 184:08ed48f1de7f | 64 | * @code |
AnnaBridge | 184:08ed48f1de7f | 65 | * |
AnnaBridge | 184:08ed48f1de7f | 66 | * #include "mbed.h" |
AnnaBridge | 184:08ed48f1de7f | 67 | * |
AnnaBridge | 184:08ed48f1de7f | 68 | * int main() { |
AnnaBridge | 184:08ed48f1de7f | 69 | * MbedCRC<POLY_32BIT_ANSI, 32> ct; |
AnnaBridge | 184:08ed48f1de7f | 70 | * |
AnnaBridge | 184:08ed48f1de7f | 71 | * char test[] = "123456789"; |
AnnaBridge | 184:08ed48f1de7f | 72 | * uint32_t crc = 0; |
AnnaBridge | 184:08ed48f1de7f | 73 | * |
AnnaBridge | 184:08ed48f1de7f | 74 | * printf("\nPolynomial = 0x%lx Width = %d \n", ct.get_polynomial(), ct.get_width()); |
AnnaBridge | 184:08ed48f1de7f | 75 | * |
AnnaBridge | 184:08ed48f1de7f | 76 | * ct.compute((void *)test, strlen((const char*)test), &crc); |
AnnaBridge | 184:08ed48f1de7f | 77 | * |
AnnaBridge | 184:08ed48f1de7f | 78 | * printf("The CRC of data \"123456789\" is : 0x%lx\n", crc); |
AnnaBridge | 184:08ed48f1de7f | 79 | * return 0; |
AnnaBridge | 184:08ed48f1de7f | 80 | * } |
AnnaBridge | 184:08ed48f1de7f | 81 | * @endcode |
AnnaBridge | 184:08ed48f1de7f | 82 | * Example: Compute CRC with data available in parts |
AnnaBridge | 184:08ed48f1de7f | 83 | * @code |
AnnaBridge | 184:08ed48f1de7f | 84 | * |
AnnaBridge | 184:08ed48f1de7f | 85 | * #include "mbed.h" |
AnnaBridge | 184:08ed48f1de7f | 86 | * int main() { |
AnnaBridge | 184:08ed48f1de7f | 87 | * MbedCRC<POLY_32BIT_ANSI, 32> ct; |
AnnaBridge | 184:08ed48f1de7f | 88 | * |
AnnaBridge | 184:08ed48f1de7f | 89 | * char test[] = "123456789"; |
AnnaBridge | 184:08ed48f1de7f | 90 | * uint32_t crc = 0; |
AnnaBridge | 184:08ed48f1de7f | 91 | * |
AnnaBridge | 184:08ed48f1de7f | 92 | * printf("\nPolynomial = 0x%lx Width = %d \n", ct.get_polynomial(), ct.get_width()); |
AnnaBridge | 184:08ed48f1de7f | 93 | * |
AnnaBridge | 184:08ed48f1de7f | 94 | * ct.compute_partial_start(&crc); |
AnnaBridge | 184:08ed48f1de7f | 95 | * ct.compute_partial((void *)&test, 4, &crc); |
AnnaBridge | 184:08ed48f1de7f | 96 | * ct.compute_partial((void *)&test[4], 5, &crc); |
AnnaBridge | 184:08ed48f1de7f | 97 | * ct.compute_partial_stop(&crc); |
AnnaBridge | 184:08ed48f1de7f | 98 | * |
AnnaBridge | 184:08ed48f1de7f | 99 | * printf("The CRC of data \"123456789\" is : 0x%lx\n", crc); |
AnnaBridge | 184:08ed48f1de7f | 100 | * return 0; |
AnnaBridge | 184:08ed48f1de7f | 101 | * } |
AnnaBridge | 184:08ed48f1de7f | 102 | * @endcode |
AnnaBridge | 184:08ed48f1de7f | 103 | * @ingroup drivers |
AnnaBridge | 184:08ed48f1de7f | 104 | */ |
AnnaBridge | 184:08ed48f1de7f | 105 | |
AnnaBridge | 184:08ed48f1de7f | 106 | template <uint32_t polynomial=POLY_32BIT_ANSI, uint8_t width=32> |
AnnaBridge | 184:08ed48f1de7f | 107 | class MbedCRC |
AnnaBridge | 184:08ed48f1de7f | 108 | { |
AnnaBridge | 184:08ed48f1de7f | 109 | public: |
AnnaBridge | 184:08ed48f1de7f | 110 | typedef uint64_t crc_data_size_t; |
AnnaBridge | 184:08ed48f1de7f | 111 | |
AnnaBridge | 184:08ed48f1de7f | 112 | /** Lifetime of CRC object |
AnnaBridge | 184:08ed48f1de7f | 113 | * |
AnnaBridge | 184:08ed48f1de7f | 114 | * @param initial_xor Inital value/seed to Xor |
AnnaBridge | 184:08ed48f1de7f | 115 | * @param final_xor Final Xor value |
AnnaBridge | 184:08ed48f1de7f | 116 | * @param reflect_data |
AnnaBridge | 184:08ed48f1de7f | 117 | * @param reflect_remainder |
AnnaBridge | 184:08ed48f1de7f | 118 | * @note Default constructor without any arguments is valid only for supported CRC polynomials. :: crc_polynomial_t |
AnnaBridge | 184:08ed48f1de7f | 119 | * MbedCRC <POLY_7BIT_SD, 7> ct; --- Valid POLY_7BIT_SD |
AnnaBridge | 184:08ed48f1de7f | 120 | * MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT |
AnnaBridge | 184:08ed48f1de7f | 121 | * MbedCRC <POLY_16BIT_CCITT, 32> ct; --- Invalid, compilation error |
AnnaBridge | 184:08ed48f1de7f | 122 | * MbedCRC <POLY_16BIT_CCITT, 32> ct (i,f,rd,rr) Consturctor can be used for not supported polynomials |
AnnaBridge | 184:08ed48f1de7f | 123 | * MbedCRC<POLY_16BIT_CCITT, 16> sd(0, 0, false, false); Constructor can also be used for supported |
AnnaBridge | 184:08ed48f1de7f | 124 | * polynomials with different intial/final/reflect values |
AnnaBridge | 184:08ed48f1de7f | 125 | * |
AnnaBridge | 184:08ed48f1de7f | 126 | */ |
AnnaBridge | 184:08ed48f1de7f | 127 | MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder) : |
AnnaBridge | 184:08ed48f1de7f | 128 | _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data), |
AnnaBridge | 184:08ed48f1de7f | 129 | _reflect_remainder(reflect_remainder), _crc_table(NULL) |
AnnaBridge | 184:08ed48f1de7f | 130 | { |
AnnaBridge | 184:08ed48f1de7f | 131 | mbed_crc_ctor(); |
AnnaBridge | 184:08ed48f1de7f | 132 | } |
AnnaBridge | 184:08ed48f1de7f | 133 | MbedCRC(); |
AnnaBridge | 184:08ed48f1de7f | 134 | virtual ~MbedCRC() |
AnnaBridge | 184:08ed48f1de7f | 135 | { |
AnnaBridge | 184:08ed48f1de7f | 136 | // Do nothing |
AnnaBridge | 184:08ed48f1de7f | 137 | } |
AnnaBridge | 184:08ed48f1de7f | 138 | |
AnnaBridge | 184:08ed48f1de7f | 139 | /** Compute CRC for the data input |
AnnaBridge | 184:08ed48f1de7f | 140 | * |
AnnaBridge | 184:08ed48f1de7f | 141 | * @param buffer Data bytes |
AnnaBridge | 184:08ed48f1de7f | 142 | * @param size Size of data |
AnnaBridge | 184:08ed48f1de7f | 143 | * @param crc CRC is the output value |
AnnaBridge | 184:08ed48f1de7f | 144 | * @return 0 on success, negative error code on failure |
AnnaBridge | 184:08ed48f1de7f | 145 | */ |
AnnaBridge | 184:08ed48f1de7f | 146 | int32_t compute(void *buffer, crc_data_size_t size, uint32_t *crc) |
AnnaBridge | 184:08ed48f1de7f | 147 | { |
AnnaBridge | 184:08ed48f1de7f | 148 | MBED_ASSERT(crc != NULL); |
AnnaBridge | 184:08ed48f1de7f | 149 | int32_t status; |
AnnaBridge | 184:08ed48f1de7f | 150 | if (0 != (status = compute_partial_start(crc))) { |
AnnaBridge | 184:08ed48f1de7f | 151 | *crc = 0; |
AnnaBridge | 184:08ed48f1de7f | 152 | return status; |
AnnaBridge | 184:08ed48f1de7f | 153 | } |
AnnaBridge | 184:08ed48f1de7f | 154 | if (0 != (status = compute_partial(buffer, size, crc))) { |
AnnaBridge | 184:08ed48f1de7f | 155 | *crc = 0; |
AnnaBridge | 184:08ed48f1de7f | 156 | return status; |
AnnaBridge | 184:08ed48f1de7f | 157 | } |
AnnaBridge | 184:08ed48f1de7f | 158 | if (0 != (status = compute_partial_stop(crc))) { |
AnnaBridge | 184:08ed48f1de7f | 159 | *crc = 0; |
AnnaBridge | 184:08ed48f1de7f | 160 | return status; |
AnnaBridge | 184:08ed48f1de7f | 161 | } |
AnnaBridge | 184:08ed48f1de7f | 162 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 163 | } |
AnnaBridge | 184:08ed48f1de7f | 164 | |
AnnaBridge | 184:08ed48f1de7f | 165 | /** Compute partial CRC for the data input. |
AnnaBridge | 184:08ed48f1de7f | 166 | * |
AnnaBridge | 184:08ed48f1de7f | 167 | * CRC data if not available fully, CRC can be computed in parts with available data. |
AnnaBridge | 184:08ed48f1de7f | 168 | * Previous CRC output should be passed as argument to the current compute_partial call. |
AnnaBridge | 184:08ed48f1de7f | 169 | * @pre: Call \ref compute_partial_start to start the partial CRC calculation. |
AnnaBridge | 184:08ed48f1de7f | 170 | * @post: Call \ref compute_partial_stop to get the final CRC value. |
AnnaBridge | 184:08ed48f1de7f | 171 | * |
AnnaBridge | 184:08ed48f1de7f | 172 | * @param buffer Data bytes |
AnnaBridge | 184:08ed48f1de7f | 173 | * @param size Size of data |
AnnaBridge | 184:08ed48f1de7f | 174 | * @param crc CRC value is intermediate CRC value filled by API. |
AnnaBridge | 184:08ed48f1de7f | 175 | * @return 0 on success or a negative error code on failure |
AnnaBridge | 184:08ed48f1de7f | 176 | * @note: CRC as output in compute_partial is not final CRC value, call @ref compute_partial_stop |
AnnaBridge | 184:08ed48f1de7f | 177 | * to get final correct CRC value. |
AnnaBridge | 184:08ed48f1de7f | 178 | */ |
AnnaBridge | 184:08ed48f1de7f | 179 | int32_t compute_partial(void *buffer, crc_data_size_t size, uint32_t *crc) |
AnnaBridge | 184:08ed48f1de7f | 180 | { |
AnnaBridge | 184:08ed48f1de7f | 181 | if (NULL == _crc_table) { |
AnnaBridge | 184:08ed48f1de7f | 182 | // Compute bitwise CRC |
AnnaBridge | 184:08ed48f1de7f | 183 | return bitwise_compute_partial(buffer, size, crc); |
AnnaBridge | 184:08ed48f1de7f | 184 | } else { |
AnnaBridge | 184:08ed48f1de7f | 185 | // Table CRC |
AnnaBridge | 184:08ed48f1de7f | 186 | return table_compute_partial(buffer, size, crc); |
AnnaBridge | 184:08ed48f1de7f | 187 | } |
AnnaBridge | 184:08ed48f1de7f | 188 | } |
AnnaBridge | 184:08ed48f1de7f | 189 | |
AnnaBridge | 184:08ed48f1de7f | 190 | /** Compute partial start, indicate start of partial computation |
AnnaBridge | 184:08ed48f1de7f | 191 | * |
AnnaBridge | 184:08ed48f1de7f | 192 | * This API should be called before performing any partial computation |
AnnaBridge | 184:08ed48f1de7f | 193 | * with compute_partial API. |
AnnaBridge | 184:08ed48f1de7f | 194 | * |
AnnaBridge | 184:08ed48f1de7f | 195 | * @param crc Initial CRC value set by the API |
AnnaBridge | 184:08ed48f1de7f | 196 | * @return 0 on success or a negative in case of failure |
AnnaBridge | 184:08ed48f1de7f | 197 | * @note: CRC is an out parameter and must be reused with compute_partial |
AnnaBridge | 184:08ed48f1de7f | 198 | * and compute_partial_stop without any modifications in application. |
AnnaBridge | 184:08ed48f1de7f | 199 | */ |
AnnaBridge | 184:08ed48f1de7f | 200 | int32_t compute_partial_start(uint32_t *crc) |
AnnaBridge | 184:08ed48f1de7f | 201 | { |
AnnaBridge | 184:08ed48f1de7f | 202 | MBED_ASSERT(crc != NULL); |
AnnaBridge | 184:08ed48f1de7f | 203 | *crc = _initial_value; |
AnnaBridge | 184:08ed48f1de7f | 204 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 205 | } |
AnnaBridge | 184:08ed48f1de7f | 206 | |
AnnaBridge | 184:08ed48f1de7f | 207 | /** Get the final CRC value of partial computation. |
AnnaBridge | 184:08ed48f1de7f | 208 | * |
AnnaBridge | 184:08ed48f1de7f | 209 | * CRC value available in partial computation is not correct CRC, as some |
AnnaBridge | 184:08ed48f1de7f | 210 | * algorithms require remainder to be reflected and final value to be XORed |
AnnaBridge | 184:08ed48f1de7f | 211 | * This API is used to perform final computation to get correct CRC value. |
AnnaBridge | 184:08ed48f1de7f | 212 | * |
AnnaBridge | 184:08ed48f1de7f | 213 | * @param crc CRC result |
AnnaBridge | 184:08ed48f1de7f | 214 | */ |
AnnaBridge | 184:08ed48f1de7f | 215 | int32_t compute_partial_stop(uint32_t *crc) |
AnnaBridge | 184:08ed48f1de7f | 216 | { |
AnnaBridge | 184:08ed48f1de7f | 217 | MBED_ASSERT(crc != NULL); |
AnnaBridge | 184:08ed48f1de7f | 218 | uint32_t p_crc = *crc; |
AnnaBridge | 184:08ed48f1de7f | 219 | if ((width < 8) && (NULL == _crc_table)) { |
AnnaBridge | 184:08ed48f1de7f | 220 | p_crc = (uint32_t)(p_crc << (8 - width)); |
AnnaBridge | 184:08ed48f1de7f | 221 | } |
AnnaBridge | 184:08ed48f1de7f | 222 | *crc = (reflect_remainder(p_crc) ^ _final_xor) & get_crc_mask(); |
AnnaBridge | 184:08ed48f1de7f | 223 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 224 | } |
AnnaBridge | 184:08ed48f1de7f | 225 | |
AnnaBridge | 184:08ed48f1de7f | 226 | /** Get the current CRC polynomial |
AnnaBridge | 184:08ed48f1de7f | 227 | * |
AnnaBridge | 184:08ed48f1de7f | 228 | * @return Polynomial value |
AnnaBridge | 184:08ed48f1de7f | 229 | */ |
AnnaBridge | 184:08ed48f1de7f | 230 | uint32_t get_polynomial(void) const |
AnnaBridge | 184:08ed48f1de7f | 231 | { |
AnnaBridge | 184:08ed48f1de7f | 232 | return polynomial; |
AnnaBridge | 184:08ed48f1de7f | 233 | } |
AnnaBridge | 184:08ed48f1de7f | 234 | |
AnnaBridge | 184:08ed48f1de7f | 235 | /** Get the current CRC width |
AnnaBridge | 184:08ed48f1de7f | 236 | * |
AnnaBridge | 184:08ed48f1de7f | 237 | * @return CRC width |
AnnaBridge | 184:08ed48f1de7f | 238 | */ |
AnnaBridge | 184:08ed48f1de7f | 239 | uint8_t get_width(void) const |
AnnaBridge | 184:08ed48f1de7f | 240 | { |
AnnaBridge | 184:08ed48f1de7f | 241 | return width; |
AnnaBridge | 184:08ed48f1de7f | 242 | } |
AnnaBridge | 184:08ed48f1de7f | 243 | |
AnnaBridge | 184:08ed48f1de7f | 244 | private: |
AnnaBridge | 184:08ed48f1de7f | 245 | uint32_t _initial_value; |
AnnaBridge | 184:08ed48f1de7f | 246 | uint32_t _final_xor; |
AnnaBridge | 184:08ed48f1de7f | 247 | bool _reflect_data; |
AnnaBridge | 184:08ed48f1de7f | 248 | bool _reflect_remainder; |
AnnaBridge | 184:08ed48f1de7f | 249 | uint32_t *_crc_table; |
AnnaBridge | 184:08ed48f1de7f | 250 | |
AnnaBridge | 184:08ed48f1de7f | 251 | /** Get the current CRC data size |
AnnaBridge | 184:08ed48f1de7f | 252 | * |
AnnaBridge | 184:08ed48f1de7f | 253 | * @return CRC data size in bytes |
AnnaBridge | 184:08ed48f1de7f | 254 | */ |
AnnaBridge | 184:08ed48f1de7f | 255 | uint8_t get_data_size(void) const |
AnnaBridge | 184:08ed48f1de7f | 256 | { |
AnnaBridge | 184:08ed48f1de7f | 257 | return (width <= 8 ? 1 : (width <= 16 ? 2 : 4)); |
AnnaBridge | 184:08ed48f1de7f | 258 | } |
AnnaBridge | 184:08ed48f1de7f | 259 | |
AnnaBridge | 184:08ed48f1de7f | 260 | /** Get the top bit of current CRC |
AnnaBridge | 184:08ed48f1de7f | 261 | * |
AnnaBridge | 184:08ed48f1de7f | 262 | * @return Top bit is set high for respective data width of current CRC |
AnnaBridge | 184:08ed48f1de7f | 263 | * Top bit for CRC width less then 8 bits will be set as 8th bit. |
AnnaBridge | 184:08ed48f1de7f | 264 | */ |
AnnaBridge | 184:08ed48f1de7f | 265 | uint32_t get_top_bit(void) const |
AnnaBridge | 184:08ed48f1de7f | 266 | { |
AnnaBridge | 184:08ed48f1de7f | 267 | return (width < 8 ? (1u << 7) : (uint32_t)(1ul << (width - 1))); |
AnnaBridge | 184:08ed48f1de7f | 268 | } |
AnnaBridge | 184:08ed48f1de7f | 269 | |
AnnaBridge | 184:08ed48f1de7f | 270 | /** Get the CRC data mask |
AnnaBridge | 184:08ed48f1de7f | 271 | * |
AnnaBridge | 184:08ed48f1de7f | 272 | * @return CRC data mask is generated based on current CRC width |
AnnaBridge | 184:08ed48f1de7f | 273 | */ |
AnnaBridge | 184:08ed48f1de7f | 274 | uint32_t get_crc_mask(void) const |
AnnaBridge | 184:08ed48f1de7f | 275 | { |
AnnaBridge | 184:08ed48f1de7f | 276 | return (width < 8 ? ((1u << 8) - 1) : (uint32_t)((uint64_t)(1ull << width) - 1)); |
AnnaBridge | 184:08ed48f1de7f | 277 | } |
AnnaBridge | 184:08ed48f1de7f | 278 | |
AnnaBridge | 184:08ed48f1de7f | 279 | /** Final value of CRC is reflected |
AnnaBridge | 184:08ed48f1de7f | 280 | * |
AnnaBridge | 184:08ed48f1de7f | 281 | * @param data final crc value, which should be reflected |
AnnaBridge | 184:08ed48f1de7f | 282 | * @return Reflected CRC value |
AnnaBridge | 184:08ed48f1de7f | 283 | */ |
AnnaBridge | 184:08ed48f1de7f | 284 | uint32_t reflect_remainder(uint32_t data) const |
AnnaBridge | 184:08ed48f1de7f | 285 | { |
AnnaBridge | 184:08ed48f1de7f | 286 | if (_reflect_remainder) { |
AnnaBridge | 184:08ed48f1de7f | 287 | uint32_t reflection = 0x0; |
AnnaBridge | 184:08ed48f1de7f | 288 | uint8_t const nBits = (width < 8 ? 8 : width); |
AnnaBridge | 184:08ed48f1de7f | 289 | |
AnnaBridge | 184:08ed48f1de7f | 290 | for (uint8_t bit = 0; bit < nBits; ++bit) { |
AnnaBridge | 184:08ed48f1de7f | 291 | if (data & 0x01) { |
AnnaBridge | 184:08ed48f1de7f | 292 | reflection |= (1 << ((nBits - 1) - bit)); |
AnnaBridge | 184:08ed48f1de7f | 293 | } |
AnnaBridge | 184:08ed48f1de7f | 294 | data = (data >> 1); |
AnnaBridge | 184:08ed48f1de7f | 295 | } |
AnnaBridge | 184:08ed48f1de7f | 296 | return (reflection); |
AnnaBridge | 184:08ed48f1de7f | 297 | } else { |
AnnaBridge | 184:08ed48f1de7f | 298 | return data; |
AnnaBridge | 184:08ed48f1de7f | 299 | } |
AnnaBridge | 184:08ed48f1de7f | 300 | } |
AnnaBridge | 184:08ed48f1de7f | 301 | |
AnnaBridge | 184:08ed48f1de7f | 302 | /** Data bytes are reflected |
AnnaBridge | 184:08ed48f1de7f | 303 | * |
AnnaBridge | 184:08ed48f1de7f | 304 | * @param data value to be reflected |
AnnaBridge | 184:08ed48f1de7f | 305 | * @return Reflected data value |
AnnaBridge | 184:08ed48f1de7f | 306 | */ |
AnnaBridge | 184:08ed48f1de7f | 307 | uint32_t reflect_bytes(uint32_t data) const |
AnnaBridge | 184:08ed48f1de7f | 308 | { |
AnnaBridge | 184:08ed48f1de7f | 309 | if(_reflect_data) { |
AnnaBridge | 184:08ed48f1de7f | 310 | uint32_t reflection = 0x0; |
AnnaBridge | 184:08ed48f1de7f | 311 | |
AnnaBridge | 184:08ed48f1de7f | 312 | for (uint8_t bit = 0; bit < 8; ++bit) { |
AnnaBridge | 184:08ed48f1de7f | 313 | if (data & 0x01) { |
AnnaBridge | 184:08ed48f1de7f | 314 | reflection |= (1 << (7 - bit)); |
AnnaBridge | 184:08ed48f1de7f | 315 | } |
AnnaBridge | 184:08ed48f1de7f | 316 | data = (data >> 1); |
AnnaBridge | 184:08ed48f1de7f | 317 | } |
AnnaBridge | 184:08ed48f1de7f | 318 | return (reflection); |
AnnaBridge | 184:08ed48f1de7f | 319 | } else { |
AnnaBridge | 184:08ed48f1de7f | 320 | return data; |
AnnaBridge | 184:08ed48f1de7f | 321 | } |
AnnaBridge | 184:08ed48f1de7f | 322 | } |
AnnaBridge | 184:08ed48f1de7f | 323 | |
AnnaBridge | 184:08ed48f1de7f | 324 | /** Bitwise CRC computation |
AnnaBridge | 184:08ed48f1de7f | 325 | * |
AnnaBridge | 184:08ed48f1de7f | 326 | * @param buffer data buffer |
AnnaBridge | 184:08ed48f1de7f | 327 | * @param size size of the data |
AnnaBridge | 184:08ed48f1de7f | 328 | * @param crc CRC value is filled in, but the value is not the final |
AnnaBridge | 184:08ed48f1de7f | 329 | * @return 0 on success or a negative error code on failure |
AnnaBridge | 184:08ed48f1de7f | 330 | */ |
AnnaBridge | 184:08ed48f1de7f | 331 | int32_t bitwise_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const |
AnnaBridge | 184:08ed48f1de7f | 332 | { |
AnnaBridge | 184:08ed48f1de7f | 333 | MBED_ASSERT(crc != NULL); |
AnnaBridge | 184:08ed48f1de7f | 334 | MBED_ASSERT(buffer != NULL); |
AnnaBridge | 184:08ed48f1de7f | 335 | |
AnnaBridge | 184:08ed48f1de7f | 336 | const uint8_t *data = static_cast<const uint8_t *>(buffer); |
AnnaBridge | 184:08ed48f1de7f | 337 | uint32_t p_crc = *crc; |
AnnaBridge | 184:08ed48f1de7f | 338 | |
AnnaBridge | 184:08ed48f1de7f | 339 | if (width < 8) { |
AnnaBridge | 184:08ed48f1de7f | 340 | uint8_t data_byte; |
AnnaBridge | 184:08ed48f1de7f | 341 | for (crc_data_size_t byte = 0; byte < size; byte++) { |
AnnaBridge | 184:08ed48f1de7f | 342 | data_byte = reflect_bytes(data[byte]); |
AnnaBridge | 184:08ed48f1de7f | 343 | for (uint8_t bit = 8; bit > 0; --bit) { |
AnnaBridge | 184:08ed48f1de7f | 344 | p_crc <<= 1; |
AnnaBridge | 184:08ed48f1de7f | 345 | if (( data_byte ^ p_crc) & get_top_bit()) { |
AnnaBridge | 184:08ed48f1de7f | 346 | p_crc ^= polynomial; |
AnnaBridge | 184:08ed48f1de7f | 347 | } |
AnnaBridge | 184:08ed48f1de7f | 348 | data_byte <<= 1; |
AnnaBridge | 184:08ed48f1de7f | 349 | } |
AnnaBridge | 184:08ed48f1de7f | 350 | } |
AnnaBridge | 184:08ed48f1de7f | 351 | } else { |
AnnaBridge | 184:08ed48f1de7f | 352 | for (crc_data_size_t byte = 0; byte < size; byte++) { |
AnnaBridge | 184:08ed48f1de7f | 353 | p_crc ^= (reflect_bytes(data[byte]) << (width - 8)); |
AnnaBridge | 184:08ed48f1de7f | 354 | |
AnnaBridge | 184:08ed48f1de7f | 355 | // Perform modulo-2 division, a bit at a time |
AnnaBridge | 184:08ed48f1de7f | 356 | for (uint8_t bit = 8; bit > 0; --bit) { |
AnnaBridge | 184:08ed48f1de7f | 357 | if (p_crc & get_top_bit()) { |
AnnaBridge | 184:08ed48f1de7f | 358 | p_crc = (p_crc << 1) ^ polynomial; |
AnnaBridge | 184:08ed48f1de7f | 359 | } else { |
AnnaBridge | 184:08ed48f1de7f | 360 | p_crc = (p_crc << 1); |
AnnaBridge | 184:08ed48f1de7f | 361 | } |
AnnaBridge | 184:08ed48f1de7f | 362 | } |
AnnaBridge | 184:08ed48f1de7f | 363 | } |
AnnaBridge | 184:08ed48f1de7f | 364 | } |
AnnaBridge | 184:08ed48f1de7f | 365 | *crc = p_crc & get_crc_mask(); |
AnnaBridge | 184:08ed48f1de7f | 366 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 367 | } |
AnnaBridge | 184:08ed48f1de7f | 368 | |
AnnaBridge | 184:08ed48f1de7f | 369 | /** CRC computation using ROM tables |
AnnaBridge | 184:08ed48f1de7f | 370 | * |
AnnaBridge | 184:08ed48f1de7f | 371 | * @param buffer data buffer |
AnnaBridge | 184:08ed48f1de7f | 372 | * @param size size of the data |
AnnaBridge | 184:08ed48f1de7f | 373 | * @param crc CRC value is filled in, but the value is not the final |
AnnaBridge | 184:08ed48f1de7f | 374 | * @return 0 on success or a negative error code on failure |
AnnaBridge | 184:08ed48f1de7f | 375 | */ |
AnnaBridge | 184:08ed48f1de7f | 376 | int32_t table_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const |
AnnaBridge | 184:08ed48f1de7f | 377 | { |
AnnaBridge | 184:08ed48f1de7f | 378 | MBED_ASSERT(crc != NULL); |
AnnaBridge | 184:08ed48f1de7f | 379 | MBED_ASSERT(buffer != NULL); |
AnnaBridge | 184:08ed48f1de7f | 380 | |
AnnaBridge | 184:08ed48f1de7f | 381 | const uint8_t *data = static_cast<const uint8_t *>(buffer); |
AnnaBridge | 184:08ed48f1de7f | 382 | uint32_t p_crc = *crc; |
AnnaBridge | 184:08ed48f1de7f | 383 | uint8_t data_byte = 0; |
AnnaBridge | 184:08ed48f1de7f | 384 | |
AnnaBridge | 184:08ed48f1de7f | 385 | if (width <= 8) { |
AnnaBridge | 184:08ed48f1de7f | 386 | uint8_t *crc_table = (uint8_t *)_crc_table; |
AnnaBridge | 184:08ed48f1de7f | 387 | for (crc_data_size_t byte = 0; byte < size; byte++) { |
AnnaBridge | 184:08ed48f1de7f | 388 | data_byte = reflect_bytes(data[byte]) ^ p_crc; |
AnnaBridge | 184:08ed48f1de7f | 389 | p_crc = crc_table[data_byte]; |
AnnaBridge | 184:08ed48f1de7f | 390 | } |
AnnaBridge | 184:08ed48f1de7f | 391 | } else if (width <= 16) { |
AnnaBridge | 184:08ed48f1de7f | 392 | uint16_t *crc_table = (uint16_t *)_crc_table; |
AnnaBridge | 184:08ed48f1de7f | 393 | for (crc_data_size_t byte = 0; byte < size; byte++) { |
AnnaBridge | 184:08ed48f1de7f | 394 | data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8)); |
AnnaBridge | 184:08ed48f1de7f | 395 | p_crc = crc_table[data_byte] ^ (p_crc << 8); |
AnnaBridge | 184:08ed48f1de7f | 396 | } |
AnnaBridge | 184:08ed48f1de7f | 397 | } else { |
AnnaBridge | 184:08ed48f1de7f | 398 | uint32_t *crc_table = (uint32_t *)_crc_table; |
AnnaBridge | 184:08ed48f1de7f | 399 | for (crc_data_size_t byte = 0; byte < size; byte++) { |
AnnaBridge | 184:08ed48f1de7f | 400 | data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8)); |
AnnaBridge | 184:08ed48f1de7f | 401 | p_crc = crc_table[data_byte] ^ (p_crc << 8); |
AnnaBridge | 184:08ed48f1de7f | 402 | } |
AnnaBridge | 184:08ed48f1de7f | 403 | } |
AnnaBridge | 184:08ed48f1de7f | 404 | *crc = p_crc & get_crc_mask(); |
AnnaBridge | 184:08ed48f1de7f | 405 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 406 | } |
AnnaBridge | 184:08ed48f1de7f | 407 | |
AnnaBridge | 184:08ed48f1de7f | 408 | /** Constructor init called from all specialized cases of constructor |
AnnaBridge | 184:08ed48f1de7f | 409 | * Note: All construtor common code should be in this function. |
AnnaBridge | 184:08ed48f1de7f | 410 | */ |
AnnaBridge | 184:08ed48f1de7f | 411 | void mbed_crc_ctor(void) const |
AnnaBridge | 184:08ed48f1de7f | 412 | { |
AnnaBridge | 184:08ed48f1de7f | 413 | MBED_STATIC_ASSERT(width <= 32, "Max 32-bit CRC supported"); |
AnnaBridge | 184:08ed48f1de7f | 414 | } |
AnnaBridge | 184:08ed48f1de7f | 415 | }; |
AnnaBridge | 184:08ed48f1de7f | 416 | |
AnnaBridge | 184:08ed48f1de7f | 417 | #if defined ( __CC_ARM ) |
AnnaBridge | 184:08ed48f1de7f | 418 | #elif defined ( __GNUC__ ) |
AnnaBridge | 184:08ed48f1de7f | 419 | #pragma GCC diagnostic pop |
AnnaBridge | 184:08ed48f1de7f | 420 | #endif |
AnnaBridge | 184:08ed48f1de7f | 421 | |
AnnaBridge | 184:08ed48f1de7f | 422 | /** @}*/ |
AnnaBridge | 184:08ed48f1de7f | 423 | } // namespace mbed |
AnnaBridge | 184:08ed48f1de7f | 424 | |
AnnaBridge | 184:08ed48f1de7f | 425 | #endif |