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