Pratyush Mallick
/
nano_dac
this is testing
noos_mbed/util/crc8.c@0:e8a1ba50c46b, 2021-01-14 (annotated)
- Committer:
- pmallick
- Date:
- Thu Jan 14 19:12:57 2021 +0530
- Revision:
- 0:e8a1ba50c46b
this is testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmallick | 0:e8a1ba50c46b | 1 | /***************************************************************************//** |
pmallick | 0:e8a1ba50c46b | 2 | * @file crc8.c |
pmallick | 0:e8a1ba50c46b | 3 | * @brief Source file of CRC-8 computation. |
pmallick | 0:e8a1ba50c46b | 4 | * @author Darius Berghe (darius.berghe@analog.com) |
pmallick | 0:e8a1ba50c46b | 5 | ******************************************************************************** |
pmallick | 0:e8a1ba50c46b | 6 | * Copyright 2020(c) Analog Devices, Inc. |
pmallick | 0:e8a1ba50c46b | 7 | * |
pmallick | 0:e8a1ba50c46b | 8 | * All rights reserved. |
pmallick | 0:e8a1ba50c46b | 9 | * |
pmallick | 0:e8a1ba50c46b | 10 | * Redistribution and use in source and binary forms, with or without |
pmallick | 0:e8a1ba50c46b | 11 | * modification, are permitted provided that the following conditions are met: |
pmallick | 0:e8a1ba50c46b | 12 | * - Redistributions of source code must retain the above copyright |
pmallick | 0:e8a1ba50c46b | 13 | * notice, this list of conditions and the following disclaimer. |
pmallick | 0:e8a1ba50c46b | 14 | * - Redistributions in binary form must reproduce the above copyright |
pmallick | 0:e8a1ba50c46b | 15 | * notice, this list of conditions and the following disclaimer in |
pmallick | 0:e8a1ba50c46b | 16 | * the documentation and/or other materials provided with the |
pmallick | 0:e8a1ba50c46b | 17 | * distribution. |
pmallick | 0:e8a1ba50c46b | 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
pmallick | 0:e8a1ba50c46b | 19 | * contributors may be used to endorse or promote products derived |
pmallick | 0:e8a1ba50c46b | 20 | * from this software without specific prior written permission. |
pmallick | 0:e8a1ba50c46b | 21 | * - The use of this software may or may not infringe the patent rights |
pmallick | 0:e8a1ba50c46b | 22 | * of one or more patent holders. This license does not release you |
pmallick | 0:e8a1ba50c46b | 23 | * from the requirement that you obtain separate licenses from these |
pmallick | 0:e8a1ba50c46b | 24 | * patent holders to use this software. |
pmallick | 0:e8a1ba50c46b | 25 | * - Use of the software either in source or binary form, must be run |
pmallick | 0:e8a1ba50c46b | 26 | * on or directly connected to an Analog Devices Inc. component. |
pmallick | 0:e8a1ba50c46b | 27 | * |
pmallick | 0:e8a1ba50c46b | 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
pmallick | 0:e8a1ba50c46b | 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
pmallick | 0:e8a1ba50c46b | 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
pmallick | 0:e8a1ba50c46b | 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
pmallick | 0:e8a1ba50c46b | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
pmallick | 0:e8a1ba50c46b | 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
pmallick | 0:e8a1ba50c46b | 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
pmallick | 0:e8a1ba50c46b | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
pmallick | 0:e8a1ba50c46b | 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
pmallick | 0:e8a1ba50c46b | 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
pmallick | 0:e8a1ba50c46b | 38 | *******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 39 | #include "crc8.h" |
pmallick | 0:e8a1ba50c46b | 40 | |
pmallick | 0:e8a1ba50c46b | 41 | /***************************************************************************//** |
pmallick | 0:e8a1ba50c46b | 42 | * @brief Creates the CRC-8 lookup table for a given polynomial. |
pmallick | 0:e8a1ba50c46b | 43 | * |
pmallick | 0:e8a1ba50c46b | 44 | * @param table - Pointer to a CRC-8 lookup table to write to. |
pmallick | 0:e8a1ba50c46b | 45 | * @param polynomial - msb-first representation of desired polynomial. |
pmallick | 0:e8a1ba50c46b | 46 | * |
pmallick | 0:e8a1ba50c46b | 47 | * Polynomials in CRC algorithms are typically represented as shown below. |
pmallick | 0:e8a1ba50c46b | 48 | * |
pmallick | 0:e8a1ba50c46b | 49 | * poly = x^8 + x^2 + x^1 + 1 |
pmallick | 0:e8a1ba50c46b | 50 | * |
pmallick | 0:e8a1ba50c46b | 51 | * Using msb-first direction, x^7 maps to the msb. |
pmallick | 0:e8a1ba50c46b | 52 | * |
pmallick | 0:e8a1ba50c46b | 53 | * msb first: poly = (1)00000111 = 0x07 |
pmallick | 0:e8a1ba50c46b | 54 | * |
pmallick | 0:e8a1ba50c46b | 55 | * @return None. |
pmallick | 0:e8a1ba50c46b | 56 | *******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 57 | void crc8_populate_msb(uint8_t * table, const uint8_t polynomial) |
pmallick | 0:e8a1ba50c46b | 58 | { |
pmallick | 0:e8a1ba50c46b | 59 | if (!table) |
pmallick | 0:e8a1ba50c46b | 60 | return; |
pmallick | 0:e8a1ba50c46b | 61 | |
pmallick | 0:e8a1ba50c46b | 62 | for (int16_t n = 0; n < CRC8_TABLE_SIZE; n++) { |
pmallick | 0:e8a1ba50c46b | 63 | uint8_t currByte = (uint8_t)n; |
pmallick | 0:e8a1ba50c46b | 64 | for (uint8_t bit = 0; bit < 8; bit++) { |
pmallick | 0:e8a1ba50c46b | 65 | if ((currByte & 0x80) != 0) { |
pmallick | 0:e8a1ba50c46b | 66 | currByte <<= 1; |
pmallick | 0:e8a1ba50c46b | 67 | currByte ^= polynomial; |
pmallick | 0:e8a1ba50c46b | 68 | } else { |
pmallick | 0:e8a1ba50c46b | 69 | currByte <<= 1; |
pmallick | 0:e8a1ba50c46b | 70 | } |
pmallick | 0:e8a1ba50c46b | 71 | } |
pmallick | 0:e8a1ba50c46b | 72 | table[n] = currByte; |
pmallick | 0:e8a1ba50c46b | 73 | } |
pmallick | 0:e8a1ba50c46b | 74 | } |
pmallick | 0:e8a1ba50c46b | 75 | |
pmallick | 0:e8a1ba50c46b | 76 | /***************************************************************************//** |
pmallick | 0:e8a1ba50c46b | 77 | * @brief Computes the CRC-8 over a buffer of data. |
pmallick | 0:e8a1ba50c46b | 78 | * |
pmallick | 0:e8a1ba50c46b | 79 | * @param table - Pointer to a CRC-8 lookup table for the desired polynomial. |
pmallick | 0:e8a1ba50c46b | 80 | * @param pdata - Pointer to 8-bit data buffer. |
pmallick | 0:e8a1ba50c46b | 81 | * @param nbytes - Number of bytes to compute the CRC-8 over. |
pmallick | 0:e8a1ba50c46b | 82 | * @param crc - Initial value for the CRC-8 computation. Can be used to |
pmallick | 0:e8a1ba50c46b | 83 | * cascade calls to this function by providing a previous |
pmallick | 0:e8a1ba50c46b | 84 | * output of this function as the crc parameter. |
pmallick | 0:e8a1ba50c46b | 85 | * |
pmallick | 0:e8a1ba50c46b | 86 | * @return crc - Computed CRC-8 value. |
pmallick | 0:e8a1ba50c46b | 87 | *******************************************************************************/ |
pmallick | 0:e8a1ba50c46b | 88 | uint8_t crc8(const uint8_t * table, const uint8_t *pdata, size_t nbytes, |
pmallick | 0:e8a1ba50c46b | 89 | uint8_t crc) |
pmallick | 0:e8a1ba50c46b | 90 | { |
pmallick | 0:e8a1ba50c46b | 91 | unsigned int idx; |
pmallick | 0:e8a1ba50c46b | 92 | |
pmallick | 0:e8a1ba50c46b | 93 | while (nbytes--) { |
pmallick | 0:e8a1ba50c46b | 94 | idx = (crc ^ *pdata); |
pmallick | 0:e8a1ba50c46b | 95 | crc = (table[idx]) & 0xff; |
pmallick | 0:e8a1ba50c46b | 96 | pdata++; |
pmallick | 0:e8a1ba50c46b | 97 | } |
pmallick | 0:e8a1ba50c46b | 98 | |
pmallick | 0:e8a1ba50c46b | 99 | return crc; |
pmallick | 0:e8a1ba50c46b | 100 | } |