diff --git a/components/zero_detection/CHANGELOG.md b/components/zero_detection/CHANGELOG.md index 668904341..64c0a9240 100644 --- a/components/zero_detection/CHANGELOG.md +++ b/components/zero_detection/CHANGELOG.md @@ -1,5 +1,12 @@ # ChangeLog +## v0.0.4 - 2023-01-10 + +### Enhancements: + +- add signal_lost_time parameter detecting signal loss +- fixed the issue of losing collection interrupts when Ccache is turned off + ## v0.0.3 - 2023-01-03 ### Bug Fix: diff --git a/components/zero_detection/README.md b/components/zero_detection/README.md index 03ea4945a..b1cd71ffc 100644 --- a/components/zero_detection/README.md +++ b/components/zero_detection/README.md @@ -9,6 +9,10 @@ The program returns results in the form of events, meeting the user's need for t After creating a new zero detection object by calling function `zero_detect_create()`, the zero detection object can create many events. +Afterward, users can register the interrupt by calling function `zero_detect_register_cb()`, + +Note: The prefix `IRAM_ATTR` should be added before registering the user-written interrupt function. + List of supported events: * SIGNAL_FREQ_OUT_OF_RANGE * SIGNAL_VALID diff --git a/components/zero_detection/idf_component.yml b/components/zero_detection/idf_component.yml index be150f8ea..f8bf8ad68 100644 --- a/components/zero_detection/idf_component.yml +++ b/components/zero_detection/idf_component.yml @@ -1,4 +1,4 @@ -version: "0.0.3" +version: "0.0.4" description: Zero Cross Detection Driver url: https://github.com/espressif/esp-iot-solution/tree/master/components/zero_detection repository: https://github.com/espressif/esp-iot-solution.git diff --git a/components/zero_detection/include/zero_detection.h b/components/zero_detection/include/zero_detection.h index 0bfe9dced..1c1d0bab1 100644 --- a/components/zero_detection/include/zero_detection.h +++ b/components/zero_detection/include/zero_detection.h @@ -1,4 +1,5 @@ -/* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -29,8 +30,9 @@ extern "C" { #endif #define ZERO_DETECTION_INIT_CONFIG_DEFAULT() { \ - .valid_time = 6, \ - .invalid_time = 5, \ + .valid_times = 6, \ + .invalid_times = 5, \ + .signal_lost_time_us = 100000, \ .freq_range_min_hz = 45,\ .freq_range_max_hz = 65,\ .capture_pin = 2,\ @@ -126,9 +128,10 @@ typedef void (*zero_cross_cb_t)(zero_detect_event_t zero_detect_event, zero_dete * @brief User config data type */ typedef struct { - int32_t capture_pin; /*!< GPIO number for zero cross detect capture */ - uint16_t valid_time; /*!< Comparison value for determining signal validity */ - uint16_t invalid_time; /*!< Comparison value for determining signal invalidity */ + int32_t capture_pin; /*!< GPIO number for zero cross detect capture */ + uint16_t valid_times; /*!< Minimum required number of times for detecting signal validity */ + uint16_t invalid_times; /*!< Minimum required number of times for detecting signal invalidity */ + uint64_t signal_lost_time_us; /*!< Minimum required duration for detecting signal loss */ zero_signal_type_t zero_signal_type; /*!< Zero Crossing Signal type */ zero_driver_type_t zero_driver_type; /*!< Zero crossing driver type */ double freq_range_max_hz; /*!< Maximum value of the frequency range when determining a valid signal */ diff --git a/components/zero_detection/test_apps/main/test_zero_deteciotn.c b/components/zero_detection/test_apps/main/test_zero_deteciotn.c index 26211e964..4bf5d23c5 100644 --- a/components/zero_detection/test_apps/main/test_zero_deteciotn.c +++ b/components/zero_detection/test_apps/main/test_zero_deteciotn.c @@ -1,4 +1,5 @@ -/* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -102,7 +103,7 @@ TEST_CASE("custom_zero_cross_detection_test", "[zero_cross_detecion][iot][pulse] config.capture_pin = 2; config.freq_range_max_hz = 65; //Hz config.freq_range_min_hz = 45; //Hz - config.valid_time = 6; + config.valid_times = 6; config.zero_signal_type = PULSE_WAVE; #if defined(SOC_MCPWM_SUPPORTED) config.zero_driver_type = MCPWM_TYPE; @@ -147,7 +148,7 @@ TEST_CASE("custom_zero_cross_detection_test", "[zero_cross_detecion][iot][square config.capture_pin = 2; config.freq_range_max_hz = 65; //Hz config.freq_range_min_hz = 45; //Hz - config.valid_time = 6; + config.valid_times = 6; config.zero_signal_type = SQUARE_WAVE; config.zero_driver_type = GPIO_TYPE; diff --git a/components/zero_detection/zero_detection.c b/components/zero_detection/zero_detection.c index a37b6fa81..d10839b3d 100644 --- a/components/zero_detection/zero_detection.c +++ b/components/zero_detection/zero_detection.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -19,10 +19,12 @@ typedef struct zero_cross { uint32_t full_cycle_us; //Tick value after half a cycle, becomes the entire cycle after multiplying by two uint16_t valid_count; //Count value of valid signals,switching during half a cycle - uint16_t valid_time; //Number of valid signal verifications + uint16_t valid_times; //Minimum required number of times for detecting signal validity uint16_t invalid_count; //Count value of invalid signals,switching during half a cycle - uint16_t invalid_time; //Number of invalid signal verifications + uint16_t invalid_times; //Minimum required number of times for detecting signal invalidity + + int64_t signal_lost_time_us; //Minimum required duration for detecting signal loss bool zero_source_power_invalid; //Power loss flag when signal source is lost bool zero_singal_invaild; //Signal is in an invalid range @@ -51,7 +53,7 @@ typedef struct zero_cross { /** * @brief Zero cross detecion driver core function */ -void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data_t *edata) +void IRAM_ATTR zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data_t *edata) { zero_cross_dev_t *zero_cross_dev = user_data; int gpio_status = 0; @@ -68,7 +70,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data #if defined(CONFIG_USE_GPTIMER) gptimer_set_raw_count(zero_cross_dev->gptimer, 0); #else - esp_timer_restart(zero_cross_dev->esp_timer, 100000); + esp_timer_restart(zero_cross_dev->esp_timer, zero_cross_dev->signal_lost_time_us); #endif if (edge_status) { if (zero_cross_dev->zero_signal_type == PULSE_WAVE) { //The methods for calculating the periods of pulse signals and square wave signals are different @@ -82,7 +84,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data if (zero_cross_dev->full_cycle_us >= zero_cross_dev->freq_range_min_us && zero_cross_dev->full_cycle_us <= zero_cross_dev->freq_range_max_us) { zero_cross_dev->valid_count++; //Reset to zero, increment and evaluate the counting value zero_cross_dev->invalid_count = 0; - if (zero_cross_dev->valid_count >= zero_cross_dev->valid_time) { + if (zero_cross_dev->valid_count >= zero_cross_dev->valid_times) { zero_cross_dev->zero_singal_invaild = false; zero_cross_dev->zero_source_power_invalid = false; //Enter the user callback function and return detection data and avoid judging upon receiving the first triggering edge @@ -110,7 +112,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data if (zero_cross_dev->full_cycle_us >= zero_cross_dev->freq_range_min_us && zero_cross_dev->full_cycle_us <= zero_cross_dev->freq_range_max_us) { //Determine whether it is within the frequency range zero_cross_dev->valid_count++; zero_cross_dev->invalid_count = 0; - if (zero_cross_dev->valid_count >= zero_cross_dev->valid_time) { + if (zero_cross_dev->valid_count >= zero_cross_dev->valid_times) { zero_cross_dev->zero_singal_invaild = false; zero_cross_dev->zero_source_power_invalid = false; //Enter the user callback function and return detection data and avoid judging upon receiving the first triggering edge @@ -138,7 +140,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data zero_cross_dev->zero_singal_invaild = true; zero_cross_dev->valid_count = 0; zero_cross_dev->invalid_count++; - if (zero_cross_dev->invalid_count >= zero_cross_dev->invalid_time) { + if (zero_cross_dev->invalid_count >= zero_cross_dev->invalid_times) { if (zero_cross_dev->event_callback && (zero_cross_dev->cap_val_end_of_sample != 0) && (zero_cross_dev->cap_val_begin_of_sample != 0)) { zero_detect_cb_param_t param = {0}; param.signal_invalid_event_data.invalid_count = zero_cross_dev->invalid_count; @@ -275,7 +277,7 @@ esp_err_t zero_detect_gpio_init(zero_detect_handle_t zcd_handle) io_conf.pull_up_en = 1; ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG, "GPIO config failed"); //Change gpio interrupt type for one pin - ESP_GOTO_ON_ERROR(gpio_install_isr_service(0), err, TAG, "GPIO install isr failed"); + ESP_GOTO_ON_ERROR(gpio_install_isr_service(ESP_INTR_FLAG_IRAM), err, TAG, "GPIO install isr failed"); return ESP_OK; @@ -302,7 +304,7 @@ esp_err_t zero_detect_gptime_init(zero_detect_handle_t zcd_handle) ESP_LOGI(TAG, "Install gptimer alarm"); gptimer_alarm_config_t gptimer_alarm = { - .alarm_count = 100000, //100ms + .alarm_count = zcd->signal_lost_time_us, .reload_count = 0, .flags.auto_reload_on_alarm = true, }; @@ -369,8 +371,9 @@ zero_detect_handle_t zero_detect_create(zero_detect_config_t *config) config->capture_pin = 2; } - zcd->valid_time = config->valid_time; - zcd->invalid_time = config->invalid_time; + zcd->valid_times = config->valid_times; + zcd->invalid_times = config->invalid_times; + zcd->signal_lost_time_us = config->signal_lost_time_us; zcd->freq_range_max_us = 1000000 / config->freq_range_min_hz; zcd->freq_range_min_us = 1000000 / config->freq_range_max_hz; zcd->capture_pin = config->capture_pin; @@ -412,7 +415,7 @@ zero_detect_handle_t zero_detect_create(zero_detect_config_t *config) ESP_LOGE(TAG, "Gptimer start failed"); } #else - if (esp_timer_start_periodic(zcd->esp_timer, 100000) != ESP_OK) { + if (esp_timer_start_periodic(zcd->esp_timer, zcd->signal_lost_time_us) != ESP_OK) { ESP_LOGE(TAG, "Esptimer start failed"); } #endif diff --git a/docs/en/others/zero_detection.rst b/docs/en/others/zero_detection.rst index 19d94e759..a8aef7fa7 100644 --- a/docs/en/others/zero_detection.rst +++ b/docs/en/others/zero_detection.rst @@ -57,7 +57,8 @@ Create a zero detection ^^^^^^^^^^^^^^^^^^^^^^^ .. code:: c - int zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param) {//User's callback API + void IRAM_ATTR zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param, void *usr_data) //User's callback API + { switch (zero_detect_event) { case SIGNAL_FREQ_OUT_OF_RANGE: ESP_LOGE(TAG, "SIGNAL_FREQ_OUT_OF_RANGE"); @@ -71,7 +72,6 @@ Create a zero detection default: break; } - return zero_detect_event; } // Create a zero detection and register call-back @@ -79,9 +79,8 @@ Create a zero detection .capture_pin = 2, .freq_range_max_hz = 65, .freq_range_min_hz = 45, //Hz - .valid_time = 6, - .invalid_time = 5, - .event_callback = zero_detection_event_cb, //Create callback + .valid_times = 6, + .invalid_times = 5, .zero_signal_type = SQUARE_WAVE, .zero_driver_type = MCPWM_TYPE, }; @@ -89,6 +88,7 @@ Create a zero detection if(NULL == g_zcds) { ESP_LOGE(TAG, "Zero Detection create failed"); } + zero_detect_register_cb(g_zcds, zero_detection_event_cb, NULL); API Reference ------------- diff --git a/docs/zh_CN/others/zero_detection.rst b/docs/zh_CN/others/zero_detection.rst index a94c81fd8..c90893154 100644 --- a/docs/zh_CN/others/zero_detection.rst +++ b/docs/zh_CN/others/zero_detection.rst @@ -52,7 +52,8 @@ ^^^^^^^^^^^^ .. code:: c - int zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param) {//User's callback API + void IRAM_ATTR zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param, void *usr_data) //User's callback API + { switch (zero_detect_event) { case SIGNAL_FREQ_OUT_OF_RANGE: ESP_LOGE(TAG, "SIGNAL_FREQ_OUT_OF_RANGE"); @@ -66,7 +67,6 @@ default: break; } - return zero_detect_event; } // Create a zero detection and register call-back @@ -74,9 +74,8 @@ .capture_pin = 2, .freq_range_max_hz = 65, .freq_range_min_hz = 45, //Hz - .valid_time = 6, - .invalid_time = 5, - .event_callback = zero_detection_event_cb, //Create callback + .valid_times = 6, + .invalid_times = 5, .zero_signal_type = SQUARE_WAVE, .zero_driver_type = MCPWM_TYPE, }; @@ -84,6 +83,7 @@ if(NULL == g_zcds) { ESP_LOGE(TAG, "Zero Detection create failed"); } + zero_detect_register_cb(g_zcds, zero_detection_event_cb, NULL); API Reference ------------- diff --git a/examples/zero_cross_detection/main/zero_detect_example_main.c b/examples/zero_cross_detection/main/zero_detect_example_main.c index d9f0ff4c4..aef97bddf 100644 --- a/examples/zero_cross_detection/main/zero_detect_example_main.c +++ b/examples/zero_cross_detection/main/zero_detect_example_main.c @@ -1,4 +1,5 @@ -/* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +/* + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -39,7 +40,7 @@ static zero_cross_relay_t zcd = ZERO_DETECTION_RELAY_CONFIG_DEFAULT(); //Default static zero_detect_handle_t *g_zcds; zero_detect_config_t config = ZERO_DETECTION_INIT_CONFIG_DEFAULT(); //Default parameter -void zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param, void *usr_data) //User's callback API +void IRAM_ATTR zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param, void *usr_data) //User's callback API { switch (zero_detect_event) { case SIGNAL_FREQ_OUT_OF_RANGE: @@ -135,7 +136,8 @@ void app_main(void) config.capture_pin = CONFIG_ZERO_DETECT_INPUT_GPIO; config.freq_range_max_hz = 65; //Hz config.freq_range_min_hz = 45; //Hz - config.valid_time = 6; + config.signal_lost_time_us = 100000; //Us + config.valid_times = 6; config.zero_signal_type = CONFIG_ZERO_DETECT_SIGNAL_TYPE; #if defined(SOC_MCPWM_SUPPORTED) config.zero_driver_type = MCPWM_TYPE;