ports/alif: Add support for CRC32.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
This commit is contained in:
iabdalkader 2025-09-24 19:33:42 +02:00
parent cec54bab3e
commit 9704ed8c62

View File

@ -25,46 +25,78 @@
#include "global_map.h" #include "global_map.h"
#include "omv_crc.h" #include "omv_crc.h"
#define CRC0 ((CRC_Type *)CRC0_BASE) // There seems to be a mismatch between how the Alif HW CRC32 algorithm
// works and the other HW CRCs and the software implementation.
// Just leaving this here just in case it works on a different series.
#if 0
#define CRC0 ((CRC_Type *) CRC0_BASE)
static bool crc_initialized = false; static bool crc32_initialized = false;
static void crc_calculate(CRC_Type *crc, const void *buf, uint32_t len, uint32_t *value);
void omv_crc_init(void) { static void omv_crc32_init(void) {
crc_clear_config(CRC0); CRC0->CRC_CONTROL = CRC_32C |
crc_enable_16bit(CRC0); CRC_CUSTOM_POLY |
crc_enable_custom_poly(CRC0); CRC_ALGO_32_BIT_SIZE;
crc_set_custom_poly(CRC0, OMV_CRC_POLY); CRC0->CRC_SEED = OMV_CRC32_INIT;
crc_set_seed(CRC0, OMV_CRC_INIT); CRC0->CRC_POLY_CUSTOM = OMV_CRC32_POLY;
crc_enable(CRC0); crc32_initialized = true;
crc_initialized = true;
} }
omv_crc_t omv_crc_start(const void *buf, size_t size) { uint32_t omv_crc32_start(const void *buf, size_t len) {
if (!crc_initialized) { uint32_t result = OMV_CRC32_INIT;
omv_crc_init();
if (!crc32_initialized) {
omv_crc32_init();
} }
if (size == 0) { if (len == 0) {
return OMV_CRC_INIT; return OMV_CRC32_INIT;
} }
uint32_t result = 0; crc_calculate(CRC0, buf, len, &result);
crc_calculate_16bit(CRC0, buf, size, &result); return result;
return (omv_crc_t)result;
} }
omv_crc_t omv_crc_update(omv_crc_t crc, const void *buf, size_t size) { uint32_t omv_crc32_update(uint32_t crc, const void *buf, size_t len) {
if (!crc_initialized) { uint32_t result = crc;
omv_crc_init();
if (!crc32_initialized) {
omv_crc32_init();
} }
if (size == 0) { if (len == 0) {
return crc; return crc;
} }
// Set current CRC value and calculate incrementally crc_calculate(CRC0, buf, len, &result);
crc_set_seed(CRC0, crc); return result;
uint32_t result = 0;
crc_calculate_16bit(CRC0, buf, size, &result);
return (omv_crc_t)result;
} }
static void crc_calculate(CRC_Type *crc, const void *buf, uint32_t len, uint32_t *value) {
const uint8_t *data = (const uint8_t *) buf;
if ((len % 4) == 0) {
// Use hardware only if length is multiple of 4
crc->CRC_SEED = *value;
crc->CRC_CONTROL |= CRC_INIT_BIT;
// Hardware path - process all data as 32-bit words
const uint32_t *data32 = (const uint32_t *) data;
for (uint32_t i = 0; i < len / 4; i++) {
crc->CRC_DATA_IN_32_0 = data32[i];
}
*value = crc->CRC_OUT;
} else {
// Software path - use lookup table
uint32_t result = *value;
extern const uint32_t crc32_table[256];
for (uint32_t i = 0; i < len; i++) {
uint8_t index = (result >> 24) ^ data[i];
result = (result << 8) ^ crc32_table[index];
}
*value = result;
}
}
#endif