px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_crc8.h
1#ifndef __PX_CRC8_H__
2#define __PX_CRC8_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2020 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_crc8.h : 8-bit CRC calculator
16 Author(s): Pieter Conradie
17 Creation Date: 2020-10-23
18
19============================================================================= */
20
21/**
22 * @ingroup UTILS
23 * @defgroup PX_CRC8 px_crc8.h : 8-bit CRC calculator
24 *
25 * 8-bit CRC (Cyclic Redundancy Check) calculator for checksums.
26 *
27 * File(s):
28 * - utils/inc/px_crc8.h
29 * - utils/inc/px_crc8_cfg_template.h
30 * - utils/src/px_crc8.c
31 *
32 * A CRC is an error-detecting code that is used for data integrity checks.
33 *
34 * @see http://en.wikipedia.org/wiki/Cyclic_redundancy_check
35 * @see https://crccalc.com
36 *
37 * @{
38 */
39
40/* _____STANDARD INCLUDES____________________________________________________ */
41
42/* _____PROJECT INCLUDES_____________________________________________________ */
43#include "px_defs.h"
44
45// Include project specific configuration. See "px_crc8_cfg_template.h"
46#include "px_crc8_cfg.h"
47
48// Check that all project specific options have been specified in "px_crc8_cfg.h"
49#if ( !defined(PX_CRC8_RAM_TABLE) \
50 || !defined(PX_CRC8_ROM_TABLE) )
51 )
52#error "One or more options not defined in 'px_crc8_cfg.h'"
53#endif
54
56#error "Use either RAM or ROM table option, not both"
57#endif
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62/* _____DEFINITIONS__________________________________________________________ */
63/// The generator polynomial CRC8 (normal representation):
64/// x^8 + x^2 + x + 1
65#define PX_CRC8_POLYNOMIAL 0x07
66/// Initial CRC value
67#define PX_CRC8_INIT_VAL 0x00
68
69/* _____TYPE DEFINITIONS_____________________________________________________ */
70
71/* _____GLOBAL VARIABLES_____________________________________________________ */
72
73/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
74/**
75 * Initialise 8-bit CRC calculator.
76 *
77 * This function will initialise the table in RAM with constants to speed up
78 * CRC calculation. For other options it does nothing.
79 */
80void px_crc8_init(void);
81
82/**
83 * Calculate the 8-bit CRC over one byte.
84 *
85 * @param crc The initial CRC to start the calculation with
86 * @param data The value to calculate the CRC over
87 *
88 * @return uint8_t The resultant CRC
89 */
90uint8_t px_crc8_update_u8(uint8_t crc, uint8_t data);
91
92/**
93 * Calculate the 8-bit CRC over a number of bytes.
94 *
95 * @param crc The initial CRC to start the calculation with
96 * @param data Pointer to the data to calculate the CRC over
97 * @param nr_of_bytes The amount of bytes to calculate the CRC over
98 *
99 * @return uint8_t The resultant CRC over the group of bytes
100 */
101uint8_t px_crc8_update_data(uint8_t crc, const void * data, size_t nr_of_bytes);
102
103/* _____MACROS_______________________________________________________________ */
104
105#ifdef __cplusplus
106}
107#endif
108
109/// @}
110#endif
#define PX_CRC8_ROM_TABLE
Use table in ROM to speed up CRC calculation.
uint8_t px_crc8_update_u8(uint8_t crc, uint8_t data)
Calculate the 8-bit CRC over one byte.
Definition: px_crc8.c:107
void px_crc8_init(void)
Initialise 8-bit CRC calculator.
Definition: px_crc8.c:79
uint8_t px_crc8_update_data(uint8_t crc, const void *data, size_t nr_of_bytes)
Calculate the 8-bit CRC over a number of bytes.
Definition: px_crc8.c:131
#define PX_CRC8_RAM_TABLE
Generate table in RAM to speed up CRC calculation.