Hardware Abstraction Layer (HAL)
All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
Timer (Timer/Counter)

General Description

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 -

Features

Quick Start

See Snippet 1: Measuring time of an operation.

Code Snippets

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.

cy_rslt_t rslt;
uint32_t read_val;
/* Use the device-configurator output for the alias "timer" */
rslt = Cy_TCPWM_Counter_Init(timer_HW, timer_NUM, &timer_config);
if (rslt == CY_RSLT_SUCCESS)
{
rslt = mtb_hal_timer_setup(&my_timer, &timer_hal_config, NULL);
if (rslt == CY_RSLT_SUCCESS)
{
Cy_TCPWM_Counter_Enable(timer_HW, timer_NUM);
}
}
if (rslt == CY_RSLT_SUCCESS)
{
// Start the timer with the configured settings
rslt = mtb_hal_timer_start(&my_timer);
}
// Delay Function simulates the time between two events
// Read the current timer value, which should be close to the amount of delay in ms * 10 (5000)
read_val = mtb_hal_timer_read(&my_timer);
cy_rslt_t mtb_hal_system_delay_ms(uint32_t milliseconds)
Requests that the current operation delays for at least the specified length of time.
Definition: mtb_hal_system.c:63
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.
Definition: mtb_hal_timer.c:152
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.
Definition: mtb_hal_timer.c:51
cy_rslt_t mtb_hal_timer_start(mtb_hal_timer_t *obj)
Starts the timer/counter with the pre-set configuration.
Definition: mtb_hal_timer.c:89
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:457
#define CY_RSLT_SUCCESS
cy_rslt_t return value indicating success
Definition: cy_result.h:484

Snippet 2: Handling an event in a callback function

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.

/*
* Timer event handler - called from mtb_hal_timer_process_interrupt()
*/
void my_timer_irq_handler(void* arg, mtb_hal_timer_event_t event)
{
(void)arg;
(void)event;
}
cy_rslt_t snippet_mtb_hal_timer_event_interrupt(void)
{
cy_rslt_t rslt;
/* Use the device-configurator output for the alias "timer" */
rslt = Cy_TCPWM_Counter_Init(timer_HW, timer_NUM, &timer_config);
if (rslt == CY_RSLT_SUCCESS)
{
rslt = mtb_hal_timer_setup(&my_timer, &timer_hal_config, NULL);
if (rslt == CY_RSLT_SUCCESS)
{
Cy_TCPWM_Counter_Enable(timer_HW, timer_NUM);
}
}
// Enable System IRQ
if (rslt == CY_RSLT_SUCCESS)
{
uint32_t savedIntrStatus = mtb_hal_system_critical_section_enter();
// Enable IRQ
cy_stc_sysint_t intr_cfg;
#if defined (CY_IP_M7CPUSS)
intr_cfg.intrSrc = _mtb_hal_get_intSrc(timer_IRQ);
#else
intr_cfg.intrSrc = (IRQn_Type)_mtb_hal_get_intSrc(timer_IRQ);
#endif
intr_cfg.intrPriority = 7u;
// Register system callback
Cy_SysInt_Init(&intr_cfg, my_test_sys_irq_handler);
NVIC_EnableIRQ((IRQn_Type)_mtb_hal_get_intVec(timer_IRQ));
}
// Register the Callback and enable the events
if (CY_RSLT_SUCCESS == rslt)
{
/* set up pointer to our timer before enabling interrupts */
my_timer_ptr = &my_timer;
// Assign the ISR to execute on timer interrupt
mtb_hal_timer_register_callback(&my_timer, my_timer_irq_handler, NULL);
// Set the event on which timer interrupt occurs and enable it
}
// Start the timer
if (rslt == CY_RSLT_SUCCESS)
{
rslt = mtb_hal_timer_start(&my_timer);
}
// Delay to allow interrupt to happen
return rslt;
}
void mtb_hal_system_critical_section_exit(uint32_t old_state)
Exit a critical section.
uint32_t mtb_hal_system_critical_section_enter(void)
Enter a critical section.
mtb_hal_timer_event_t
HAL Timer events.
Definition: mtb_hal_timer.h:113
void mtb_hal_timer_enable_event(mtb_hal_timer_t *obj, mtb_hal_timer_event_t event, bool enable)
Configure timer/counter event enablement
Definition: mtb_hal_timer.c:186
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
Definition: mtb_hal_timer.c:169
@ MTB_HAL_TIMER_EVENT_TERMINAL_COUNT
Timer Event Terminal Count = (timer reached max count),.
Definition: mtb_hal_timer.h:117

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...
 

Enumeration Type Documentation

◆ mtb_hal_timer_event_t

HAL Timer events.

Enumerator
MTB_HAL_TIMER_EVENT_NONE 

No event.

MTB_HAL_TIMER_EVENT_TERMINAL_COUNT 

Timer Event Terminal Count = (timer reached max count),.

MTB_HAL_TIMER_EVENT_COMPARE_CC0 

Timer Event Compare Count 0 = (timer reached compare 0 count),.

MTB_HAL_TIMER_EVENT_COMPARE_CC0_OR_TERMINAL_COUNT 

Timer Event Compare Count 0 -or- Terminal Count.

MTB_HAL_TIMER_EVENT_COMPARE_CC1 

Timer Event Compare Count 1 = (timer reached compare 1 count),.

MTB_HAL_TIMER_EVENT_ALL 

All Timer Events Combined.

Function Documentation

◆ mtb_hal_timer_setup()

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.

Parameters
[out]objThe HAL driver instance object. The caller must allocate the memory for this object, but the HAL will initialize its contents
[in]configThe configurator-generated HAL config structure for this peripheral instance
[in]clockThe HAL clock object that is connected to this peripheral instance
Returns
the status of the HAL setup

◆ mtb_hal_timer_start()

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.

Parameters
[in]objThe timer/counter object
Returns
The status of the start request

◆ mtb_hal_timer_stop()

cy_rslt_t mtb_hal_timer_stop ( mtb_hal_timer_t obj)

Stops the timer/counter.

Does not reset counter value.

Parameters
[in]objThe timer/counter object
Returns
The status of the stop request

◆ mtb_hal_timer_reset()

cy_rslt_t mtb_hal_timer_reset ( mtb_hal_timer_t obj,
uint32_t  start_value 
)

Reset the timer/counter value.

Parameters
[in]objThe timer/counter object
[in]start_valueStart value for the timer.
Returns
The status of the reset request

◆ mtb_hal_timer_read()

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.

Parameters
[in]objThe timer/counter object
Returns
The current value of the timer/counter

◆ mtb_hal_timer_register_callback()

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.

Parameters
[in]objThe timer/counter object
[in]callbackThe callback handler which will be invoked when the event occurs
[in]callback_argGeneric argument that will be provided to the callback when called

◆ mtb_hal_timer_enable_event()

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.

Parameters
[in]objThe timer/counter object
[in]eventThe timer/counter event mtb_hal_timer_event_t
[in]enableTrue to turn on interrupts, False to turn off

◆ mtb_hal_timer_process_interrupt()

cy_rslt_t mtb_hal_timer_process_interrupt ( mtb_hal_timer_t obj)

Process interrupts related related to a Timer instance.

Parameters
objHAL object for which the interrupt should be processed
Returns
CY_RSLT_SUCCESS if the interrupt was processed successfully; otherwise an error