Writing S-Functions | ![]() ![]() |
Synchronizing Multirate S-Function Blocks
If tasks running at different rates need to share data, you must ensure that data generated by one task is valid when accessed by another task running at a different rate. You can use the ssIsSpecialSampleHit
macro in the mdlUpdate
or mdlOutputs
routine of a multirate S-function to ensure that the shared data is valid. This macro returns true if a sample hit has occurred at one rate and a sample hit has also occurred at another rate in the same time step. It thus permits a higher rate task to provide data needed by a slower rate task at a rate the slower task can accommodate.
Suppose, for example, that your model has an input port operating at one rate, 0, and an output port operating at a slower rate, 1. Further, suppose that you want the output port to output the value currently on the input. The following example illustrates usage of this macro.
if (ssISampleHit(S, 0, tid) { if (ssIsSpecialSampleHit(S, 0, 1, tid) { /* Transfer input to output memory. */ ... } } if (ssIsSampleHit(S, 1, tid) { /* Emit output. */ ... }
In this example, the first block runs when a sample hit occurs at the input rate. If the hit also occurs at the output rate, the block transfers the input to the output memory. The second block runs when a sample hit occurs at the output rate. It transfers the output in its memory area to the block's output.
Note that higher-rate tasks always run before slower-rate tasks. Thus, the input task in the preceding example always runs before the output task, ensuring that valid data is always present at the output port.
![]() | Multirate S-Function Blocks | Work Vectors | ![]() |