Embedded-Coder (C-Code)#
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
Use
single
data type in Matlab/Simulink, which is a 32-bit float
How to use#
The 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:
uz_codegen_init(uz_codegen *self)
Initialization function for the codegen struct. Has to be passed a pointer to a variable of type
uz_codegen
.uz_codegen_step(uz_codegen *self)
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
uz_codegen
.
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
Note
Code generation with Simulink creates a lot of auxiliary files when generating code (slprj
folder, codeInfo.mat
, ..). Please do not add them to git.
First usage#
Open Matlab
Set your Matlab path to
ultrazohm_sw/vitis/SimulinkCodegen
Open
uz_codegen.slx
with Matlab/Simulink.The Simulink model calls
uz_setCodegenPaths.m
(defined as preLoadFcn callback)uz_setCodegenPaths.m
sets the appropriate paths for the code generation on the UltraZohmRight-click on the subsystem
uz_codegen
Click
C/C++ Code -> Build This System
Code Generation Advisor will open
Check for warnings and errors
Build code for Subsystem:uz_codegen
opensClick Build
Code is generated and usable in Vitis
Alternative: execute the script
uz_generateSimulinkModel.m
which executes all of the above stepsOpen Vitis
The code is generated in
ultrazohm_sw/vitis/software/Baremetal/src/Codegen/uz_codegen0_ert_rtw
Code is not called by the standard Vitis project
Define
uz_codegen codegenInstance
as a global variable inmain.c
of the Baremetal projectCall the init function (
uz_codegen_init(&codegenInstance)
) inside the mainAdd
extern uz_codegen codegenInstance
toBaremtal/sw/isr.c
to use the global inside the ISRCall
uz_codegen_step(&codegenInstance)
inside the ISR to execute the generated codecodegenInstance.input
holds all input values and can be set directlycodegenInstance.output
holds all output values and can be set directly
Define multiple instances as globals inside main.c
(e.g., codegenInstance2) and call uz_codegen_init
and 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;
Warning
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
Language: C
Build process: Generate code only (checked)
Build process: Package code and artifacts (not checked)
Toolchain: Automatically locate an installed toolchain
- Optimization
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
More Information#
Deploy Generated Standalone Executable Programs To Target Hardware
How Generated Code Stores Internal Signal, State, and Parameter Data