| 
									
										
										
										
											2021-05-24 02:09:38 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-12-30 15:56:08 +08:00
										 |  |  |  * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD | 
					
						
							| 
									
										
										
										
											2021-05-24 02:09:38 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include "esp_log.h"
 | 
					
						
							|  |  |  | #include "esp_err.h"
 | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  | #include "esp_check.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-26 16:30:43 +08:00
										 |  |  | #include "esp_intr_alloc.h"
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | #include "freertos/FreeRTOS.h"
 | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  | #include "driver/timer_types_legacy.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | #include "hal/timer_hal.h"
 | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  | #include "hal/timer_ll.h"
 | 
					
						
							|  |  |  | #include "hal/check.h"
 | 
					
						
							| 
									
										
										
										
											2020-10-14 11:46:30 +08:00
										 |  |  | #include "soc/timer_periph.h"
 | 
					
						
							| 
									
										
										
										
											2023-04-23 15:49:59 +08:00
										 |  |  | #include "esp_clk_tree.h"
 | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  | #include "soc/timer_group_reg.h"
 | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  | #include "esp_private/periph_ctrl.h"
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | static const char *TIMER_TAG = "timer_group"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | #define TIMER_GROUP_NUM_ERROR   "TIMER GROUP NUM ERROR"
 | 
					
						
							|  |  |  | #define TIMER_NUM_ERROR         "HW TIMER NUM ERROR"
 | 
					
						
							|  |  |  | #define TIMER_PARAM_ADDR_ERROR  "HW TIMER PARAM ADDR ERROR"
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | #define TIMER_NEVER_INIT_ERROR  "HW TIMER NEVER INIT ERROR"
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | #define TIMER_COUNT_DIR_ERROR   "HW TIMER COUNTER DIR ERROR"
 | 
					
						
							|  |  |  | #define TIMER_AUTORELOAD_ERROR  "HW TIMER AUTORELOAD ERROR"
 | 
					
						
							|  |  |  | #define TIMER_SCALE_ERROR       "HW TIMER SCALE ERROR"
 | 
					
						
							|  |  |  | #define TIMER_ALARM_ERROR       "HW TIMER ALARM ERROR"
 | 
					
						
							| 
									
										
										
										
											2017-09-25 06:20:12 +02:00
										 |  |  | #define DIVIDER_RANGE_ERROR     "HW TIMER divider outside of [2, 65536] range error"
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 16:08:28 +05:30
										 |  |  | #define TIMER_ENTER_CRITICAL(mux)      portENTER_CRITICAL_SAFE(mux);
 | 
					
						
							|  |  |  | #define TIMER_EXIT_CRITICAL(mux)       portEXIT_CRITICAL_SAFE(mux);
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     timer_isr_t fn;  /*!< isr function */ | 
					
						
							|  |  |  |     void *args;      /*!< isr function args */ | 
					
						
							|  |  |  |     timer_isr_handle_t timer_isr_handle;  /*!< interrupt handle */ | 
					
						
							|  |  |  |     timer_group_t isr_timer_group;        /*!< timer group of interrupt triggered */ | 
					
						
							|  |  |  | } timer_isr_func_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     timer_hal_context_t hal; | 
					
						
							|  |  |  |     timer_isr_func_t timer_isr_fun; | 
					
						
							| 
									
										
										
										
											2022-04-13 13:12:30 +08:00
										 |  |  |     timer_src_clk_t clk_src; | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     gptimer_count_direction_t direction; | 
					
						
							|  |  |  |     uint32_t divider; | 
					
						
							| 
									
										
										
										
											2021-12-17 09:57:27 +08:00
										 |  |  |     uint64_t alarm_value; | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     bool alarm_en; | 
					
						
							|  |  |  |     bool auto_reload_en; | 
					
						
							|  |  |  |     bool counter_en; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | } timer_obj_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static timer_obj_t *p_timer_obj[TIMER_GROUP_MAX][TIMER_MAX] = {0}; | 
					
						
							| 
									
										
										
										
											2021-11-06 17:26:37 +08:00
										 |  |  | static portMUX_TYPE timer_spinlock[TIMER_GROUP_MAX] = { [0 ... TIMER_GROUP_MAX - 1] = portMUX_INITIALIZER_UNLOCKED, }; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_get_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t *timer_val) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_val != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_PARAM_ADDR_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2022-07-13 13:17:32 +08:00
										 |  |  |     *timer_val = timer_hal_capture_and_get_counter_value(&p_timer_obj[group_num][timer_num]->hal); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | esp_err_t timer_get_counter_time_sec(timer_group_t group_num, timer_idx_t timer_num, double *time) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(time != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_PARAM_ADDR_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2022-07-13 13:17:32 +08:00
										 |  |  |     uint64_t timer_val = timer_hal_capture_and_get_counter_value(&p_timer_obj[group_num][timer_num]->hal); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     uint32_t div = p_timer_obj[group_num][timer_num]->divider; | 
					
						
							| 
									
										
										
										
											2022-12-30 15:56:08 +08:00
										 |  |  |     // get clock source frequency
 | 
					
						
							|  |  |  |     uint32_t counter_src_hz = 0; | 
					
						
							| 
									
										
										
										
											2023-04-23 15:49:59 +08:00
										 |  |  |     ESP_RETURN_ON_ERROR(esp_clk_tree_src_get_freq_hz((soc_module_clk_t)p_timer_obj[group_num][timer_num]->clk_src, | 
					
						
							|  |  |  |                         ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &counter_src_hz), | 
					
						
							| 
									
										
										
										
											2022-12-30 15:56:08 +08:00
										 |  |  |                         TIMER_TAG, "get clock source frequency failed"); | 
					
						
							|  |  |  |     *time = (double)timer_val * div / counter_src_hz; | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     return ESP_OK; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_set_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t load_val) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_hal_set_counter_value(&(p_timer_obj[group_num][timer_num]->hal), load_val); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_start(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_counter(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, true); | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->counter_en = true; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_pause(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_counter(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, false); | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->counter_en = false; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_set_counter_mode(timer_group_t group_num, timer_idx_t timer_num, timer_count_dir_t counter_dir) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(counter_dir < TIMER_COUNT_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_COUNT_DIR_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_set_count_direction(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, counter_dir); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_set_auto_reload(timer_group_t group_num, timer_idx_t timer_num, timer_autoreload_t reload) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(reload < TIMER_AUTORELOAD_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_AUTORELOAD_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_auto_reload(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, reload); | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->auto_reload_en = reload; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-25 06:20:12 +02:00
										 |  |  | esp_err_t timer_set_divider(timer_group_t group_num, timer_idx_t timer_num, uint32_t divider) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(divider > 1 && divider < 65537, ESP_ERR_INVALID_ARG, TIMER_TAG,  DIVIDER_RANGE_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_set_clock_prescale(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, divider); | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->divider = divider; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_set_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_set_alarm_value(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, alarm_value); | 
					
						
							| 
									
										
										
										
											2021-12-17 09:57:27 +08:00
										 |  |  |     p_timer_obj[group_num][timer_num]->alarm_value = alarm_value; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | esp_err_t timer_get_alarm_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t *alarm_value) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(alarm_value != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_PARAM_ADDR_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-12-17 09:57:27 +08:00
										 |  |  |     *alarm_value = p_timer_obj[group_num][timer_num]->alarm_value; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_set_alarm(timer_group_t group_num, timer_idx_t timer_num, timer_alarm_t alarm_en) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(alarm_en < TIMER_ALARM_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_ALARM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_alarm(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, alarm_en); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | static void IRAM_ATTR timer_isr_default(void *arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-10 22:11:30 +08:00
										 |  |  |     bool is_awoken = false; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_obj_t *timer_obj = (timer_obj_t *)arg; | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     if (timer_obj == NULL || timer_obj->timer_isr_fun.fn == NULL) { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     uint32_t timer_id = timer_obj->hal.timer_id; | 
					
						
							|  |  |  |     timer_hal_context_t *hal = &timer_obj->hal; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[timer_obj->timer_isr_fun.isr_timer_group]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     uint32_t intr_status = timer_ll_get_intr_status(hal->dev); | 
					
						
							| 
									
										
										
										
											2021-12-17 10:02:33 +08:00
										 |  |  |     uint64_t old_alarm_value = timer_obj->alarm_value; | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     if (intr_status & TIMER_LL_EVENT_ALARM(timer_id)) { | 
					
						
							| 
									
										
										
										
											2021-12-17 10:02:33 +08:00
										 |  |  |         // Clear interrupt status
 | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |         timer_ll_clear_intr_status(hal->dev, TIMER_LL_EVENT_ALARM(timer_id)); | 
					
						
							| 
									
										
										
										
											2021-12-17 10:02:33 +08:00
										 |  |  |         // call user registered callback
 | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |         is_awoken = timer_obj->timer_isr_fun.fn(timer_obj->timer_isr_fun.args); | 
					
						
							| 
									
										
										
										
											2021-12-17 10:02:33 +08:00
										 |  |  |         // reenable alarm if required
 | 
					
						
							|  |  |  |         uint64_t new_alarm_value = timer_obj->alarm_value; | 
					
						
							|  |  |  |         bool reenable_alarm = (new_alarm_value != old_alarm_value) || timer_obj->auto_reload_en; | 
					
						
							|  |  |  |         timer_ll_enable_alarm(hal->dev, timer_id, reenable_alarm); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[timer_obj->timer_isr_fun.isr_timer_group]); | 
					
						
							| 
									
										
										
										
											2020-06-10 22:11:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (is_awoken) { | 
					
						
							|  |  |  |         portYIELD_FROM_ISR(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  | esp_err_t timer_enable_intr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     timer_ll_enable_intr(p_timer_obj[group_num][timer_num]->hal.dev, TIMER_LL_EVENT_ALARM(timer_num), true); | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_disable_intr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     timer_ll_enable_intr(p_timer_obj[group_num][timer_num]->hal.dev, TIMER_LL_EVENT_ALARM(timer_num), false); | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(fn != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_PARAM_ADDR_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							|  |  |  |     timer_hal_context_t *hal = &p_timer_obj[group_num][timer_num]->hal; | 
					
						
							|  |  |  |     return esp_intr_alloc_intrstatus(timer_group_periph_signals.groups[group_num].timer_irq_id[timer_num], | 
					
						
							|  |  |  |                                      intr_alloc_flags, | 
					
						
							|  |  |  |                                      (uint32_t)timer_ll_get_intr_status_reg(hal->dev), | 
					
						
							|  |  |  |                                      TIMER_LL_EVENT_ALARM(timer_num), fn, arg, handle); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | esp_err_t timer_isr_callback_add(timer_group_t group_num, timer_idx_t timer_num, timer_isr_t isr_handler, void *args, int intr_alloc_flags) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2022-08-29 13:28:21 +08:00
										 |  |  |     esp_err_t ret = ESP_OK; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     timer_disable_intr(group_num, timer_num); | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->timer_isr_fun.fn = isr_handler; | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->timer_isr_fun.args = args; | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->timer_isr_fun.isr_timer_group = group_num; | 
					
						
							| 
									
										
										
										
											2022-08-29 13:28:21 +08:00
										 |  |  |     ret = timer_isr_register(group_num, timer_num, timer_isr_default, (void *)p_timer_obj[group_num][timer_num], | 
					
						
							|  |  |  |                              intr_alloc_flags, &(p_timer_obj[group_num][timer_num]->timer_isr_fun.timer_isr_handle)); | 
					
						
							|  |  |  |     ESP_RETURN_ON_ERROR(ret, TIMER_TAG, "register interrupt service failed"); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_enable_intr(group_num, timer_num); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-29 13:28:21 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_isr_callback_remove(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     timer_disable_intr(group_num, timer_num); | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->timer_isr_fun.fn = NULL; | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->timer_isr_fun.args = NULL; | 
					
						
							|  |  |  |     esp_intr_free(p_timer_obj[group_num][timer_num]->timer_isr_fun.timer_isr_handle); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-13 18:33:33 +01:00
										 |  |  | esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer_config_t *config) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(config != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_PARAM_ADDR_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(config->divider > 1 && config->divider < 65537, ESP_ERR_INVALID_ARG, TIMER_TAG,  DIVIDER_RANGE_ERROR); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(config->intr_type < TIMER_INTR_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG, "only support Level Interrupt"); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     if (p_timer_obj[group_num][timer_num] == NULL) { | 
					
						
							|  |  |  |         p_timer_obj[group_num][timer_num] = (timer_obj_t *) heap_caps_calloc(1, sizeof(timer_obj_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |         ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num], ESP_ERR_NO_MEM, TIMER_TAG, "no mem for timer object"); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_hal_context_t *hal = &p_timer_obj[group_num][timer_num]->hal; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     periph_module_enable(timer_group_periph_signals.groups[group_num].module); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_hal_init(hal, group_num, timer_num); | 
					
						
							|  |  |  |     timer_hal_set_counter_value(hal, 0); | 
					
						
							| 
									
										
										
										
											2022-12-30 15:56:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     timer_src_clk_t clk_src = TIMER_SRC_CLK_DEFAULT; | 
					
						
							|  |  |  |     if (config->clk_src) { | 
					
						
							|  |  |  |         clk_src = config->clk_src; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-13 13:12:30 +08:00
										 |  |  |     // although `clk_src` is of `timer_src_clk_t` type, but it's binary compatible with `gptimer_clock_source_t`,
 | 
					
						
							|  |  |  |     // as the underlying enum entries come from the same `soc_module_clk_t`
 | 
					
						
							| 
									
										
										
										
											2022-12-30 15:56:08 +08:00
										 |  |  |     timer_ll_set_clock_source(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, (gptimer_clock_source_t)clk_src); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_set_clock_prescale(hal->dev, timer_num, config->divider); | 
					
						
							|  |  |  |     timer_ll_set_count_direction(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, config->counter_dir); | 
					
						
							|  |  |  |     timer_ll_enable_intr(hal->dev, TIMER_LL_EVENT_ALARM(timer_num), false); | 
					
						
							|  |  |  |     timer_ll_clear_intr_status(hal->dev, TIMER_LL_EVENT_ALARM(timer_num)); | 
					
						
							|  |  |  |     timer_ll_enable_alarm(hal->dev, timer_num, config->alarm_en); | 
					
						
							|  |  |  |     timer_ll_enable_auto_reload(hal->dev, timer_num, config->auto_reload); | 
					
						
							|  |  |  |     timer_ll_enable_counter(hal->dev, timer_num, config->counter_en); | 
					
						
							| 
									
										
										
										
											2022-12-30 15:56:08 +08:00
										 |  |  |     p_timer_obj[group_num][timer_num]->clk_src = clk_src; | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     p_timer_obj[group_num][timer_num]->alarm_en = config->alarm_en; | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->auto_reload_en = config->auto_reload; | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->direction = config->counter_dir; | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->counter_en = config->counter_en; | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->divider = config->divider; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_deinit(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_hal_context_t *hal = &p_timer_obj[group_num][timer_num]->hal; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_intr(hal->dev, TIMER_LL_EVENT_ALARM(timer_num), false); | 
					
						
							|  |  |  |     timer_ll_clear_intr_status(hal->dev, TIMER_LL_EVENT_ALARM(timer_num)); | 
					
						
							| 
									
										
										
										
											2022-07-13 13:17:32 +08:00
										 |  |  |     timer_hal_deinit(hal); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     free(p_timer_obj[group_num][timer_num]); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     p_timer_obj[group_num][timer_num] = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_get_config(timer_group_t group_num, timer_idx_t timer_num, timer_config_t *config) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(timer_num < TIMER_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(config != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_PARAM_ADDR_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num][timer_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     config->alarm_en = p_timer_obj[group_num][timer_num]->alarm_en; | 
					
						
							|  |  |  |     config->auto_reload = p_timer_obj[group_num][timer_num]->auto_reload_en; | 
					
						
							|  |  |  |     config->counter_dir = p_timer_obj[group_num][timer_num]->direction; | 
					
						
							|  |  |  |     config->counter_en = p_timer_obj[group_num][timer_num]->counter_en; | 
					
						
							|  |  |  |     config->divider = p_timer_obj[group_num][timer_num]->divider; | 
					
						
							|  |  |  |     config->intr_type = TIMER_INTR_LEVEL; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | esp_err_t timer_group_intr_enable(timer_group_t group_num, timer_intr_t en_mask) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_intr(p_timer_obj[group_num][0]->hal.dev, en_mask, true); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | esp_err_t timer_group_intr_disable(timer_group_t group_num, timer_intr_t disable_mask) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-27 15:52:19 +08:00
										 |  |  |     ESP_RETURN_ON_FALSE(group_num < TIMER_GROUP_MAX, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_GROUP_NUM_ERROR); | 
					
						
							|  |  |  |     ESP_RETURN_ON_FALSE(p_timer_obj[group_num] != NULL, ESP_ERR_INVALID_ARG, TIMER_TAG,  TIMER_NEVER_INIT_ERROR); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_intr(p_timer_obj[group_num][0]->hal.dev, disable_mask, false); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | uint32_t IRAM_ATTR timer_group_get_intr_status_in_isr(timer_group_t group_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t intr_status = 0; | 
					
						
							|  |  |  |     if (p_timer_obj[group_num][TIMER_0] != NULL) { | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |         intr_status = timer_ll_get_intr_status(TIMER_LL_GET_HW(group_num)) & TIMER_LL_EVENT_ALARM(0); | 
					
						
							| 
									
										
										
										
											2020-10-14 11:46:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | #if SOC_TIMER_GROUP_TIMERS_PER_GROUP > 1
 | 
					
						
							|  |  |  |     else if (p_timer_obj[group_num][TIMER_1] != NULL) { | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |         intr_status = timer_ll_get_intr_status(TIMER_LL_GET_HW(group_num)) & TIMER_LL_EVENT_ALARM(1); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-14 11:46:30 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     return intr_status; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  | void IRAM_ATTR timer_group_clr_intr_status_in_isr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  |     timer_ll_clear_intr_status(p_timer_obj[group_num][timer_num]->hal.dev, TIMER_LL_EVENT_ALARM(timer_num)); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | void IRAM_ATTR timer_group_enable_alarm_in_isr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_alarm(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, true); | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint64_t IRAM_ATTR timer_group_get_counter_value_in_isr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-13 13:17:32 +08:00
										 |  |  |     timer_ll_trigger_soft_capture(p_timer_obj[group_num][timer_num]->hal.dev, timer_num); | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     uint64_t val = timer_ll_get_counter_value(p_timer_obj[group_num][timer_num]->hal.dev, timer_num); | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  |     return val; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | void IRAM_ATTR timer_group_set_alarm_value_in_isr(timer_group_t group_num, timer_idx_t timer_num, uint64_t alarm_val) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_set_alarm_value(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, alarm_val); | 
					
						
							| 
									
										
										
										
											2021-12-17 09:57:27 +08:00
										 |  |  |     p_timer_obj[group_num][timer_num]->alarm_value = alarm_val; | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IRAM_ATTR timer_group_set_counter_enable_in_isr(timer_group_t group_num, timer_idx_t timer_num, timer_start_t counter_en) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     timer_ll_enable_counter(p_timer_obj[group_num][timer_num]->hal.dev, timer_num, counter_en); | 
					
						
							|  |  |  |     p_timer_obj[group_num][timer_num]->counter_en = counter_en; | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IRAM_ATTR timer_group_get_auto_reload_in_isr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-27 12:46:51 +08:00
										 |  |  |     return p_timer_obj[group_num][timer_num]->auto_reload_en; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief This function will be called during start up, to check that this legacy timer group driver is not running along with the gptimer driver | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | __attribute__((constructor)) | 
					
						
							|  |  |  | static void check_legacy_timer_driver_conflict(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-08 15:11:59 +08:00
										 |  |  |     // This function was declared as weak here. gptimer driver has one implementation.
 | 
					
						
							|  |  |  |     // So if gptimer driver is not linked in, then `gptimer_new_timer()` should be NULL at runtime.
 | 
					
						
							|  |  |  |     extern __attribute__((weak)) esp_err_t gptimer_new_timer(const void *config, void **ret_timer); | 
					
						
							| 
									
										
										
										
											2022-04-13 13:12:30 +08:00
										 |  |  |     if ((void *)gptimer_new_timer != NULL) { | 
					
						
							| 
									
										
										
										
											2022-04-08 15:11:59 +08:00
										 |  |  |         ESP_EARLY_LOGE(TIMER_TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver"); | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  |         abort(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-08 15:11:59 +08:00
										 |  |  |     ESP_EARLY_LOGW(TIMER_TAG, "legacy driver is deprecated, please migrate to `driver/gptimer.h`"); | 
					
						
							| 
									
										
										
										
											2022-01-02 16:14:17 +08:00
										 |  |  | } |