1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include "soc/soc_caps.h"
22 
23 #define PCNT_PIN_NOT_USED     (-1)  /*!< When selected for a pin, this pin will not be used */
24 
25 /**
26  * @brief PCNT port number, the max port number is (PCNT_PORT_MAX - 1).
27  */
28 typedef enum {
29     PCNT_PORT_0 = 0,                 /*!< PCNT port 0 */
30     PCNT_PORT_MAX,                   /*!< PCNT port max */
31 } pcnt_port_t;
32 
33 /**
34  * @brief Selection of all available PCNT units
35  */
36 typedef enum {
37     PCNT_UNIT_0 = 0,                 /*!< PCNT unit 0 */
38     PCNT_UNIT_1 = 1,                 /*!< PCNT unit 1 */
39     PCNT_UNIT_2 = 2,                 /*!< PCNT unit 2 */
40     PCNT_UNIT_3 = 3,                 /*!< PCNT unit 3 */
41 #if SOC_PCNT_UNIT_NUM > 4
42     PCNT_UNIT_4 = 4,                 /*!< PCNT unit 4 */
43     PCNT_UNIT_5 = 5,                 /*!< PCNT unit 5 */
44     PCNT_UNIT_6 = 6,                 /*!< PCNT unit 6 */
45     PCNT_UNIT_7 = 7,                 /*!< PCNT unit 7 */
46 #endif
47     PCNT_UNIT_MAX,
48 } pcnt_unit_t;
49 
50 /**
51  * @brief Selection of available modes that determine the counter's action depending on the state of the control signal's input GPIO
52  * @note  Configuration covers two actions, one for high, and one for low level on the control input
53  */
54 typedef enum {
55     PCNT_MODE_KEEP = 0,             /*!< Control mode: won't change counter mode*/
56     PCNT_MODE_REVERSE = 1,          /*!< Control mode: invert counter mode(increase -> decrease, decrease -> increase) */
57     PCNT_MODE_DISABLE = 2,          /*!< Control mode: Inhibit counter(counter value will not change in this condition) */
58     PCNT_MODE_MAX
59 } pcnt_ctrl_mode_t;
60 
61 /**
62  * @brief Selection of available modes that determine the counter's action on the edge of the pulse signal's input GPIO
63  * @note  Configuration covers two actions, one for positive, and one for negative edge on the pulse input
64  */
65 typedef enum {
66     PCNT_COUNT_DIS = 0,            /*!< Counter mode: Inhibit counter(counter value will not change in this condition) */
67     PCNT_COUNT_INC = 1,            /*!< Counter mode: Increase counter value */
68     PCNT_COUNT_DEC = 2,            /*!< Counter mode: Decrease counter value */
69     PCNT_COUNT_MAX
70 } pcnt_count_mode_t;
71 
72 /**
73  * @brief Selection of channels available for a single PCNT unit
74  */
75 typedef enum {
76     PCNT_CHANNEL_0 = 0x00,           /*!< PCNT channel 0 */
77     PCNT_CHANNEL_1 = 0x01,           /*!< PCNT channel 1 */
78     PCNT_CHANNEL_MAX,
79 } pcnt_channel_t;
80 
81 /**
82  * @brief Selection of counter's events the may trigger an interrupt
83  */
84 typedef enum {
85     PCNT_EVT_THRES_1 = BIT(2),           /*!< PCNT watch point event: threshold1 value event */
86     PCNT_EVT_THRES_0 = BIT(3),           /*!< PCNT watch point event: threshold0 value event */
87     PCNT_EVT_L_LIM = BIT(4),             /*!< PCNT watch point event: Minimum counter value */
88     PCNT_EVT_H_LIM = BIT(5),             /*!< PCNT watch point event: Maximum counter value */
89     PCNT_EVT_ZERO = BIT(6),              /*!< PCNT watch point event: counter value zero event */
90     PCNT_EVT_MAX
91 } pcnt_evt_type_t;
92 
93 /**
94  * @brief Pulse Counter configuration for a single channel
95  */
96 typedef struct {
97     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 */
98     int ctrl_gpio_num;              /*!< Control signal input GPIO number, a negative value will be ignored */
99     pcnt_ctrl_mode_t lctrl_mode;    /*!< PCNT low control mode */
100     pcnt_ctrl_mode_t hctrl_mode;    /*!< PCNT high control mode */
101     pcnt_count_mode_t pos_mode;     /*!< PCNT positive edge count mode */
102     pcnt_count_mode_t neg_mode;     /*!< PCNT negative edge count mode */
103     int16_t counter_h_lim;          /*!< Maximum counter value */
104     int16_t counter_l_lim;          /*!< Minimum counter value */
105     pcnt_unit_t unit;               /*!< PCNT unit number */
106     pcnt_channel_t channel;         /*!< the PCNT channel */
107 } pcnt_config_t;
108 
109 #ifdef __cplusplus
110 }
111 #endif
112