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