1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "esp_intr_alloc.h"
16 #include "driver/pcnt.h"
17 #include "driver/periph_ctrl.h"
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"
29 #define PCNT_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
30 #define PCNT_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
32 #define PCNT_CHECK(a, str, ret_val) \
34 ESP_LOGE(PCNT_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
39 void(*fn)(void *args); /*!< isr function */
40 void* args; /*!< isr function args */
43 static pcnt_isr_func_t *pcnt_isr_func = NULL;
44 static pcnt_isr_handle_t pcnt_isr_service = NULL;
45 static portMUX_TYPE pcnt_spinlock = portMUX_INITIALIZER_UNLOCKED;
46 static const char* PCNT_TAG = "pcnt";
48 esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config)
50 uint8_t unit = pcnt_config->unit;
51 uint8_t channel = pcnt_config->channel;
52 int input_io = pcnt_config->pulse_gpio_num;
53 int ctrl_io = pcnt_config->ctrl_gpio_num;
55 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
56 PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
57 PCNT_CHECK(input_io < 0 || (GPIO_IS_VALID_GPIO(input_io) && (input_io != ctrl_io)), "PCNT pluse input io error", ESP_ERR_INVALID_ARG);
58 PCNT_CHECK(ctrl_io < 0 || GPIO_IS_VALID_GPIO(ctrl_io), "PCNT ctrl io error", ESP_ERR_INVALID_ARG);
59 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);
60 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);
61 /*Enalbe hardware module*/
62 periph_module_enable(PERIPH_PCNT_MODULE);
64 pcnt_set_event_value(unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim);
65 pcnt_set_event_value(unit, PCNT_EVT_L_LIM, pcnt_config->counter_l_lim);
66 /*Default value after reboot is positive, we disable these events like others*/
67 pcnt_event_disable(unit, PCNT_EVT_H_LIM);
68 pcnt_event_disable(unit, PCNT_EVT_L_LIM);
69 pcnt_event_disable(unit, PCNT_EVT_ZERO);
70 pcnt_filter_disable(unit);
71 /*set pulse input and control mode*/
72 pcnt_set_mode(unit, channel, pcnt_config->pos_mode, pcnt_config->neg_mode, pcnt_config->hctrl_mode, pcnt_config->lctrl_mode);
73 /*Set pulse input and control pins*/
74 pcnt_set_pin(unit, channel, input_io, ctrl_io);
78 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)
80 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
81 PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
82 PCNT_CHECK((pos_mode < PCNT_COUNT_MAX) && (neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
83 PCNT_CHECK((hctrl_mode < PCNT_MODE_MAX) && (lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
86 PCNT.conf_unit[unit].conf0.ch0_pos_mode = pos_mode;
87 PCNT.conf_unit[unit].conf0.ch0_neg_mode = neg_mode;
88 PCNT.conf_unit[unit].conf0.ch0_hctrl_mode = hctrl_mode;
89 PCNT.conf_unit[unit].conf0.ch0_lctrl_mode = lctrl_mode;
91 PCNT.conf_unit[unit].conf0.ch1_pos_mode = pos_mode;
92 PCNT.conf_unit[unit].conf0.ch1_neg_mode = neg_mode;
93 PCNT.conf_unit[unit].conf0.ch1_hctrl_mode = hctrl_mode;
94 PCNT.conf_unit[unit].conf0.ch1_lctrl_mode = lctrl_mode;
99 esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
101 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
102 PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
103 PCNT_CHECK(GPIO_IS_VALID_GPIO(pulse_io) || pulse_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
104 PCNT_CHECK(GPIO_IS_VALID_GPIO(ctrl_io) || ctrl_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
106 int sig_base = (channel == 0) ? PCNT_SIG_CH0_IN0_IDX : PCNT_SIG_CH1_IN0_IDX;
107 int ctrl_base = (channel == 0) ? PCNT_CTRL_CH0_IN0_IDX : PCNT_CTRL_CH1_IN0_IDX;
109 sig_base += 12; // GPIO matrix assignments have a gap between units 4 & 5
112 int input_sig_index = sig_base + (4 * unit);
113 int ctrl_sig_index = ctrl_base + (4 * unit);
116 PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[pulse_io], PIN_FUNC_GPIO);
117 gpio_set_direction(pulse_io, GPIO_MODE_INPUT);
118 gpio_set_pull_mode(pulse_io, GPIO_PULLUP_ONLY);
119 gpio_matrix_in(pulse_io, input_sig_index, 0);
122 PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[ctrl_io], PIN_FUNC_GPIO);
123 gpio_set_direction(ctrl_io, GPIO_MODE_INPUT);
124 gpio_set_pull_mode(ctrl_io, GPIO_PULLUP_ONLY);
125 gpio_matrix_in(ctrl_io, ctrl_sig_index, 0);
130 esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t* count)
132 PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
133 PCNT_CHECK(count != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
134 *count = (int16_t) PCNT.cnt_unit[pcnt_unit].cnt_val;
138 esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit)
140 PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
141 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
142 PCNT.ctrl.val |= BIT(PCNT_CNT_PAUSE_U0_S + (pcnt_unit * 2));
143 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
147 esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit)
149 PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
150 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
151 PCNT.ctrl.val &= (~(BIT(PCNT_CNT_PAUSE_U0_S + (pcnt_unit * 2))));
152 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
156 esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit)
158 PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
159 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
160 uint32_t reset_bit = BIT(PCNT_PLUS_CNT_RST_U0_S + (pcnt_unit * 2));
161 PCNT.ctrl.val |= reset_bit;
162 PCNT.ctrl.val &= ~reset_bit;
163 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
167 esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit)
169 PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
170 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
171 PCNT.int_ena.val |= BIT(PCNT_CNT_THR_EVENT_U0_INT_ENA_S + pcnt_unit);
172 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
176 esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit)
178 PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
179 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
180 PCNT.int_ena.val &= (~(BIT(PCNT_CNT_THR_EVENT_U0_INT_ENA_S + pcnt_unit)));
181 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
185 esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
187 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
188 PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
189 if(evt_type == PCNT_EVT_L_LIM) {
190 PCNT.conf_unit[unit].conf0.thr_l_lim_en = 1;
191 } else if(evt_type == PCNT_EVT_H_LIM) {
192 PCNT.conf_unit[unit].conf0.thr_h_lim_en = 1;
193 } else if(evt_type == PCNT_EVT_THRES_0) {
194 PCNT.conf_unit[unit].conf0.thr_thres0_en = 1;
195 } else if(evt_type == PCNT_EVT_THRES_1) {
196 PCNT.conf_unit[unit].conf0.thr_thres1_en = 1;
197 } else if(evt_type == PCNT_EVT_ZERO) {
198 PCNT.conf_unit[unit].conf0.thr_zero_en = 1;
203 esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
205 PCNT_CHECK(unit < PCNT_UNIT_MAX, 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 if(evt_type == PCNT_EVT_L_LIM) {
208 PCNT.conf_unit[unit].conf0.thr_l_lim_en = 0;
209 } else if(evt_type == PCNT_EVT_H_LIM) {
210 PCNT.conf_unit[unit].conf0.thr_h_lim_en = 0;
211 } else if(evt_type == PCNT_EVT_THRES_0) {
212 PCNT.conf_unit[unit].conf0.thr_thres0_en = 0;
213 } else if(evt_type == PCNT_EVT_THRES_1) {
214 PCNT.conf_unit[unit].conf0.thr_thres1_en = 0;
215 } else if(evt_type == PCNT_EVT_ZERO) {
216 PCNT.conf_unit[unit].conf0.thr_zero_en = 0;
221 esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
223 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
224 PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
225 PCNT_CHECK(!(evt_type == PCNT_EVT_L_LIM && value > 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
226 PCNT_CHECK(!(evt_type == PCNT_EVT_H_LIM && value < 0), PCNT_LIMT_VAL_ERR_STR, ESP_ERR_INVALID_ARG);
227 if(evt_type == PCNT_EVT_L_LIM) {
228 PCNT.conf_unit[unit].conf2.cnt_l_lim = value;
229 } else if(evt_type == PCNT_EVT_H_LIM) {
230 PCNT.conf_unit[unit].conf2.cnt_h_lim = value;
231 } else if(evt_type == PCNT_EVT_THRES_0) {
232 PCNT.conf_unit[unit].conf1.cnt_thres0 = value;
233 } else if(evt_type == PCNT_EVT_THRES_1) {
234 PCNT.conf_unit[unit].conf1.cnt_thres1 = value;
239 esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
241 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
242 PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
243 PCNT_CHECK(value != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
245 if(evt_type == PCNT_EVT_L_LIM) {
246 *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_l_lim;
247 } else if(evt_type == PCNT_EVT_H_LIM) {
248 *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_h_lim;
249 } else if(evt_type == PCNT_EVT_THRES_0) {
250 *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres0;
251 } else if(evt_type == PCNT_EVT_THRES_1) {
252 *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres1;
259 esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val)
261 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
262 PCNT_CHECK(filter_val < 1024, PCNT_PARAM_ERR_STR, ESP_ERR_INVALID_ARG);
263 PCNT.conf_unit[unit].conf0.filter_thres = filter_val;
267 esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val)
269 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
270 PCNT_CHECK(filter_val != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
272 *filter_val = PCNT.conf_unit[unit].conf0.filter_thres;
276 esp_err_t pcnt_filter_enable(pcnt_unit_t unit)
278 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
279 PCNT.conf_unit[unit].conf0.filter_en = 1;
283 esp_err_t pcnt_filter_disable(pcnt_unit_t unit)
285 PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
286 PCNT.conf_unit[unit].conf0.filter_en = 0;
290 esp_err_t pcnt_isr_register(void (*fun)(void*), void * arg, int intr_alloc_flags, pcnt_isr_handle_t *handle)
292 PCNT_CHECK(fun != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
293 return esp_intr_alloc(ETS_PCNT_INTR_SOURCE, intr_alloc_flags, fun, arg, handle);
296 // pcnt interrupt service
297 static void IRAM_ATTR pcnt_intr_service(void* arg)
299 uint32_t intr_status = PCNT.int_st.val;
300 for (int unit = 0; unit < PCNT_UNIT_MAX; unit++) {
301 if (intr_status & (BIT(unit))) {
302 if (pcnt_isr_func[unit].fn != NULL) {
303 (pcnt_isr_func[unit].fn)(pcnt_isr_func[unit].args);
305 PCNT.int_clr.val = BIT(unit);
310 esp_err_t pcnt_isr_handler_add(pcnt_unit_t unit, void(*isr_handler)(void *), void *args)
312 PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed, call pcnt_install_isr_service() first", ESP_ERR_INVALID_STATE);
313 PCNT_CHECK(unit < PCNT_UNIT_MAX, "PCNT unit error", ESP_ERR_INVALID_ARG);
314 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
315 pcnt_intr_disable(unit);
317 pcnt_isr_func[unit].fn = isr_handler;
318 pcnt_isr_func[unit].args = args;
320 pcnt_intr_enable(unit);
321 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
325 esp_err_t pcnt_isr_handler_remove(pcnt_unit_t unit)
327 PCNT_CHECK(pcnt_isr_func != NULL, "ISR service is not installed", ESP_ERR_INVALID_STATE);
328 PCNT_CHECK(unit < PCNT_UNIT_MAX, "PCNT unit error", ESP_ERR_INVALID_ARG);
329 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
330 pcnt_intr_disable(unit);
332 pcnt_isr_func[unit].fn = NULL;
333 pcnt_isr_func[unit].args = NULL;
335 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
339 esp_err_t pcnt_isr_service_install(int intr_alloc_flags)
341 PCNT_CHECK(pcnt_isr_func == NULL, "ISR service already installed", ESP_ERR_INVALID_STATE);
342 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
343 esp_err_t ret = ESP_FAIL;
344 pcnt_isr_func = (pcnt_isr_func_t*) calloc(PCNT_UNIT_MAX, sizeof(pcnt_isr_func_t));
345 if (pcnt_isr_func == NULL) {
346 ret = ESP_ERR_NO_MEM;
348 ret = pcnt_isr_register(pcnt_intr_service, NULL, intr_alloc_flags, &pcnt_isr_service);
350 PCNT_EXIT_CRITICAL(&pcnt_spinlock);
354 void pcnt_isr_service_uninstall(void)
356 if (pcnt_isr_func == NULL) {
359 PCNT_ENTER_CRITICAL(&pcnt_spinlock);
360 esp_intr_free(pcnt_isr_service);
362 pcnt_isr_func = NULL;
363 pcnt_isr_service = NULL;
364 PCNT_EXIT_CRITICAL(&pcnt_spinlock);