1 /*
2 * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include "freertos/FreeRTOS.h"
7 #include "esp_log.h"
8 #include "esp_check.h"
9 #include "soc/soc_caps.h"
10 #include "esp_private/periph_ctrl.h"
11 #include "driver/pcnt_types_legacy.h"
12 #include "driver/gpio.h"
13 #include "hal/pcnt_hal.h"
14 #include "hal/pcnt_ll.h"
15 #include "hal/gpio_hal.h"
16 #include "soc/pcnt_periph.h"
17 #include "esp_rom_gpio.h"
18
19 #define PCNT_CHANNEL_ERR_STR "PCNT CHANNEL ERROR"
20 #define PCNT_UNIT_ERR_STR "PCNT UNIT ERROR"
21 #define PCNT_GPIO_ERR_STR "PCNT GPIO NUM ERROR"
22 #define PCNT_ADDRESS_ERR_STR "PCNT ADDRESS ERROR"
23 #define PCNT_PARAM_ERR_STR "PCNT PARAM ERROR"
24 #define PCNT_COUNT_MODE_ERR_STR "PCNT COUNTER MODE ERROR"
25 #define PCNT_CTRL_MODE_ERR_STR "PCNT CTRL MODE ERROR"
26 #define PCNT_EVT_TYPE_ERR_STR "PCNT value type error"
27 #define PCNT_LIMT_VAL_ERR_STR "PCNT limit value error"
28 #define PCNT_NUM_ERR_STR "PCNT num error"
29 #define PCNT_DRIVER_ERR_STR "PCNT driver error"
30
31 #define PCNT_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
32 #define PCNT_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
33
34 static const char *TAG = "pcnt(legacy)";
35
36 #define PCNT_CHECK(a, str, ret_val) ESP_RETURN_ON_FALSE(a, ret_val, TAG, "%s", str)
37
38 typedef struct {
39 pcnt_hal_context_t hal; /*!< PCNT hal context*/
40 } pcnt_obj_t;
41
42 static pcnt_obj_t *p_pcnt_obj[PCNT_PORT_MAX] = {0};
43
44 #define PCNT_OBJ_CHECK(pcnt_port) { \
45 PCNT_CHECK((pcnt_port < PCNT_PORT_MAX), PCNT_NUM_ERR_STR, ESP_ERR_INVALID_ARG); \
46 PCNT_CHECK((p_pcnt_obj[pcnt_port]), PCNT_DRIVER_ERR_STR, ESP_ERR_INVALID_STATE); \
47 }
48
49 typedef struct {
50 void(*fn)(void *args); /*!< isr function */
51 void *args; /*!< isr function args */
52 } pcnt_isr_func_t;
53
54 static pcnt_isr_func_t *pcnt_isr_func = NULL;
55 static pcnt_isr_handle_t pcnt_isr_service = NULL;
56 static portMUX_TYPE pcnt_spinlock = portMUX_INITIALIZER_UNLOCKED;
57
58 esp_err_t pcnt_isr_register(void (*fun)(void *), void *arg, int intr_alloc_flags, pcnt_isr_handle_t *handle);
59 esp_err_t pcnt_isr_unregister(pcnt_isr_handle_t handle);
60
_pcnt_set_mode(pcnt_port_t pcnt_port,pcnt_unit_t unit,pcnt_channel_t channel,pcnt_count_mode_t pos_mode,pcnt_count_mode_t neg_mode,pcnt_ctrl_mode_t hctrl_mode,pcnt_ctrl_mode_t lctrl_mode)61 static inline esp_err_t _pcnt_set_mode(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_channel_t channel, pcnt_count_mode_t pos_mode, pcnt_count_mode_t neg_mode, pcnt_ctrl_mode_t hctrl_mode, pcnt_ctrl_mode_t lctrl_mode)
62 {
63 PCNT_OBJ_CHECK(pcnt_port);
64 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
65 PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
66 PCNT_CHECK((pos_mode < PCNT_COUNT_MAX) && (neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
67 PCNT_CHECK((hctrl_mode < PCNT_MODE_MAX) && (lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
68
69 pcnt_ll_set_edge_action(p_pcnt_obj[pcnt_port]->hal.dev, unit, channel, pos_mode, neg_mode);
70 pcnt_ll_set_level_action(p_pcnt_obj[pcnt_port]->hal.dev, unit, channel, hctrl_mode, lctrl_mode);
71 return ESP_OK;
72 }
73
_pcnt_set_pin(pcnt_port_t pcnt_port,pcnt_unit_t unit,pcnt_channel_t channel,int pulse_io,int ctrl_io)74 static inline esp_err_t _pcnt_set_pin(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
75 {
76 PCNT_OBJ_CHECK(pcnt_port);
77 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
78 PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
79 PCNT_CHECK(GPIO_IS_VALID_GPIO(pulse_io) || pulse_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
80 PCNT_CHECK(GPIO_IS_VALID_GPIO(ctrl_io) || ctrl_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
81
82 if (pulse_io >= 0) {
83 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[pulse_io], PIN_FUNC_GPIO);
84 gpio_set_direction(pulse_io, GPIO_MODE_INPUT);
85 gpio_set_pull_mode(pulse_io, GPIO_PULLUP_ONLY);
86 esp_rom_gpio_connect_in_signal(pulse_io, pcnt_periph_signals.groups[pcnt_port].units[unit].channels[channel].pulse_sig, 0);
87 }
88
89 if (ctrl_io >= 0) {
90 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[ctrl_io], PIN_FUNC_GPIO);
91 gpio_set_direction(ctrl_io, GPIO_MODE_INPUT);
92 gpio_set_pull_mode(ctrl_io, GPIO_PULLUP_ONLY);
93 esp_rom_gpio_connect_in_signal(ctrl_io, pcnt_periph_signals.groups[pcnt_port].units[unit].channels[channel].control_sig, 0);
94 }
95
96 return ESP_OK;
97 }
98
_pcnt_get_counter_value(pcnt_port_t pcnt_port,pcnt_unit_t pcnt_unit,int16_t * count)99 static inline esp_err_t _pcnt_get_counter_value(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit, int16_t *count)
100 {
101 PCNT_OBJ_CHECK(pcnt_port);
102 PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
103 PCNT_CHECK(count != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
104 *count = pcnt_ll_get_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
105 return ESP_OK;
106 }
107
_pcnt_counter_pause(pcnt_port_t pcnt_port,pcnt_unit_t pcnt_unit)108 static inline esp_err_t _pcnt_counter_pause(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit)
109 {
110 PCNT_OBJ_CHECK(pcnt_port);
111 PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
112 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
113 pcnt_ll_stop_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
114 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
115 return ESP_OK;
116 }
117
_pcnt_counter_resume(pcnt_port_t pcnt_port,pcnt_unit_t pcnt_unit)118 static inline esp_err_t _pcnt_counter_resume(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit)
119 {
120 PCNT_OBJ_CHECK(pcnt_port);
121 PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
122 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
123 pcnt_ll_start_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
124 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
125 return ESP_OK;
126 }
127
_pcnt_counter_clear(pcnt_port_t pcnt_port,pcnt_unit_t pcnt_unit)128 static inline esp_err_t _pcnt_counter_clear(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit)
129 {
130 PCNT_OBJ_CHECK(pcnt_port);
131 PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
132 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
133 pcnt_ll_clear_count(p_pcnt_obj[pcnt_port]->hal.dev, pcnt_unit);
134 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
135 return ESP_OK;
136 }
137
_pcnt_intr_enable(pcnt_port_t pcnt_port,pcnt_unit_t pcnt_unit,bool enable)138 static inline esp_err_t _pcnt_intr_enable(pcnt_port_t pcnt_port, pcnt_unit_t pcnt_unit, bool enable)
139 {
140 PCNT_OBJ_CHECK(pcnt_port);
141 PCNT_CHECK(pcnt_unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
142 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
143 pcnt_ll_enable_intr(p_pcnt_obj[pcnt_port]->hal.dev, 1 << pcnt_unit, enable);
144 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
145 return ESP_OK;
146 }
147
_pcnt_event_enable(pcnt_port_t pcnt_port,pcnt_unit_t unit,pcnt_evt_type_t evt_type,bool enable)148 static inline esp_err_t _pcnt_event_enable(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_evt_type_t evt_type, bool enable)
149 {
150 PCNT_OBJ_CHECK(pcnt_port);
151 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
152 PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
153 switch (evt_type) {
154 case PCNT_EVT_THRES_1:
155 pcnt_ll_enable_thres_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, 1, enable);
156 break;
157 case PCNT_EVT_THRES_0:
158 pcnt_ll_enable_thres_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, 0, enable);
159 break;
160 case PCNT_EVT_L_LIM:
161 pcnt_ll_enable_low_limit_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
162 break;
163 case PCNT_EVT_H_LIM:
164 pcnt_ll_enable_high_limit_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
165 break;
166 case PCNT_EVT_ZERO:
167 pcnt_ll_enable_zero_cross_event(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
168 break;
169 default:
170 PCNT_CHECK(false, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
171 break;
172 }
173 return ESP_OK;
174 }
175
_pcnt_set_event_value(pcnt_port_t pcnt_port,pcnt_unit_t unit,pcnt_evt_type_t evt_type,int16_t value)176 static inline esp_err_t _pcnt_set_event_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
177 {
178 PCNT_OBJ_CHECK(pcnt_port);
179 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
180 PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
181 PCNT_CHECK(!(evt_type == PCNT_EVT_L_LIM && value > 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
182 PCNT_CHECK(!(evt_type == PCNT_EVT_H_LIM && value < 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
183 switch (evt_type) {
184 case PCNT_EVT_THRES_1:
185 pcnt_ll_set_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 1, value);
186 break;
187 case PCNT_EVT_THRES_0:
188 pcnt_ll_set_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 0, value);
189 break;
190 case PCNT_EVT_L_LIM:
191 pcnt_ll_set_low_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, value);
192 break;
193 case PCNT_EVT_H_LIM:
194 pcnt_ll_set_high_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, value);
195 break;
196 default:
197 break;
198 }
199 return ESP_OK;
200 }
201
_pcnt_get_event_value(pcnt_port_t pcnt_port,pcnt_unit_t unit,pcnt_evt_type_t evt_type,int16_t * value)202 static inline esp_err_t _pcnt_get_event_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
203 {
204 PCNT_OBJ_CHECK(pcnt_port);
205 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
206 PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
207 PCNT_CHECK(value != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
208 switch (evt_type) {
209 case PCNT_EVT_THRES_1:
210 *value = pcnt_ll_get_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 1);
211 break;
212 case PCNT_EVT_THRES_0:
213 *value = pcnt_ll_get_thres_value(p_pcnt_obj[pcnt_port]->hal.dev, unit, 0);
214 break;
215 case PCNT_EVT_L_LIM:
216 *value = pcnt_ll_get_low_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit);
217 break;
218 case PCNT_EVT_H_LIM:
219 *value = pcnt_ll_get_high_limit_value(p_pcnt_obj[pcnt_port]->hal.dev, unit);
220 break;
221 default:
222 break;
223 }
224 return ESP_OK;
225 }
226
_pcnt_get_event_status(pcnt_port_t pcnt_port,pcnt_unit_t unit,uint32_t * status)227 static inline esp_err_t _pcnt_get_event_status(pcnt_port_t pcnt_port, pcnt_unit_t unit, uint32_t *status)
228 {
229 PCNT_OBJ_CHECK(pcnt_port);
230 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
231 PCNT_CHECK(status != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
232
233 *status = pcnt_ll_get_unit_status(p_pcnt_obj[pcnt_port]->hal.dev, unit);
234 return ESP_OK;
235 }
236
_pcnt_set_filter_value(pcnt_port_t pcnt_port,pcnt_unit_t unit,uint16_t filter_val)237 static inline esp_err_t _pcnt_set_filter_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, uint16_t filter_val)
238 {
239 PCNT_OBJ_CHECK(pcnt_port);
240 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
241 PCNT_CHECK(filter_val < 1024, PCNT_PARAM_ERR_STR, ESP_ERR_INVALID_ARG);
242 pcnt_ll_set_glitch_filter_thres(p_pcnt_obj[pcnt_port]->hal.dev, unit, filter_val);
243 return ESP_OK;
244 }
245
_pcnt_get_filter_value(pcnt_port_t pcnt_port,pcnt_unit_t unit,uint16_t * filter_val)246 static inline esp_err_t _pcnt_get_filter_value(pcnt_port_t pcnt_port, pcnt_unit_t unit, uint16_t *filter_val)
247 {
248 PCNT_OBJ_CHECK(pcnt_port);
249 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
250 PCNT_CHECK(filter_val != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
251
252 *filter_val = (uint16_t)pcnt_ll_get_glitch_filter_thres(p_pcnt_obj[pcnt_port]->hal.dev, unit);
253 return ESP_OK;
254 }
255
_pcnt_filter_enable(pcnt_port_t pcnt_port,pcnt_unit_t unit,bool enable)256 static inline esp_err_t _pcnt_filter_enable(pcnt_port_t pcnt_port, pcnt_unit_t unit, bool enable)
257 {
258 PCNT_OBJ_CHECK(pcnt_port);
259 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
260 pcnt_ll_enable_glitch_filter(p_pcnt_obj[pcnt_port]->hal.dev, unit, enable);
261 return ESP_OK;
262 }
263
_pcnt_isr_handler_add(pcnt_port_t pcnt_port,pcnt_unit_t unit,void (* isr_handler)(void *),void * args)264 static inline esp_err_t _pcnt_isr_handler_add(pcnt_port_t pcnt_port, pcnt_unit_t unit, void(*isr_handler)(void *), void *args)
265 {
266 PCNT_OBJ_CHECK(pcnt_port);
267 PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed, call pcnt_install_isr_service() first", ESP_ERR_INVALID_STATE);
268 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, "PCNT unit error", ESP_ERR_INVALID_ARG);
269 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
270 _pcnt_intr_enable(PCNT_PORT_0, unit, false);
271
272 if (pcnt_isr_func) {
273 pcnt_isr_func[unit].fn = isr_handler;
274 pcnt_isr_func[unit].args = args;
275 }
276
277 _pcnt_intr_enable(PCNT_PORT_0, unit, true);
278 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
279 return ESP_OK;
280 }
281
_pcnt_isr_handler_remove(pcnt_port_t pcnt_port,pcnt_unit_t unit)282 static inline esp_err_t _pcnt_isr_handler_remove(pcnt_port_t pcnt_port, pcnt_unit_t unit)
283 {
284 PCNT_OBJ_CHECK(pcnt_port);
285 PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed", ESP_ERR_INVALID_STATE);
286 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, "PCNT unit error", ESP_ERR_INVALID_ARG);
287 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
288 _pcnt_intr_enable(PCNT_PORT_0, unit, false);
289
290 if (pcnt_isr_func) {
291 pcnt_isr_func[unit].fn = NULL;
292 pcnt_isr_func[unit].args = NULL;
293 }
294
295 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
296 return ESP_OK;
297 }
298
299 // pcnt interrupt service
pcnt_intr_service(void * arg)300 static void IRAM_ATTR pcnt_intr_service(void *arg)
301 {
302 uint32_t status = 0;
303 pcnt_port_t pcnt_port = (pcnt_port_t)arg;
304 status = pcnt_ll_get_intr_status(p_pcnt_obj[pcnt_port]->hal.dev);
305 pcnt_ll_clear_intr_status(p_pcnt_obj[pcnt_port]->hal.dev, status);
306
307 while (status) {
308 int unit = __builtin_ffs(status) - 1;
309 status &= ~(1 << unit);
310
311 if (pcnt_isr_func[unit].fn != NULL) {
312 (pcnt_isr_func[unit].fn)(pcnt_isr_func[unit].args);
313 }
314 }
315 }
316
_pcnt_isr_service_install(pcnt_port_t pcnt_port,int intr_alloc_flags)317 static inline esp_err_t _pcnt_isr_service_install(pcnt_port_t pcnt_port, int intr_alloc_flags)
318 {
319 PCNT_OBJ_CHECK(pcnt_port);
320 PCNT_CHECK(pcnt_isr_func == NULL, "ISR service already installed", ESP_ERR_INVALID_STATE);
321 esp_err_t ret = ESP_FAIL;
322 pcnt_isr_func = (pcnt_isr_func_t *) calloc(SOC_PCNT_UNITS_PER_GROUP, sizeof(pcnt_isr_func_t));
323
324 if (pcnt_isr_func == NULL) {
325 ret = ESP_ERR_NO_MEM;
326 } else {
327 ret = pcnt_isr_register(pcnt_intr_service, (void *)pcnt_port, intr_alloc_flags, &pcnt_isr_service);
328 if (ret != ESP_OK) {
329 ESP_LOGE(TAG, "pcnt isr registration failed, maybe you need `pcnt_isr_unregister` to unregister your isr");
330 free(pcnt_isr_func);
331 pcnt_isr_func = NULL;
332 }
333 }
334
335 return ret;
336 }
337
_pcnt_isr_service_uninstall(pcnt_port_t pcnt_port)338 static inline esp_err_t _pcnt_isr_service_uninstall(pcnt_port_t pcnt_port)
339 {
340 PCNT_OBJ_CHECK(pcnt_port);
341 PCNT_CHECK(pcnt_isr_func != NULL, "ISR Service not installed yet.", ESP_ERR_INVALID_STATE);
342 esp_err_t ret = ESP_FAIL;
343 ret = pcnt_isr_unregister(pcnt_isr_service);
344 free(pcnt_isr_func);
345 pcnt_isr_func = NULL;
346 pcnt_isr_service = NULL;
347
348 return ret;
349 }
350
_pcnt_unit_config(pcnt_port_t pcnt_port,const pcnt_config_t * pcnt_config)351 static inline esp_err_t _pcnt_unit_config(pcnt_port_t pcnt_port, const pcnt_config_t *pcnt_config)
352 {
353 PCNT_OBJ_CHECK(pcnt_port);
354 uint8_t unit = pcnt_config->unit;
355 uint8_t channel = pcnt_config->channel;
356 int input_io = pcnt_config->pulse_gpio_num;
357 int ctrl_io = pcnt_config->ctrl_gpio_num;
358
359 PCNT_CHECK(unit < SOC_PCNT_UNITS_PER_GROUP, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
360 PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
361 PCNT_CHECK(input_io < 0 || (GPIO_IS_VALID_GPIO(input_io) && (input_io != ctrl_io)), "PCNT pulse input io error", ESP_ERR_INVALID_ARG);
362 PCNT_CHECK(ctrl_io < 0 || GPIO_IS_VALID_GPIO(ctrl_io), "PCNT ctrl io error", ESP_ERR_INVALID_ARG);
363 PCNT_CHECK((pcnt_config->pos_mode < PCNT_COUNT_MAX) && (pcnt_config->neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
364 PCNT_CHECK((pcnt_config->hctrl_mode < PCNT_MODE_MAX) && (pcnt_config->lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
365 /*Enalbe hardware module*/
366 static bool pcnt_enable = false;
367 if (pcnt_enable == false) {
368 periph_module_reset(pcnt_periph_signals.groups[pcnt_port].module);
369 pcnt_enable = true;
370 }
371 periph_module_enable(pcnt_periph_signals.groups[pcnt_port].module);
372 /*Set counter range*/
373 _pcnt_set_event_value(pcnt_port, unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim);
374 _pcnt_set_event_value(pcnt_port, unit, PCNT_EVT_L_LIM, pcnt_config->counter_l_lim);
375 /*Default value after reboot is positive, we disable these events like others*/
376 _pcnt_event_enable(pcnt_port, unit, PCNT_EVT_H_LIM, false);
377 _pcnt_event_enable(pcnt_port, unit, PCNT_EVT_L_LIM, false);
378 _pcnt_event_enable(pcnt_port, unit, PCNT_EVT_ZERO, false);
379 _pcnt_filter_enable(pcnt_port, unit, false);
380 /*set pulse input and control mode*/
381 _pcnt_set_mode(pcnt_port, unit, channel, pcnt_config->pos_mode, pcnt_config->neg_mode, pcnt_config->hctrl_mode, pcnt_config->lctrl_mode);
382 /*Set pulse input and control pins*/
383 _pcnt_set_pin(pcnt_port, unit, channel, input_io, ctrl_io);
384 return ESP_OK;
385 }
386
pcnt_deinit(pcnt_port_t pcnt_port)387 esp_err_t pcnt_deinit(pcnt_port_t pcnt_port)
388 {
389 PCNT_OBJ_CHECK(pcnt_port);
390
391 heap_caps_free(p_pcnt_obj[pcnt_port]);
392 p_pcnt_obj[pcnt_port] = NULL;
393 return ESP_OK;
394 }
395
pcnt_init(pcnt_port_t pcnt_port)396 esp_err_t pcnt_init(pcnt_port_t pcnt_port)
397 {
398 PCNT_CHECK((pcnt_port < PCNT_PORT_MAX), PCNT_NUM_ERR_STR, ESP_ERR_INVALID_ARG);
399 PCNT_CHECK((p_pcnt_obj[pcnt_port]) == NULL, "pcnt driver already initted", ESP_ERR_INVALID_STATE);
400
401 p_pcnt_obj[pcnt_port] = (pcnt_obj_t *)heap_caps_calloc(1, sizeof(pcnt_obj_t), MALLOC_CAP_DEFAULT);
402
403 if (p_pcnt_obj[pcnt_port] == NULL) {
404 ESP_LOGE(TAG, "PCNT driver malloc error");
405 return ESP_FAIL;
406 }
407
408 pcnt_hal_init(&(p_pcnt_obj[pcnt_port]->hal), pcnt_port);
409 return ESP_OK;
410 }
411
pcnt_unit_config(const pcnt_config_t * pcnt_config)412 esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config)
413 {
414 esp_err_t ret;
415
416 if ((p_pcnt_obj[PCNT_PORT_0]) == NULL) {
417 ret = pcnt_init(PCNT_PORT_0);
418 if (ret != ESP_OK) {
419 return ret;
420 }
421 }
422 return _pcnt_unit_config(PCNT_PORT_0, pcnt_config);
423 }
424
pcnt_set_mode(pcnt_unit_t unit,pcnt_channel_t channel,pcnt_count_mode_t pos_mode,pcnt_count_mode_t neg_mode,pcnt_ctrl_mode_t hctrl_mode,pcnt_ctrl_mode_t lctrl_mode)425 esp_err_t pcnt_set_mode(pcnt_unit_t unit, pcnt_channel_t channel, pcnt_count_mode_t pos_mode, pcnt_count_mode_t neg_mode, pcnt_ctrl_mode_t hctrl_mode, pcnt_ctrl_mode_t lctrl_mode)
426 {
427 return _pcnt_set_mode(PCNT_PORT_0, unit, channel, pos_mode, neg_mode, hctrl_mode, lctrl_mode);
428 }
429
pcnt_set_pin(pcnt_unit_t unit,pcnt_channel_t channel,int pulse_io,int ctrl_io)430 esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
431 {
432 return _pcnt_set_pin(PCNT_PORT_0, unit, channel, pulse_io, ctrl_io);
433 }
434
pcnt_get_counter_value(pcnt_unit_t pcnt_unit,int16_t * count)435 esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t *count)
436 {
437 return _pcnt_get_counter_value(PCNT_PORT_0, pcnt_unit, count);
438 }
439
pcnt_counter_pause(pcnt_unit_t pcnt_unit)440 esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit)
441 {
442 return _pcnt_counter_pause(PCNT_PORT_0, pcnt_unit);
443 }
444
pcnt_counter_resume(pcnt_unit_t pcnt_unit)445 esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit)
446 {
447 return _pcnt_counter_resume(PCNT_PORT_0, pcnt_unit);
448 }
449
pcnt_counter_clear(pcnt_unit_t pcnt_unit)450 esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit)
451 {
452 return _pcnt_counter_clear(PCNT_PORT_0, pcnt_unit);
453 }
454
pcnt_intr_enable(pcnt_unit_t pcnt_unit)455 esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit)
456 {
457 return _pcnt_intr_enable(PCNT_PORT_0, pcnt_unit, true);
458 }
459
pcnt_intr_disable(pcnt_unit_t pcnt_unit)460 esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit)
461 {
462 return _pcnt_intr_enable(PCNT_PORT_0, pcnt_unit, false);
463 }
464
pcnt_event_enable(pcnt_unit_t unit,pcnt_evt_type_t evt_type)465 esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
466 {
467 return _pcnt_event_enable(PCNT_PORT_0, unit, evt_type, true);
468 }
469
pcnt_event_disable(pcnt_unit_t unit,pcnt_evt_type_t evt_type)470 esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
471 {
472 return _pcnt_event_enable(PCNT_PORT_0, unit, evt_type, false);
473 }
474
pcnt_set_event_value(pcnt_unit_t unit,pcnt_evt_type_t evt_type,int16_t value)475 esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
476 {
477 return _pcnt_set_event_value(PCNT_PORT_0, unit, evt_type, value);
478 }
479
pcnt_get_event_value(pcnt_unit_t unit,pcnt_evt_type_t evt_type,int16_t * value)480 esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
481 {
482 return _pcnt_get_event_value(PCNT_PORT_0, unit, evt_type, value);
483 }
484
pcnt_get_event_status(pcnt_unit_t unit,uint32_t * status)485 esp_err_t pcnt_get_event_status(pcnt_unit_t unit, uint32_t *status)
486 {
487 return _pcnt_get_event_status(PCNT_PORT_0, unit, status);
488 }
489
pcnt_set_filter_value(pcnt_unit_t unit,uint16_t filter_val)490 esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val)
491 {
492 return _pcnt_set_filter_value(PCNT_PORT_0, unit, filter_val);
493 }
494
pcnt_get_filter_value(pcnt_unit_t unit,uint16_t * filter_val)495 esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val)
496 {
497 return _pcnt_get_filter_value(PCNT_PORT_0, unit, filter_val);
498 }
499
pcnt_filter_enable(pcnt_unit_t unit)500 esp_err_t pcnt_filter_enable(pcnt_unit_t unit)
501 {
502 return _pcnt_filter_enable(PCNT_PORT_0, unit, true);
503 }
504
pcnt_filter_disable(pcnt_unit_t unit)505 esp_err_t pcnt_filter_disable(pcnt_unit_t unit)
506 {
507 return _pcnt_filter_enable(PCNT_PORT_0, unit, false);
508 }
509
pcnt_isr_unregister(pcnt_isr_handle_t handle)510 esp_err_t pcnt_isr_unregister(pcnt_isr_handle_t handle)
511 {
512 esp_err_t ret = ESP_FAIL;
513 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
514 ret = esp_intr_free(handle);
515 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
516 return ret;
517 }
518
pcnt_isr_register(void (* fun)(void *),void * arg,int intr_alloc_flags,pcnt_isr_handle_t * handle)519 esp_err_t pcnt_isr_register(void (*fun)(void *), void *arg, int intr_alloc_flags, pcnt_isr_handle_t *handle)
520 {
521 esp_err_t ret = ESP_FAIL;
522 PCNT_CHECK(fun != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
523 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
524 ret = esp_intr_alloc(pcnt_periph_signals.groups[0].irq, intr_alloc_flags, fun, arg, handle);
525 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
526 return ret;
527 }
528
pcnt_isr_handler_add(pcnt_unit_t unit,void (* isr_handler)(void *),void * args)529 esp_err_t pcnt_isr_handler_add(pcnt_unit_t unit, void(*isr_handler)(void *), void *args)
530 {
531 return _pcnt_isr_handler_add(PCNT_PORT_0, unit, isr_handler, args);
532 }
533
pcnt_isr_handler_remove(pcnt_unit_t unit)534 esp_err_t pcnt_isr_handler_remove(pcnt_unit_t unit)
535 {
536 return _pcnt_isr_handler_remove(PCNT_PORT_0, unit);
537 }
538
pcnt_isr_service_install(int intr_alloc_flags)539 esp_err_t pcnt_isr_service_install(int intr_alloc_flags)
540 {
541 return _pcnt_isr_service_install(PCNT_PORT_0, intr_alloc_flags);
542 }
543
pcnt_isr_service_uninstall(void)544 void pcnt_isr_service_uninstall(void)
545 {
546 _pcnt_isr_service_uninstall(PCNT_PORT_0);
547 }
548
549 /**
550 * @brief This function will be called during start up, to check that pulse_cnt driver is not running along with the legacy pcnt driver
551 */
552 __attribute__((constructor))
check_pcnt_driver_conflict(void)553 static void check_pcnt_driver_conflict(void)
554 {
555 // This function was declared as weak here. pulse_cnt driver has one implementation.
556 // So if pulse_cnt driver is not linked in, then `pcnt_new_unit` should be NULL at runtime.
557 extern __attribute__((weak)) esp_err_t pcnt_new_unit(const void *config, void **ret_unit);
558 if ((void *)pcnt_new_unit != NULL) {
559 ESP_EARLY_LOGE(TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver");
560 abort();
561 }
562 ESP_EARLY_LOGW(TAG, "legacy driver is deprecated, please migrate to `driver/pulse_cnt.h`");
563 }
564