Main Content

Configure Model to Generate C API Code for Signals

This example shows how to configure the model to include the C API for signals in the generated code.

Open the model CapiConfigDemo.

capiMdl = "CapiConfigDemo";
open_system(capiMdl);

Enable C API Generation for Signals

To enable C API code generation for model signals:

  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, select signals.

  4. Click OK.

Configuration Parameters dialog box. The Code Generation > Interface pane is open. In the Generate C API for section, the signals box is selected.

Alternatively, enter this in the Command Window:

set_param(capiMdl,RTWCAPISignals=true)

Add Signals to Model Mappings and Specify Signal Storage Classes

To prevent the code generator from optimizing the signals away, you need to add them to the code mappings and specify their storage class as different from Auto.

Use the Code Mappings editor to add the signals to the code mappings:

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

  2. Select the signals in the model and then click the Add Selected Signals button Add Selected Signals Button.

Model with Code Mappings editor open. The output signals of the Unit Delay blocks are selected. The Signals/States tab of the mappings editor is open with the Signals group expanded. The Add Selected Signals is shown to be clicked.

The C API is not generated for signals with storage class set to Auto. To demonstrate this, specify, in the Code Mappings editor, Storage Class for the first signal as Auto, and for the second signals as Model default.

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

Alternatively, to programmatically add the signals to the code mappings and specify their storage classes, use the capiConfigSignalHelper MATLAB® program.

capiConfigSignalHelper("mappings")

You expect the C API to be generated for the second mySig2, but not for mySig1.

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 Signal Is Included in C API

The C API structure array that corresponds to model signals is rtBlockSignals. To view the instantiation code for this structure, examine the generated C API source file CapiConfigDemo_capi.c (located in the folder CapiConfigDemo_grt_rtw).

This is the instantiation code for the structure 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("CapiConfigDemo/unitDelay2"),
    TARGET_STRING("mySig2"), 0, 0, 0, 0, 0 },

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

The C API is generated for the second signal, which is configured with storage class specified as Model default, but not for the first signal, which is configured with storage class specified as Auto.

Configure Signal as Test Points

Another way to prevent the code generator from optimizing signals away, is to configure them as test points (see Configure Signals as Test Points). Configure the first signal as a test point:

  1. In the model, select the first signal.

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

  3. In the Monitor group, click Test Point.

CapiConfigDemo model. The output signal of unitDelay1 is 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 capiConfigSignalHelper MATLAB® program to programmatically configure the signal as a test point.

capiConfigSignalHelper("testPoint")

Use the slbuild command to regenerate code from your model.

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

Examine the generated C API source file CapiConfigDemo_capi.c again and verify that rtBlockSignals now contains instantiation code for both signals.

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

  { 1, 0, TARGET_STRING("CapiConfigDemo/unitDelay2"),
    TARGET_STRING("mySig2"), 0, 0, 0, 0, 0 },

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

Related Topics