foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
mxm_crc8.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file mxm_crc8.c
44  * @author foxBMS Team
45  * @date 2019-02-05 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVERS
49  * @prefix MXM
50  *
51  * @brief CRC8 calculation for Maxim Integrated Monitoring devices
52  *
53  * @details This module supports the calculation of a CRC8 based on the
54  * polynomial described in the Maxim data sheets.
55  * The polynomial is 0xA6.
56  *
57  */
58 
59 /*========== Includes =======================================================*/
60 #include "general.h"
61 
62 #include "mxm_crc8.h"
63 
64 #include "fassert.h"
65 #include "fstd_types.h"
66 
67 #include <stdint.h>
68 
69 /*========== Macros and Definitions =========================================*/
70 
71 /*========== Static Constant and Variable Definitions =======================*/
72 
73 /*========== Extern Constant and Variable Definitions =======================*/
74 
75 /*========== Static Function Prototypes =====================================*/
76 /**
77  * @brief Compute CRC8 (0xA6) with initial value.
78  * @details Computes the CRC8 for the given data with the polynomial 0xA6.
79  * @param[in] pData array-pointer with data to be cyclic redundancy checked
80  * @param[in] lenData length of array
81  * @param[in] crcInit initial CRC-value
82  * @return uint8_t containing the computed CRC
83  */
84 static uint8_t MXM_CRC8WithInitValue(uint16_t *pData, int32_t lenData, uint8_t crcInit);
85 
86 /*========== Static Function Implementations ================================*/
87 static uint8_t MXM_CRC8WithInitValue(uint16_t *pData, int32_t lenData, uint8_t crcInit) {
88  FAS_ASSERT(pData != NULL_PTR);
89  /* AXIVION Routine Generic-MissingParameterAssert: lenData: parameter accepts whole range */
90  /* AXIVION Routine Generic-MissingParameterAssert: crcInit: parameter accepts whole range */
91 
92  /**
93  * @brief Precomputed CRC8-table for polynomial 0xA6
94  * @details This array contains the precomputed results for the
95  * look-up-table-based computation of a CRC8 with polynomial 0xA6.
96  * The polynomial-number represents the coefficients of the polynomial
97  * like this:
98  * 8 7 6 5 4 3 2 1 0
99  * 1 0 1 0 0 1 1 0 1
100  * --> 14D
101  *
102  * This polynomial can also be
103  * called 0x14D according to the notation (0xA6 << 1).
104  */
105  const uint8_t mxm_crc8Table[256] = {
106  0x00u, 0x3Eu, 0x7Cu, 0x42u, 0xF8u, 0xC6u, 0x84u, 0xBAu, 0x95u, 0xABu, 0xE9u, 0xD7u, 0x6Du, 0x53u, 0x11u, 0x2Fu,
107  0x4Fu, 0x71u, 0x33u, 0x0Du, 0xB7u, 0x89u, 0xCBu, 0xF5u, 0xDAu, 0xE4u, 0xA6u, 0x98u, 0x22u, 0x1Cu, 0x5Eu, 0x60u,
108  0x9Eu, 0xA0u, 0xE2u, 0xDCu, 0x66u, 0x58u, 0x1Au, 0x24u, 0x0Bu, 0x35u, 0x77u, 0x49u, 0xF3u, 0xCDu, 0x8Fu, 0xB1u,
109  0xD1u, 0xEFu, 0xADu, 0x93u, 0x29u, 0x17u, 0x55u, 0x6Bu, 0x44u, 0x7Au, 0x38u, 0x06u, 0xBCu, 0x82u, 0xC0u, 0xFEu,
110  0x59u, 0x67u, 0x25u, 0x1Bu, 0xA1u, 0x9Fu, 0xDDu, 0xE3u, 0xCCu, 0xF2u, 0xB0u, 0x8Eu, 0x34u, 0x0Au, 0x48u, 0x76u,
111  0x16u, 0x28u, 0x6Au, 0x54u, 0xEEu, 0xD0u, 0x92u, 0xACu, 0x83u, 0xBDu, 0xFFu, 0xC1u, 0x7Bu, 0x45u, 0x07u, 0x39u,
112  0xC7u, 0xF9u, 0xBBu, 0x85u, 0x3Fu, 0x01u, 0x43u, 0x7Du, 0x52u, 0x6Cu, 0x2Eu, 0x10u, 0xAAu, 0x94u, 0xD6u, 0xE8u,
113  0x88u, 0xB6u, 0xF4u, 0xCAu, 0x70u, 0x4Eu, 0x0Cu, 0x32u, 0x1Du, 0x23u, 0x61u, 0x5Fu, 0xE5u, 0xDBu, 0x99u, 0xA7u,
114  0xB2u, 0x8Cu, 0xCEu, 0xF0u, 0x4Au, 0x74u, 0x36u, 0x08u, 0x27u, 0x19u, 0x5Bu, 0x65u, 0xDFu, 0xE1u, 0xA3u, 0x9Du,
115  0xFDu, 0xC3u, 0x81u, 0xBFu, 0x05u, 0x3Bu, 0x79u, 0x47u, 0x68u, 0x56u, 0x14u, 0x2Au, 0x90u, 0xAEu, 0xECu, 0xD2u,
116  0x2Cu, 0x12u, 0x50u, 0x6Eu, 0xD4u, 0xEAu, 0xA8u, 0x96u, 0xB9u, 0x87u, 0xC5u, 0xFBu, 0x41u, 0x7Fu, 0x3Du, 0x03u,
117  0x63u, 0x5Du, 0x1Fu, 0x21u, 0x9Bu, 0xA5u, 0xE7u, 0xD9u, 0xF6u, 0xC8u, 0x8Au, 0xB4u, 0x0Eu, 0x30u, 0x72u, 0x4Cu,
118  0xEBu, 0xD5u, 0x97u, 0xA9u, 0x13u, 0x2Du, 0x6Fu, 0x51u, 0x7Eu, 0x40u, 0x02u, 0x3Cu, 0x86u, 0xB8u, 0xFAu, 0xC4u,
119  0xA4u, 0x9Au, 0xD8u, 0xE6u, 0x5Cu, 0x62u, 0x20u, 0x1Eu, 0x31u, 0x0Fu, 0x4Du, 0x73u, 0xC9u, 0xF7u, 0xB5u, 0x8Bu,
120  0x75u, 0x4Bu, 0x09u, 0x37u, 0x8Du, 0xB3u, 0xF1u, 0xCFu, 0xE0u, 0xDEu, 0x9Cu, 0xA2u, 0x18u, 0x26u, 0x64u, 0x5Au,
121  0x3Au, 0x04u, 0x46u, 0x78u, 0xC2u, 0xFCu, 0xBEu, 0x80u, 0xAFu, 0x91u, 0xD3u, 0xEDu, 0x57u, 0x69u, 0x2Bu, 0x15u,
122  };
123 
124  uint8_t crc = crcInit;
125  int32_t len = lenData;
126  uint16_t *data = pData;
127  while (len > 0) {
128  /* The lookup table on this algorithm is not intended for values larger
129  than uint8_t, SPI transmissions have size uint16_t due to the HAL */
130  FAS_ASSERT(*data <= (uint8_t)UINT8_MAX);
131  crc = mxm_crc8Table[*data ^ crc];
132  data++;
133  len--;
134  }
135  return crc;
136 }
137 
138 /*========== Extern Function Implementations ================================*/
139 
140 extern uint8_t MXM_CRC8(uint16_t *pData, int32_t lenData) {
141  FAS_ASSERT(pData != NULL_PTR);
142  /* AXIVION Routine Generic-MissingParameterAssert: lenData: parameter accepts whole range */
143 
144  return MXM_CRC8WithInitValue(pData, lenData, 0);
145 }
146 
148  /* AXIVION Disable Style Generic-NoMagicNumbers: This test function uses magic numbers to test predefined values. */
149  uint16_t testSequence1[4] = {0x02u, 0x12u, 0xB1u, 0xB2u};
150  const uint8_t sequence1Result = MXM_CRC8(testSequence1, 4);
151  FAS_ASSERT(sequence1Result == 0xC4u);
152 
153  uint16_t testSequence2[3] = {0x03u, 0x12u, 0x00u};
154  const uint8_t sequence2Result = MXM_CRC8(testSequence2, 3);
155  FAS_ASSERT(sequence2Result == 0xCBu);
156 
157  uint16_t testSequence3[10] = {0x02u, 0x5Bu, 0x12u, 0x42u, 0xFFu, 0xD3u, 0x13u, 0x77u, 0xA1u, 0x31u};
158  const uint8_t sequence3Result = MXM_CRC8(testSequence3, 10);
159  FAS_ASSERT(sequence3Result == 0x7Eu);
160 
161  uint16_t testSequence4[3] = {0x03u, 0x66u, 0x00u};
162  const uint8_t sequence4Result = MXM_CRC8(testSequence4, 3);
163  FAS_ASSERT(sequence4Result == 0x43u);
164 
165  /* AXIVION Enable Style Generic-NoMagicNumbers: */
166 
167  return STD_OK;
168 }
169 
170 /*========== Externalized Static Function Implementations (Unit Test) =======*/
171 #ifdef UNITY_UNIT_TEST
172 #endif
Assert macro implementation.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
Definition of foxBMS standard types.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
General macros and definitions for the whole platform.
#define GEN_MUST_CHECK_RETURN
Allows functions to generate warnings in GCC for unused returns.
Definition: general.h:87
uint8_t MXM_CRC8(uint16_t *pData, int32_t lenData)
Compute CRC8 with initial value set to 0x00.
Definition: mxm_crc8.c:140
STD_RETURN_TYPE_e GEN_MUST_CHECK_RETURN MXM_CRC8SelfTest(void)
Test the CRC8-algorithm with a known pattern.
Definition: mxm_crc8.c:147
static uint8_t MXM_CRC8WithInitValue(uint16_t *pData, int32_t lenData, uint8_t crcInit)
Compute CRC8 (0xA6) with initial value.
Definition: mxm_crc8.c:87
Headers for the CRC8 calculation for Maxim Integrated Monitoring devices.