C Function

Integrate and call external C code from a Simulink model

  • Library:
  • Simulink / User-Defined Functions

  • C Function block

Description

The C Function block integrates and calls external C code from a Simulink® model. Use this block to define external code and customize the integration of your code by preprocessing or postprocessing the data. In addition, you can specify customized code for simulation and C code generation. You can call functions defined in your code conditionally, and you can also call multiple functions in one block. Using this block, you can initialize the block's persistent data and pass it to an external function.

You can also use C Function block to call a subset of C Math Library functions. See Call C Library Functions From C Function Block for more information.

The C Function block supports initializing persistent data and calling external functions from the block dialog. The C Function block supports only initializing and terminating persistent data; the block does not support updating the data during simulation. To model a dynamic system with continuous states, use an S-Function block. To learn more about S-functions, see What Is an S-Function?.

Define the source code and supporting files to be called by the C Function block in the Simulation Target pane of the configuration parameters.

Limitations

The following features of Simulink are not compatible with the C Function block.

  • Simulink Coverage™

    Only execution coverage is measured.

  • Simulink Code Inspector™

  • Simulink Design Verifier™

  • Simulink Test™

    – Test harness

  • Use of the C Function block in Simulink Report Generator™

In addition, the following limitations apply to the source code referenced by a C Function block.

  • The C keyword static is not supported.

    To cache values across time steps, define the symbol as Persistent in the Symbols table of the block dialog.

  • Files cannot be included in the script.

    External functions must be specified in the Simulation Target pane of the Configuration Parameters dialog.

    If you have existing includes defined in the Simulation Target pane that cannot be mixed with the new include, consider adding the include and C Function block in a library model and use a link to the block in the main model. For more information, see Create a Custom Library.

  • Pointers of different types cannot be assigned to each other. The pointer types should match when you call external functions.

  • The code cannot take the address of a constant.

  • Directly calling of some C library functions from the C Function block is not supported. See <link here> to see a list of the C Math Library functions that you can directly call. To call other C library functions, create a wrapper function that calls the C library function.

Ports

Input

expand all

Input to the C Function block.

The number of inputs and their names are determined by the definitions in your external C code and the definitions of the symbols in the Symbols table in the block parameters dialog.

The input port label is the same as the name of the input symbol unless you change it by editing the Label field in the Symbols table of the blocks dialog.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | enumerated | bus

Output

expand all

Output from the C Function block.

Number of outputs and their names are determined by the definitions in your external C code and the definitions of the symbols in the Symbols table in the block parameters dialog.

The output port label is the same as the name of the output symbol unless you change it by editing the Label field in the Symbols table of the block parameters dialog.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | enumerated | bus

Parameters

expand all

Specifies the code that the C Function block executes during simulation. For example, you can call a function from the external C code specified in the Simulation Target pane, make modifications to the results, and perform operations to pass the results to other blocks.

Specifies the initialization code of the C Function block. This code executes one time at the start of simulation. For example, you can initialize persistent data.

Specifies cleanup code that the C Function block runs at model termination. This code executes once at the end of a simulation. For example, use this code to free the memory cached on persistent symbols specified as void pointers.

The Symbols table specifies the attributes of the symbols in the C code. You must enter the attributes of each symbol in the table.

  • Name — Symbol name in the source code.

  • Scope — Scope of the symbol. You can change the scope of a symbol at any time. The following scopes are available:

    • Input — Input symbol to the C Function block.

    • Output — Output symbol to the C Function block.

    • InputOutput — Define a symbol as both input and output to the C Function block.

    • Parameter — Define a symbol as a parameter. The parameter name is defined by the Label property of the symbol.

    • Persistent — Define a symbol as persistent data.

      You can define a void pointer using the Persistent scope. A void pointer is a pointer that can store any type of data that you create or allocate.

    • Constant — Define a symbol as constant using value-size or numeric expressions.

  • Label — Label of the symbol. For symbols with their scope set to Input or Output, this label appears as the port name on the block. For symbols with their scope set to Parameter, this label is the label that appears on the block parameter mask. If the scope is Constant, the label is the constant expression. You cannot define a label for Persistent symbols.

  • Type — Data type of the symbol. Select a data type from the drop-down list or specify custom data type.

    To use a custom type such as Simulink.Bus, Simulink Enum or Simulink.AliasType that does not have an external header definition associated with a C Function block, set the type accordingly on the Symbol table.

  • Size — Size of the symbol data. The C Function block supports only scalars and vectors; matrices and higher-dimension arrays are not supported. You can use a size expression to define the size of an output or use -1 to inherit size.

  • Port — For input and output symbols, Port indicates the port index on the block of the symbol data. For parameter symbols, Port indicates the order that the symbol appears in the block parameter mask.

Block Characteristics

Data Types

Boolean[a] | bus[a] | double[a] | enumerated[a] | integer[a] | single[a]

Direct Feedthrough

no

Multidimensional Signals

yes

Variable-Size Signals

no

Zero-Crossing Detection

no

[a] Actual data type or capability support depends on block implementation.

Extended Capabilities

Introduced in R2020a