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