Simulink®
Developing S-Functions
R2016b
How to Contact MathWorks
Latest news:
Sales and services:
User community:
Technical support:
Phone:
www.mathworks.com
www.mathworks.com/sales_and_services
www.mathworks.com/matlabcentral
www.mathworks.com/support/contact_us
508-647-7000
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
Developing S-Functions
© COPYRIGHT 1998–2016 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program
or Documentation, the government hereby agrees that this software or documentation qualifies as
commercial computer software or commercial computer software documentation as such terms are used
or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and
conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and
govern the use, modification, reproduction, release, performance, display, and disclosure of the Program
and Documentation by the federal government (or other entity acquiring for or through the federal
government) and shall supersede any conflicting contractual terms or conditions. If this License fails
to meet the government's needs or is inconsistent in any respect with federal procurement law, the
government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
First printing
October 1998
Second printing
November 2000
Third printing
July 2002
Online only
April 2003
Online only
April 2004
Online only
June 2004
Online only
October 2004
March 2005
Online only
September 2005 Online Only
March 2006
Online only
September 2006 Online only
March 2007
Online only
September 2007 Online only
Online only
March 2008
Online only
October 2008
March 2009
Online only
September 2009 Online only
March 2010
Online only
September 2010 Online only
April 2011
Online only
September 2011 Online only
March 2012
Online only
September 2012 Online only
March 2013
Online only
September 2013 Online only
March 2014
Online only
Online only
October 2014
March 2015
Online only
September 2015 Online only
October 2015
Online only
March 2016
Online only
September 2016 Online only
Revised for Version 3.0 (Release R11)
Revised for Version 4.0 (Release R12)
Revised for Version 5.0 Release R13)
Revised for Version 5.1 (Release R13SP1)
Revised for Version 5.1.1 (Release R13SP1+)
Revised for Version 6.0 (Release R14)
Revised for Version 6.1 (Release R14SP1)
Revised for Version 6.2 (Release R14SP2)
Revised for Version 6.3 (Release R14SP3)
Revised for Version 6.4 (Release 2006a)
Revised for Version 6.5 (Release 2006b)
Revised for Version 6.6 (Release 2007a)
Revised for Version 7.0 (Release 2007b)
Revised for Version 7.1 (Release 2008a)
Revised for Version 7.2 (Release 2008b)
Revised for Version 7.3 (Release 2009a)
Revised for Version 7.4 (Release 2009b)
Revised for Version 7.5 (Release 2010a)
Revised for Version 7.6 (Release 2010b)
Revised for Version 7.7 (Release 2011a)
Revised for Version 7.8 (Release 2011b)
Revised for Version 7.9 (Release 2012a)
Revised for Version 8.0 (Release 2012b)
Revised for Version 8.1 (Release 2013a)
Revised for Version 8.2 (Release 2013b)
Revised for Version 8.3 (Release 2014a)
Revised for Version 8.4 (Release 2014b)
Revised for Version 8.5 (Release 2015a)
Revised for Version 8.6 (Release 2015b)
Rereleased for Simulink 8.5.1 (Release
2015aSP1)
Revised for Version 8.7 (Release 2016a)
Revised for Version 8.8 (Release 2016b)
Contents
1
Overview of S-Functions
What Is an S-Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use S-Functions in Models . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Parameters to S-Functions . . . . . . . . . . . . . . . . . . . .
When to Use an S-Function . . . . . . . . . . . . . . . . . . . . . . . . .
How S-Functions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mathematics of Simulink Blocks . . . . . . . . . . . . . . . . . . . . . .
Simulation Stages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
S-Function Callback Methods . . . . . . . . . . . . . . . . . . . . . . .
Implementing S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
MATLAB S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MEX S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
S-Function Callback Methods . . . . . . . . . . . . . . . . . . . . . . . .
Callback Methods Overview . . . . . . . . . . . . . . . . . . . . . . . .
Callback Methods for C MEX S-Functions . . . . . . . . . . . . . .
Callback Methods for Level-2 MATLAB S-Functions . . . . . .
S-Function Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . .
S-Function Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Direct Feedthrough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dynamically Sized Arrays . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Sample Times and Offsets . . . . . . . . . . . . . . . . . . .
S-Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overview of Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Level-2 MATLAB S-Function Examples . . . . . . . . . . . . . . .
1-2
1-3
1-3
1-5
1-7
1-8
1-8
1-8
1-8
1-11
1-12
1-12
1-13
1-14
1-14
1-15
1-16
1-19
1-20
1-20
1-21
1-22
1-26
1-26
1-29
v
Level-1 MATLAB S-Function Examples . . . . . . . . . . . . . . .
C S-Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fortran S-Function Examples . . . . . . . . . . . . . . . . . . . . . . .
C++ S-Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . .
1-29
1-31
1-34
1-35
2
3
Selecting an S-Function Implementation
Available S-Function Implementations . . . . . . . . . . . . . . . . . .
S-Function Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implement S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
S-Function Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
S-Function Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
S-Functions Incorporate Legacy C Code . . . . . . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using a Hand-Written S-Function to Incorporate Legacy
Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the S-Function Builder to Incorporate Legacy Code . .
Using the Legacy Code Tool to Incorporate Legacy Code . . .
2-2
2-3
2-4
2-6
2-10
2-12
2-12
2-13
2-15
2-18
Writing S-Functions in MATLAB
Custom Blocks using MATLAB S-Functions . . . . . . . . . . . . . .
Write Level-2 MATLAB S-Functions . . . . . . . . . . . . . . . . . . . .
About Level-2 MATLAB S-Functions . . . . . . . . . . . . . . . . . .
About Run-Time Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Level-2 MATLAB S-Function Template . . . . . . . . . . . . . . . . .
Level-2 MATLAB S-Function Callback Methods . . . . . . . . . .
Using the setup Method . . . . . . . . . . . . . . . . . . . . . . . . . . .
Example of Writing a Level-2 MATLAB S-Function . . . . . . .
3-2
3-3
3-3
3-4
3-4
3-5
3-6
3-7
vi
Contents
Instantiating a Level-2 MATLAB S-Function . . . . . . . . . . .
Operations for Variable-Size Signals . . . . . . . . . . . . . . . . . .
Generating Code from a Level-2 MATLAB S-Function . . . . .
MATLAB S-Function Examples . . . . . . . . . . . . . . . . . . . . . .
Maintain Level-1 MATLAB S-Functions . . . . . . . . . . . . . . . .
About the Maintenance of Level-1 MATLAB S-Functions . .
Level-1 MATLAB S-Function Arguments . . . . . . . . . . . . . .
Level-1 MATLAB S-Function Outputs . . . . . . . . . . . . . . . . .
Define S-Function Block Characteristics . . . . . . . . . . . . . . .
Processing S-Function Parameters . . . . . . . . . . . . . . . . . . .
Convert Level-1 MATLAB S-Functions to Level-2 . . . . . . . .
3-10
3-11
3-11
3-11
3-12
3-12
3-13
3-14
3-15
3-15
3-16
4
Writing S-Functions in C
About C S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating C MEX S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . .
Build S-Functions Automatically . . . . . . . . . . . . . . . . . . . . . . .
About Building S-Functions Automatically . . . . . . . . . . . . . .
Deploying the Generated S-Function . . . . . . . . . . . . . . . . . . .
How the S-Function Builder Builds an S-Function . . . . . . . .
S-Function Builder Dialog Box . . . . . . . . . . . . . . . . . . . . . . .
About S-Function Builder . . . . . . . . . . . . . . . . . . . . . . . . . .
Parameters/S-Function Name Pane . . . . . . . . . . . . . . . . . . .
Port/Parameter Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initialization Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Properties Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Input Ports Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Output Ports Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parameters Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Type Attributes Pane . . . . . . . . . . . . . . . . . . . . . . . . .
Libraries Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Outputs Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Continuous Derivatives Pane . . . . . . . . . . . . . . . . . . . . . . .
Discrete Update Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Build Info Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-2
4-4
4-5
4-5
4-9
4-9
4-11
4-11
4-13
4-14
4-14
4-15
4-16
4-17
4-18
4-19
4-19
4-21
4-24
4-25
4-26
vii
Example: Modeling a Two-Input/Two-Output System . . . . .
Basic C MEX S-Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing an Example of a Basic C MEX S-Function . . . .
Defines and Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Callback Method Implementations . . . . . . . . . . . . . . . . . . .
Simulink/Simulink Coder Interfaces . . . . . . . . . . . . . . . . . .
Building the Timestwo Example . . . . . . . . . . . . . . . . . . . . .
Templates for C S-Functions . . . . . . . . . . . . . . . . . . . . . . . . .
About the Templates for C S-Functions . . . . . . . . . . . . . . . .
S-Function Source File Requirements . . . . . . . . . . . . . . . . .
The SimStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data Types in S-Functions . . . . . . . . . . . . . . . . . . . . . . . . .
Compiling C S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
Integrate C Functions Using Legacy Code Tool . . . . . . . . . .
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Integrate C Functions into Simulink Models with Legacy Code
Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Integrate C Function Whose Arguments Are Pointers to
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Registering Legacy Code Tool Data Structures . . . . . . . . . .
Declaring Legacy Code Tool Function Specifications . . . . . .
Generating and Compiling the S-Functions . . . . . . . . . . . . .
Generating a Masked S-Function Block for Calling a Generated
S-Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Forcing Simulink Accelerator Mode to Use S-Function TLC
Inlining Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Calling Legacy C++ Functions . . . . . . . . . . . . . . . . . . . . . . .
Handling Multiple Registration Files . . . . . . . . . . . . . . . . .
Deploying Generated S-Functions . . . . . . . . . . . . . . . . . . . .
Legacy Code Tool Examples . . . . . . . . . . . . . . . . . . . . . . . .
Legacy Code Tool Limitations . . . . . . . . . . . . . . . . . . . . . . .
Simulink Engine Interaction with C S-Functions . . . . . . . .
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Process View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Data View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Write Callback Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
viii
Contents
4-27
4-33
4-33
4-35
4-36
4-38
4-38
4-39
4-39
4-39
4-41
4-42
4-42
4-43
4-43
4-46
4-49
4-54
4-55
4-62
4-63
4-64
4-64
4-65
4-66
4-66
4-66
4-68
4-68
4-68
4-76
4-80