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