Skip to content

Commit

Permalink
Merge branch 'feature/zero_detection_add_regist_callback' into 'master'
Browse files Browse the repository at this point in the history
feature:add register callback and get signal type function

See merge request ae_group/esp-iot-solution!926
  • Loading branch information
Yuan Ming Fu committed Dec 26, 2023
2 parents 2d256ef + dc1d736 commit a8a66f0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 26 deletions.
6 changes: 6 additions & 0 deletions components/zero_detection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 4 additions & 1 deletion components/zero_detection/idf_component.yml
Original file line number Diff line number Diff line change
@@ -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.*"
Expand Down
28 changes: 25 additions & 3 deletions components/zero_detection/include/zero_detection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions components/zero_detection/test_apps/main/test_zero_deteciotn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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]")
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 35 additions & 11 deletions components/zero_detection/zero_detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, &param);
zero_cross_dev->event_callback(SIGNAL_VALID, &param, zero_cross_dev->user_data);
}
}
}
Expand All @@ -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, &param);
zero_cross_dev->event_callback(SIGNAL_RISING_EDGE, &param, 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
Expand All @@ -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, &param);
zero_cross_dev->event_callback(SIGNAL_VALID, &param, zero_cross_dev->user_data);
}
}
}
Expand All @@ -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, &param);
zero_cross_dev->event_callback(SIGNAL_FALLING_EDGE, &param, zero_cross_dev->user_data);
}
}

Expand All @@ -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, &param);
zero_cross_dev->event_callback(SIGNAL_INVALID, &param, 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)) {
Expand All @@ -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, &param);
zero_cross_dev->event_callback(SIGNAL_FREQ_OUT_OF_RANGE, &param, zero_cross_dev->user_data);
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions examples/zero_cross_detection/main/zero_detect_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit a8a66f0

Please sign in to comment.