Wednesday, 8 November 2023

Deploying Matlab On ARM Using Codegen

Several years ago I worked on two projects that required code to be deployed on ARM devices, where the original algorithms had been developed in Matlab. I researched Matlab's Codegen capabilities and realized that it has some very useful features so I've generated a generic example, with no customer code, to demonstrate the capabilities.

Among the many great features of Codegen is Whole Project code optimization. For example, if a value is stored in a global variable then passed to the underlying generated C code function, Codegen will bypass passing the value on the stack and merely read it from the global memory pool, in the sub-function.

The project can be found here: https://github.com/Numerix-DSP/Matlab_To_C.

There are several tricks to achieve good quality code from Matlab/Codegen and I've covered a few of them in the documentation. There are also several areas that Codegen is not able to support directly like interrupt service routines on the embedded devices - The project shows how to integrate the code with embedded I/O routines.

The project is Windows based but with a few minor tweeks can run perfectly well under Linux or OSX. For testing the generated C code on a host, the project includes an example host program that shows how to handle file I/O in a Matlab compatible format and also uses SigLib for .csv file I/O and Gnuplot to replace the Matlab graphics plotting functions.

This project also includes a batch file to convert the Matlab code to C to allow it to run on an STM32 device, with the simulation files stored on a USM memory stick. The File I/O funcationality can easily be replaced to run the code from and Interrupt Service Routine instead.

For my NXP loving friends I have tried to run the program on an LPC55S69 EVK but unfortunately, I'm having a few problems with USB file I/O. Once that is resolved, I'll port to that device.

Following that I will add scripts to swap out the Matlab DSP function calls (e.g. FFT) and replace them with calls to the ARM CMSIS-DSP library.

Copyright © 2024 Delta Numerix