Main Content

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:

  1. Create a C MEX S-function. See Create a Basic C MEX S-Function and C MEX S-Function Examples.

  2. Compile the C MEX S-function by using the mex function.

  3. Create an S-function target file (sfunction-name.tlc) 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.

  4. Add 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.

  5. 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.

  6. 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.

  7. 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:

  1. Enable the enhanced TLC interface, which enables block function optimizations. Call the LibEnableBlockFcnOptimizations(block) function from within the function BlockInstanceSetup.The BlockInstanceSetup function generates code for each instance of a given block type.

  2. Call TLC library functions as required for your S-function algorithm.

     GoalLibrary Function Call
    Absolute TimeGet 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 TimeGet 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 a tid require the tid to be global. If you specify the local tid, the code generator might produce incorrect code for the S-Function block. Compute the global tid by specifying the block task identifier sfcnTID 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 function ssSetNeedAbsoluteTime with the absolute time Boolean flag set to 1.

      • 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 function ssSetNeedElapseTime with the elapsed time Boolean flag set to 1.

      • 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)

Related Topics