pcnt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "esp_log.h"
  14. #include "esp_intr_alloc.h"
  15. #include "driver/pcnt.h"
  16. #include "driver/periph_ctrl.h"
  17. #define PCNT_CHANNEL_ERR_STR "PCNT CHANNEL ERROR"
  18. #define PCNT_UNIT_ERR_STR "PCNT UNIT ERROR"
  19. #define PCNT_GPIO_ERR_STR "PCNT GPIO NUM ERROR"
  20. #define PCNT_ADDRESS_ERR_STR "PCNT ADDRESS ERROR"
  21. #define PCNT_PARAM_ERR_STR "PCNT PARAM ERROR"
  22. #define PCNT_COUNT_MODE_ERR_STR "PCNT COUNTER MODE ERROR"
  23. #define PCNT_CTRL_MODE_ERR_STR "PCNT CTRL MODE ERROR"
  24. #define PCNT_EVT_TYPE_ERR_STR "PCNT value type error"
  25. static const char* PCNT_TAG = "pcnt";
  26. #define PCNT_CHECK(a, str, ret_val) \
  27. if (!(a)) { \
  28. ESP_LOGE(PCNT_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  29. return (ret_val); \
  30. }
  31. static portMUX_TYPE pcnt_spinlock = portMUX_INITIALIZER_UNLOCKED;
  32. #define PCNT_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
  33. #define PCNT_EXIT_CRITICAL(mux) portEXIT_CRITICAL(mux)
  34. #define PCNT_ENTER_CRITICAL_ISR(mux) portENTER_CRITICAL_ISR(mux)
  35. #define PCNT_EXIT_CRITICAL_ISR(mux) portEXIT_CRITICAL_ISR(mux)
  36. esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config)
  37. {
  38. uint8_t unit = pcnt_config->unit;
  39. uint8_t channel = pcnt_config->channel;
  40. int input_io = pcnt_config->pulse_gpio_num;
  41. int ctrl_io = pcnt_config->ctrl_gpio_num;
  42. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  43. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  44. PCNT_CHECK(input_io < 0 || (GPIO_IS_VALID_GPIO(input_io) && (input_io != ctrl_io)), "PCNT pluse input io error", ESP_ERR_INVALID_ARG);
  45. PCNT_CHECK(ctrl_io < 0 || GPIO_IS_VALID_GPIO(ctrl_io), "PCNT ctrl io error", ESP_ERR_INVALID_ARG);
  46. 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);
  47. 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);
  48. /*Enalbe hardware module*/
  49. periph_module_enable(PERIPH_PCNT_MODULE);
  50. /*Set counter range*/
  51. pcnt_set_event_value(unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim);
  52. pcnt_set_event_value(unit, PCNT_EVT_L_LIM, pcnt_config->counter_l_lim);
  53. /*Default value after reboot is positive, we disable these events like others*/
  54. pcnt_event_disable(unit, PCNT_EVT_H_LIM);
  55. pcnt_event_disable(unit, PCNT_EVT_L_LIM);
  56. pcnt_event_disable(unit, PCNT_EVT_ZERO);
  57. pcnt_filter_disable(unit);
  58. /*set pulse input and control mode*/
  59. pcnt_set_mode(unit, channel, pcnt_config->pos_mode, pcnt_config->neg_mode, pcnt_config->hctrl_mode, pcnt_config->lctrl_mode);
  60. /*Set pulse input and control pins*/
  61. pcnt_set_pin(unit, channel, input_io, ctrl_io);
  62. return ESP_OK;
  63. }
  64. 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)
  65. {
  66. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  67. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  68. PCNT_CHECK((pos_mode < PCNT_COUNT_MAX) && (neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  69. PCNT_CHECK((hctrl_mode < PCNT_MODE_MAX) && (lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG);
  70. if(channel == 0) {
  71. PCNT.conf_unit[unit].conf0.ch0_pos_mode = pos_mode;
  72. PCNT.conf_unit[unit].conf0.ch0_neg_mode = neg_mode;
  73. PCNT.conf_unit[unit].conf0.ch0_hctrl_mode = hctrl_mode;
  74. PCNT.conf_unit[unit].conf0.ch0_lctrl_mode = lctrl_mode;
  75. } else {
  76. PCNT.conf_unit[unit].conf0.ch1_pos_mode = pos_mode;
  77. PCNT.conf_unit[unit].conf0.ch1_neg_mode = neg_mode;
  78. PCNT.conf_unit[unit].conf0.ch1_hctrl_mode = hctrl_mode;
  79. PCNT.conf_unit[unit].conf0.ch1_lctrl_mode = lctrl_mode;
  80. }
  81. return ESP_OK;
  82. }
  83. esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
  84. {
  85. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  86. PCNT_CHECK(channel < PCNT_CHANNEL_MAX, PCNT_CHANNEL_ERR_STR, ESP_ERR_INVALID_ARG);
  87. PCNT_CHECK(GPIO_IS_VALID_GPIO(pulse_io) || pulse_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
  88. PCNT_CHECK(GPIO_IS_VALID_GPIO(ctrl_io) || ctrl_io < 0, PCNT_GPIO_ERR_STR, ESP_ERR_INVALID_ARG);
  89. int sig_base = (channel == 0) ? PCNT_SIG_CH0_IN0_IDX : PCNT_SIG_CH1_IN0_IDX;
  90. int ctrl_base = (channel == 0) ? PCNT_CTRL_CH0_IN0_IDX : PCNT_CTRL_CH1_IN0_IDX;
  91. if (unit > 4) {
  92. sig_base += 12; // GPIO matrix assignments have a gap between units 4 & 5
  93. ctrl_base += 12;
  94. }
  95. int input_sig_index = sig_base + (4 * unit);
  96. int ctrl_sig_index = ctrl_base + (4 * unit);
  97. if(pulse_io >= 0) {
  98. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[pulse_io], PIN_FUNC_GPIO);
  99. gpio_set_direction(pulse_io, GPIO_MODE_INPUT);
  100. gpio_set_pull_mode(pulse_io, GPIO_PULLUP_ONLY);
  101. gpio_matrix_in(pulse_io, input_sig_index, 0);
  102. }
  103. if(ctrl_io >= 0) {
  104. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[ctrl_io], PIN_FUNC_GPIO);
  105. gpio_set_direction(ctrl_io, GPIO_MODE_INPUT);
  106. gpio_set_pull_mode(ctrl_io, GPIO_PULLUP_ONLY);
  107. gpio_matrix_in(ctrl_io, ctrl_sig_index, 0);
  108. }
  109. return ESP_OK;
  110. }
  111. esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t* count)
  112. {
  113. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  114. PCNT_CHECK(count != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  115. *count = (int16_t) PCNT.cnt_unit[pcnt_unit].cnt_val;
  116. return ESP_OK;
  117. }
  118. esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit)
  119. {
  120. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  121. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  122. PCNT.ctrl.val |= BIT(PCNT_CNT_PAUSE_U0_S + (pcnt_unit * 2));
  123. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  124. return ESP_OK;
  125. }
  126. esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit)
  127. {
  128. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  129. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  130. PCNT.ctrl.val &= (~(BIT(PCNT_CNT_PAUSE_U0_S + (pcnt_unit * 2))));
  131. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  132. return ESP_OK;
  133. }
  134. esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit)
  135. {
  136. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  137. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  138. uint32_t reset_bit = BIT(PCNT_PLUS_CNT_RST_U0_S + (pcnt_unit * 2));
  139. PCNT.ctrl.val |= reset_bit;
  140. PCNT.ctrl.val &= ~reset_bit;
  141. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  142. return ESP_OK;
  143. }
  144. esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit)
  145. {
  146. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  147. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  148. PCNT.int_ena.val |= BIT(PCNT_CNT_THR_EVENT_U0_INT_ENA_S + pcnt_unit);
  149. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  150. return ESP_OK;
  151. }
  152. esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit)
  153. {
  154. PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  155. PCNT_ENTER_CRITICAL(&pcnt_spinlock);
  156. PCNT.int_ena.val &= (~(BIT(PCNT_CNT_THR_EVENT_U0_INT_ENA_S + pcnt_unit)));
  157. PCNT_EXIT_CRITICAL(&pcnt_spinlock);
  158. return ESP_OK;
  159. }
  160. esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
  161. {
  162. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  163. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  164. if(evt_type == PCNT_EVT_L_LIM) {
  165. PCNT.conf_unit[unit].conf0.thr_l_lim_en = 1;
  166. } else if(evt_type == PCNT_EVT_H_LIM) {
  167. PCNT.conf_unit[unit].conf0.thr_h_lim_en = 1;
  168. } else if(evt_type == PCNT_EVT_THRES_0) {
  169. PCNT.conf_unit[unit].conf0.thr_thres0_en = 1;
  170. } else if(evt_type == PCNT_EVT_THRES_1) {
  171. PCNT.conf_unit[unit].conf0.thr_thres1_en = 1;
  172. } else if(evt_type == PCNT_EVT_ZERO) {
  173. PCNT.conf_unit[unit].conf0.thr_zero_en = 1;
  174. }
  175. return ESP_OK;
  176. }
  177. esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
  178. {
  179. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  180. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  181. if(evt_type == PCNT_EVT_L_LIM) {
  182. PCNT.conf_unit[unit].conf0.thr_l_lim_en = 0;
  183. } else if(evt_type == PCNT_EVT_H_LIM) {
  184. PCNT.conf_unit[unit].conf0.thr_h_lim_en = 0;
  185. } else if(evt_type == PCNT_EVT_THRES_0) {
  186. PCNT.conf_unit[unit].conf0.thr_thres0_en = 0;
  187. } else if(evt_type == PCNT_EVT_THRES_1) {
  188. PCNT.conf_unit[unit].conf0.thr_thres1_en = 0;
  189. } else if(evt_type == PCNT_EVT_ZERO) {
  190. PCNT.conf_unit[unit].conf0.thr_zero_en = 0;
  191. }
  192. return ESP_OK;
  193. }
  194. esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
  195. {
  196. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  197. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  198. if(evt_type == PCNT_EVT_L_LIM) {
  199. PCNT.conf_unit[unit].conf2.cnt_l_lim = value;
  200. } else if(evt_type == PCNT_EVT_H_LIM) {
  201. PCNT.conf_unit[unit].conf2.cnt_h_lim = value;
  202. } else if(evt_type == PCNT_EVT_THRES_0) {
  203. PCNT.conf_unit[unit].conf1.cnt_thres0 = value;
  204. } else if(evt_type == PCNT_EVT_THRES_1) {
  205. PCNT.conf_unit[unit].conf1.cnt_thres1 = value;
  206. }
  207. return ESP_OK;
  208. }
  209. esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
  210. {
  211. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  212. PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG);
  213. PCNT_CHECK(value != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  214. if(evt_type == PCNT_EVT_L_LIM) {
  215. *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_l_lim;
  216. } else if(evt_type == PCNT_EVT_H_LIM) {
  217. *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_h_lim;
  218. } else if(evt_type == PCNT_EVT_THRES_0) {
  219. *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres0;
  220. } else if(evt_type == PCNT_EVT_THRES_1) {
  221. *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres1;
  222. } else {
  223. *value = 0;
  224. }
  225. return ESP_OK;
  226. }
  227. esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val)
  228. {
  229. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  230. PCNT_CHECK(filter_val < 1024, PCNT_PARAM_ERR_STR, ESP_ERR_INVALID_ARG);
  231. PCNT.conf_unit[unit].conf0.filter_thres = filter_val;
  232. return ESP_OK;
  233. }
  234. esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val)
  235. {
  236. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  237. PCNT_CHECK(filter_val != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  238. *filter_val = PCNT.conf_unit[unit].conf0.filter_thres;
  239. return ESP_OK;
  240. }
  241. esp_err_t pcnt_filter_enable(pcnt_unit_t unit)
  242. {
  243. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  244. PCNT.conf_unit[unit].conf0.filter_en = 1;
  245. return ESP_OK;
  246. }
  247. esp_err_t pcnt_filter_disable(pcnt_unit_t unit)
  248. {
  249. PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG);
  250. PCNT.conf_unit[unit].conf0.filter_en = 0;
  251. return ESP_OK;
  252. }
  253. esp_err_t pcnt_isr_register(void (*fun)(void*), void * arg, int intr_alloc_flags, pcnt_isr_handle_t *handle)
  254. {
  255. PCNT_CHECK(fun != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG);
  256. return esp_intr_alloc(ETS_PCNT_INTR_SOURCE, intr_alloc_flags, fun, arg, handle);
  257. }