Sturct alignment when using c2000 blockset for code generation

6 次查看(过去 30 天)
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).

回答(1 个)

Sumukh
Sumukh 2024-9-12
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:
  1. 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
  2. Type “crtool” in the command window to open “Code Replacement Tool” window. Click on “File -> Generate registration file” and input the following:
  3. Registry Name: “test”
  4. Base CRL: TI C28x
  5. 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".
  6. 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.
  7. Click OK. The code replacement file “rtwTargetInfo.m” will be created in the present working directory.
  8. Register the file created by typing the following line in the command window: RTW.TargetRegistry.getInstance('reset');
  9. 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.
  10. Open the Type Editor App, and now set the alignment to “1” for the Simulink.Bus object.
  11. 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.
You can refer to the following documentation to understand more about the code replacement tool:
I hope this solves your issue.

类别

Help CenterFile Exchange 中查找有关 Code Interface Configuration and Integration 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by