mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
pmcorreia
Date:
Tue Oct 09 14:42:37 2018 +0000
Revision:
187:fa51feb62426
Parent:
186:707f6e361f3e
Updated version to work with F446RE

Who changed what in which revision?

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