| Real-Time Workshop Embedded Coder | ![]() |
Sample Code Excerpts
In the model shown in Figure 4-1, block parameters and signals are associated with data objects belonging to each of the predefined custom storage classes, as follows:
c, cv, and d reference Simulink.CustomParameter objects with custom storage class Const, ConstVolatile and Define, respectively.
v and intl reference Simulink.CustomSignal objects with custom storage class Volatile and Internal, respectively.
sw1 and sw2 reference Simulink.CustomSignal objects with custom storage class Struct, whose StructName storage class attribute is set to testpoints.
b1 and b2 reference Simulink.CustomSignal objects with custom storage class BitField, whose BitFieldName storage class attribute is set to signalBit.
eg references a Simulink.CustomParameter object with custom storage class ExportToFile, whose FileName storage class attribute is set to exportedSignals.h.
ig references a Simulink.CustomParameter object with custom storage class ImportFromFile, whose FileName storage class attribute is set to importedSignals.h, and whose IncludeDelimeter attribute is set to Braces.
Figure 4-1: Model Using Custom Storage Classes
The structure definitions for the bit field signalBit and the struct testpoints are in the generated file model_types.h, as shown in the following code excerpt. Notice also the inclusion of the generated file exportedSignals.h and the file importedSignals.h. The latter is assumed to be a hand-written file containing external signal definitions:
#include "exportedSignals.h" #include <importedSignals.h> typedef struct signalBit_tag { unsigned int b1:1; unsigned int b2:1; } signalBit_bitfield; /* Struct data */ typedef struct testpoints_tag { real_T sw1; real_T sw2; } testpoints_struct;
A code excerpt from exportedSignals.h follows, declaring the parameter eg and making it visible to externally written code:
#ifndef _exportedSignals_h #include "tmwtypes.h" extern real_T eg; #define _exportedSignals_h #endif
The following excerpt from the generated file model_data.c contains the storage declarations and initializers for the parameters c, cv, and d; signals v and intl; exported signal eg; signals b1 and b2 (embedded in signalBit); and sw1 and sw2 (embedded in testpoints):
/* Data with custom storage class Const */ const real_T c = 2.0; /* Data with custom storage class ConstVolatile */ const volatile real_T cv = 4.0; /* Data with custom storage class Define */ #define d 5.0 /* Data with custom storage class ExportToFile */ real_T eg; /* Data with custom storage class Internal */ real_T intl; /* Data with custom storage class Volatile */ volatile real_T v; /* External Outputs Structure */ ExternalOutputs rtY; /* user code (bottom of parameter file) */ signalBit_bitfield signalBit = {0,0}; testpoints_struct testpoints = {0.0,0.0};
The following code excerpt from model.c illustrates the application of these variables in the generated program:
/* Switch: '<Root>/Switch' incorporates:* Inport: '<Root>/In1'* Constant: '<Root>/Constant'* Constant: '<Root>/Constant1'*/if (signalBit.b1) {testpoints.sw1 = c;} else {testpoints.sw1 = cv;}/* Switch: '<Root>/Switch1' incorporates:* Inport: '<Root>/In2'* Constant: '<Root>/Constant2'* Constant: '<Root>/Constant3'*/if (signalBit.b2) {testpoints.sw2 = eg;} else {testpoints.sw2 = ig;}/* Sum: '<Root>/Sum' */v = testpoints.sw1 + testpoints.sw2;/* Gain: '<Root>/Gain'** Regarding '<Root>/Gain':* Gain value: d*/intl = v * d;/* Outport: '<Root>/Out1' */rtY.Out1 = intl;
| Ordering of Generated Storage Declarations | Requirements, Restrictions, Target Files | ![]() |