Main Content

C Caller

Integrate C code in Simulink

  • C Caller block

Libraries:
Simulink / User-Defined Functions

Description

The C Caller block integrates your external C code into Simulink®. This block imports and lists the functions in your external C code, and enables you to select your resolved C functions to integrate in your Simulink models. The C Caller block standalone supports code generation. For more complex models, code generation depends on the capabilities of your Simulink model.

To use the C Caller block, define your source code and any supporting files using Simulation Target under Configuration Parameters. Then, bring a C Caller block to the Simulink canvas, using Library Browser > Simulink > User Defined Functions. To change the defined source code file and its dependencies, go to Simulation Target tab in Configuration Parameters by clicking the from the block dialog. After changing your source code or any of its dependencies, refresh the list of functions by clicking the on the block dialog. To browse the function definitions in your source code, use the icon to access your source files.

You can use the addGlobalArg function to add global variables from custom code as global arguments to the block, or use the deleteGlobalArg function to delete global arguments from the block. (since R2024b)

Examples

expand all

Since R2024b

This example shows how to use fixed length and dynamic Simulink® strings for a C Caller block. In this example, the block calls a C function which declares input and output using C strings (char* or char[ ]). The C Caller block receives and outputs Simulink® strings at its input and output ports, respectively.

Open the model that uses simulink strings for block ports using C strings.

mdl = "SimulinkStringCCaller.slx";
open_system(mdl)

C Caller blocks in the Simulink model call C function that declares C string as inputs and outputs.

This model uses the following C string prototypes.

  • char [10] :

void strConcatFcnArg(const char s3[10], const char s4[10], char dst[20])
  • char* :

const char* strConcatFcnReturn(const char* s1, const char* s2);

Fixed Length String

The blocks enclosed in this component calls the strConcatFcnArg function. The C Caller block receives a fixed length Simulink® string at its input ports and outputs the concatenated string. To configure the block for Simulink® strings, in the Port specification table in the Block Parameters dialog box, set Type to stringtype(maximum_length) for each input and output port.

Simulink automatically accounts for the ending null character (\0) for C strings. As a result, the maximum length of the Simulink string you can use is n for a C string of length n+1. For more information, see Simulink Strings and Null Characters.

Here, in this model, a fixed length Simulink string of type stringtype(9) is used for input C string char s3[10].

Fixed length Simulink string for C Caller block.

Dynamic String

The blocks enclosed in this component calls the strConcatFcnReturn function. The C Caller block receives a dynamic Simulink® string at its input ports and outputs a concatenated string. To configure the block for Simulink® strings, in the Port specification table in the Block Parameters dialog box, set Type to string for each input and output port.

Dynamic Simulink string for C Caller block.

Run Simulation and Log Output Data

Run the simulation and log the output data in a Simulink.SimulationOutput object.

out = sim(mdl);

You can access the logged data using dot notation.

ConcatFcnReturnOutput = out.yout{1}.Values.Data;
ConcatFcnArgOutput = out.yout{2}.Values.Data;

Extended Examples

Ports

Input

expand all

The number of input ports and their names are inferred through the selected function in your external C code. To provide data to a C Caller block, connect an input signal to the input ports.

The input label has the same name as your input port unless changed by editing the Label column under Port Specification from the Block Dialog. If you rename the label to an input port, the C Caller block changes the name of the port.

For input variables, you can change the input scope to parameters or constants using the Scope column.

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

Output

expand all

The number of output ports and their names are inferred through the selected function in your external C code. To send data from your C Caller block, connect a block to the output port of your C Caller block.

The output port label has the same name as your output port unless you change it by editing the Label column under Port Specification from the Block Dialog. If you rename the label to an input port, the C Caller block changes the name of the port.

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

Parameters

expand all

Name of the function parsed for the C Caller block, specified as a character vector or string scalar. For a list of available functions, see Available Functions.

Tunable: Yes

Programmatic Use

Block Parameter: FunctionName
Type: character vector or string scalar

myFunctionName = get_param(gcb,"FunctionName")
myFunctionName =

    'mean_filter'

Port properties, specified as a table. The table indicates the attributes of each input and output element of the block. If the scope is an input, you can modify this variable to a parameter or a constant. These properties include

Name — Demonstrates the variable name inferred from your source code.

A bold argument name indicates that the port or parameter is a global argument.

Scope — Indicates the role of the variables from your source code. If the variable is an input argument in the C Caller block source code, you can change the scope type to a constant or a parameter. If the variable is an output argument in the source code, you cannot change the scope type.

Label — Labels the input or output variable for the Simulink model. You can change the labels using this table. If the scope is a parameter, enter the parameter name in this field. If the scope is a constant, enter the constant value.

Type — Indicates the data type coming from the ports.

Size — Indicates the size of the input and output data.

Name, scope, type, and size are inferred from your source code.

Programmatic Use

Block Parameter: FunctionPortSpecification
Type: FunctionPortSpecification object

List of all available functions that can be mapped to a C Caller block, specified as a cell array.

The C Caller block in your model imports all functions in your external source code, and shows the function names next to the Function name on the block dialog. To select and use a function in your block, confirm that the function name appears in the Available Functions table. If you are missing one of the functions, reload the source code by clicking on the block dialog. To change the names of functions, modify your source code and click the Refresh button to reload.

Programmatic Use

Block Parameter: AvailableFunctions
Type: cell array

allAvailableFunctions = get_param(gcb, "AvailableFunctions")
allAvailableFunctions =

  1×1 cell array

    {'add'}

Sample period, specified in seconds. See Types of Sample Time and Specify Sample Time.

Programmatic Use

Block Parameter: SampleTime
Type: string scalar or character vector
Default: "-1"

Block Characteristics

Data Types

Booleana | busa | doublea | enumerateda | fixed pointa | integera | singlea

Direct Feedthrough

no

Multidimensional Signals

yesa

Variable-Size Signals

no

Zero-Crossing Detection

no

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

More About

expand all

Extended Capabilities

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Version History

Introduced in R2018b

expand all