Calling C Functions in Actions

www.kxcad.net Home > CAE Software Index > MATLAB Index >


Your Ad Here

Calling C Library Functions

You can call the following small 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**

 

* Stateflow extends the abs function beyond that of its standard C counterpart with its own built in functionality. See Calling the abs Function.

** Stateflow also allows you to replace calls to the C Math Library with target-specific implementations for this subset of functions. For more information, see Replacing C Math Library Functions with Target-Specific Implementations

You can call the above C Math Library functions without doing anything special as long as you are careful to call them with the right data types. In case of a type mismatch, Stateflow replaces the input argument with a cast of the original argument to the expected type. For example, if you call the sin function with an integer argument, Stateflow replaces the argument with a cast of the original argument to a floating-point number of type double.

If you call other C library functions not specified above, be sure to include the appropriate #include... directive in the Custom code included at the top of generated code field of the Target Options dialog. See the section Specifying Custom Code Options for Stateflow Targets.

Calling the abs Function

Stateflow extends the interpretation of its abs function 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 Stateflow in the strict sense of standard C, be sure to cast its argument or return values to integer types. See Type Cast Operations.

Calling min and max Functions

Although min and max are not C library functions, Stateflow enables them 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() or max(), Stateflow generates code for them with a mangled name of the following form: <prefix>_min. However, if you export min() or max() graphical functions to other Stateflow charts in the Stateflow machine, 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() and max() graphical functions.

Replacing C Math Library Functions with Target-Specific Implementations

You can use the target function library published by Real-Time Workshop® Embedded Coder to replace the default implementations of a subset of C-library functions with target-specific implementations (see Supported Target Library Functions). When you specify a target function library, Stateflow generates code that calls the target implementations instead of the associated C library functions. Furthermore, Stateflow also uses target implementations in cases where the compiler generates calls to math functions, such as in fixed-point arithmetic utilities.

Using Target Function Libraries

To learn how to create and register function replacement tables in a target function library, see Target Function Libraries in the Real-Time Workshop Embedded Coder User's Guide documentation. To select and view target function libraries, see Selecting and Viewing Target Function Libraries in the Real-Time Workshop User's Guide.

Supported Target Library Functions

You can replace the following C Math Library functions with target-specific implementations:

Replacing Calls to abs

Stateflow replaces calls to abs with target functions as follows:

For:Action:
abs with floating-point argumentsReplaces with target function
abs with integer argumentsReplaces with ANSI C function
abs with fixed-point arguments (zero bias)Replaces with ANSI C function
abs with fixed-point arguments (non-zero bias)Generates error

Calling Custom C Code Functions

You can install your own C code functions for use in the Stateflow action language for simulation and for C code generation.

For additional information, seeSpecifying Custom Code Options for Stateflow Targets.

  1. From the Tools menu, select the Open (RTW or Simulation) Target dialog.

  2. When the Open Target dialog appears, select Target Options.

  3. Enter the following:

Specifying Custom C Functions for Simulation

To specify custom C functions for simulation, see Including Custom Code in Stateflow Simulation Targets.

Specifying Custom C Functions for Generated Code

To specify custom C functions for generated code, follow these steps:

  1. In the Stateflow Editor, select Tools > Open RTW Target.

    The Configuration Parameters dialog box opens, displaying the general Real-Time Workshop configuration parameters.

  2. In the left pane of the Configuration Parameters dialog box, select Custom Code and specify your custom C files as described in Real-Time Workshop Pane: Custom Code.

Guidelines for Using Custom C Functions in Stateflow

Follow these guidelines when using your own C code functions in the Stateflow action language:

Function Call Transition Action Example

These are example formats of function calls using transition action notation.

If S1 is active, event e occurs, c is true, and the transition destination is determined, then a function call is made to function_name with arg1, arg2, and arg3. The transition action in the transition from S2 to S3 shows a function call nested within another function call.

Function Call State Action Example

These are example formats of function calls using state action notation.

When the default transition into S1 occurs, S1 is marked active and then its entry action, a function call to function_name1 with the specified arguments, is executed and completed. If S2 is active and an event occurs, the during action, a function call to function_name3 with the specified arguments, executes and completes.

Passing 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

f(&x);

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

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

  


© 1984-2007 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments

Your Ad Here