main content

integrate and call external c/c code from a simulink model -凯发k8网页登录

integrate and call external c/c code from a simulink model

since r2020a

  • c function block

libraries:
simulink / user-defined functions

description

the c function block integrates and calls external c/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 conditionally call functions defined in your code, and you can also call multiple functions in one block. using this block, you can initialize persistent data and pass it to an external function.

the c function block supports initializing persistent data and calling external functions from the block dialog box. persistent data can include an object of a c class defined in your custom code. see . the 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

define the source code and supporting files to be called by the c function block in the simulation target pane of the model configuration parameters dialog box. see .

note

c99 is the standard version of c language supported for custom c code integration into simulink.

call c library functions

you can call these c math library functions directly from the c function block.

absacosasinatanatan2ceil
coscoshexpfabsfloorfmod
labsldexploglog10powsin
sinhsqrttantanh  

when you call these functions, double precision applies unless all the input arguments are explicitly single precision. when a type mismatch occurs, a cast of the input arguments to the expected type replaces the original arguments. for example, if you call the sin function with an integer argument, a cast of the input argument to a floating-point number of type double replaces the original argument.

to call other c library functions, create and call an external wrapper function that calls the c library function.

call the abs, fabs, and labs functions

interpretation of the abs, fabs, and labs functions in c function block goes beyond the standard c version to include integer and floating-point arguments:

  • if x is an integer, the standard c function abs applies to x, or abs(x).

  • if x is a double, the standard c function labs applies to x, or labs(x).

  • if x is a single, the standard c function fabs applies to x, or fabs(x).

code replacement library (crl) based on type

the call to the function should call the correct crl based on the type of data passed into the function. if no crl is specified, the call to the function should call to type-specific library. the crl for c99 generates a type-specific function. for example:

type passed incode generation call
sin(doublein)sin(doublein)
sin(floatin)sinf(floatin)

examples

limitations

limitations apply when using these simulink features with the c function block.

  • simulink coverage™

    only execution coverage of the c function block is measured.

  • simulink design verifier™

    simulink design verifier does not generate test cases for coverage objectives inside the c function block. the limitations that apply to c/c s-functions also apply to c/c code in a c function block. in particular, calls to external or library functions are replaced by stubs for analysis. for more information on these limitations, see (simulink design verifier).

  • simulink code inspector™

    simulink code inspector does not inspect the code generated from the c function block.

these limitations apply to the c code that you specify in a c function block.

  • local static variables using the static keyword are not supported. to cache values across time steps, define a symbol as persistent in the symbols table of the block dialog box.

  • you cannot #include files in the code. files containing external functions must be specified in the simulation target pane of the configuration parameters window.

  • taking the address of a constant symbol is not supported.

  • directly calling c library functions other than the c math library functions listed above under call c library functions is not supported. to call other c library functions, create and call a wrapper function that calls the c library function.

ports

input

the number of input ports is determined by the number of symbols with input or inputoutput scope defined in the symbols table in the block parameters dialog box. each input port label is the same as the name of the input or inputoutput symbol unless you change it by editing the label field in the symbols table of the block dialog box.

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | boolean | enumerated | bus | fixedpoint

output

the number of output ports is determined by the number of symbols with output or inputoutput scope defined in the symbols table in the block parameters dialog box. each output port label is the same as the name of the output or inputoutput symbol unless you change it by editing the label field in the symbols table of the block dialog box.

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | boolean | enumerated | bus | fixedpoint

parameters

output code that the block executes at each time step during simulation, specified as a character vector or string scalar. for example, you can call a function from external c code, make modifications to the results, and perform operations to pass the results to other blocks.

programmatic use

block parameter: outputcode
type: character vector or string scalar
value: "" | c code
default: ""

initialization code that the block executes one time at the start of simulation, specified as a character vector or string scalar. for example, you can initialize persistent symbols.

programmatic use

block parameter: startcode
type: character vector or string scalar
value: "" | c code
default: ""

reinitialization code for the block to execute when enabling a subsystem or model in which the block is placed, specified as a character vector or string scalar. the code executes one time at the start of simulation, and if the block is inside a subsystem or model containing an block with the states when enabling parameter set to reset, the code also executes each time the subsystem or model switches from disabled to enabled. see . you can use this code, for example, to set an initial output value or reset the value of a persistent variable.

programmatic use

block parameter: initializeconditionscode
type: character vector or string scalar
value: "" | c code
default: ""

termination code that the block executes one time at the end of simulation, specified as a character vector or string scalar. for example, use this code to free the memory cached on persistent symbols specified as void pointers.

programmatic use

block parameter: terminatecode
type: character vector or string scalar
value: "" | c code
default: ""

symbols and symbol properties used in the c code, specified as a table. you must enter the following attributes of each symbol in the table:

  • name — symbol name in the code.

    if the symbol represents a c class object, the name field serves as a call to the class constructor:

    objectname(argument1,argument2,...)
  • scope — scope of the symbol. these scopes are available:

    • input — input to the block.

    • output — output to the block.

    • inputoutput — both input and output to the block.

      use the inputoutput scope to map an input passed by a pointer in your c code. ports created using an inputoutput scope have the same name for input and output ports. inputoutput scope enables buffer reuse for input and output ports. buffer reuse may optimize memory use and improve code simulation and code generation efficiency, depending on the signal size and the block layout. limitations include:

      • an inputoutput symbol cannot be used in start code, initialize conditions code, or terminate code.

      • inputoutput symbols do not support the void* data type.

      • inputoutput symbols do not support size() expressions.

    • parameter — block parameter that appears on the block parameter mask. the parameter name is defined by the label of the symbol.

    • persistent — persistent block data, which retains its value from one time step to the next during simulation.

      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.

      you can instantiate an object of a c class defined in your custom code by defining a symbol with persistent scope and using class: classname as the type for the symbol. see .

    • constant — constant value, defined using value-size or numeric expressions.

  • label — label of the symbol. for a symbol with input, inputoutput, or output scope, the label appears as the port name on the block. for a symbol with parameter scope, the label appears on the block parameter mask. if the scope is constant, the label is the constant expression. you cannot define a label for persistent scope symbols.

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

    c class types defined in your custom code are supported, as are , , and types. enter the type as shown in this table.

    custom typespecification in type field
    c classclass: c classname
    simulink.busbus: bustypename
    simulink.enumenum: enumtypename
    simulink.aliastypealiastypename
  • size — size of the symbol data. you can use a size expression to define the size of an output or use -1 to inherit size.

  • port — port index of the symbol. for an input, inputoutput, or output symbol, port specifies the port index on the block of the port or ports corresponding to the symbol. for a parameter symbol, port specifies the order that the symbol appears in the block parameter mask.

programmatic use

block parameter: symbolspec
type: object
value: object
default: empty array of objects

sample period, specified in seconds. see and . if the block defines persistent symbols, you cannot specify a continuous 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

yes

variable-size signals

no

zero-crossing detection

no

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

extended capabilities

version history

introduced in r2020a

see also

blocks

objects

  • |

functions

  • | |

topics

    网站地图