Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
crc8.c
00001 /***************************************************************************//** 00002 * @file crc8.c 00003 * @brief Source file of CRC-8 computation. 00004 * @author Darius Berghe (darius.berghe@analog.com) 00005 ******************************************************************************** 00006 * Copyright 2020(c) Analog Devices, Inc. 00007 * 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions are met: 00012 * - Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * - Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in 00016 * the documentation and/or other materials provided with the 00017 * distribution. 00018 * - Neither the name of Analog Devices, Inc. nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * - The use of this software may or may not infringe the patent rights 00022 * of one or more patent holders. This license does not release you 00023 * from the requirement that you obtain separate licenses from these 00024 * patent holders to use this software. 00025 * - Use of the software either in source or binary form, must be run 00026 * on or directly connected to an Analog Devices Inc. component. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 00029 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 00030 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00031 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00033 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 00034 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00036 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00037 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 *******************************************************************************/ 00039 #include "crc8.h" 00040 00041 /***************************************************************************//** 00042 * @brief Creates the CRC-8 lookup table for a given polynomial. 00043 * 00044 * @param table - Pointer to a CRC-8 lookup table to write to. 00045 * @param polynomial - msb-first representation of desired polynomial. 00046 * 00047 * Polynomials in CRC algorithms are typically represented as shown below. 00048 * 00049 * poly = x^8 + x^2 + x^1 + 1 00050 * 00051 * Using msb-first direction, x^7 maps to the msb. 00052 * 00053 * msb first: poly = (1)00000111 = 0x07 00054 * 00055 * @return None. 00056 *******************************************************************************/ 00057 void crc8_populate_msb(uint8_t * table, const uint8_t polynomial) 00058 { 00059 if (!table) 00060 return; 00061 00062 for (int16_t n = 0; n < CRC8_TABLE_SIZE; n++) { 00063 uint8_t currByte = (uint8_t)n; 00064 for (uint8_t bit = 0; bit < 8; bit++) { 00065 if ((currByte & 0x80) != 0) { 00066 currByte <<= 1; 00067 currByte ^= polynomial; 00068 } else { 00069 currByte <<= 1; 00070 } 00071 } 00072 table[n] = currByte; 00073 } 00074 } 00075 00076 /***************************************************************************//** 00077 * @brief Computes the CRC-8 over a buffer of data. 00078 * 00079 * @param table - Pointer to a CRC-8 lookup table for the desired polynomial. 00080 * @param pdata - Pointer to 8-bit data buffer. 00081 * @param nbytes - Number of bytes to compute the CRC-8 over. 00082 * @param crc - Initial value for the CRC-8 computation. Can be used to 00083 * cascade calls to this function by providing a previous 00084 * output of this function as the crc parameter. 00085 * 00086 * @return crc - Computed CRC-8 value. 00087 *******************************************************************************/ 00088 uint8_t crc8(const uint8_t * table, const uint8_t *pdata, size_t nbytes, 00089 uint8_t crc) 00090 { 00091 unsigned int idx; 00092 00093 while (nbytes--) { 00094 idx = (crc ^ *pdata); 00095 crc = (table[idx]) & 0xff; 00096 pdata++; 00097 } 00098 00099 return crc; 00100 }
Generated on Tue Jul 12 2022 17:15:46 by
