Adding CMSIS DSP functions #16130
Replies: 3 comments
-
I found a temporary solution, move the linker script commands from cexample/micropython.mk to ports/stm32/Makefile to make sure they appear at the end of the gcc command. I dont know why this is neccesary, but it allows to find the required funcions in the .a library. /micropython/examples/usercmodule/cexample/examplemodule.c: #include "arm_math.h"
// This is the function which will be called from Python as cexample.add_ints(a, b).
static mp_obj_t example_add_ints(mp_obj_t a_obj, mp_obj_t b_obj) {
// Extract the ints from the micropython input objects.
int a = mp_obj_get_int(a_obj);
int b = mp_obj_get_int(b_obj);
float fa[4] = {a,a,a,a};
float fb[4] = {b,b,b,b};
float fc[4] = {0,0,0,0};
arm_conv_f32( fa, 4, fb, 4, fc );
// Calculate the addition and convert to MicroPython object.
return mp_obj_new_int(a + b + (int)fc[0]);
}
// Define a Python reference to the function above.
static MP_DEFINE_CONST_FUN_OBJ_2(example_add_ints_obj, example_add_ints); /micropython/examples/usercmodule/cexample/micropython.mk: CEXAMPLE_MOD_DIR := $(USERMOD_DIR)
# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/examplemodule.c
# We can add our module folder to include paths if needed
# This is not actually needed in this example.
CFLAGS_USERMOD += -I$(CEXAMPLE_MOD_DIR)
CFLAGS_USERMOD += -DARM_MATH_CM4
CFLAGS_USERMOD += -I$(TOP)/examples/usercmodule/cexample/CMSIS/DSP/Include
# Link CMSIS DSP library
#LDFLAGS_USERMOD += -L$(TOP)/examples/usercmodule/cexample/CMSIS/Lib/GCC
#LDFLAGS_USERMOD += -larm_cortexM4lf_math /micropython/ports/stm32/Makefile: define GENERATE_ELF
$(ECHO) "LINK $(1)"
$(Q)$(CC) $(LDFLAGS) -o $(1) $(2) $(LDFLAGS_MOD) $(LIBS) -L../../examples/usercmodule/cexample/CMSIS/Lib/GCC -larm_cortexM4lf_math
$(Q)$(SIZE) $(1)
$(if $(filter-out $(TEXT0_ADDR),0x08000000), \
$(ECHO) "INFO: this build requires mboot to be installed first")
$(if $(filter $(TEXT1_ADDR),0x90000000), \
$(ECHO) "INFO: this build places firmware in external QSPI flash")
endef |
Beta Was this translation helpful? Give feedback.
-
Here is the CMSIS module, currently with 70 of 300 functions implemented. static mp_obj_t cmsis_dsp_arm_conv_f32(size_t n_args, const mp_obj_t *args)
{
// Get input parameters
mp_buffer_info_t pSrcA_bufinfo, pSrcB_bufinfo;
mp_get_buffer_raise(args[0], &pSrcA_bufinfo, MP_BUFFER_READ);
mp_get_buffer_raise(args[1], &pSrcB_bufinfo, MP_BUFFER_READ);
uint32_t srcALen = mp_obj_get_int(args[2]);
uint32_t srcBLen = mp_obj_get_int(args[3]);
// Get pointers to output parameters
mp_buffer_info_t pDst_bufinfo;
mp_get_buffer_raise(args[4], &pDst_bufinfo, MP_BUFFER_WRITE);
// Check buffer types
if (pSrcA_bufinfo.typecode != 'f' || pSrcB_bufinfo.typecode != 'f' || pDst_bufinfo.typecode != 'f') {
mp_raise_ValueError(MP_ERROR_TEXT("Buffers must contain float data"));
}
// Check buffer sizes
if (pSrcA_bufinfo.len < srcALen * sizeof(float32_t) || pSrcB_bufinfo.len < srcBLen * sizeof(float32_t) || pDst_bufinfo.len < (srcALen + srcBLen - 1) * sizeof(float32_t)) {
mp_raise_ValueError(MP_ERROR_TEXT("Buffer sizes must match"));
}
// Process convolution
arm_conv_f32(
(float32_t *)pSrcA_bufinfo.buf,
srcALen,
(float32_t *)pSrcB_bufinfo.buf,
srcBLen,
(float32_t *)pDst_bufinfo.buf
);
return mp_const_none;
} |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I want to add this module to micropython to use convolution operations available at CMSIS DSP library, but I dont know how to include the /workspace/micropython/examples/usercmodule/conv/CMSIS/Lib/GCC/libarm_cortexM4l_math.a in the build, so the linker is always compliying about missing arm_conv_X functions.
How should I do it?
I added this into the makefile, but it dosnt help:
CFLAGS_USERMOD += -DARM_MATH_CM
-I/workspace/micropython/examples/usercmodule/conv/CMSIS/DSP/Include
LDFLAGS_USERMOD += -L/workspace/micropython/examples/usercmodule/conv/CMSIS/Lib/GCC
LDFLAGS_USERMOD += -larm_cortexM4l_math
Beta Was this translation helpful? Give feedback.
All reactions