S-Functions That Support Timer Service Interfaces for Accessing Time Values
You can implement inlined S-Function blocks for C MEX S-functions that access 32-bit time values and use the blocks in models configured to use timer service interfaces.
Implement and Use Inlined S-Function Blocks That Support Timer Service Interfaces
To implement an inlined S-Function block that relies on time values and that you can use in models that are configured to use a service code interface:
Create a C MEX S-function. See Create and Implement a Basic C MEX S-Function and C MEX S-Function Examples.
Compile the C MEX S-function by using the
mex
function.Create an S-function target file (
) for the S-Function block, as described in the next section. The name of the target file and the folder that contains the file must match the name and folder of the compiled MEX file. When the code generator finds a target file that matches the compiled MEX file, the code generator inlines the S-function by directing the Target Language Compiler (TLC) to insert only statements defined in the target file into the code generated from the model. See Create S-Function Target File That Supports Timer Service Interfaces.sfunction-name
.tlcAdd the compiled MEX S-function to a model by using the S-Function block. Use the S-Function block parameters to specify the name and parameters of the compiled MEX S-function.
Configure the model to use an Embedded Coder Dictionary that includes definitions for timer service interfaces. For more information about model dictionary configuration, see Shared coder dictionary and Embedded Coder Dictionary.
In the Code Mappings editor, for each function in the model that needs to access time values, configure a timer service interface. See Configure Timer Service Interfaces, Generate C Timer Service Interface Code for Component Deployment, and Code Mappings editor.
Generate and inspect the C code.
Create S-Function Target File That Supports Timer Service Interfaces
To develop an S-Function block that relies on time values and can be used in a model that is configured for timer service interface code generation, in the S-function target file:
Enable the enhanced TLC interface, which enables block function optimizations. Call the LibEnableBlockFcnOptimizations(block) function from within the function
BlockInstanceSetup
.TheBlockInstanceSetup
function generates code for each instance of a given block type.Call TLC library functions as required for your S-function algorithm.
Goal Library Function Call Absolute Time Get the clock tick of the task timer as an integer value. LibGetClockTick(tid) Get the step size of the clock tick (resolution of task time). LibGetClockTickStepSize(tid) Get the data type ID of the clock tick. LibGetClockTickDataTypeId(tid) Get a string to use for accessing the absolute time of the task as a floating-point value. LibGetTaskTime(tid) Get a string to use for accessing the absolute time of the task associated with the block. LibGetTaskTimeFromTID(block) Elapsed Time Get an integer value that represents the elapsed time (the number of clock ticks elapsed since the last time the system started). LibGetElapseTimeCounter(system) Get the resolution of the elapsed time. LibGetElapseTimeResolution(system) Get the data type ID of the elapsed time integer value. LibGetElapseTimeCounterDTypeId(system) Get the elapsed time since the last time the subsystem started to execute. LibGetElapseTime(system) Usage notes:
When using these function calls, specify a global task identifier (
tid
). TLC library functions that use atid
require thetid
to be global. If you specify the localtid
, the code generator might produce incorrect code for the S-Function block. Compute the globaltid
by specifying the block task identifiersfcnTID
in function call LibGetGlobalTIDFromLocalSFcnTID(sfcnTID).LibGetClockTick(tid)
LibGetClockTickStepSize(tid)
LibGetClockTickDataTypeID(tid)
LibGetTaskTime(tid)
If you use these function calls, configure the S-Function block to use absolute time by calling the
SimStruct
functionssSetNeedAbsoluteTime
with the absolute time Boolean flag set to1
.LibGetClockTick(tid)
LibGetTaskTime(tid)
LibGetTaskTimeFromTID(block)
If you use these function calls, configure the S-Function block to use elapsed time by calling the
SimStruct
functionssSetNeedElapseTime
with the elapsed time Boolean flag set to1
.LibGetElapseTimeCounter(system)
LibGetElapseTime(system)
To use these function calls, configure your model to support floating-point numbers (select model configuration parameter Support: floating-point numbers).
LibGetTaskTime(tid)
LibGetTaskTimeFromTID(block)
LibGetElapseTime(system)