Functions¶
esp_err_t timer_get_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t *timer_val)¶
Read the counter value of hardware timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
timer_val: Pointer to accept timer counter value.
esp_err_t timer_get_counter_time_sec(timer_group_t group_num, timer_idx_t timer_num, double *time)¶
Read the counter value of hardware timer, in unit of a given scale.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
time: Pointer, type of double*, to accept timer counter value, in seconds.
esp_err_t timer_set_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t load_val)¶
Set counter value to hardware timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
load_val: Counter value to write to the hardware timer.
esp_err_t timer_start(timer_group_t group_num, timer_idx_t timer_num)¶
Start the counter of hardware timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
esp_err_t timer_pause(timer_group_t group_num, timer_idx_t timer_num)¶
Pause the counter of hardware timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
esp_err_t timer_set_counter_mode(timer_group_t group_num, timer_idx_t timer_num, timer_count_dir_t counter_dir)¶
Set counting mode for hardware timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
counter_dir: Counting direction of timer, count-up or count-down
esp_err_t timer_set_auto_reload(timer_group_t group_num, timer_idx_t timer_num, timer_autoreload_t reload)¶
Enable or disable counter reload function when alarm event occurs.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
reload: Counter reload mode.
esp_err_t timer_set_divider(timer_group_t group_num, timer_idx_t timer_num, uint32_t divider)¶
Set hardware timer source clock divider. Timer groups clock are divider from APB clock.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
divider: Timer clock divider value. The divider’s range is from from 2 to 65536.
esp_err_t timer_set_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_value)¶
Set timer alarm value.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
alarm_value: A 64-bit value to set the alarm value.
esp_err_t timer_get_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t *alarm_value)¶
Get timer alarm value.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
alarm_value: Pointer of A 64-bit value to accept the alarm value.
esp_err_t timer_set_alarm(timer_group_t group_num, timer_idx_t timer_num, timer_alarm_t alarm_en)¶
Enable or disable generation of timer alarm events.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
alarm_en: To enable or disable timer alarm function.
esp_err_t timer_isr_callback_add(timer_group_t group_num, timer_idx_t timer_num, timer_isr_t isr_handler, void *arg, int intr_alloc_flags)¶
Add ISR handle callback for the corresponding timer.
The callback should return a bool value to determine whether need to do YIELD at the end of the ISR.
NoteThis ISR handler will be called from an ISR. This ISR handler do not need to handle interrupt status, and should be kept short. If you want to realize some specific applications or write the whole ISR, you can call timer_isr_register(…) to register ISR.
Parameters
group_num: Timer group number
timer_num: Timer index of timer group
isr_handler: Interrupt handler function, it is a callback function.
arg: Parameter for handler function
intr_alloc_flags: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
If the intr_alloc_flags value ESP_INTR_FLAG_IRAM is set, the handler function must be declared with IRAM_ATTR attribute and can only call functions in IRAM or ROM. It cannot call other timer APIs.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
esp_err_t timer_isr_callback_remove(timer_group_t group_num, timer_idx_t timer_num)¶
Remove ISR handle callback for the corresponding timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number
timer_num: Timer index of timer group
esp_err_t timer_isr_register(timer_group_t group_num, timer_idx_t timer_num, void (*fn)(void *), void *arg, int intr_alloc_flags, timer_isr_handle_t *handle, )¶
Register Timer interrupt handler, the handler is an ISR. The handler will be attached to the same CPU core that this function is running on.
If the intr_alloc_flags value ESP_INTR_FLAG_IRAM is set, the handler function must be declared with IRAM_ATTR attribute and can only call functions in IRAM or ROM. It cannot call other timer APIs. Use direct register access to configure timers from inside the ISR in this case.
NoteIf use this function to reigster ISR, you need to write the whole ISR. In the interrupt handler, you need to call timer_spinlock_take(..) before your handling, and call timer_spinlock_give(…) after your handling.
Parameters
group_num: Timer group number
timer_num: Timer index of timer group
fn: Interrupt handler function.
arg: Parameter for handler function
intr_alloc_flags: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
handle: Pointer to return handle. If non-NULL, a handle for the interrupt will be returned here.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer_config_t *config)¶
Initializes and configure the timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
config: Pointer to timer initialization parameters.
esp_err_t timer_deinit(timer_group_t group_num, timer_idx_t timer_num)¶
Deinitializes the timer.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
esp_err_t timer_get_config(timer_group_t group_num, timer_idx_t timer_num, timer_config_t *config)¶
Get timer configure value.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
config: Pointer of struct to accept timer parameters.
esp_err_t timer_group_intr_enable(timer_group_t group_num, timer_intr_t intr_mask)¶
Enable timer group interrupt, by enable mask.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
intr_mask: Timer interrupt enable mask.
TIMER_INTR_T0: t0 interrupt
TIMER_INTR_T1: t1 interrupt
TIMER_INTR_WDT: watchdog interrupt
esp_err_t timer_group_intr_disable(timer_group_t group_num, timer_intr_t intr_mask)¶
Disable timer group interrupt, by disable mask.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
intr_mask: Timer interrupt disable mask.
TIMER_INTR_T0: t0 interrupt
TIMER_INTR_T1: t1 interrupt
TIMER_INTR_WDT: watchdog interrupt
esp_err_t timer_enable_intr(timer_group_t group_num, timer_idx_t timer_num)¶
Enable timer interrupt.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
esp_err_t timer_disable_intr(timer_group_t group_num, timer_idx_t timer_num)¶
Disable timer interrupt.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
void timer_group_intr_clr_in_isr(timer_group_t group_num, timer_idx_t timer_num)¶
Clear timer interrupt status, just used in ISR.
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
void timer_group_clr_intr_status_in_isr(timer_group_t group_num, timer_idx_t timer_num)¶
Clear timer interrupt status, just used in ISR.
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
void timer_group_enable_alarm_in_isr(timer_group_t group_num, timer_idx_t timer_num)¶
Enable alarm interrupt, just used in ISR.
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
uint64_t timer_group_get_counter_value_in_isr(timer_group_t group_num, timer_idx_t timer_num)¶
Get the current counter value, just used in ISR.
Return
Counter value
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
void timer_group_set_alarm_value_in_isr(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_val)¶
Set the alarm threshold for the timer, just used in ISR.
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
alarm_val: Alarm threshold.
void timer_group_set_counter_enable_in_isr(timer_group_t group_num, timer_idx_t timer_num, timer_start_t counter_en)¶
Enable/disable a counter, just used in ISR.
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index.
counter_en: Enable/disable.
timer_intr_t timer_group_intr_get_in_isr(timer_group_t group_num)¶
Get the masked interrupt status, just used in ISR.
Return
Interrupt status
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
uint32_t timer_group_get_intr_status_in_isr(timer_group_t group_num)¶
Get interrupt status, just used in ISR.
Return
Interrupt status
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
void timer_group_clr_intr_sta_in_isr(timer_group_t group_num, timer_intr_t intr_mask)¶
Clear the masked interrupt status, just used in ISR.
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
intr_mask: Masked interrupt.
bool timer_group_get_auto_reload_in_isr(timer_group_t group_num, timer_idx_t timer_num)¶
Get auto reload enable status, just used in ISR.
Return
True Auto reload enabled
False Auto reload disabled
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
timer_num: Timer index
esp_err_t timer_spinlock_take(timer_group_t group_num)¶
Take timer spinlock to enter critical protect.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1
esp_err_t timer_spinlock_give(timer_group_t group_num)¶
Give timer spinlock to exit critical protect.
Return
ESP_OK Success
ESP_ERR_INVALID_ARG Parameter error
Parameters
group_num: Timer group number, 0 for TIMERG0 or 1 for TIMERG1