mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
mbed library release version 165

Who changed what in which revision?

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