Skip to Main Content Skip to Search
Product Documentation

Declare Constant Data as Volatile

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:

If you choose to add the volatile qualifier to type definitions in your generated code, note the following:

Consider the following simple lookup table model.

  1. On the Configuration Parameters dialog box, in the Optimization > Signals and Parameters pane, select Inline parameters.

  2. In the Code Generation pane, set System target file to ert.tlc.

  3. In the Code Generation > Memory Sections pane, set Package to Simulink or mpt, and Constants to MemConstVolatile.

  4. 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.

  5. 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]));
    

  


Related Products & Applications

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