High level interface for interacting with the Timer/Counter hardware resource.
The timer block is commonly used to measure the time of occurrence of an event, to measure the time difference between two events or perform an action after a specified period of time. The driver also allows the user to invoke a callback function when a particular event occurs.
Some use case scenarios of timer -
See Snippet 1: Measuring time of an operation.
The following snippet initializes a Timer and measures the time between two events. The clock will be defined in design.modus, and can be over-ridden with the mtb_hal_timer_setup() call.
The following snippet initializes a Timer and triggers an event after every one second. The clock will be defined in design.modus, and can be over-ridden with the mtb_hal_timer_setup() call.
API Reference | |
Timer HAL Results | |
Timer specific return codes. | |
Typedefs | |
typedef void(* | mtb_hal_timer_event_callback_t) (void *callback_arg, mtb_hal_timer_event_t event) |
Handler for timer events, callback_arg is a pointer to the timer obj. | |
Enumerations | |
enum | mtb_hal_timer_event_t { MTB_HAL_TIMER_EVENT_NONE = (MTB_HAL_MAP_TIMER_EVENT_NONE) , MTB_HAL_TIMER_EVENT_TERMINAL_COUNT = (MTB_HAL_MAP_TIMER_EVENT_TERMINAL_COUNT) , MTB_HAL_TIMER_EVENT_COMPARE_CC0 = (MTB_HAL_MAP_TIMER_EVENT_COMPARE_CC0) , MTB_HAL_TIMER_EVENT_COMPARE_CC0_OR_TERMINAL_COUNT = (MTB_HAL_MAP_TIMER_EVENT_COMPARE_CC0_OR_TERMINAL_COUNT) , MTB_HAL_TIMER_EVENT_COMPARE_CC1 = (MTB_HAL_MAP_TIMER_EVENT_COMPARE_CC1) , MTB_HAL_TIMER_EVENT_ALL = (MTB_HAL_MAP_TIMER_EVENT_ALL) } |
HAL Timer events. More... | |
Functions | |
cy_rslt_t | mtb_hal_timer_setup (mtb_hal_timer_t *obj, const mtb_hal_timer_configurator_t *config, mtb_hal_clock_t *clock) |
Sets up a HAL instance to use the specified hardware resource. More... | |
cy_rslt_t | mtb_hal_timer_start (mtb_hal_timer_t *obj) |
Starts the timer/counter with the pre-set configuration. More... | |
cy_rslt_t | mtb_hal_timer_stop (mtb_hal_timer_t *obj) |
Stops the timer/counter. More... | |
cy_rslt_t | mtb_hal_timer_reset (mtb_hal_timer_t *obj, uint32_t start_value) |
Reset the timer/counter value. More... | |
uint32_t | mtb_hal_timer_read (const mtb_hal_timer_t *obj) |
Reads the current value from the timer/counter See Snippet 1: Measuring time of an operation. More... | |
void | mtb_hal_timer_register_callback (mtb_hal_timer_t *obj, mtb_hal_timer_event_callback_t callback, void *callback_arg) |
Register a timer/counter callback handler More... | |
void | mtb_hal_timer_enable_event (mtb_hal_timer_t *obj, mtb_hal_timer_event_t event, bool enable) |
Configure timer/counter event enablement More... | |
cy_rslt_t | mtb_hal_timer_process_interrupt (mtb_hal_timer_t *obj) |
Process interrupts related related to a Timer instance. More... | |
HAL Timer events.
cy_rslt_t mtb_hal_timer_setup | ( | mtb_hal_timer_t * | obj, |
const mtb_hal_timer_configurator_t * | config, | ||
mtb_hal_clock_t * | clock | ||
) |
Sets up a HAL instance to use the specified hardware resource.
This hardware resource must have already been configured via the PDL.
[out] | obj | The HAL driver instance object. The caller must allocate the memory for this object, but the HAL will initialize its contents |
[in] | config | The configurator-generated HAL config structure for this peripheral instance |
[in] | clock | The HAL clock object that is connected to this peripheral instance |
cy_rslt_t mtb_hal_timer_start | ( | mtb_hal_timer_t * | obj | ) |
Starts the timer/counter with the pre-set configuration.
This does not reset the counter. The count value will start from the value that was set by the last operation to modify it. See Snippet 1: Measuring time of an operation.
[in] | obj | The timer/counter object |
cy_rslt_t mtb_hal_timer_stop | ( | mtb_hal_timer_t * | obj | ) |
Stops the timer/counter.
Does not reset counter value.
[in] | obj | The timer/counter object |
cy_rslt_t mtb_hal_timer_reset | ( | mtb_hal_timer_t * | obj, |
uint32_t | start_value | ||
) |
Reset the timer/counter value.
[in] | obj | The timer/counter object |
[in] | start_value | Start value for the timer. |
uint32_t mtb_hal_timer_read | ( | const mtb_hal_timer_t * | obj | ) |
Reads the current value from the timer/counter
See Snippet 1: Measuring time of an operation.
[in] | obj | The timer/counter object |
void mtb_hal_timer_register_callback | ( | mtb_hal_timer_t * | obj, |
mtb_hal_timer_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register a timer/counter callback handler
This function will be called when one of the events enabled by mtb_hal_timer_enable_event occurs.
See Snippet 2: Handling an event in a callback function.
[in] | obj | The timer/counter object |
[in] | callback | The callback handler which will be invoked when the event occurs |
[in] | callback_arg | Generic argument that will be provided to the callback when called |
void mtb_hal_timer_enable_event | ( | mtb_hal_timer_t * | obj, |
mtb_hal_timer_event_t | event, | ||
bool | enable | ||
) |
Configure timer/counter event enablement
When an enabled event occurs, the function specified by mtb_hal_timer_register_callback will be called.
See Snippet 2: Handling an event in a callback function.
[in] | obj | The timer/counter object |
[in] | event | The timer/counter event mtb_hal_timer_event_t |
[in] | enable | True to turn on interrupts, False to turn off |
cy_rslt_t mtb_hal_timer_process_interrupt | ( | mtb_hal_timer_t * | obj | ) |
Process interrupts related related to a Timer instance.
obj | HAL object for which the interrupt should be processed |