Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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