Main Content

Configure Model to Add C API Signals in Code Generation

In this step of the example, you configure the model to include the C API elements for model signals and model parameters, in the generated model code.

Open the model CapiGetStarted.

capiMdl = "CapiGetStarted";
open_system(capiMdl);

Enable C API Code Generation for Model Signals and Model Parameters

To enable C API code generation for model signals and model parameters:

  1. Open the Configuration Parameters dialog box.

  2. Navigate to the Code Generation > Interface pane.

  3. In the Data exchange interface > Generate C API for section, select signals and parameters.

  4. Click OK.

Configuration Parameters dialog box. The Code Generation > Interface pane is open. In the Generate C API for section, the states and parameters boxes are selected.

Alternatively, enter this in the Command Window:

set_param(capiMdl,RTWCAPISignals=true)
set_param(capiMdl,RTWCAPIParams=true)

Configure Signals as Test Points

To prevent the code generator from optimizing the signals away, configure them to be test points (see Configure Signals as Test Points):

  1. In the model, select the signals.

  2. Open the Signal tab of the Simulink® canvas.

  3. In the Monitor group, click Test Point.

CapiGetStarted model. The output signals of the Gain blocks are selected in the model canvas. The model Signal tab is selected and the Test Point toggle button in the Monitor section is selected.

Alternatively, use the capiGetStartHelper MATLAB® program to programmatically configure the signals as test points.

capiGetStartHelper("signals")

Specify Model Parameters Storage Class

To prevent the code generator from optimizing the parameters away, specify their Storage Class as Model default.

  1. Open the Code Mappings editor (see Open the Code Mappings Editor – C) and select the Parameters tab.

  2. Specify Storage Class for each parameter as Model default.

Code Mappings editor. Signals/States tab is open. The storage classes of the two signals are specified as Auto and Model default.

Alternatively, use the capiGetStartHelper MATLAB® program to programmatically set the storage classes of the parameters.

capiGetStartHelper("parameters")

Generate Model Code

Use the slbuild command to generate code from your model. Use evalc to suppress the output of the slbuild command.

evalc("slbuild(capiMdl,GenerateCodeOnly=true)");

Verify That Signals and Parameters Are Included in C API

The C API structure arrays that corresponds to model signals and model parameters are rtBlockSignals and rtModelParameters, respectively. To view the instantiation code for these structures, examine the generated C API source file CapiGetStarted_capi.c (located in the folder CapiGetStarted_grt_rtw).

This is the instantiation code for the structures in this file:

/* Block output signal information */
static const rtwCAPI_Signals rtBlockSignals[] = {
  /* addrMapIndex, sysNum, blockPath,
   * signalName, portNumber, dataTypeIndex, dimIndex, fxpIndex, sTimeIndex
   */
  { 0, 0, TARGET_STRING("CapiGetStarted/Amplifier_1"),
    TARGET_STRING("mySig1"), 0, 0, 0, 0, 0 },

  { 1, 0, TARGET_STRING("CapiGetStarted/Amplifier_2"),
    TARGET_STRING("mySig2"), 0, 0, 0, 0, 0 },

  {
    0, 0, (NULL), (NULL), 0, 0, 0, 0, 0
  }
};

/* Tunable variable parameters */
static const rtwCAPI_ModelParameters rtModelParameters[] = {
  /* addrMapIndex, varName, dataTypeIndex, dimIndex, fixPtIndex */
  { 2, TARGET_STRING("Amp_coef_1"), 0, 0, 0 },

  { 3, TARGET_STRING("Amp_coef_2"), 0, 0, 0 },

  { 0, (NULL), 0, 0, 0 }
};

In the next step of the example, you write external code that interacts with the generated C API code. To continue to the next step of the example, use this at the Command Window:

openExample("simulinkcoder/CapiGetStarted03Example")