| Contents | Index |
In the C language, the value of data declared with the storage type qualifier, volatile, can be read from memory and written back to memory when changed without compiler control or detection. Examples of use include variables for initialization at system power-up or for system clock updates.
You can add the volatile qualifier to type definitions generated in code for model constant block I/O, constant parameters, and ground data (zero representation).
To add the volatile qualifier to type definitions, you must configure your model as follows:
Enable inline parameters
Specify an ERT target
Set the memory section for constant data to MemVolatile or MemConstVolatile
If you choose to add the volatile qualifier to type definitions in your generated code, note the following:
If constant data that is qualified with volatile is passed by pointer, the code generator casts away the volatility. This occurs because generated functions assume that data values do not change during execution and, therefore, pass their arguments as const * (not const volatile *).
If a variable must be declared const and you specify MemVolatile, no warning occurs and the code generator declares the variable with the const and volatile qualifiers.
If you set Constants to MemConst or MemConstVolatile, and a variable cannot be declared as constant data, a TLC warning appears and the code generator does not qualify the variable with const.
Consider the following simple lookup table model.

On the Configuration Parameters dialog box, in the Optimization > Signals and Parameters pane, select Inline parameters.
In the Code Generation pane, set System target file to ert.tlc.
In the Code Generation > Memory Sections pane, set Package to Simulink or mpt, and Constants to MemConstVolatile.
Open the Signal Properties dialog box for signal INPUT. On the Code Generation tab, set the Package to Simulink or mpt and the Storage class to ExportedGlobal for storing state in a global variable.
Generate code. You should see the volatile qualifier in the generated files model_data.c and model.h.
model_data.c
/* Constant parameters (auto storage) */
/* ConstVolatile memory section */
const volatile ConstParam_simple_lookup simple_lookup_ConstP = {
/* Expression: [-5:5]
* Referenced by: '<Root>/Lookup Table'
*/
{ -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 },
/* Expression: tanh([-5:5])
* Referenced by: '<Root>/Lookup Table'
*/
{ -0.99990920426259511, -0.999329299739067,
-0.99505475368673046, -0.9640275800758169,
-0.76159415595576485, 0.0, 0.76159415595576485,
0.9640275800758169, 0.99505475368673046,
0.999329299739067, 0.99990920426259511 }
};model.h
/* Real-time Model Data Structure */
struct RT_MODEL_simple_lookup {
const char_T * volatile errorStatus;
};
/* Constant parameters (auto storage) */
extern const volatile ConstParam_simple_lookup simple_lookup_ConstP;Also note in the model.c file that a typecast is inserted in the rt_Lookup function call, removing the volatile qualifier.
/* Lookup: '<Root>/Lookup Table' incorporates: * Inport: '<Root>/In1' */ OUTPUT = rt_Lookup(((const real_T*) &simple_lookup_ConstP.LookupTable_XData[0]), 11, INPUT, (( const real_T*) &simple_lookup_ConstP.LookupTable_YData[0]));
![]() | Configure Memory Sections | Apply Memory Sections | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |