diff --git a/components/driver/test/test_timer.c b/components/driver/test/test_timer.c index 9a58eed1dd..867f7bd2eb 100644 --- a/components/driver/test/test_timer.c +++ b/components/driver/test/test_timer.c @@ -7,10 +7,9 @@ #include "nvs_flash.h" #include "driver/timer.h" #include "soc/rtc.h" +#include "soc/soc_caps.h" #include "esp_rom_sys.h" -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3) - #define TIMER_DIVIDER 16 #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) /*!< used to calculate counter value */ #define TIMER_DELTA 0.001 @@ -38,13 +37,15 @@ static timer_info_t timer_info[4] = { TIMER_INFO_INIT(TIMER_GROUP_1, TIMER_1), }; -#define GET_TIMER_INFO(TG, TID) (&timer_info[(TG)*2+(TID)]) +static intr_handle_t timer_isr_handles[SOC_TIMER_GROUP_TOTAL_TIMERS]; + +#define GET_TIMER_INFO(TG, TID) (&timer_info[(TG)*SOC_TIMER_GROUP_TIMERS_PER_GROUP+(TID)]) // timer group interruption handle callback static bool test_timer_group_isr_cb(void *arg) { bool is_awoken = false; - timer_info_t* info = (timer_info_t*) arg; + timer_info_t *info = (timer_info_t *) arg; const timer_group_t timer_group = info->timer_group; const timer_idx_t timer_idx = info->timer_idx; uint64_t timer_val; @@ -74,7 +75,9 @@ static bool test_timer_group_isr_cb(void *arg) BaseType_t awoken = pdFALSE; BaseType_t ret = xQueueSendFromISR(timer_queue, &evt, &awoken); TEST_ASSERT_EQUAL(pdTRUE, ret); - if (awoken) is_awoken = true; + if (awoken) { + is_awoken = true; + } } return is_awoken; } @@ -90,8 +93,8 @@ static void test_timer_group_isr(void *arg) // initialize all timer static void all_timer_init(timer_config_t *config, bool expect_init) { - for (uint32_t tg_idx=0; tg_idxhal), &div); *time = (double)timer_val * div / rtc_clk_apb_freq_get(); -#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK +#if SOC_TIMER_GROUP_SUPPORT_XTAL if (timer_hal_get_use_xtal(&(p_timer_obj[group_num][timer_num]->hal))) { *time = (double)timer_val * div / ((int)rtc_clk_xtal_freq_get() * 1000000); } @@ -266,36 +266,10 @@ esp_err_t timer_isr_register(timer_group_t group_num, timer_idx_t timer_num, TIMER_CHECK(fn != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); TIMER_CHECK(p_timer_obj[group_num][timer_num] != NULL, TIMER_NEVER_INIT_ERROR, ESP_ERR_INVALID_ARG); - int intr_source = 0; uint32_t status_reg = 0; uint32_t mask = 0; - switch (group_num) { - case TIMER_GROUP_0: - default: - intr_source = ETS_TG0_T0_LEVEL_INTR_SOURCE + timer_num; -#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 - if ((intr_alloc_flags & ESP_INTR_FLAG_EDGE)) { - intr_source = ETS_TG0_T0_EDGE_INTR_SOURCE + timer_num; - } -#endif - timer_hal_get_status_reg_mask_bit(&(p_timer_obj[TIMER_GROUP_0][timer_num]->hal), &status_reg, &mask); - break; - case TIMER_GROUP_1: - intr_source = ETS_TG1_T0_LEVEL_INTR_SOURCE + timer_num; -#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 - if ((intr_alloc_flags & ESP_INTR_FLAG_EDGE)) { - intr_source = ETS_TG1_T0_EDGE_INTR_SOURCE + timer_num; - } -#endif - 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_LEVEL_INTR_SOURCE + timer_num; - } - timer_hal_get_status_reg_mask_bit(&(p_timer_obj[TIMER_GROUP_1][timer_num]->hal), &status_reg, &mask); - break; - } - return esp_intr_alloc_intrstatus(intr_source, intr_alloc_flags, status_reg, mask, fn, arg, handle); + timer_hal_get_status_reg_mask_bit(&(p_timer_obj[group_num][timer_num]->hal), &status_reg, &mask); + return esp_intr_alloc_intrstatus(timer_group_periph_signals.groups[group_num].t0_irq_id + timer_num, intr_alloc_flags, status_reg, mask, fn, arg, handle); } esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer_config_t *config) @@ -305,11 +279,7 @@ esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer TIMER_CHECK(config != NULL, TIMER_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG); TIMER_CHECK(config->divider > 1 && config->divider < 65537, DIVIDER_RANGE_ERROR, ESP_ERR_INVALID_ARG); - if (group_num == TIMER_GROUP_0) { - periph_module_enable(PERIPH_TIMG0_MODULE); - } else if (group_num == TIMER_GROUP_1) { - periph_module_enable(PERIPH_TIMG1_MODULE); - } + periph_module_enable(timer_group_periph_signals.groups[group_num].module); 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); @@ -327,15 +297,12 @@ esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer 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); + timer_hal_set_level_int_enable(&(p_timer_obj[group_num][timer_num]->hal), true); + if (config->intr_type != TIMER_INTR_LEVEL) { + ESP_LOGW(TIMER_TAG, "only support Level Interrupt, switch to Level Interrupt instead"); } - // 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); -#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK +#if SOC_TIMER_GROUP_SUPPORT_XTAL timer_hal_set_use_xtal(&(p_timer_obj[group_num][timer_num]->hal), config->clk_src); #endif TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); @@ -450,9 +417,12 @@ 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) { + } +#if SOC_TIMER_GROUP_TIMERS_PER_GROUP > 1 + 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); } +#endif return intr_status; } diff --git a/components/hal/esp32/include/hal/timer_ll.h b/components/hal/esp32/include/hal/timer_ll.h index adfe26616f..2ec4286f77 100644 --- a/components/hal/esp32/include/hal/timer_ll.h +++ b/components/hal/esp32/include/hal/timer_ll.h @@ -302,7 +302,7 @@ FORCE_INLINE_ATTR void timer_ll_clear_intr_status(timg_dev_t *hw, timer_idx_t ti */ FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_status) { - *intr_status = hw->int_st_timers.val; + *intr_status = hw->int_st_timers.val & 0x03; } /** @@ -316,7 +316,7 @@ FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_s FORCE_INLINE_ATTR void timer_ll_get_intr_raw_status(timer_group_t group_num, uint32_t *intr_raw_status) { timg_dev_t *hw = TIMER_LL_GET_HW(group_num); - *intr_raw_status = hw->int_raw.val; + *intr_raw_status = hw->int_raw.val & 0x03; } /** diff --git a/components/hal/esp32s2/include/hal/timer_ll.h b/components/hal/esp32s2/include/hal/timer_ll.h index 389748b525..6a9a235f8a 100644 --- a/components/hal/esp32s2/include/hal/timer_ll.h +++ b/components/hal/esp32s2/include/hal/timer_ll.h @@ -298,7 +298,7 @@ FORCE_INLINE_ATTR void timer_ll_clear_intr_status(timg_dev_t *hw, timer_idx_t ti */ FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_status) { - *intr_status = hw->int_st.val; + *intr_status = hw->int_st.val & 0x03; } /** @@ -312,7 +312,7 @@ FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_s FORCE_INLINE_ATTR void timer_ll_get_intr_raw_status(timer_group_t group_num, uint32_t *intr_raw_status) { timg_dev_t *hw = TIMER_LL_GET_HW(group_num); - *intr_raw_status = hw->int_raw.val; + *intr_raw_status = hw->int_raw.val & 0x03; } /** diff --git a/components/hal/esp32s3/include/hal/timer_ll.h b/components/hal/esp32s3/include/hal/timer_ll.h index 5d19fe1327..ced9251201 100644 --- a/components/hal/esp32s3/include/hal/timer_ll.h +++ b/components/hal/esp32s3/include/hal/timer_ll.h @@ -297,7 +297,7 @@ FORCE_INLINE_ATTR void timer_ll_clear_intr_status(timg_dev_t *hw, timer_idx_t ti */ FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_status) { - *intr_status = hw->int_st.val; + *intr_status = hw->int_st.val & 0x03; } /** @@ -311,7 +311,7 @@ FORCE_INLINE_ATTR void timer_ll_get_intr_status(timg_dev_t *hw, uint32_t *intr_s FORCE_INLINE_ATTR void timer_ll_get_intr_raw_status(timer_group_t group_num, uint32_t *intr_raw_status) { timg_dev_t *hw = TIMER_LL_GET_HW(group_num); - *intr_raw_status = hw->int_raw.val; + *intr_raw_status = hw->int_raw.val & 0x03; } /** diff --git a/components/hal/include/hal/timer_types.h b/components/hal/include/hal/timer_types.h index e35009fab6..962fae043b 100644 --- a/components/hal/include/hal/timer_types.h +++ b/components/hal/include/hal/timer_types.h @@ -29,7 +29,9 @@ extern "C" { */ typedef enum { TIMER_GROUP_0 = 0, /*! 1 TIMER_GROUP_1 = 1, /*! 1 TIMER_1 = 1, /*!