Skip to content

Commit 3d2eed4

Browse files
committed
crc8: update variants to be capable of calculation from data in chunks
1 parent 7b24b5f commit 3d2eed4

27 files changed

+417
-155
lines changed

Inc/crc/crc8_variants/crc8.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@
4848
*
4949
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
5050
* @param[in] crc_length The length of the data block in bytes.
51+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
5152
*
5253
* @return The calculated CRC checksum.
5354
*/
5455
uint8_t Crc8(
5556
const uint8_t* crc_data_ptr,
56-
uint32_t crc_length
57+
uint32_t crc_length,
58+
const uint8_t* last_crc_ptr
5759
);
5860

5961
#endif /* UTILITY_CRC8_H_ */

Inc/crc/crc8_variants/crc8_8h2f.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,21 @@
4343
* This function calculates an 8-bit CRC checksum for the given block of data
4444
* using the 8H2F polynomial (0x2F). The 'crc_data_ptr' parameter should be
4545
* a pointer to the block of data to calculate the checksum for. The 'crc_length'
46-
* parameter is the length of the data block in bytes. The 'final_xor' parameter
47-
* indicates whether to perform a final XOR operation on the calculated CRC
48-
* value before returning it. The calculation is performed using a lookup table for efficiency.
46+
* parameter is the length of the data block in bytes.
47+
* The calculation is performed using a lookup table for efficiency.
4948
*
5049
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
5150
* @param[in] crc_length The length of the data block in bytes.
52-
* @param[in] final_xor Whether to perform a final XOR operation on the calculated CRC value.
51+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
52+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
5353
*
5454
* @return The calculated CRC checksum.
5555
*/
5656
uint8_t Crc8_8h2f(
5757
const uint8_t* crc_data_ptr,
5858
uint32_t crc_length,
59-
bool final_xor
59+
bool final_crc,
60+
const uint8_t* last_crc_ptr
6061
);
6162

6263
#endif /* UTILITY_CRC8_8H2F_H_ */

Inc/crc/crc8_variants/crc8_cdma2000.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@
4949
*
5050
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
5151
* @param[in] crc_length The length of the data block in bytes.
52+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
5253
*
5354
* @return The calculated CRC checksum.
5455
*/
5556
uint8_t Crc8_cdma2000(
5657
const uint8_t* crc_data_ptr,
57-
uint32_t crc_length
58+
uint32_t crc_length,
59+
const uint8_t* last_crc_ptr
5860
);
5961

6062
#endif /* UTILITY_CRC8_CDMA2000_H_ */

Inc/crc/crc8_variants/crc8_darc.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,16 @@
4747
*
4848
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4949
* @param[in] crc_length The length of the data block in bytes.
50+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
51+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
5052
*
5153
* @return The calculated CRC-8 checksum.
5254
*/
5355
uint8_t Crc8_darc(
5456
const uint8_t* crc_data_ptr,
55-
uint32_t crc_length
57+
uint32_t crc_length,
58+
bool final_crc,
59+
const uint8_t* last_crc_ptr
5660
);
5761

5862
#endif /* UTILITY_CRC8_DARC_H_ */

Inc/crc/crc8_variants/crc8_dvb_s2.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@
4545
*
4646
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4747
* @param[in] crc_length The length of the data block in bytes.
48+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4849
*
4950
* @return The calculated CRC8-DVB-S2 checksum.
5051
*/
5152
uint8_t Crc8_dvbS2(
5253
const uint8_t* crc_data_ptr,
53-
uint32_t crc_length
54+
uint32_t crc_length,
55+
const uint8_t* last_crc_ptr
5456
);
5557

5658
#endif /* UTILITY_CRC8_DVB_S2_H_ */

Inc/crc/crc8_variants/crc8_ebu.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@
4646
*
4747
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4848
* @param[in] crc_length The length of the data block in bytes.
49+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
50+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4951
*
5052
* @return The calculated CRC-8 checksum.
5153
*/
5254
uint8_t Crc8_ebu(
5355
const uint8_t* crc_data_ptr,
54-
uint32_t crc_length
56+
uint32_t crc_length,
57+
bool final_crc,
58+
const uint8_t* last_crc_ptr
5559
);
5660

5761
#endif /* UTILITY_CRC8_EBU_H_ */

Inc/crc/crc8_variants/crc8_icode.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@
4646
*
4747
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4848
* @param[in] crc_length The length of the data block in bytes.
49+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4950
*
5051
* @return The calculated CRC-8 checksum.
5152
*/
5253
uint8_t Crc8_icode(
5354
const uint8_t* crc_data_ptr,
54-
uint32_t crc_length
55+
uint32_t crc_length,
56+
const uint8_t* last_crc_ptr
5557
);
5658

5759
#endif /* UTILITY_CRC8_ICODE_H_ */

Inc/crc/crc8_variants/crc8_itu.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@
4646
*
4747
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4848
* @param[in] crc_length The length of the data block in bytes.
49+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
50+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4951
*
5052
* @return The calculated CRC-8 checksum.
5153
*/
5254
uint8_t Crc8_itu(
5355
const uint8_t* crc_data_ptr,
5456
uint32_t crc_length,
55-
bool final_xor
57+
bool final_crc,
58+
const uint8_t* last_crc_ptr
5659
);
5760

5861
#endif /* UTILITY_CRC8_ITU_H_ */

Inc/crc/crc8_variants/crc8_maxim.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@
4646
*
4747
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4848
* @param[in] crc_length The length of the data block in bytes.
49+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
50+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4951
*
5052
* @return The calculated CRC-8 checksum.
5153
*/
5254
uint8_t Crc8_maxim(
5355
const uint8_t* crc_data_ptr,
54-
uint32_t crc_length
56+
uint32_t crc_length,
57+
bool final_crc,
58+
const uint8_t* last_crc_ptr
5559
);
5660

5761
#endif /* UTILITY_CRC8_MAXIM_H_ */

Inc/crc/crc8_variants/crc8_rohc.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@
4646
*
4747
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4848
* @param[in] crc_length The length of the data block in bytes.
49+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
50+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4951
*
5052
* @return The calculated CRC-8 checksum.
5153
*/
5254
uint8_t Crc8_rohc(
5355
const uint8_t* crc_data_ptr,
54-
uint32_t crc_length
56+
uint32_t crc_length,
57+
bool final_crc,
58+
const uint8_t* last_crc_ptr
5559
);
5660

5761
#endif /* UTILITY_CRC8_ROHC_H_ */

Inc/crc/crc8_variants/crc8_sae_j1850.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@
4343
* This function calculates an 8-bit CRC checksum for the given block of data
4444
* using the SAE J1850 polynomial (0x1D). The 'crc_data_ptr' parameter should be
4545
* a pointer to the block of data to calculate the checksum for. The 'crc_length'
46-
* parameter is the length of the data block in bytes. The 'final_xor' parameter
47-
* indicates whether to perform a final XOR operation on the calculated CRC
48-
* value before returning it. The calculation is performed using a lookup table for efficiency.
46+
* parameter is the length of the data block in bytes.
47+
* The calculation is performed using a lookup table for efficiency.
4948
*
5049
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
5150
* @param[in] crc_length The length of the data block in bytes.
52-
* @param[in] final_xor Whether to perform a final XOR operation on the calculated CRC value.
51+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
52+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
5353
*/
5454
uint8_t Crc8_saeJ1850(
5555
const uint8_t* crc_data_ptr,
5656
uint32_t crc_length,
57-
bool final_xor
57+
bool final_crc,
58+
const uint8_t* last_crc_ptr
5859
);
5960

6061
#endif /* UTILITY_CRC8_SAE_J1850_H_ */

Inc/crc/crc8_variants/crc8_sae_j1850_zero.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@
4646
*
4747
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4848
* @param[in] crc_length The length of the data block in bytes.
49+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4950
*
5051
* @return The calculated CRC-8 checksum.
5152
*/
5253
uint8_t Crc8_saeJ1850Zero(
5354
const uint8_t* crc_data_ptr,
54-
uint32_t crc_length
55+
uint32_t crc_length,
56+
const uint8_t* last_crc_ptr
5557
);
5658

5759
#endif /* UTILITY_CRC8_SAE_J1850_ZERO_H_ */

Inc/crc/crc8_variants/crc8_wcdma.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@
4646
*
4747
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
4848
* @param[in] crc_length The length of the data block in bytes.
49+
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
50+
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
4951
*
5052
* @return The calculated CRC-8 checksum.
5153
*/
5254
uint8_t Crc8_wcdma(
5355
const uint8_t* crc_data_ptr,
54-
uint32_t crc_length
56+
uint32_t crc_length,
57+
bool final_crc,
58+
const uint8_t* last_crc_ptr
5559
);
5660

5761
#endif /* UTILITY_CRC8_WCDMA_H_ */

Src/crc/crc8_variants/crc8.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@
4040
#define FINAL_XOR_VALUE (0x0U)
4141
#define REFLECTED_OUTPUT (false)
4242
#define REFLECTED_INPUT (false)
43+
#define FINAL_XOR (false)
4344

4445
uint8_t
4546
Crc8(
4647
const uint8_t* crc_data_ptr,
47-
uint32_t crc_length) {
48+
uint32_t crc_length,
49+
const uint8_t* last_crc_ptr) {
4850

4951
/* Table for CRC-8 (Polynomial 0x07) */
5052
static const uint8_t crc_table[256] = {
@@ -66,14 +68,20 @@ Crc8(
6668
0xDEU, 0xD9U, 0xD0U, 0xD7U, 0xC2U, 0xC5U, 0xCCU, 0xCBU, 0xE6U, 0xE1U, 0xE8U, 0xEFU, 0xFAU, 0xFDU, 0xF4U, 0xF3U
6769
};
6870

71+
uint32_t crc_initial_value = INITIAL_CRC8_VALUE;
72+
73+
if (NULL_PTR != last_crc_ptr) {
74+
crc_initial_value = *last_crc_ptr;
75+
}
76+
6977
return Crc8Base(
7078
crc_table,
7179
crc_data_ptr,
7280
crc_length,
73-
INITIAL_CRC8_VALUE,
81+
crc_initial_value,
7482
FINAL_XOR_VALUE,
7583
REFLECTED_OUTPUT,
7684
REFLECTED_INPUT,
77-
false
85+
FINAL_XOR
7886
);
7987
}

Src/crc/crc8_variants/crc8_8h2f.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
#define FINAL_XOR_VALUE (0xFFU)
4141
#define REFLECTED_OUTPUT (false)
4242
#define REFLECTED_INPUT (false)
43+
#define FINAL_XOR (true)
4344

4445
uint8_t
4546
Crc8_8h2f(
4647
const uint8_t* crc_data_ptr,
4748
uint32_t crc_length,
48-
bool final_xor) {
49+
bool final_crc,
50+
const uint8_t* last_crc_ptr) {
4951

5052
/* Table for CRC-8 8H2F (Polynomial 0x2F) */
5153
static const uint8_t crc_table[256] = {
@@ -67,11 +69,22 @@ Crc8_8h2f(
6769
0xD8U, 0xF7U, 0x86U, 0xA9U, 0x64U, 0x4BU, 0x3AU, 0x15U, 0x8FU, 0xA0U, 0xD1U, 0xFEU, 0x33U, 0x1CU, 0x6DU, 0x42U
6870
};
6971

72+
bool final_xor = false;
73+
uint32_t crc_initial_value = INITIAL_CRC8_VALUE;
74+
75+
if (NULL_PTR != last_crc_ptr) {
76+
crc_initial_value = *last_crc_ptr;
77+
}
78+
79+
if (final_crc) {
80+
final_xor = FINAL_XOR;
81+
}
82+
7083
return Crc8Base(
7184
crc_table,
7285
crc_data_ptr,
7386
crc_length,
74-
INITIAL_CRC8_VALUE,
87+
crc_initial_value,
7588
FINAL_XOR_VALUE,
7689
REFLECTED_OUTPUT,
7790
REFLECTED_INPUT,

Src/crc/crc8_variants/crc8_cdma2000.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@
4040
#define FINAL_XOR_VALUE (0x00U)
4141
#define REFLECTED_OUTPUT (false)
4242
#define REFLECTED_INPUT (false)
43+
#define FINAL_XOR (false)
4344

4445
uint8_t
4546
Crc8_cdma2000(
4647
const uint8_t* crc_data_ptr,
47-
uint32_t crc_length) {
48+
uint32_t crc_length,
49+
const uint8_t* last_crc_ptr) {
4850

4951
/* Table for CRC-8 CDMA2000 (Polynomial 0x9B) */
5052
static const uint8_t crc_table[256] = {
@@ -66,14 +68,20 @@ Crc8_cdma2000(
6668
0x95U, 0x0EU, 0x38U, 0xA3U, 0x54U, 0xCFU, 0xF9U, 0x62U, 0x8CU, 0x17U, 0x21U, 0xBAU, 0x4DU, 0xD6U, 0xE0U, 0x7BU
6769
};
6870

71+
uint32_t crc_initial_value = INITIAL_CRC8_VALUE;
72+
73+
if (NULL_PTR != last_crc_ptr) {
74+
crc_initial_value = *last_crc_ptr;
75+
}
76+
6977
return Crc8Base(
7078
crc_table,
7179
crc_data_ptr,
7280
crc_length,
73-
INITIAL_CRC8_VALUE,
81+
crc_initial_value,
7482
FINAL_XOR_VALUE,
7583
REFLECTED_OUTPUT,
7684
REFLECTED_INPUT,
77-
false
85+
FINAL_XOR
7886
);
7987
}

0 commit comments

Comments
 (0)