Compare commits

...

3 Commits

Author SHA1 Message Date
iabdalkader
00ba887043 boards: Enable CRC module for AE3 and N6.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2025-09-25 18:06:00 +02:00
iabdalkader
1b5da3ebfe modules/py_crc: Add hardware-accelerated CRC module.
Optional, small module for testing.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2025-09-25 18:06:00 +02:00
iabdalkader
9704ed8c62 ports/alif: Add support for CRC32.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2025-09-25 18:05:57 +02:00
5 changed files with 165 additions and 29 deletions

View File

@ -174,6 +174,11 @@ MPY_CFLAGS += -DMICROPY_PY_IMU=1
MPY_MKARGS += MICROPY_PY_IMU=1
endif
ifeq ($(MICROPY_PY_CRC), 1)
MPY_CFLAGS += -DMICROPY_PY_CRC=1
MPY_MKARGS += MICROPY_PY_CRC=1
endif
ifeq ($(MICROPY_PY_BTREE), 1)
MPY_CFLAGS += -DMICROPY_PY_BTREE=1
MPY_MKARGS += MICROPY_PY_BTREE=1

View File

@ -20,6 +20,7 @@ MICROPY_PY_TOF = 1
MICROPY_PY_ULAB = 1
MICROPY_PY_DISPLAY = 1
MICROPY_PY_IMU = 1
MICROPY_PY_CRC = 1
MICROPY_PY_AUDIO = 1
MICROPY_PY_ML = 1
MICROPY_PY_ML_TFLM = 1

View File

@ -30,6 +30,7 @@ MICROPY_PY_CSI = 1
MICROPY_PY_CSI_NG = 1
MICROPY_PY_FIR = 1
MICROPY_PY_IMU = 1
MICROPY_PY_CRC = 1
MICROPY_PY_ULAB = 1
MICROPY_PY_DISPLAY = 1
MICROPY_PY_TV = 1

97
modules/py_crc.c Normal file
View File

@ -0,0 +1,97 @@
/*
* SPDX-License-Identifier: MIT
*
* Copyright (C) 2025 OpenMV, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* CRC Python module for testing CRC implementations.
*/
#if MICROPY_PY_CRC
#include "py/runtime.h"
#include "py/obj.h"
#include "omv_crc.h"
// CRC16 function
static mp_obj_t py_crc16(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kwargs) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kwargs, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[0].u_obj, &bufinfo, MP_BUFFER_READ);
uint16_t crc;
if (args[1].u_obj == MP_OBJ_NULL) {
crc = omv_crc16_start(bufinfo.buf, bufinfo.len);
} else {
uint16_t prev_crc = mp_obj_get_int_truncated(args[1].u_obj) & 0xFFFF;
crc = omv_crc16_update(prev_crc, bufinfo.buf, bufinfo.len);
}
return mp_obj_new_int_from_uint(crc);
}
static MP_DEFINE_CONST_FUN_OBJ_KW(py_crc16_obj, 1, py_crc16);
// CRC32 function
static mp_obj_t py_crc32(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kwargs) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kwargs, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[0].u_obj, &bufinfo, MP_BUFFER_READ);
uint32_t crc;
if (args[1].u_obj == MP_OBJ_NULL) {
crc = omv_crc32_start(bufinfo.buf, bufinfo.len);
} else {
uint32_t prev_crc = mp_obj_get_int_truncated(args[1].u_obj);
crc = omv_crc32_update(prev_crc, bufinfo.buf, bufinfo.len);
}
return mp_obj_new_int_from_uint(crc);
}
static MP_DEFINE_CONST_FUN_OBJ_KW(py_crc32_obj, 1, py_crc32);
// Module globals table
static const mp_rom_map_elem_t crc_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_crc) },
{ MP_ROM_QSTR(MP_QSTR_crc16), MP_ROM_PTR(&py_crc16_obj) },
{ MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&py_crc32_obj) },
};
static MP_DEFINE_CONST_DICT(crc_module_globals, crc_module_globals_table);
// Define module object
const mp_obj_module_t crc_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *) &crc_module_globals,
};
// Register the module to make it available in Python
MP_REGISTER_MODULE(MP_QSTR_crc, crc_user_cmodule);
#endif // MICROPY_PY_CRC

View File

@ -25,46 +25,78 @@
#include "global_map.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) {
crc_clear_config(CRC0);
crc_enable_16bit(CRC0);
crc_enable_custom_poly(CRC0);
crc_set_custom_poly(CRC0, OMV_CRC_POLY);
crc_set_seed(CRC0, OMV_CRC_INIT);
crc_enable(CRC0);
crc_initialized = true;
static void omv_crc32_init(void) {
CRC0->CRC_CONTROL = CRC_32C |
CRC_CUSTOM_POLY |
CRC_ALGO_32_BIT_SIZE;
CRC0->CRC_SEED = OMV_CRC32_INIT;
CRC0->CRC_POLY_CUSTOM = OMV_CRC32_POLY;
crc32_initialized = true;
}
omv_crc_t omv_crc_start(const void *buf, size_t size) {
if (!crc_initialized) {
omv_crc_init();
uint32_t omv_crc32_start(const void *buf, size_t len) {
uint32_t result = OMV_CRC32_INIT;
if (!crc32_initialized) {
omv_crc32_init();
}
if (size == 0) {
return OMV_CRC_INIT;
if (len == 0) {
return OMV_CRC32_INIT;
}
uint32_t result = 0;
crc_calculate_16bit(CRC0, buf, size, &result);
return (omv_crc_t)result;
crc_calculate(CRC0, buf, len, &result);
return result;
}
omv_crc_t omv_crc_update(omv_crc_t crc, const void *buf, size_t size) {
if (!crc_initialized) {
omv_crc_init();
uint32_t omv_crc32_update(uint32_t crc, const void *buf, size_t len) {
uint32_t result = crc;
if (!crc32_initialized) {
omv_crc32_init();
}
if (size == 0) {
if (len == 0) {
return crc;
}
// Set current CRC value and calculate incrementally
crc_set_seed(CRC0, crc);
uint32_t result = 0;
crc_calculate_16bit(CRC0, buf, size, &result);
return (omv_crc_t)result;
crc_calculate(CRC0, buf, len, &result);
return 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