Main Content

Timers in Asynchronous Tasks

An S-function for blocks that includes an interrupt service routine (ISR) can set a source for absolute time. You do this by calling the function ssSetTimeSource. Before calling ssSetTimeSource, get the port width of the output port by calling ssSetOutputPortWidth. Otherwise, the program comes to a halt and returns an error message. ssSetTimeSource has these options:

  • SS_TIMESOURCE_SELF: Each generated ISR maintains its own absolute time counter, which is distinct from periodic base rate or subrate counters in the system. The counter value and the timer resolution value (specified with the block parameter Timer resolution (seconds) of the Async Interrupt block) are used by downstream blocks to determine absolute time values required by block computations.

  • SS_TIMESOURCE_CALLER: The ISR reads time from a counter maintained by its caller. Time resolution is thus the same as the caller resolution.

  • SS_TIMESOURCE_BASERATE: The ISR can read absolute time from the model periodic fixed-step size. Time resolution is thus the same as its fixed-step size resolution.

Note

The operating system integration techniques that are demonstrated in this topic use blocks that are in the Interrupt Templates block library. The blocks in that library serve as examples to help you develop custom blocks for a specific target environment.

By default, the counter is implemented as a 32-bit unsigned integer member of the Timing substructure of the real-time model structure. For a target that supports the rtModel data structure, when the time data type is not set by using ssSetAsyncTimeDataType, the counter word size is determined by the setting of model configuration parameter Application lifespan (days).

This example shows code generated for a model configured to use an ERT-based system target file.

/* Real-time Model Data Structure */
struct _RT_MODEL_elapseTime_exp_Tag {
   const char *errorStatus;
  
  /*
   * Timing:
   * The following substructure contains information regarding
   * the timing information for the model.
   */
  struct {
    uint32_T clockTick1;
    uint32_T clockTick2;
  } Timing;
};

The example omits unused fields in the Timing data structure (a feature of ERT-based code). For a target that supports the rtModel data structure, the counter word size is determined by the setting of model configuration parameter Application lifespan (days).

By default, the vxlib1 library blocks for the example RTOS (VxWorks®) set the timer source to SS_TIMESOURCE_SELF and update their counters by using the function tickGet. The tickGet function returns a timer value maintained by the RTOS kernel. The maximum word size for the timer is UINT32. The following example shows a generated call to tickGet.

/* VxWorks Interrupt Block: '<Root>/Async Interrupt' */
void isr_num2_vec193(void)
{

 /* Use tickGet() as a portable tick counter example. A much
    higher resolution can be achieved with a hardware counter */
 rtM->Timing.clockTick2 = tickGet();
. . .

The tickGet call is supplied only as an example. It can (and in many instances should) be replaced by a timing source that has better resolution. If you are implementing a custom asynchronous block for an RTOS other than the example RTOS (VxWorks), you should either generate an equivalent call to the target RTOS, or generate code to read a timer register on the target hardware.

Change the setting for the block parameter Timer resolution (seconds) for your Async Interrupt block to match the resolution of timing source for you target computer.

The counter is updated at interrupt level. Its value represents the tick value of the timing source at the most recent execution of the ISR. The rate of this timing source is unrelated to sample rates in the model. In fact, typically it is faster than the model's base rate. Select the timer source and set its rate and resolution based on the expected rate of interrupts to be serviced by the Async Interrupt block.

For an example of timer code generation, see Async Interrupt Block Implementation.

Related Examples

More About