this is testing

Committer:
pmallick
Date:
Thu Jan 14 18:54:16 2021 +0530
Revision:
0:3afcd581558d
this is testing

Who changed what in which revision?

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