Main Content

C Function

Integrate and call external C code from a Simulink model

  • Library:
  • Simulink / User-Defined Functions

  • C Function block

Description

TheC Functionblock 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 useC Functionblock to call a subset of C Math Library functions. SeeCall C Library Functions From C Function Blockfor more information.

TheC Functionblock supports initializing persistent data and calling external functions from the block dialog. TheC Functionblock 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 anS-Functionblock. To learn more about S-functions, seeWhat Is an S-Function?.

Define the source code and supporting files to be called by theC Functionblock in theSimulation Targetpane of the configuration parameters.

Limitations

The following features of Simulink are not compatible with theC Functionblock.

  • Simulink Coverage™

    Only execution coverage is measured.

  • Simulink Code Inspector™

  • Simulink Design Verifier™

  • Simulink Test™

    – Test harness

In addition, the following limitations apply to the source code referenced by aC Functionblock.

  • The C keywordstaticis not supported.

    To cache values across time steps, define the symbol asPersistentin theSymbolstable of the block dialog.

  • Files cannot be included in the script.

    External functions must be specified in theSimulation Targetpane of the Configuration Parameters dialog.

    If you have existing includes defined in theSimulation Targetpane 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, seeCreate Custom Library.

  • 不能分配给不同类型的指针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 theC Functionblock is not supported. To see a list of the C Math Library functions that you can call directly, seeCall C Library Functions From C Function Block. To call other C library functions, create a wrapper function that calls the C library function.

Ports

Input

expand all

Input to theC Functionblock.

The number of inputs and their names are determined by the definitions in your external C code and the definitions of the symbols in theSymbolstable 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 theLabelfield in theSymbolstable of the blocks dialog.

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

Output

expand all

Output from theC Functionblock.

Number of outputs and their names are determined by the definitions in your external C code and the definitions of the symbols in theSymbolstable 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 theLabelfield in theSymbolstable 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 theC Functionblock executes during simulation. For example, you can call a function from the external C code specified in theSimulation Targetpane, make modifications to the results, and perform operations to pass the results to other blocks.

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

Specifies cleanup code that theC Functionblock 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.

TheSymbolstable 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 theC Functionblock.

    • Output— Output symbol to theC Functionblock.

    • InputOutput— Define a symbol as both input and output to theC Functionblock.

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

    • Persistent— Define a symbol as persistent data.

      You can define a void pointer using thePersistentscope. Avoid pointeris 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 toInputorOutput, this label appears as the port name on the block. For symbols with their scope set toParameter, this label is the label that appears on the block parameter mask. If the scope isConstant, the label is the constant expression. You cannot define a label forPersistentsymbols.

  • 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金宝app仿真软件。公共汽车,Simulink Enumor金宝app仿真软件。AliasTypethat does not have an external header definition associated with aC Functionblock, set the type accordingly on theSymboltable.

  • Size— Size of the symbol data. TheC Functionblock 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-1to inherit size.

  • Port— For input and output symbols,Portindicates the port index on the block of the symbol data. For parameter symbols,Portindicates 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

介绍了R2020a