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
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.
abs | acos | asin | atan | atan2 | ceil |
cos | cosh | exp | fabs | floor | fmod |
labs | ldexp | log | log10 | pow | sin |
sinh | sqrt | tan | tanh |
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.
abs
, fabs
, and labs
functionsinterpretation 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 functionabs
applies tox
, orabs(x)
.if
x
is a double, the standard c functionlabs
applies tox
, orlabs(x)
.if
x
is a single, the standard c functionfabs
applies tox
, orfabs(x)
.
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 in | code 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 aspersistent
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
output
parameters
block characteristics
extended capabilities
version history
introduced in r2020a
see also
blocks
objects
- |
functions
- | |