Specify Clock Resolution Used by Target Environment Clock
For model functions that include blocks that request absolute or elapsed time values, improve the entry-point code generated for those functions by configuring the model to use a specific clock resolution. Clock resolution is the smallest increment of a clock value. For example, if a clock increments its value once per second, the clock resolution is 1 second.
Benefits of specifying a clock resolution include:
Clock resolution that aligns with target environment clock requirements. For models configured to use a service code interface, specifying a clock resolution results in code that reads time values more accurately.
Decoupling of the clock resolution and the solver properties that Simulink® uses during simulation, such as the fixed-step size and sample times. The decoupling enables you to generate code that aligns with the target environment clock resolution and produces more accurate results.
Influencing the data type that Simulink and the code generator use to represent time values. For example, in normal, accelerator, and rapid accelerator modes, Simulink uses the specified clock resolution to deduce fixed-point data types, which produces fixed-point simulation and generated code execution output that match.
Portability of models between code interface configurations. You can configure a model that has a specified clock resolution to use a shared Embedded Coder Dictionary that defines a data or service code interface configuration.
Generated code that is easier to read.
Specify Clock Resolution
To specify a clock resolution for the code generator to apply for a model, configure the model with these model configuration parameter settings:
Set System target file (
SystemTargetFile
) toert.tlc
.Set solver parameter Type (
SolverType
) toFixed-step
.Set Clock resolution (seconds, -1 inherited) (
ClockResolution
) to a scalar value of typedouble
, which represents the clock resolution in seconds.
If the model includes periodic functions, the clock resolution that you specify
must divide the sample times used by the periodic functions with no remainder. For
example, if a model includes periodic functions that use sample times
1
, 0.2
, and 0.1
, then
0.05
is a valid clock resolution setting, but
0.03
is not valid. If you must specify a clock resolution
that is not a divisor of the sample times, change the offending sample time.
You can specify a clock resolution for components and subcomponents in a model hierarchy. The clock resolution settings for models across the hierarchy must match. For models configured to use a service interface, the code generator checks for clock resolution consistency among corresponding entry-point functions generated for models across the model hierarchy. If the clock resolution settings are not consistent, the code generator returns an error.
How Code Generator Uses Specified Clock Resolution
How the code generator uses a specified clock resolution depends on the model code interface configuration. For models configured to use a data code interface, a specified clock resolution influences how the code generator represents the clock tick in the generated code. The clock tick is the increment of a processor clock. For models that you configure with a service code interface, a specified clock resolution influences timer service interface calls in the code.
For a model configured to use a service code interface, the code generator checks
the value of the related Application lifespan (days) parameter.
When the parameter is set to inf
(the default), the code
generator returns a 64-bit integer for a timer interface function call, honoring the
64-bit integer data type requirement for continuous execution. If the target
hardware does not support 64-bit integers, the code generator represents a 64-bit
timer as a multiword unsigned 64-bit integer data type.
You can generate a timer service for a 32-bit timer with an unsigned 32-bit
integer data type by setting model configuration parameters Application
lifespan (days) and Clock resolution (seconds, -1
inherited) so that the product of the application lifespan setting
and 86,400 divided by the clock resolution setting is less than or equal to
MAX_uint32_T
:
(Application lifespan x 86400)/Clock resolution) ≤ MAX_uint32_T
For aperiodic functions that are driven by an S-function that specifies the
SS_OPTION_ASYNCHRONOUS
option and a clock resolution, the
clock resolution that the S-function specifies overrides the setting of the
Clock resolution (seconds, -1 inherited) parameter.
For a model that uses a service code interface, when you specify a clock resolution, the code generator facilitates implementation of the timer service functions that run in the target environment and are called by the generated entry-point functions by including the clock resolution in two places:
In the Timer Service Interface section of the Code Interface Report. See Analyze Generated Service Code Interface Report (Embedded Coder).
As metadata in the generated code descriptor file
codedescriptor.dmr
. You can access the metadata by using the code descriptor programming interface. See Get Metadata About Service Interface (Embedded Coder).
If you choose not to specify a clock resolution, the code generator uses these default values for the clock resolution:
Model fixed-step size (fundamental sample time), for aperiodic functions
Function sample time, for periodic functions
Suppose a model includes a periodic function and an aperiodic function, both of
which use a block that requests absolute time from a target environment clock. The
fixed-step size (fundamental sample time) configured for the model is
1
second. The sample time configured for the periodic
function is 4
seconds. The model is configured to use a service
code interface that applies the during-execution data communication method.
For the preceding scenario, the figure below shows the periodic and aperiodic functions requesting the absolute time value at time A. The first row shows time for the Simulink scheduling clock. The second row shows the timing for function requests based on inherited clock resolution behavior. The third row shows the timing for function requests based on a specified scalar clock resolution value.
The horizontal arrows represent time (t).
The spacing between tick marks along the horizontal arrows represents the sample time/rate (r).
The blue boxes above the horizontal arrows represent execution time of the function.
The curved arrows represent function requests for the absolute time value at time A.
When using the inherited clock resolution behavior (with Clock
resolution (seconds, -1 inherited) set to -1
), you
can get time values that are more accurate by using aperiodic functions rather than
periodic functions. For this example:
Clock resolution behavior varies whether a function is periodic or aperiodic.
With a sample rate of 4 seconds, the periodic function requests the absolute time at 0 seconds.
Based on the model fixed-step size of 1 second, the aperiodic function requests the absolute time at 1 second.
Because the time that elapses between the aperiodic function request and time A is less than that of the periodic function, the time value received by the aperiodic function is more accurate than the value received by the periodic function.
You can get time values that are even more accurate by specifying a clock resolution as a scalar value. As the figure shows:
If you set the Clock resolution (seconds, -1 inherited) parameter to
0.5
, the periodic and aperiodic functions request the absolute time at 1.5 seconds.Because the time that elapses between the function requests and time A is less than that of the requests when the clock resolution is inherited, the time value received by the functions is more accurate.
Related Topics
- Improve Accuracy of Time Values Read by Generated Entry-Point Functions (Embedded Coder)
- Timer Service Interfaces (Embedded Coder)
- Configure Timer Service Interfaces (Embedded Coder)