fix(esp_crc): clarify CRC helper usage documentation

Document the implicit bitwise inversion behavior in the CRC ROM wrappers
and add regression tests covering continuous-buffer examples.

Closes https://github.com/espressif/esp-idf/issues/18715
This commit is contained in:
morris
2026-06-30 15:37:06 +08:00
parent 8b8a2115e4
commit aa4532395a
6 changed files with 180 additions and 70 deletions
+26 -6
View File
@@ -14,10 +14,25 @@ extern "C" {
// This header is only a wrapper on ROM CRC API
#include "esp_rom_crc.h"
/**
* @brief Convenience wrappers for the ROM CRC helpers.
*
* These wrappers keep the ROM helper behavior unchanged: each call bitwise
* inverts the CRC value before and after processing. To start a calculation,
* pass the bitwise inverse of the CRC flavor's initial value. To continue over
* another buffer, pass the previous return value. After the last chunk, bitwise
* invert the return value again and then apply the CRC flavor's final XOR.
*
* The correct initial value and final XOR depend on the CRC flavor that you are
* implementing, so there is no single universal `INITIAL` or `FINAL_XOR`
* constant for a given helper.
*/
/**
* @brief CRC32 value in little endian.
*
* @param crc: Initial CRC value (result of last calculation or 0 for the first time)
* @param crc: Bitwise inverse of the CRC variant's initial value on the first
* call, or the previous return value to continue a calculation
* @param buf: Data buffer that used to calculate the CRC value
* @param len: Length of the data buffer
* @return CRC32 value
@@ -30,7 +45,8 @@ static inline uint32_t esp_crc32_le(uint32_t crc, uint8_t const *buf, uint32_t l
/**
* @brief CRC32 value in big endian.
*
* @param crc: Initial CRC value (result of last calculation or 0 for the first time)
* @param crc: Bitwise inverse of the CRC variant's initial value on the first
* call, or the previous return value to continue a calculation
* @param buf: Data buffer that used to calculate the CRC value
* @param len: Length of the data buffer
* @return CRC32 value
@@ -43,7 +59,8 @@ static inline uint32_t esp_crc32_be(uint32_t crc, uint8_t const *buf, uint32_t l
/**
* @brief CRC16 value in little endian.
*
* @param crc: Initial CRC value (result of last calculation or 0 for the first time)
* @param crc: Bitwise inverse of the CRC variant's initial value on the first
* call, or the previous return value to continue a calculation
* @param buf: Data buffer that used to calculate the CRC value
* @param len: Length of the data buffer
* @return CRC16 value
@@ -56,7 +73,8 @@ static inline uint16_t esp_crc16_le(uint16_t crc, uint8_t const *buf, uint32_t l
/**
* @brief CRC16 value in big endian.
*
* @param crc: Initial CRC value (result of last calculation or 0 for the first time)
* @param crc: Bitwise inverse of the CRC variant's initial value on the first
* call, or the previous return value to continue a calculation
* @param buf: Data buffer that used to calculate the CRC value
* @param len: Length of the data buffer
* @return CRC16 value
@@ -69,7 +87,8 @@ static inline uint16_t esp_crc16_be(uint16_t crc, uint8_t const *buf, uint32_t l
/**
* @brief CRC8 value in little endian.
*
* @param crc: Initial CRC value (result of last calculation or 0 for the first time)
* @param crc: Bitwise inverse of the CRC variant's initial value on the first
* call, or the previous return value to continue a calculation
* @param buf: Data buffer that used to calculate the CRC value
* @param len: Length of the data buffer
* @return CRC8 value
@@ -82,7 +101,8 @@ static inline uint8_t esp_crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len)
/**
* @brief CRC8 value in big endian.
*
* @param crc: Initial CRC value (result of last calculation or 0 for the first time)
* @param crc: Bitwise inverse of the CRC variant's initial value on the first
* call, or the previous return value to continue a calculation
* @param buf: Data buffer that used to calculate the CRC value
* @param len: Length of the data buffer
* @return CRC8 value
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "esp_crc.h"
#include "esp_random.h"
/* Note: these are just sanity tests, the implementation of esp_random() relies on getentropy() on Linux.
@@ -123,6 +124,32 @@ TEST_CASE("esp_fill_random() fills exactly 257 bytes", "[random]")
TEST_ASSERT_GREATER_THAN(0, one_buf[0]);
}
TEST_CASE("esp_crc32_le supports continuous buffers with standard CRC-32 parameters", "[crc]")
{
static const uint8_t buf0[] = "1234";
static const uint8_t buf1[] = "56789";
uint32_t crc = (uint32_t)~UINT32_MAX;
crc = esp_crc32_le(crc, buf0, sizeof(buf0) - 1);
crc = esp_crc32_le(crc, buf1, sizeof(buf1) - 1);
crc = ~crc ^ UINT32_MAX;
TEST_ASSERT_EQUAL_HEX32(0xCBF43926, crc);
}
TEST_CASE("esp_crc16_be supports continuous buffers with CRC-16/XMODEM parameters", "[crc]")
{
static const uint8_t buf0[] = "1234";
static const uint8_t buf1[] = "56789";
uint16_t crc = (uint16_t)~0x0000;
crc = esp_crc16_be(crc, buf0, sizeof(buf0) - 1);
crc = esp_crc16_be(crc, buf1, sizeof(buf1) - 1);
crc = (uint16_t)(~crc ^ 0x0000);
TEST_ASSERT_EQUAL_HEX16(0x31C3, crc);
}
void app_main(void)
{
printf("Running hw support linux API host test app");