Access Timers Programmatically
About Timer APIs
This topic describes APIs that let your S-functions take advantage of the efficiencies offered by absolute and elapsed timers. SimStruct macros are provided for use in simulation, and TLC functions are provided for inlined code generation. Note that
To generate and use the new timers as described above, your S-functions must register the need to use an absolute or elapsed timer by calling
ssSetNeedAbsoluteTime
orssSetNeedElapseTime
inmdlInitializeSampleTime
.Existing S-functions that read absolute time but do not register by using these macros continue to operate as expected, but generate less efficient code.
C API for S-Functions
The SimStruct macros described in this topic provide access to absolute and elapsed timers for S-functions during simulation.
In the functions below, the SimStruct *S
argument
is a pointer to the simstruct
of the calling S-function.
void ssSetNeedAbsoluteTime(SimStruct *S, boolean b)
: ifb
isTRUE
, registers that the calling S-function requires absolute time data, and allocates an absolute time counter for the rate at which the S-function executes (if such a counter has not already been allocated).int ssGetNeedAbsoluteTime(SimStruct *S)
: returns 1 if the S-function has registered that it requires absolute time.double ssGetTaskTime(SimStruct *S, tid)
: read absolute time for a given task with task identifiertid
.ssGetTaskTime
operates transparently, regardless of whether or not you use the new timer features.ssGetTaskTime
is documented in the SimStruct Functions chapter of the Simulink® documentation.void ssSetNeedElapseTime(SimStruct *S, boolean b)
: ifb
isTRUE
, registers that the calling S-function requires elapsed time data, and allocates an elapsed time counter for the triggered subsystem in which the S-function executes (if such a counter has not already been allocated). See also Elapsed Time Counters in Triggered Subsystems.int ssGetNeedElapseTime(SimStruct *S)
: returns 1 if the S-function has registered that it requires elapsed time.void ssGetElapseTime(SimStruct *S, (double *)elapseTime)
: returns, to the location pointed to byelapseTime
, the value (as adouble
) of the elapsed time counter associated with the S-function.void ssGetElapseTimeCounterDtype(SimStruct *S, (int *)dtype)
: returns the data type of the elapsed time counter associated with the S-function to the location pointed to bydtype
. This function is intended for use with thessGetElapseTimeCounter
function (see below).void ssGetElapseResolution(SimStruct *S, (double *)resolution)
: returns the resolution (that is, the sample time) of the elapsed time counter associated with the S-function to the location pointed to byresolution
. This function is intended for use with thessGetElapseTimeCounter
function (see below).void ssGetElapseTimeCounter(SimStruct *S, (void *)elapseTime)
: This function is provided for the use of blocks that require the elapsed time values for fixed-point computations.ssGetElapseTimeCounter
returns, to the location pointed to byelapseTime
, the integer value of the elapsed time counter associated with the S-function. If the counter size is 64 bits, the value is returned as an array of two 32-bit words, with the low-order word stored at the lower address.To determine how to access the returned counter value, obtain the data type of the counter by calling
ssGetElapseTimeCounterDtype
, as in the following code:int *y_dtype; ssGetElapseTimeCounterDtype(S, y_dtype); switch(*y_dtype) { case SS_DOUBLE_UINT32: { uint32_T dataPtr[2]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_UINT32: { uint32_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_UINT16: { uint16_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_UINT8: { uint8_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_DOUBLE: { real_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; default: ssSetErrorStatus(S, "Invalid data type for elaspe time counter"); break; }
If you want to use the actual elapsed time, issue a call to the
ssGetElapseTime
function to access the elapsed time directly. You do not need to get the counter value and then calculate the elapsed time.double *y_elapseTime; . . . ssGetElapseTime(S, elapseTime)
TLC API for Code Generation
The following TLC functions support elapsed time counters in generated code when you inline S-functions by writing TLC scripts for them.
LibGetTaskTimeFromTID
(block)
: Generates code to read the absolute time for the task in whichblock
executes.LibGetTaskTimeFromTID
is documented with other sample time functions in the TLC Function Library Reference pages of the Target Language Compiler documentation.Note
Do not use
LibGetT
for this purpose.LibGetT
always reads the base rate (tid 0
) timer. IfLibGetT
is called for a block executing at a subrate, the wrong timer is read, causing serious errors.LibGetElapseTime
(system)
: Generates code to read the elapsed time counter forsystem
. (system
is the parent system of the calling block.) See Generate Code for an Elapsed Time Counter for an example of code generated by this function.LibGetElapseTimeCounter
(system)
: Generates code to read the integer value of the elapsed time counter forsystem
. (system
is the parent system of the calling block.) This function should be used in conjunction withLibGetElapseTimeCounterDtypeId
andLibGetElapseTimeResolution
. (See the discussion ofssGetElapseTimeCounter
above.)LibGetElapseTimeCounterDtypeId
(system)
: Generates code that returns the data type of the elapsed time counter forsystem
. (system
is the parent system of the calling block.)LibGetElapseTimeResolution
(system)
: Generates code that returns the resolution of the elapsed time counter forsystem
. (system
is the parent system of the calling block.)