MATLAB: How to implement a pragma compiler directory for the custom target using Real-Time Embedded Coder 4.0 (R14) and above

compilerdirectivesEmbedded Coderpragmasimulink

My custom target application requires the use of pragmas such that one can control compilation details that are not otherwise under your control. I would like to know if it is possible to implement such compiler directories using Real-Time Workshop Embedded coder 4.0 and above.

Best Answer

The following example uses Real-Time Workshop Embedded Coder's Custom Storage Classes to implement a pragma compiler directive for an Embedded Real Time (ERT) target for the attached calibdemo.mdl model file. All files that pertain to this example are in the attached ZIP file. You can use this example as a reference to implement your own compiler directives that will be appropriate to your particular custom target’s compiler.
The following steps will create a pragma compiler directive for an Embedded Real Time target:
1. Use "sldataclassdesigner" to create a new package (MyPragma). To accomplish this follow the steps below:
Open the Simulink Data Class Designer by typing the following command at the MATLAB command prompt:
Sldataclassdesigner
The Data Class Designer loads all packages that exist on the MATLAB path.
Create a new package. Click New next to the Package name field. Edit the Package name and type MyPragma. Then, click OK.
In the Parent directory field, enter the path to the directory where you want to store the new package.
Click on the Classes tab.
Create a new class by clicking New next to the Class name field. Edit the Package name and type MyPragmaClass. Then, click OK.
In the Derived from menus, select Simulink.Parameter.
The Create your own custom storage classes for this class option is now enabled. This option is enabled when the selected class is derived from Simulink.Parameter. You must select this option to create CSCs for the new class. If the Create your own custom storage classes for this class option is not selected, the new class inherits the CSCs of the parent class.
Click Confirm Changes. In the Confirm Changes pane, select the package you created. Add the parent directory to the MATLAB path if necessary. Then, click Write Selected.
The package directories and files, including the CSC registration file, are written out to the parent directory.
Click Close.
You can now view and edit the CSCs belonging to your package (MyPragma) in the Custom Storage Class Designer. Initially, the package contains only the Default CSC definition, as shown in the figure below.
2. Open the Custom Storage Class Designer in advanced mode, type the following command at the MATLAB prompt:
cscdesigner –advanced
3. Create a memory section and custom storage class in the MyPragma package. When first opened, cscdesigner scans all data class packages on the MATLAB path to detect packages that have a CSC registration file. A message window is displayed while scanning proceeds.
4. Select the MyPragma package in the drop down menu at the top of the ccsdesigner GUI and click on the Memory Section tab.
5. Crate a Memory section definition by clicking New next to the Memory section definitions pane.
6. Edit the fields in the bottom with the following selections:
Memory section name: CALIBDATA
Pre-memory-section pragma: #pragma section CALIBDATA ".calibdata" ".zerocalibdata" far-absolute
7. Click on the Custom Storage Class tab. Create a new custom storage class definition by clicking New next to the Create a new custom storage class pane.
8. Edit the fields in the bottom with the following selections:
Name: CALIBDATA
Type: Other
Memory section: CALIBDATA
Data Initialization: None
Header file: Specify
Check the For parameters check box
Uncheck the For signals check box
Data scope: Exported
9. Create a new folder under the @MyPragma directory and name it tlc. You should now have the following folders and files under the @MyPragma directory:
@CustomRTWInfo_Parameter
@CustomRTWInfo_Signal
@Parameter
tlc
csc_registration.m
packagedefn.mat
schema.p
10. Copy the file $MATLABROOT/toolbox/simulink/simulink/@Simulink/tlc/Unstructured.tlc (where $MATLABROOT is the directory where your MATLAB version is installed) into the tlc folder from above and rename it CALIBDATA.tlc.
11. Edit the CALIBDATA.tlc file and replace the following code under the DataAccess function.
From:
%case "define"

%return GetDefnUnstructured(cscDefn, msDefn, record)
%break

To:
%case "define"
%openfile tmpBuf
#pragma use_section CALIBDATA
%<GetDefnUnstructured(cscDefn, msDefn, record)>
%closefile tmpBuf
%return tmpBuf
%break
12. Save CALIBDATA.tlc and change your current MATLAB directory to where the @MyPragma class folder is located.
13. Open cscdesigner select the Custom Storage Class tab. Select / highlight CALIBDATA and click on the Other Attributes tab.
14. Under the Other Attributes tab, edit the TLC filename field and type CALIBDATA.tlc
15. Click on the Save button and close cscdesigner by clicking on the OK button.
16. Open the attached model calibdemo.mdl.
17. At the MATLAB command prompt type:
P=MyPragma.Parameter
P1=MyPragma.Parameter
18. Open Model Explorer by clicking on the Model Explorer icon on the top of calibdemo.mdl model.
19. Select / highlight Base Workspace in the left hand side pane.
20. Select / Highlight the parameter P. Edit parameter P value to 1 and Select its storage class to CALIBDATA(Custom).
21. Repeat step 20 for parameter P1
22. In the calibdemo.mdl model, go to Simulation, select Configuration Parameters and under Real-Time Workshop click on the Generate Code button to generate the C code for your model. You will now see the generated pragma compiler directives in the model’s calibdemo.c C source code file.