lib: crc: Add crc8-ccitt implementation

This patch adds crc8-ccitt calculation routine.

CRC8 CCITT is required for Flash Circular Buffer
module (originally mynewt module).

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
This commit is contained in:
Andrzej Puzdrowski 2017-12-13 14:57:38 +01:00 committed by Anas Nashif
commit 9a5a3e06f0
3 changed files with 66 additions and 1 deletions

39
include/crc8.h Normal file
View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2017 Nordic Semiconductor ASA
* Copyright (c) 2015 Runtime Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __CRC8_H_
#define __CRC8_H_
#include <zephyr/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Initial value expected to be used at the beginning of the crc8_ccitt
* computation.
*/
#define CRC8_CCITT_INITIAL_VALUE 0xFF
/**
* @brief Compute CCITT variant of CRC 8
*
* Normal CCITT variant of CRC 8 is using 0x07.
*
* @param initial_value Initial value for the CRC computation
* @param buf Input bytes for the computation
* @param len Length of the input in bytes
*
* @return The computed CRC8 value
*/
u8_t crc8_ccitt(u8_t initial_value, void *buf, int len);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1 +1 @@
zephyr_sources(crc16_sw.c)
zephyr_sources(crc16_sw.c crc8_sw.c)

26
lib/crc/crc8_sw.c Normal file
View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2017 Nordic Semiconductor ASA
* Copyright (c) 2015 Runtime Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "crc8.h"
static u8_t crc8_ccitt_small_table[16] = {
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d
};
u8_t crc8_ccitt(u8_t val, void *buf, int cnt)
{
int i;
u8_t *p = buf;
for (i = 0; i < cnt; i++) {
val ^= p[i];
val = (val << 4) ^ crc8_ccitt_small_table[val >> 4];
val = (val << 4) ^ crc8_ccitt_small_table[val >> 4];
}
return val;
}