2017-03-30 17:54:29 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
* @brief CRC 16 computation function
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __CRC16_H
|
|
|
|
#define __CRC16_H
|
|
|
|
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2017-03-30 17:54:29 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generic function for computing CRC 16
|
|
|
|
*
|
|
|
|
* Compute CRC 16 by passing in the address of the input, the input length
|
|
|
|
* and polynomial used in addition to the initial value.
|
|
|
|
*
|
|
|
|
* @param src Input bytes for the computation
|
|
|
|
* @param len Length of the input in bytes
|
|
|
|
* @param polynomial The polynomial to use omitting the leading x^16
|
|
|
|
* coefficient
|
|
|
|
* @param initial_value Initial value for the CRC computation
|
|
|
|
*
|
|
|
|
* @return The computed CRC16 value
|
|
|
|
*/
|
|
|
|
uint16_t crc16(const uint8_t *src, size_t len, uint16_t polynomial,
|
|
|
|
uint16_t initial_value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compute CCITT variant of CRC 16
|
|
|
|
*
|
|
|
|
* CCITT variant of CRC 16 is using 0x1021 as its polynomial with the initial
|
|
|
|
* value set to 0xffff.
|
|
|
|
*
|
|
|
|
* @param src Input bytes for the computation
|
|
|
|
* @param len Length of the input in bytes
|
|
|
|
*
|
|
|
|
* @return The computed CRC16 value
|
|
|
|
*/
|
|
|
|
static inline uint16_t crc16_ccitt(const uint8_t *src, size_t len)
|
|
|
|
{
|
|
|
|
return crc16(src, len, 0x1021, 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compute ANSI variant of CRC 16
|
|
|
|
*
|
|
|
|
* ANSI variant of CRC 16 is using 0x8005 as its polynomial with the initial
|
|
|
|
* value set to 0xffff.
|
|
|
|
*
|
|
|
|
* @param src Input bytes for the computation
|
|
|
|
* @param len Length of the input in bytes
|
|
|
|
*
|
|
|
|
* @return The computed CRC16 value
|
|
|
|
*/
|
|
|
|
static inline uint16_t crc16_ansi(const uint8_t *src, size_t len)
|
|
|
|
{
|
|
|
|
return crc16(src, len, 0x8005, 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|