PSoC 6 Hardware Abstraction Layer (HAL)
TIMER (Timer/Counter)

General Description

High level interface for interacting with the Cypress Timer.

This block can be used either to measure timing between events, or perform some action after a specified period of time.

The timer operation is highly configurable; see cyhal_timer_cfg_t.

Data Structures

struct  cyhal_timer_cfg_t
 Describes the current configuration of a timer/counter. More...
 

Macros

#define CYHAL_TIMER_RSLT_ERR_BAD_ARGUMENT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 0))
 
#define CYHAL_TIMER_RSLT_ERR_CLOCK_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 1))
 
#define CYHAL_TIMER_RSLT_ERR_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 2))
 
#define CYHAL_TIMER_RSLT_ERR_SHARED_CLOCK   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 3))
 
#define CYHAL_TIMER_DEFAULT_FREQ   (1000000u)
 

Typedefs

typedef void(* cyhal_timer_event_callback_t) (void *callback_arg, cyhal_timer_event_t event)
 

Enumerations

enum  cyhal_timer_direction_t { CYHAL_TIMER_DIR_UP, CYHAL_TIMER_DIR_DOWN, CYHAL_TIMER_DIR_UP_DOWN }
 
enum  cyhal_timer_event_t { CYHAL_TIMER_IRQ_NONE = 0, CYHAL_TIMER_IRQ_TERMINAL_COUNT = 1 << 0, CYHAL_TIMER_IRQ_CAPTURE_COMPARE = 1 << 1, CYHAL_TIMER_IRQ_ALL = (1 << 2) - 1 }
 

Functions

cy_rslt_t cyhal_timer_init (cyhal_timer_t *obj, cyhal_gpio_t pin, const cyhal_clock_divider_t *clk)
 
void cyhal_timer_free (cyhal_timer_t *obj)
 
cy_rslt_t cyhal_timer_configure (cyhal_timer_t *obj, const cyhal_timer_cfg_t *cfg)
 
cy_rslt_t cyhal_timer_set_frequency (cyhal_timer_t *obj, uint32_t hz)
 
cy_rslt_t cyhal_timer_start (cyhal_timer_t *obj)
 
cy_rslt_t cyhal_timer_stop (cyhal_timer_t *obj)
 
void cyhal_timer_register_callback (cyhal_timer_t *obj, cyhal_timer_event_callback_t callback, void *callback_arg)
 
void cyhal_timer_enable_event (cyhal_timer_t *obj, cyhal_timer_event_t event, uint8_t intrPriority, bool enable)
 

Data Structure Documentation

◆ cyhal_timer_cfg_t

struct cyhal_timer_cfg_t
Data Fields
bool is_continuous Whether the timer/counter operates continuous (true) or one shot (false)

Whether the timer is set to continously run. If true, the timer will run forever. Otherwise, the timer will run once and stop (one shot).

cyhal_timer_direction_t direction Direction the timer/counter is running.
bool is_compare Is it in compare (true) or capture (false) mode.
uint32_t period Timer/counter period.
uint32_t compare_value Timer/counter comparison value.
uint32_t value Current value of the timer/counter.

Macro Definition Documentation

◆ CYHAL_TIMER_RSLT_ERR_BAD_ARGUMENT

#define CYHAL_TIMER_RSLT_ERR_BAD_ARGUMENT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 0))

Bad argument. eg: null pointer

◆ CYHAL_TIMER_RSLT_ERR_CLOCK_INIT

#define CYHAL_TIMER_RSLT_ERR_CLOCK_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 1))

Failed to initialize Timer clock

◆ CYHAL_TIMER_RSLT_ERR_INIT

#define CYHAL_TIMER_RSLT_ERR_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 2))

Failed to initialize Timer

◆ CYHAL_TIMER_RSLT_ERR_SHARED_CLOCK

#define CYHAL_TIMER_RSLT_ERR_SHARED_CLOCK   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_TIMER, 3))

Cannot change the timer frequency when a shared clock divider is in use

◆ CYHAL_TIMER_DEFAULT_FREQ

#define CYHAL_TIMER_DEFAULT_FREQ   (1000000u)

Default timer frequency, used when an existing clock divider is not provided to init

Typedef Documentation

◆ cyhal_timer_event_callback_t

typedef void(* cyhal_timer_event_callback_t) (void *callback_arg, cyhal_timer_event_t event)

Handler for timer events

Enumeration Type Documentation

◆ cyhal_timer_direction_t

Timer directions

Enumerator
CYHAL_TIMER_DIR_UP 

Counts up.

CYHAL_TIMER_DIR_DOWN 

Counts down.

CYHAL_TIMER_DIR_UP_DOWN 

Counts up and down, terminal count occurs on both overflow and underflow.

◆ cyhal_timer_event_t

Timer/counter interrupt triggers

Function Documentation

◆ cyhal_timer_init()

cy_rslt_t cyhal_timer_init ( cyhal_timer_t *  obj,
cyhal_gpio_t  pin,
const cyhal_clock_divider_t *  clk 
)

Initialize the timer/counter peripheral and configure the pin.

Parameters
[out]objThe timer/counter object to initialize
[in]pinoptional - The timer/counter compare/capture pin to initialize
[in]clkoptional - The shared clock to use, if not provided a new clock will be allocated and the timer frequency will be set to CYHAL_TIMER_DEFAULT_FREQ
Returns
The status of the init request

◆ cyhal_timer_free()

void cyhal_timer_free ( cyhal_timer_t *  obj)

Deinitialize the timer/counter object

Parameters
[in,out]objThe timer/counter object

◆ cyhal_timer_configure()

cy_rslt_t cyhal_timer_configure ( cyhal_timer_t *  obj,
const cyhal_timer_cfg_t *  cfg 
)

Updates the configuration of the timer/counter object

Parameters
[in]objThe timer/counter object
[in]cfgThe configuration of the timer/counter
Returns
The status of the configure request

◆ cyhal_timer_set_frequency()

cy_rslt_t cyhal_timer_set_frequency ( cyhal_timer_t *  obj,
uint32_t  hz 
)

Configures the timer frequency. This is not valid to call if a non-null clock divider was provided to cyhal_timer_init

Parameters
[in]objThe timer/counter object
[in]hzThe frequency rate in Hz
Returns
The status of the set_frequency request

◆ cyhal_timer_start()

cy_rslt_t cyhal_timer_start ( cyhal_timer_t *  obj)

Starts the timer/counter with the pre-set configuration.

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

◆ cyhal_timer_stop()

cy_rslt_t cyhal_timer_stop ( cyhal_timer_t *  obj)

Stops the timer/counter.

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

◆ cyhal_timer_register_callback()

void cyhal_timer_register_callback ( cyhal_timer_t *  obj,
cyhal_timer_event_callback_t  callback,
void *  callback_arg 
)

The timer/counter callback handler registration

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

◆ cyhal_timer_enable_event()

void cyhal_timer_enable_event ( cyhal_timer_t *  obj,
cyhal_timer_event_t  event,
uint8_t  intrPriority,
bool  enable 
)

Configure timer/counter event enablement.

Parameters
[in]objThe timer/counter object
[in]eventThe timer/counter event type
[in]intrPriorityThe priority for NVIC interrupt events
[in]enableTrue to turn on interrupts, False to turn off