-
Notifications
You must be signed in to change notification settings - Fork 733
/
zero_detection.h
222 lines (195 loc) · 6.64 KB
/
zero_detection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ZERO_DETECTION_H_
#define _ZERO_DETECTION_H_
#include "driver/gpio.h"
#include "driver/mcpwm_cap.h"
#include "hal/gpio_ll.h"
#if defined(CONFIG_USE_GPTIMER)
#include "driver/gptimer.h"
#endif
#include "esp_timer.h"
#include "soc/gpio_struct.h"
#include "esp_log.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "zero_detection.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ZERO_DETECTION_INIT_CONFIG_DEFAULT() { \
.valid_times = 6, \
.invalid_times = 5, \
.signal_lost_time_us = 100000, \
.freq_range_min_hz = 45,\
.freq_range_max_hz = 65,\
.capture_pin = 2,\
.zero_signal_type = SQUARE_WAVE,\
.zero_driver_type = GPIO_TYPE,\
}
/**
* @brief Zero detection events
*/
typedef enum {
SIGNAL_FREQ_OUT_OF_RANGE = 0, //Event when the current signal frequency exceeds the set range
SIGNAL_VALID, //Event when the frequency of the current signal is within the range, and the number of occurrences surpasses the user-set limit
SIGNAL_INVALID, //Event when the frequency of the current signal exceeds the range, and the number of occurrences surpasses the user-set limit
SIGNAL_LOST, //Event occurring when no signal is received within 100ms
SIGNAL_RISING_EDGE, //Event on the rising edge of the signal
SIGNAL_FALLING_EDGE, //Event on the falling edge of the signal
} zero_detect_event_t;
/**
* @brief Zero detection wave type
*/
typedef enum {
SQUARE_WAVE = 0,
PULSE_WAVE,
} zero_signal_type_t;
/**
* @brief Zero detection driver type
*/
typedef enum {
#if SOC_MCPWM_SUPPORTED
MCPWM_TYPE = 0, /*!< Use MCPWM as driver */
#endif
GPIO_TYPE, /*!< Use GPIO as driver */
} zero_driver_type_t;
/**
* @brief Event callback parameters union
*/
typedef union {
/**
* @brief Signal exceeds frequency range data return type
*/
struct signal_freq_event_data_t {
mcpwm_capture_edge_t cap_edge; /*!< Trigger edge of zero cross signal */
uint32_t full_cycle_us; /*!< Current signal cycle */
} signal_freq_event_data; /*!< Signal freq event data struct */
/**
* @brief Signal valid data return type
*/
struct signal_valid_event_data_t {
mcpwm_capture_edge_t cap_edge; /*!< Trigger edge of zero cross signal */
uint32_t full_cycle_us; /*!< Current signal cycle */
uint16_t valid_count; /*!< Counting when the signal is valid */
} signal_valid_event_data; /*!< Signal valid event data struct */
/**
* @brief Signal invalid data return type
*/
struct signal_invalid_event_data_t {
mcpwm_capture_edge_t cap_edge; /*!< Trigger edge of zero cross signal */
uint32_t full_cycle_us; /*!< Current signal cycle */
uint16_t invalid_count; /*!< Counting when the signal is invalid */
} signal_invalid_event_data; /*!< Signal invalid event data struct */
/**
* @brief Signal rising edge data return type
*/
struct signal_rising_edge_event_data_t {
uint16_t valid_count; /*!< Counting when the signal is valid */
uint16_t invalid_count; /*!< Counting when the signal is invalid */
uint32_t full_cycle_us; /*!< Current signal cycle */
} signal_rising_edge_event_data; /*!< Signal rising edge event data struct */
/**
* @brief Signal falling edge data return type
*/
struct signal_falling_edge_event_data_t {
uint16_t valid_count; /*!< Counting when the signal is valid */
uint16_t invalid_count; /*!< Counting when the signal is invalid */
uint32_t full_cycle_us; /*!< Current signal cycle */
} signal_falling_edge_event_data; /*!< Signal falling edge event_data */
} zero_detect_cb_param_t;
/**
* @brief Callback format
*/
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
*/
typedef struct {
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 */
double freq_range_min_hz; /*!< Minimum value of the frequency range when determining a valid signal */
} zero_detect_config_t;
typedef void *zero_detect_handle_t;
/**
* @brief Create a zero detect target
*
* @param config A zero detect object to config
*
* @return
* - zero_detect_handle_t A zero cross detection object
*/
zero_detect_handle_t zero_detect_create(zero_detect_config_t *config);
/**
* @brief Show zero detect test data
*
* @param zcd_handle A zero detect handle
*
*/
void zero_show_data(zero_detect_handle_t zcd_handle);
/**
* @brief Delete a zero detect device
*
* @param zcd_handle A zero detect handle
*
* @return
* - ESP_OK Success
* - ESP_FAIL Failure
*/
esp_err_t zero_detect_delete(zero_detect_handle_t zcd_handle);
/**
* @brief Get relay power status
*
* @param zcd_handle A zero detect handle
*
* @return
* - true Power on
* - false Power off
*/
bool zero_detect_get_power_status(zero_detect_handle_t zcd_handle);
/**
* @brief Get singal invaild status
*
* @param zcd_handle A zero detect handle
*
* @return
* - true Signal is invaild
* - false Signal is vaild
*/
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);
#ifdef __cplusplus
}
#endif
#endif