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 /*******************************************************************************
16  * NOTICE
17  * The hal is not public api, don't use in application code.
18  * See readme.md in hal/include/hal/readme.md
19  ******************************************************************************/
20 
21 // The HAL layer for PCNT.
22 // There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters.
23 
24 #pragma once
25 
26 #include <stdio.h>
27 #include "soc/pcnt_periph.h"
28 #include "hal/pcnt_types.h"
29 #include "hal/pcnt_ll.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /**
36  * Context that should be maintained by both the driver and the HAL
37  */
38 
39 typedef struct {
40     pcnt_dev_t *dev;
41 } pcnt_hal_context_t;
42 
43 /**
44  * @brief Set PCNT counter mode
45  *
46  * @param hal Context of the HAL layer
47  * @param unit PCNT unit number
48  * @param channel PCNT channel number
49  * @param pos_mode Counter mode when detecting positive edge
50  * @param neg_mode Counter mode when detecting negative edge
51  * @param hctrl_mode Counter mode when control signal is high level
52  * @param lctrl_mode Counter mode when control signal is low level
53  */
54 #define pcnt_hal_set_mode(hal, unit, channel, pos_mode, neg_mode, hctrl_mode, lctrl_mode) pcnt_ll_set_mode((hal)->dev, unit, channel, pos_mode, neg_mode, hctrl_mode, lctrl_mode)
55 
56 /**
57  * @brief Get pulse counter value
58  *
59  * @param hal Context of the HAL layer
60  * @param unit  Pulse Counter unit number
61  * @param count Pointer to accept counter value
62  */
63 #define pcnt_hal_get_counter_value(hal, unit, count) pcnt_ll_get_counter_value((hal)->dev, unit, count)
64 
65 /**
66  * @brief Pause PCNT counter of PCNT unit
67  *
68  * @param hal Context of the HAL layer
69  * @param unit PCNT unit number
70  */
71 #define pcnt_hal_counter_pause(hal, unit) pcnt_ll_counter_pause((hal)->dev, unit)
72 
73 /**
74  * @brief Resume counting for PCNT counter
75  *
76  * @param hal Context of the HAL layer
77  * @param unit PCNT unit number, select from unit_t
78  */
79 #define pcnt_hal_counter_resume(hal, unit) pcnt_ll_counter_resume((hal)->dev, unit)
80 
81 /**
82  * @brief Clear and reset PCNT counter value to zero
83  *
84  * @param hal Context of the HAL layer
85  * @param  unit PCNT unit number, select from unit_t
86  */
87 #define pcnt_hal_counter_clear(hal, unit) pcnt_ll_counter_clear((hal)->dev, unit)
88 
89 /**
90  * @brief Enable PCNT interrupt for PCNT unit
91  *        @note
92  *        Each Pulse counter unit has five watch point events that share the same interrupt.
93  *        Configure events with pcnt_event_enable() and pcnt_event_disable()
94  *
95  * @param hal Context of the HAL layer
96  * @param unit PCNT unit number
97  */
98 #define pcnt_hal_intr_enable(hal, unit) pcnt_ll_intr_enable((hal)->dev, unit)
99 
100 /**
101  * @brief Disable PCNT interrupt for PCNT unit
102  *
103  * @param hal Context of the HAL layer
104  * @param unit PCNT unit number
105  */
106 #define pcnt_hal_intr_disable(hal, unit) pcnt_ll_intr_disable((hal)->dev, unit)
107 
108 /**
109  * @brief Get PCNT interrupt status
110  *
111  * @param  hal Context of the HAL layer
112  * @param  mask The interrupt status mask to be cleared. Pointer to accept value interrupt status mask.
113  */
114 #define pcnt_hal_get_intr_status(hal, mask)  pcnt_ll_get_intr_status((hal)->dev, mask)
115 
116 /**
117  * @brief Clear PCNT interrupt status
118  *
119  * @param hal Context of the HAL layer
120  * @param mask The interrupt status mask to be cleared.
121  */
122 #define pcnt_hal_clear_intr_status(hal, mask)  pcnt_ll_clear_intr_status((hal)->dev, mask)
123 
124 /**
125  * @brief Enable PCNT event of PCNT unit
126  *
127  * @param hal Context of the HAL layer
128  * @param unit PCNT unit number
129  * @param evt_type Watch point event type.
130  *                 All enabled events share the same interrupt (one interrupt per pulse counter unit).
131  */
132 #define pcnt_hal_event_enable(hal, unit, evt_type) pcnt_ll_event_enable((hal)->dev, unit, evt_type)
133 
134 /**
135  * @brief Disable PCNT event of PCNT unit
136  *
137  * @param hal Context of the HAL layer
138  * @param unit PCNT unit number
139  * @param evt_type Watch point event type.
140  *                 All enabled events share the same interrupt (one interrupt per pulse counter unit).
141  */
142 #define pcnt_hal_event_disable(hal, unit, evt_type) pcnt_ll_event_disable((hal)->dev, unit, evt_type)
143 
144 /**
145  * @brief Set PCNT event value of PCNT unit
146  *
147  * @param hal Context of the HAL layer
148  * @param unit PCNT unit number
149  * @param evt_type Watch point event type.
150  *                 All enabled events share the same interrupt (one interrupt per pulse counter unit).
151  *
152  * @param value Counter value for PCNT event
153  */
154 #define pcnt_hal_set_event_value(hal, unit, evt_type, value) pcnt_ll_set_event_value((hal)->dev, unit, evt_type, value)
155 
156 /**
157  * @brief Get PCNT event value of PCNT unit
158  *
159  * @param hal Context of the HAL layer
160  * @param unit PCNT unit number
161  * @param evt_type Watch point event type.
162  *                 All enabled events share the same interrupt (one interrupt per pulse counter unit).
163  * @param value Pointer to accept counter value for PCNT event
164  */
165 #define pcnt_hal_get_event_value(hal, unit, evt_type, value) pcnt_ll_get_event_value((hal)->dev, unit, evt_type, value)
166 
167 /**
168  * @brief Get PCNT event status
169  *
170  * @param hal Context of the HAL layer
171  * @param unit PCNT unit number
172  * @return event status word
173  */
174 #define pcnt_hal_get_event_status(hal, unit) pcnt_ll_get_event_status((hal)->dev, unit)
175 
176 /**
177  * @brief Set PCNT filter value
178  *
179  * @param hal Context of the HAL layer
180  * @param unit PCNT unit number
181  * @param filter_val PCNT signal filter value, counter in APB_CLK cycles.
182  *        Any pulses lasting shorter than this will be ignored when the filter is enabled.
183  *        @note
184  *        filter_val is a 10-bit value, so the maximum filter_val should be limited to 1023.
185  */
186 #define pcnt_hal_set_filter_value(hal, unit, filter_val) pcnt_ll_set_filter_value((hal)->dev, unit, filter_val)
187 
188 /**
189  * @brief Get PCNT filter value
190  *
191  * @param hal Context of the HAL layer
192  * @param unit PCNT unit number
193  * @param filter_val Pointer to accept PCNT filter value.
194  */
195 #define pcnt_hal_get_filter_value(hal, unit, filter_val) pcnt_ll_get_filter_value((hal)->dev, unit, filter_val)
196 
197 /**
198  * @brief Enable PCNT input filter
199  *
200  * @param hal Context of the HAL layer
201  * @param unit PCNT unit number
202  */
203 #define pcnt_hal_filter_enable(hal, unit) pcnt_ll_filter_enable((hal)->dev, unit)
204 
205 /**
206  * @brief Disable PCNT input filter
207  *
208  * @param hal Context of the HAL layer
209  * @param unit PCNT unit number
210  */
211 #define pcnt_hal_filter_disable(hal, unit) pcnt_ll_filter_disable((hal)->dev, unit)
212 
213 /**
214  * @brief Init the PCNT hal and set the PCNT to the default configuration. This function should be called first before other hal layer function is called
215  *
216  * @param hal Context of the HAL layer
217  * @param pcnt_num The uart port number, the max port number is (PCNT_NUM_MAX -1)
218  */
219 void pcnt_hal_init(pcnt_hal_context_t *hal, int pcnt_num);
220 
221 #ifdef __cplusplus
222 }
223 #endif
224