The Matlab/Simulink Embedded-Coder generates C-Code from Simulink models.
This page describes an easy way to get started with the Embedded-Coder on the UltraZohm.
Use the example model
uz_codegen.slx as a template for your own models.
It has the recommended coding settings configured.
Note the following key concepts and see the comprehensive documentation of the Embedded-Coder:
Every discrete-time is synchronous to the highest discrete-time in the generated model
independent of which sample rate is set in Simulink, each call to the step function is one discrete time step
Slower sample rates within the model are generated relative to the number of times the step function is called
singledata type in Matlab/Simulink, which is a 32-bit float
How to use
uz_codegen module encapsulates the code that is generated by the Embedded-Coder.
This enables the usage of multiple instances of the same generated Simulink model.
The API of
uz_codegen has two functions:
Initialization function for the codegen struct. Has to be passed a pointer to a variable of type
steps the model one time, i.e., all calculations inside the Simulink model are executed once. Has to be passed a pointer to a variable of type
ultrazohm_sw/ └── vitis/ ├── software/ | └── Baremetal/src | └── Codegen/ | ├── uz_codegen.c | ├── uz_codegen.h | └── uz_codegen0_ert_rtw/ └── SimulinkCodegen ├── uz_generateSimulinkModel.m ├── uz_setCodegenPaths.m └── uz_codegen.slx
Code generation with Simulink creates a lot of auxiliary files when generating code (
codeInfo.mat, ..). Please do not add them to git.
Set your Matlab path to
The Simulink model calls
uz_setCodegenPaths.m(defined as preLoadFcn callback)
uz_setCodegenPaths.msets the appropriate paths for the code generation on the UltraZohm
Right-click on the subsystem
C/C++ Code -> Build This System
Code Generation Advisor will open
Check for warnings and errors
Build code for Subsystem:uz_codegenopens
Code is generated and usable in Vitis
Alternative: execute the script
uz_generateSimulinkModel.mwhich executes all of the above steps
The code is generated in
Code is not called by the standard Vitis project
uz_codegen codegenInstanceas a global variable in
main.cof the Baremetal project
Call the init function (
uz_codegen_init(&codegenInstance)) inside the main
extern uz_codegen codegenInstanceto
Baremtal/sw/isr.cto use the global inside the ISR
uz_codegen_step(&codegenInstance)inside the ISR to execute the generated code
codegenInstance.inputholds all input values and can be set directly
codegenInstance.outputholds all output values and can be set directly
Define multiple instances as globals inside
main.c (e.g., codegenInstance2) and call
uz_codegen_step with the respective instance to use multiple independent instances of the generated code.
The input and output variables are directly accessible.
float timeFeedback; codegenInstance.input.time=time; uz_codegen_step(&codegenInstance); timeFeedback=codegenInstance.output.timeFeedback;
The direct access of the variables is not recommended and not good coding practice. The approach above is simply shown to get over the first configuration barrier of the Embedded-Coder. The user is expected to fit the configuration and coding practices to their application using the information below.
Code generation settings
All Simulink configuration parameters that are not mentioned below are set to the Simulink standard settings. These settings are present if a new Simulink model from the Embedded-Coder templates is created.
- Code Generation
System target file: ert.tlc
Build process: Generate code only (checked)
Build process: Package code and artifacts (not checked)
Toolchain: Automatically locate an installed toolchain
Remove root level I/O zero initialization unchecked to ensure everything is there and to have a way to reset
Remove internal data zero initialization unchecked to ensure everything is there and to have a way to reset
Deploy Generated Standalone Executable Programs To Target Hardware
How Generated Code Stores Internal Signal, State, and Parameter Data