Call C Library Functions in C Charts - MATLAB & Simulink (original) (raw)

Stateflow® charts in Simulink® models have an action language property that defines the syntax for state and transition actions. An icon in the lower-left corner of the chart canvas indicates the action language for the chart.

Call C Library Functions

You can call this subset of the C Math Library functions:

abs* ** acos** asin** atan** atan2** ceil**
cos** cosh** exp** fabs floor** fmod**
labs ldexp** log** log10** pow** rand
sin** sinh** sqrt** tan** tanh**
* The Stateflowabs function goes beyond that of its standard C counterpart with its own built-in functionality. For more information, see Call the abs Function.
** You can also replace calls to the C Math Library with application-specific implementations for this subset of functions. For more information, see Replacement of Math Library Functions with Application Implementations.

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 replace the original arguments. For example, if you call thesin function with an integer argument, a cast of the input argument to a floating-point number of type double replaces the original argument.

Note

Because the input arguments to the C library functions are first cast to floating-point numbers, function calls with arguments of type int64 oruint64 can result in loss of precision.

If you call other C library functions not listed above, open the Configuration Parameters dialog box and, in the Simulation Target pane, enter the appropriate #include statements, as described in Configure Custom Code.

Call the abs Function

Interpretation of the Stateflowabs function goes beyond the standard C version to include integer and floating-point arguments of all types as follows:

If you want to use the abs function in the strict sense of standard C, cast its argument or return values to integer types. For more information, see Type Cast Operations.

Call min and max Functions

You can call min and max by emitting the following macros automatically at the top of generated code.

#define min(x1,x2) ((x1) > (x2) ? (x2):(x1)) #define max(x1,x2) ((x1) > (x2) ? (x1):(x2))

To allow compatibility with user graphical functions named min() ormax(), generated code uses a mangled name of the following form:<prefix>_min. However, if you export min() ormax() graphical functions to other charts in your model, the name of these functions can no longer be emitted with mangled names in generated code and conflict occurs. To avoid this conflict, rename the min() andmax() graphical functions.

Replacement of Math Library Functions with Application Implementations

You can configure the code generator to change the code that it generates for math library functions such that the code meets application requirements. To do this you configure the code generator to apply a code replacement library (CRL) during code generation. If you have an Embedded Coder® license, you can develop and apply custom code replacement libraries.

For more information about replacing code, using code replacement libraries that MathWorks® provides, see What Is Code Replacement? (Simulink Coder) and Code Replacement Libraries (Simulink Coder). For information about developing custom code replacement libraries, see What Is Code Replacement Customization? (Embedded Coder) and Code You Can Replace From Simulink Models (Embedded Coder).

Call Custom C Code Functions

You can specify custom code functions for use in C charts for simulation and C code generation. For more information, see Configure Custom Code.

Guidelines for Calling Custom C Functions in Your Chart

Guidelines for Writing Custom C Functions That Access Input Vectors

Function Call in Transition Action

Example formats of function calls using transition action notation appear in the following chart.

Chart that contains transition actions with calls to custom code functions fcn1 and fcn2.

A function call to fcn1 occurs with arg1,arg2, and arg3 if the following are true:

The transition action in the transition from S2 toS3 shows a function call nested within another function call.

Function Call in State Action

Example formats of function calls using state action notation appear in the following chart.

Chart that contains state actions with calls to custom code functions fcn1 and fcn2.

Chart execution occurs as follows:

  1. When the default transition into S1 occurs,S1 becomes active.
  2. The entry action, a function call to fcn1 with the specified arguments, executes.
  3. After 5 seconds of simulation time, S1 becomes inactive andS2 becomes active.
  4. The during action, a function call to fcn2 with the specified arguments, executes.
  5. After 10 seconds of simulation time, S2 becomes inactive andS1 becomes active again.
  6. Steps 2 through 5 repeat until the simulation ends.

Pass Arguments by Reference

A Stateflow action can pass arguments to a user-written function by reference rather than by value. In particular, an action can pass a pointer to a value rather than the value itself. For example, an action could contain the following call:

where f is a custom-code C function that expects a pointer tox as an argument.

If x is the name of a data item defined in the Stateflow hierarchy, the following rules apply: