Timer
Timers are one of the most fundamental building blocks of Embedded Systems. Times are so widely used and you will not be able to find an embedded micro-controller without at least a dozen hardware timers implemented directly on its silicon. Timers are used for a wide variety of applications, from managing the RTOS scheduler to triggering ADC sampling and so on.
The implemented Timer API in Weld allows developers to use the timing functionalities in the context of State Machines. These functionalities, which are used in transitions, include:
- Time-base state transition
- Time-based thread activation
- General purpose scheduling
- Transition guards
- Time-based resource allocation
The Weld built-in timer can only be used in transition conditions. If the transition originates from a State Object, the timer measures the time that has passed since the activation of that state. If the transition originates from a Junction, it measures the time, during which, at-least one transition path has been valid ending to that Junction.
Usage¶
The built-in API call after(t, unit)
is the main method of using timers in a Weld application. There is no other setup or initialization required to use the Timers. The only parameter that is required is the execution frequency of the thread, in which the API is called. The Weld Code Generator (WCG) assumes the Thread function is called with the specified frequency in order to calculate the timing.
Note
The Execution Frequency of the Thread should be set properly in order for successful code generation.
Timer Parameters¶
The timer API call after(t, unit)
includes two mandatory parameters. The first parameter, t
is the time that should be passed and the second parameter unit
denotes the units of the first parameter which can be:
ns
for nanosecondus
for microsecondms
for millisecondss
for seconds
So, for example if order to measure 10 milliseconds of time inside a transition, we should add if(after(10, ms))
as the transition condition.
It should be noted that the timer API returns a boolean, so, ot can be chained to other conditional statements using logical operators