- As per the above documentation, the “Code Replacement” functionality must be used to provide data alignment specification for the struct. You can refer to the following documentation on data alignment options available: https://www.mathworks.com/help/ecoder/ug/data-alignment-for-code-replacement-1.html#bs6iwux-1
- Type “crtool” in the command window to open “Code Replacement Tool” window. Click on “File -> Generate registration file” and input the following:
- Registry Name: “test”
- Base CRL: TI C28x
- Check the “Generate data alignment specification” checkbox and create two alignment specifications, for the Alignment Types "DATA_ALIGNMENT_GLOBAL_VAR" and "DATA_ALIGNMENT_WHOLE_STRUCT".
- For both the specifications, set the Alignment position to “DATA_ALIGNMENT_PRECEDING_STATEMENT”, the alignment syntax to “#pragma pack(%n);”and the Supported languages to: "c, c+". The pragma directive will be coded before the struct type declaration, replacing the placeholder “%n” with the byte size specified in the “Alignment” option for the Simulink.bus object.
- Click OK. The code replacement file “rtwTargetInfo.m” will be created in the present working directory.
- Register the file created by typing the following line in the command window: RTW.TargetRegistry.getInstance('reset');
- Go back to the Simulink model and set the “Code replacement libraries” option available in “Configuration Parameters -> Code Generation -> Interface” to the registry file “test”. Apply this change and click OK.
- Open the Type Editor App, and now set the alignment to “1” for the Simulink.Bus object.
- Generate the code for the model using the Embedded Coder app. The code replacement library will paste the pragma directive “#pragma pack(1);” before the struct initialization using the byte size specified in the “Alignment” option.
Sturct alignment when using c2000 blockset for code generation
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am using the c2000 blockset for code generation.
I wonder if I can modify the data storage alignment of the struct in the generated code.
I have tried to modified the 'alignment' settings in the simulink bus parameter for -1 (default) to 1 (whici i believe is equal to #pragma pack(1) in the c code.) as shown in the following figure.
However, in the generated code, the struct alignment is still defualt. see the code attached below
Please help me to modify the struct alignment to #pragma pack(1).
0 Kommentare
Antworten (1)
Sumukh
am 12 Sep. 2024
Hi Xavier,
I understand that you are using Embedded Coder to generate code for C2000 targets. Based on the screenshots you have provided, I am assuming you are using a struct with the type defined using Simulink.Bus object in the Type Editor App. The following documentation link states that data alignment can be added to structs using the code replacement library tool:
To ensure that the value passed to the “Alignment” parameter in the “Code Generation” tab of the Simulink Bus object is used to create the pragma directive, the following steps must be taken:
You can refer to the following documentation to understand more about the code replacement tool:
I hope this solves your issue.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Code Interface Configuration and Integration finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!