1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include "esp_intr_alloc.h"
10 #include "soc/soc_caps.h"
11 #include "hal/pcnt_types.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #define PCNT_PIN_NOT_USED     (-1)  /*!< When selected for a pin, this pin will not be used */
18 
19 /**
20  * @brief PCNT interrupt handle
21  */
22 typedef intr_handle_t pcnt_isr_handle_t;
23 
24 /**
25  * @brief PCNT port number, the max port number is (PCNT_PORT_MAX - 1).
26  */
27 typedef enum {
28     PCNT_PORT_0,   /*!< PCNT port 0 */
29     PCNT_PORT_MAX, /*!< PCNT port max */
30 } pcnt_port_t;
31 
32 /**
33  * @brief Selection of all available PCNT units
34  */
35 typedef enum {
36     PCNT_UNIT_0, /*!< PCNT unit 0 */
37     PCNT_UNIT_1, /*!< PCNT unit 1 */
38     PCNT_UNIT_2, /*!< PCNT unit 2 */
39     PCNT_UNIT_3, /*!< PCNT unit 3 */
40 #if SOC_PCNT_UNITS_PER_GROUP > 4
41     PCNT_UNIT_4, /*!< PCNT unit 4 */
42     PCNT_UNIT_5, /*!< PCNT unit 5 */
43     PCNT_UNIT_6, /*!< PCNT unit 6 */
44     PCNT_UNIT_7, /*!< PCNT unit 7 */
45 #endif
46     PCNT_UNIT_MAX,
47 } pcnt_unit_t;
48 
49 /**
50  * @brief Selection of channels available for a single PCNT unit
51  */
52 typedef enum {
53     PCNT_CHANNEL_0, /*!< PCNT channel 0 */
54     PCNT_CHANNEL_1, /*!< PCNT channel 1 */
55     PCNT_CHANNEL_MAX,
56 } pcnt_channel_t;
57 
58 /**
59  * @brief Selection of counter's events the may trigger an interrupt
60  */
61 typedef enum {
62     PCNT_EVT_THRES_1 = 1 << 2, /*!< PCNT watch point event: threshold1 value event */
63     PCNT_EVT_THRES_0 = 1 << 3, /*!< PCNT watch point event: threshold0 value event */
64     PCNT_EVT_L_LIM = 1 << 4,   /*!< PCNT watch point event: Minimum counter value */
65     PCNT_EVT_H_LIM = 1 << 5,   /*!< PCNT watch point event: Maximum counter value */
66     PCNT_EVT_ZERO = 1 << 6,    /*!< PCNT watch point event: counter value zero event */
67     PCNT_EVT_MAX
68 } pcnt_evt_type_t;
69 
70 /**
71  * @brief Selection of available modes that determine the counter's action depending on the state of the control signal's input GPIO
72  * @note  Configuration covers two actions, one for high, and one for low level on the control input
73  */
74 typedef pcnt_channel_level_action_t pcnt_ctrl_mode_t;
75 #define PCNT_MODE_KEEP    PCNT_CHANNEL_LEVEL_ACTION_KEEP    /*!< Control mode: won't change counter mode*/
76 #define PCNT_MODE_REVERSE PCNT_CHANNEL_LEVEL_ACTION_INVERSE /*!< Control mode: invert counter mode(increase -> decrease, decrease -> increase) */
77 #define PCNT_MODE_DISABLE PCNT_CHANNEL_LEVEL_ACTION_HOLD    /*!< Control mode: Inhibit counter(counter value will not change in this condition) */
78 #define PCNT_MODE_MAX     3
79 
80 /**
81  * @brief Selection of available modes that determine the counter's action on the edge of the pulse signal's input GPIO
82  * @note  Configuration covers two actions, one for positive, and one for negative edge on the pulse input
83  */
84 typedef pcnt_channel_edge_action_t pcnt_count_mode_t;
85 #define PCNT_COUNT_DIS PCNT_CHANNEL_EDGE_ACTION_HOLD     /*!< Counter mode: Inhibit counter(counter value will not change in this condition) */
86 #define PCNT_COUNT_INC PCNT_CHANNEL_EDGE_ACTION_INCREASE /*!< Counter mode: Increase counter value */
87 #define PCNT_COUNT_DEC PCNT_CHANNEL_EDGE_ACTION_DECREASE /*!< Counter mode: Decrease counter value */
88 #define PCNT_COUNT_MAX 3
89 
90 /**
91  * @brief Pulse Counter configuration for a single channel
92  */
93 typedef struct {
94     int pulse_gpio_num;          /*!< Pulse input GPIO number, if you want to use GPIO16, enter pulse_gpio_num = 16, a negative value will be ignored */
95     int ctrl_gpio_num;           /*!< Control signal input GPIO number, a negative value will be ignored */
96     pcnt_ctrl_mode_t lctrl_mode; /*!< PCNT low control mode */
97     pcnt_ctrl_mode_t hctrl_mode; /*!< PCNT high control mode */
98     pcnt_count_mode_t pos_mode;  /*!< PCNT positive edge count mode */
99     pcnt_count_mode_t neg_mode;  /*!< PCNT negative edge count mode */
100     int16_t counter_h_lim;       /*!< Maximum counter value */
101     int16_t counter_l_lim;       /*!< Minimum counter value */
102     pcnt_unit_t unit;            /*!< PCNT unit number */
103     pcnt_channel_t channel;      /*!< the PCNT channel */
104 } pcnt_config_t;
105 
106 #ifdef __cplusplus
107 }
108 #endif
109