Daniel Vizcaya / Mbed OS 04_RTOS_Embebidos
Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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