From dc1d7362f38eafc1b764cb5d4b415df28fa9d652 Mon Sep 17 00:00:00 2001 From: MinZai97 Date: Mon, 25 Dec 2023 20:40:06 +0800 Subject: [PATCH] feat(zero_detect): Add register callback and get signal type function --- components/zero_detection/CHANGELOG.md | 6 +++ components/zero_detection/idf_component.yml | 5 +- .../zero_detection/include/zero_detection.h | 28 +++++++++-- .../test_apps/main/test_zero_deteciotn.c | 16 +++---- components/zero_detection/zero_detection.c | 46 ++++++++++++++----- .../main/zero_detect_example_main.c | 5 +- 6 files changed, 80 insertions(+), 26 deletions(-) diff --git a/components/zero_detection/CHANGELOG.md b/components/zero_detection/CHANGELOG.md index c5ec7bc53..e135348cc 100644 --- a/components/zero_detection/CHANGELOG.md +++ b/components/zero_detection/CHANGELOG.md @@ -1,5 +1,11 @@ # ChangeLog +## v0.0.2 - 2023-12-26 + +### Enhancements: + +- add register callback function and get signal type function + ## v0.0.1 - 2023-11-28 First release version. diff --git a/components/zero_detection/idf_component.yml b/components/zero_detection/idf_component.yml index e29a82cd8..e5d89a6d9 100644 --- a/components/zero_detection/idf_component.yml +++ b/components/zero_detection/idf_component.yml @@ -1,6 +1,9 @@ -version: "0.0.1" +version: "0.0.2" 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 +documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/others/zero_detection.html +issues: https://github.com/espressif/esp-iot-solution/issues dependencies: idf: ">=5.0" espressif/cmake_utilities: "0.*" diff --git a/components/zero_detection/include/zero_detection.h b/components/zero_detection/include/zero_detection.h index b38d9116d..ee47314b0 100644 --- a/components/zero_detection/include/zero_detection.h +++ b/components/zero_detection/include/zero_detection.h @@ -34,7 +34,6 @@ extern "C" { .freq_range_min_hz = 45,\ .freq_range_max_hz = 65,\ .capture_pin = 2,\ - .event_callback = NULL,\ .zero_signal_type = SQUARE_WAVE,\ .zero_driver_type = GPIO_TYPE,\ } @@ -121,7 +120,7 @@ typedef union { /** * @brief Callback format */ -typedef int (*esp_zero_detect_cb_t)(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param); +typedef void (*zero_cross_cb_t)(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param, void *usr_data); /** * @brief User config data type @@ -134,7 +133,6 @@ typedef struct { 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 */ double freq_range_min_hz; /*!< Minimum value of the frequency range when determining a valid signal */ - esp_zero_detect_cb_t event_callback; /*!< Various event returns in zero cross detection */ } zero_detect_config_t; typedef void *zero_detect_handle_t; @@ -190,6 +188,30 @@ bool zero_detect_get_power_status(zero_detect_handle_t zcd_handle); */ bool zero_detect_singal_invaild_status(zero_detect_handle_t zcd_handle); +/** + * @brief Get singal type + * + * @param zcd_handle A zero detect handle + * + * @return + * - SQUARE_WAVE Signal type is square + * - PULSE_WAVE Signal type is pulse + */ +zero_signal_type_t zero_detect_get_signal_type(zero_detect_handle_t zcd_handle); + +/** + * @brief Register zero cross detection callback + * + * @param zcd_handle A zero detect handle + * @param cb A callback function + * @param usr_data User data + * + * @return + * - ESP_OK Success + * - ESP_FAIL Failure + */ +esp_err_t zero_detect_register_cb(zero_detect_handle_t zcd_handle, zero_cross_cb_t cb, void *usr_data); + #endif #ifdef __cplusplus 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 f401cb935..26211e964 100644 --- a/components/zero_detection/test_apps/main/test_zero_deteciotn.c +++ b/components/zero_detection/test_apps/main/test_zero_deteciotn.c @@ -33,7 +33,7 @@ static size_t before_free_32bit; uint32_t delay_gap_begin = 0; uint32_t delay_gap_end = 0; -static IRAM_ATTR int square_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param) //User's callback API +static IRAM_ATTR void square_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: @@ -60,10 +60,9 @@ static IRAM_ATTR int square_event_cb(zero_detect_event_t zero_detect_event, zero default: break; } - return zero_detect_event; } -static IRAM_ATTR int pulse_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param) //User's callback API +static IRAM_ATTR void pulse_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: @@ -84,7 +83,6 @@ static IRAM_ATTR int pulse_event_cb(zero_detect_event_t zero_detect_event, zero_ default: break; } - return zero_detect_event; } TEST_CASE("custom_zero_cross_detection_test", "[zero_cross_detecion][iot][pulse]") @@ -99,13 +97,12 @@ TEST_CASE("custom_zero_cross_detection_test", "[zero_cross_detecion][iot][pulse] gpio_ll_set_level(&GPIO, 5, 1); gpio_config(&io_conf); - static zero_detect_handle_t *g_zcds; + static zero_detect_handle_t g_zcds = NULL; zero_detect_config_t config = ZERO_DETECTION_INIT_CONFIG_DEFAULT(); //Default parameter config.capture_pin = 2; config.freq_range_max_hz = 65; //Hz config.freq_range_min_hz = 45; //Hz config.valid_time = 6; - config.event_callback = pulse_event_cb; //Create callback config.zero_signal_type = PULSE_WAVE; #if defined(SOC_MCPWM_SUPPORTED) config.zero_driver_type = MCPWM_TYPE; @@ -116,6 +113,8 @@ TEST_CASE("custom_zero_cross_detection_test", "[zero_cross_detecion][iot][pulse] g_zcds = zero_detect_create(&config); TEST_ASSERT_NOT_EQUAL(NULL, g_zcds); + zero_detect_register_cb(g_zcds, pulse_event_cb, NULL); + for (int i = 0; i < 100; i++) { delay_gap_begin = esp_timer_get_time(); gpio_ll_set_level(&GPIO, 5, 1); @@ -143,19 +142,20 @@ TEST_CASE("custom_zero_cross_detection_test", "[zero_cross_detecion][iot][square gpio_ll_set_level(&GPIO, 5, 1); gpio_config(&io_conf); - static zero_detect_handle_t *g_zcds; + static zero_detect_handle_t g_zcds = NULL; zero_detect_config_t config = ZERO_DETECTION_INIT_CONFIG_DEFAULT(); //Default parameter config.capture_pin = 2; config.freq_range_max_hz = 65; //Hz config.freq_range_min_hz = 45; //Hz config.valid_time = 6; - config.event_callback = square_event_cb; //Create callback config.zero_signal_type = SQUARE_WAVE; config.zero_driver_type = GPIO_TYPE; g_zcds = zero_detect_create(&config); TEST_ASSERT_NOT_EQUAL(NULL, g_zcds); + zero_detect_register_cb(g_zcds, square_event_cb, NULL); + for (int i = 0; i < 100; i++) { delay_gap_begin = esp_timer_get_time(); gpio_ll_set_level(&GPIO, 5, i % 2); diff --git a/components/zero_detection/zero_detection.c b/components/zero_detection/zero_detection.c index 233db32b1..a37b6fa81 100644 --- a/components/zero_detection/zero_detection.c +++ b/components/zero_detection/zero_detection.c @@ -35,7 +35,8 @@ typedef struct zero_cross { double freq_range_max_us; //Tick value calculated after the user inputs the frequency double freq_range_min_us; - esp_zero_detect_cb_t event_callback; + zero_cross_cb_t event_callback; //Zero cross event callback + void *user_data; //User's data when regsister for callback mcpwm_cap_timer_handle_t cap_timer; mcpwm_cap_channel_handle_t cap_chan; @@ -90,7 +91,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data param.signal_valid_event_data.valid_count = zero_cross_dev->valid_count; param.signal_valid_event_data.full_cycle_us = zero_cross_dev->full_cycle_us; param.signal_valid_event_data.cap_edge = MCPWM_CAP_EDGE_POS; - zero_cross_dev->event_callback(SIGNAL_VALID, ¶m); + zero_cross_dev->event_callback(SIGNAL_VALID, ¶m, zero_cross_dev->user_data); } } } @@ -99,7 +100,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data param.signal_rising_edge_event_data.valid_count = zero_cross_dev->valid_count; param.signal_rising_edge_event_data.invalid_count = zero_cross_dev->invalid_count; param.signal_rising_edge_event_data.full_cycle_us = zero_cross_dev->full_cycle_us; - zero_cross_dev->event_callback(SIGNAL_RISING_EDGE, ¶m); + zero_cross_dev->event_callback(SIGNAL_RISING_EDGE, ¶m, zero_cross_dev->user_data); } } else if (!edge_status) { if (zero_cross_dev->zero_signal_type == SQUARE_WAVE) { //The falling edge is only used with square wave signals @@ -118,7 +119,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data param.signal_valid_event_data.valid_count = zero_cross_dev->valid_count; param.signal_valid_event_data.full_cycle_us = zero_cross_dev->full_cycle_us; param.signal_valid_event_data.cap_edge = MCPWM_CAP_EDGE_NEG; - zero_cross_dev->event_callback(SIGNAL_VALID, ¶m); + zero_cross_dev->event_callback(SIGNAL_VALID, ¶m, zero_cross_dev->user_data); } } } @@ -128,7 +129,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data param.signal_falling_edge_event_data.valid_count = zero_cross_dev->valid_count; param.signal_falling_edge_event_data.invalid_count = zero_cross_dev->invalid_count; param.signal_falling_edge_event_data.full_cycle_us = zero_cross_dev->full_cycle_us; - zero_cross_dev->event_callback(SIGNAL_FALLING_EDGE, ¶m); + zero_cross_dev->event_callback(SIGNAL_FALLING_EDGE, ¶m, zero_cross_dev->user_data); } } @@ -147,7 +148,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data } else { param.signal_invalid_event_data.cap_edge = MCPWM_CAP_EDGE_NEG; } - zero_cross_dev->event_callback(SIGNAL_INVALID, ¶m); + zero_cross_dev->event_callback(SIGNAL_INVALID, ¶m, zero_cross_dev->user_data); } } if (zero_cross_dev->event_callback && (zero_cross_dev->cap_val_end_of_sample != 0) && (zero_cross_dev->cap_val_begin_of_sample != 0)) { @@ -158,7 +159,7 @@ void zero_cross_handle_interrupt(void *user_data, const mcpwm_capture_event_data param.signal_freq_event_data.cap_edge = MCPWM_CAP_EDGE_NEG; } param.signal_freq_event_data.full_cycle_us = zero_cross_dev->full_cycle_us; - zero_cross_dev->event_callback(SIGNAL_FREQ_OUT_OF_RANGE, ¶m); + zero_cross_dev->event_callback(SIGNAL_FREQ_OUT_OF_RANGE, ¶m, zero_cross_dev->user_data); } } } @@ -188,8 +189,9 @@ static IRAM_ATTR bool zero_source_power_invalid_cb(gptimer_handle_t timer, const zero_cross_dev->cap_val_end_of_sample = 0; zero_cross_dev->valid_count = 0; zero_cross_dev->zero_singal_invaild = 0; - zero_cross_dev->event_callback(SIGNAL_LOST, NULL); - + if (zero_cross_dev->event_callback) { + zero_cross_dev->event_callback(SIGNAL_LOST, NULL, zero_cross_dev->user_data); + } return false; } #else @@ -202,7 +204,9 @@ void IRAM_ATTR zero_source_power_invalid_cb(void *arg) zero_cross_dev->cap_val_end_of_sample = 0; zero_cross_dev->valid_count = 0; zero_cross_dev->zero_singal_invaild = 0; - zero_cross_dev->event_callback(SIGNAL_LOST, NULL); + if (zero_cross_dev->event_callback) { + zero_cross_dev->event_callback(SIGNAL_LOST, NULL, zero_cross_dev->user_data); + } } #endif @@ -370,7 +374,6 @@ zero_detect_handle_t zero_detect_create(zero_detect_config_t *config) 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; - zcd->event_callback = config->event_callback; zcd->zero_signal_type = config->zero_signal_type; #if defined(SOC_MCPWM_SUPPORTED) zcd->zero_driver_type = config->zero_driver_type; @@ -460,6 +463,21 @@ esp_err_t zero_detect_delete(zero_detect_handle_t zcd_handle) return ret; } +esp_err_t zero_detect_register_cb(zero_detect_handle_t zcd_handle, zero_cross_cb_t cb, void *usr_data) +{ + esp_err_t ret = ESP_OK; + if (zcd_handle == NULL || cb == NULL) { + ESP_LOGW(TAG, "arg is invalid"); + return ESP_ERR_INVALID_ARG; + } + + zero_cross_dev_t *zcd = (zero_cross_dev_t *)zcd_handle; + zcd->user_data = usr_data; + zcd->event_callback = cb; + + return ret; +} + void zero_show_data(zero_detect_handle_t zcd_handle) { zero_cross_dev_t *zcd = (zero_cross_dev_t *)zcd_handle; @@ -482,6 +500,12 @@ bool zero_detect_get_power_status(zero_detect_handle_t zcd_handle) return zcd->zero_source_power_invalid; } +zero_signal_type_t zero_detect_get_signal_type(zero_detect_handle_t zcd_handle) +{ + zero_cross_dev_t *zcd = (zero_cross_dev_t *)zcd_handle; + return zcd->zero_signal_type; +} + bool zero_detect_signal_invaild_status(zero_detect_handle_t zcd_handle) { zero_cross_dev_t *zcd = (zero_cross_dev_t *)zcd_handle; 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 406d0e9b9..d9f0ff4c4 100644 --- a/examples/zero_cross_detection/main/zero_detect_example_main.c +++ b/examples/zero_cross_detection/main/zero_detect_example_main.c @@ -39,7 +39,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 -int zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_cb_param_t *param) //User's callback API +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 { switch (zero_detect_event) { case SIGNAL_FREQ_OUT_OF_RANGE: @@ -121,7 +121,6 @@ int zero_detection_event_cb(zero_detect_event_t zero_detect_event, zero_detect_c default: break; } - return zero_detect_event; } esp_err_t relay_on_off(bool on_off) @@ -137,7 +136,6 @@ void app_main(void) config.freq_range_max_hz = 65; //Hz config.freq_range_min_hz = 45; //Hz config.valid_time = 6; - config.event_callback = zero_detection_event_cb; //Create callback config.zero_signal_type = CONFIG_ZERO_DETECT_SIGNAL_TYPE; #if defined(SOC_MCPWM_SUPPORTED) config.zero_driver_type = MCPWM_TYPE; @@ -156,6 +154,7 @@ void app_main(void) io_conf.pull_up_en = 0; gpio_ll_set_level(&GPIO, zcd.control_pin, !zcd.relay_active_level); gpio_config(&io_conf); + zero_detect_register_cb(g_zcds, zero_detection_event_cb, NULL); while (1) { vTaskDelay(500 / portTICK_PERIOD_MS);