| 
									
										
										
										
											2019-06-06 14:20:39 +08:00
										 |  |  | // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							|  |  |  | #include "freertos/xtensa_api.h"
 | 
					
						
							|  |  |  | #include "driver/timer.h"
 | 
					
						
							|  |  |  | #include "driver/periph_ctrl.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | #include "hal/timer_hal.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-06 14:20:39 +08:00
										 |  |  | #include "soc/rtc.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_CHECK(a, str, ret_val) \
 | 
					
						
							|  |  |  |     if (!(a)) { \ | 
					
						
							|  |  |  |         ESP_LOGE(TIMER_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \ | 
					
						
							|  |  |  |         return (ret_val); \ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #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; | 
					
						
							|  |  |  | } timer_obj_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static timer_obj_t *p_timer_obj[TIMER_GROUP_MAX][TIMER_MAX] = {0}; | 
					
						
							|  |  |  | static portMUX_TYPE timer_spinlock[TIMER_GROUP_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_val != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     timer_hal_get_counter_value(&(p_timer_obj[group_num][timer_num]->hal), timer_val); | 
					
						
							|  |  |  |     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
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(time != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     uint64_t timer_val; | 
					
						
							|  |  |  |     esp_err_t err = timer_get_counter_value(group_num, timer_num, &timer_val); | 
					
						
							|  |  |  |     if (err == ESP_OK) { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |         uint16_t div; | 
					
						
							|  |  |  |         timer_hal_get_divider(&(p_timer_obj[group_num][timer_num]->hal), &div); | 
					
						
							| 
									
										
										
										
											2019-11-25 20:10:27 +08:00
										 |  |  |         *time = (double)timer_val * div / rtc_clk_apb_freq_get(); | 
					
						
							|  |  |  | #ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |         if (timer_hal_get_use_xtal(&(p_timer_obj[group_num][timer_num]->hal))) { | 
					
						
							| 
									
										
										
										
											2019-06-06 14:20:39 +08:00
										 |  |  |             *time = (double)timer_val * div / ((int)rtc_clk_xtal_freq_get() * 1000000); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_set_counter_value(timer_group_t group_num, timer_idx_t timer_num, uint64_t load_val) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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_enable(&(p_timer_obj[group_num][timer_num]->hal), TIMER_START); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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_enable(&(p_timer_obj[group_num][timer_num]->hal), TIMER_PAUSE); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(counter_dir < TIMER_COUNT_MAX, TIMER_COUNT_DIR_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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_increase(&(p_timer_obj[group_num][timer_num]->hal), 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(reload < TIMER_AUTORELOAD_MAX, TIMER_AUTORELOAD_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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_auto_reload(&(p_timer_obj[group_num][timer_num]->hal), 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
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2017-09-25 06:20:12 +02:00
										 |  |  |     TIMER_CHECK(divider > 1 && divider < 65537, DIVIDER_RANGE_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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_divider(&(p_timer_obj[group_num][timer_num]->hal), (uint16_t) 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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_alarm_value(&(p_timer_obj[group_num][timer_num]->hal), 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
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(alarm_value != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     timer_hal_get_alarm_value(&(p_timer_obj[group_num][timer_num]->hal), alarm_value); | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(alarm_en < TIMER_ALARM_MAX, TIMER_ALARM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											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_alarm_enable(&(p_timer_obj[group_num][timer_num]->hal), 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     timer_obj_t *timer_obj = (timer_obj_t *)arg; | 
					
						
							|  |  |  |     if (timer_obj == NULL) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (timer_obj->timer_isr_fun.fn == NULL) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[timer_obj->timer_isr_fun.isr_timer_group]); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         uint32_t intr_status = 0; | 
					
						
							|  |  |  |         timer_hal_get_intr_status(&(timer_obj->hal), &intr_status); | 
					
						
							|  |  |  |         if (intr_status & BIT(timer_obj->hal.idx)) { | 
					
						
							|  |  |  |             timer_obj->timer_isr_fun.fn(timer_obj->timer_isr_fun.args); | 
					
						
							|  |  |  |             //Clear intrrupt status
 | 
					
						
							|  |  |  |             timer_hal_clear_intr_status(&(timer_obj->hal)); | 
					
						
							|  |  |  |             //After the alarm has been triggered, we need enable it again, so it is triggered the next time.
 | 
					
						
							|  |  |  |             timer_hal_set_alarm_enable(&(timer_obj->hal), TIMER_ALARM_EN); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[timer_obj->timer_isr_fun.isr_timer_group]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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; | 
					
						
							|  |  |  |     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)); | 
					
						
							|  |  |  |     timer_enable_intr(group_num, timer_num); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_isr_callback_remove(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | esp_err_t timer_isr_register(timer_group_t group_num, timer_idx_t timer_num, | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |                              void (*fn)(void *), void *arg, int intr_alloc_flags, timer_isr_handle_t *handle) | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(fn != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int intr_source = 0; | 
					
						
							|  |  |  |     uint32_t status_reg = 0; | 
					
						
							|  |  |  |     int mask = 0; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     switch (group_num) { | 
					
						
							|  |  |  |     case TIMER_GROUP_0: | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         if ((intr_alloc_flags & ESP_INTR_FLAG_EDGE) == 0) { | 
					
						
							|  |  |  |             intr_source = ETS_TG0_T0_LEVEL_INTR_SOURCE + timer_num; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             intr_source = ETS_TG0_T0_EDGE_INTR_SOURCE + timer_num; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         timer_hal_get_intr_status_reg(&(p_timer_obj[TIMER_GROUP_0][timer_num]->hal), &status_reg); | 
					
						
							|  |  |  |         mask = 1 << timer_num; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case TIMER_GROUP_1: | 
					
						
							|  |  |  |         if ((intr_alloc_flags & ESP_INTR_FLAG_EDGE) == 0) { | 
					
						
							|  |  |  |             intr_source = ETS_TG1_T0_LEVEL_INTR_SOURCE + timer_num; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             intr_source = ETS_TG1_T0_EDGE_INTR_SOURCE + timer_num; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         timer_hal_get_intr_status_reg(&(p_timer_obj[TIMER_GROUP_1][timer_num]->hal), &status_reg); | 
					
						
							|  |  |  |         mask = 1 << timer_num; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return esp_intr_alloc_intrstatus(intr_source, intr_alloc_flags, status_reg, mask, fn, arg, handle); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(config != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2017-09-25 06:20:12 +02:00
										 |  |  |     TIMER_CHECK(config->divider > 1 && config->divider < 65537, DIVIDER_RANGE_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     if (group_num == TIMER_GROUP_0) { | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |         periph_module_enable(PERIPH_TIMG0_MODULE); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     } else if (group_num == TIMER_GROUP_1) { | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |         periph_module_enable(PERIPH_TIMG1_MODULE); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |         if (p_timer_obj[group_num][timer_num] == NULL) { | 
					
						
							|  |  |  |             ESP_LOGE(TIMER_TAG, "TIMER driver malloc error"); | 
					
						
							|  |  |  |             return ESP_FAIL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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_init(&(p_timer_obj[group_num][timer_num]->hal), group_num, timer_num); | 
					
						
							|  |  |  |     timer_hal_intr_disable(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     timer_hal_clear_intr_status(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     timer_hal_set_auto_reload(&(p_timer_obj[group_num][timer_num]->hal), config->auto_reload); | 
					
						
							|  |  |  |     timer_hal_set_divider(&(p_timer_obj[group_num][timer_num]->hal), config->divider); | 
					
						
							|  |  |  |     timer_hal_set_counter_increase(&(p_timer_obj[group_num][timer_num]->hal), config->counter_dir); | 
					
						
							|  |  |  |     timer_hal_set_alarm_enable(&(p_timer_obj[group_num][timer_num]->hal), config->alarm_en); | 
					
						
							|  |  |  |     if (config->intr_type == TIMER_INTR_LEVEL) { | 
					
						
							|  |  |  |         timer_hal_set_level_int_enable(&(p_timer_obj[group_num][timer_num]->hal), true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // currently edge interrupt is not supported
 | 
					
						
							|  |  |  |     // if (config->intr_type == TIMER_INTR_EDGE) {
 | 
					
						
							|  |  |  |     //     timer_hal_set_edge_int_enable(&(p_timer_obj[group_num][timer_num]->hal), true);
 | 
					
						
							|  |  |  |     // }
 | 
					
						
							|  |  |  |     timer_hal_set_counter_enable(&(p_timer_obj[group_num][timer_num]->hal), config->counter_en); | 
					
						
							| 
									
										
										
										
											2019-11-25 20:10:27 +08:00
										 |  |  | #ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_hal_set_use_xtal(&(p_timer_obj[group_num][timer_num]->hal), config->clk_src); | 
					
						
							| 
									
										
										
										
											2019-06-06 14:20:39 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     timer_hal_set_counter_enable(&(p_timer_obj[group_num][timer_num]->hal), TIMER_PAUSE); | 
					
						
							|  |  |  |     timer_hal_intr_disable(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     timer_hal_clear_intr_status(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     heap_caps_free(p_timer_obj[group_num][timer_num]); | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(config != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     config->alarm_en = timer_hal_get_alarm_enable(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     config->auto_reload = timer_hal_get_auto_reload(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     config->counter_dir = timer_hal_get_counter_increase(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     config->counter_en = timer_hal_get_counter_enable(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uint16_t div; | 
					
						
							|  |  |  |     timer_hal_get_divider(&(p_timer_obj[group_num][timer_num]->hal), &div); | 
					
						
							|  |  |  |     if (div == 0) { | 
					
						
							|  |  |  |         config->divider = 65536; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         config->divider = div; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (timer_hal_get_level_int_enable(&(p_timer_obj[group_num][timer_num]->hal))) { | 
					
						
							| 
									
										
										
										
											2017-09-25 06:20:12 +02:00
										 |  |  |         config->intr_type = TIMER_INTR_LEVEL; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         config->intr_type = TIMER_INTR_MAX; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     for (int i = 0; i < TIMER_MAX; i++) { | 
					
						
							|  |  |  |         if (en_mask & BIT(i)) { | 
					
						
							|  |  |  |             timer_hal_intr_enable(&(p_timer_obj[group_num][i]->hal)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     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
										 |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     for (int i = 0; i < TIMER_MAX; i++) { | 
					
						
							|  |  |  |         if (disable_mask & BIT(i)) { | 
					
						
							|  |  |  |             timer_hal_intr_disable(&(p_timer_obj[group_num][i]->hal)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_enable_intr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     timer_hal_intr_enable(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_disable_intr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     timer_hal_intr_disable(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							| 
									
										
										
										
											2016-12-15 09:45:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | /* This function is deprecated */ | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | timer_intr_t IRAM_ATTR timer_group_intr_get_in_isr(timer_group_t group_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-25 14:47:19 +08:00
										 |  |  |     uint32_t intr_raw_status = 0; | 
					
						
							|  |  |  |     timer_hal_get_intr_raw_status(group_num, &intr_raw_status); | 
					
						
							|  |  |  |     return intr_raw_status; | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |         timer_hal_get_intr_status(&(p_timer_obj[group_num][TIMER_0]->hal), &intr_status); | 
					
						
							|  |  |  |     } else if (p_timer_obj[group_num][TIMER_1] != NULL) { | 
					
						
							|  |  |  |         timer_hal_get_intr_status(&(p_timer_obj[group_num][TIMER_1]->hal), &intr_status); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return intr_status; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This function is deprecated */ | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | void IRAM_ATTR timer_group_intr_clr_in_isr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_group_clr_intr_status_in_isr(group_num, timer_num); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IRAM_ATTR timer_group_clr_intr_status_in_isr(timer_group_t group_num, timer_idx_t timer_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     timer_hal_clear_intr_status(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_hal_set_alarm_enable(&(p_timer_obj[group_num][timer_num]->hal), 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint64_t val; | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_hal_get_counter_value(&(p_timer_obj[group_num][timer_num]->hal), &val); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_hal_set_alarm_value(&(p_timer_obj[group_num][timer_num]->hal), 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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     timer_hal_set_counter_enable(&(p_timer_obj[group_num][timer_num]->hal), counter_en); | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | /* This function is deprecated */ | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | void IRAM_ATTR timer_group_clr_intr_sta_in_isr(timer_group_t group_num, timer_intr_t intr_mask) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     for (uint32_t timer_idx = 0; timer_idx < TIMER_MAX; timer_idx++) { | 
					
						
							|  |  |  |         if (intr_mask & BIT(timer_idx)) { | 
					
						
							|  |  |  |             timer_group_clr_intr_status_in_isr(group_num, timer_idx); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  |     return timer_hal_get_auto_reload(&(p_timer_obj[group_num][timer_num]->hal)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_err_t timer_spinlock_take(timer_group_t group_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_ENTER_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							| 
									
										
										
										
											2019-07-25 09:52:36 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-16 19:06:34 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 14:21:36 +08:00
										 |  |  | esp_err_t timer_spinlock_give(timer_group_t group_num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); | 
					
						
							|  |  |  |     TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } |