From 7571b8c2e4cc86f4746376288cbd255bc479d609 Mon Sep 17 00:00:00 2001 From: Wangjialin Date: Wed, 23 Nov 2016 18:10:45 +0800 Subject: [PATCH] driver: PCNT minor changes --- components/driver/include/driver/pcnt.h | 44 ++++++++++++++----------- components/driver/ledc.c | 1 + components/driver/pcnt.c | 31 ++++++++--------- docs/api/pcnt.rst | 44 +++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 docs/api/pcnt.rst diff --git a/components/driver/include/driver/pcnt.h b/components/driver/include/driver/pcnt.h index c49dc387ce..47bf25fe76 100644 --- a/components/driver/include/driver/pcnt.h +++ b/components/driver/include/driver/pcnt.h @@ -13,13 +13,10 @@ #include "soc/gpio_sig_map.h" #include "driver/gpio.h" - #ifdef __cplusplus extern "C" { #endif -#define PCNT_PIN_NOT_USED (-1) - typedef enum { PCNT_MODE_KEEP = 0, /*!< Control mode: won't change counter mode*/ PCNT_MODE_REVERSE = 1, /*!< Control mode: invert counter mode(increase -> decrease, decrease -> increase);*/ @@ -35,14 +32,14 @@ typedef enum { } pcnt_count_mode_t; typedef enum { - PCNT_UNIT0 = 0, /*!< PCNT unit0 */ - PCNT_UNIT1 = 1, /*!< PCNT unit1 */ - PCNT_UNIT2 = 2, /*!< PCNT unit2 */ - PCNT_UNIT3 = 3, /*!< PCNT unit3 */ - PCNT_UNIT4 = 4, /*!< PCNT unit4 */ - PCNT_UNIT5 = 5, /*!< PCNT unit5 */ - PCNT_UNIT6 = 6, /*!< PCNT unit6 */ - PCNT_UNIT7 = 7, /*!< PCNT unit7 */ + PCNT_UNIT_0 = 0, /*!< PCNT unit0 */ + PCNT_UNIT_1 = 1, /*!< PCNT unit1 */ + PCNT_UNIT_2 = 2, /*!< PCNT unit2 */ + PCNT_UNIT_3 = 3, /*!< PCNT unit3 */ + PCNT_UNIT_4 = 4, /*!< PCNT unit4 */ + PCNT_UNIT_5 = 5, /*!< PCNT unit5 */ + PCNT_UNIT_6 = 6, /*!< PCNT unit6 */ + PCNT_UNIT_7 = 7, /*!< PCNT unit7 */ PCNT_UNIT_MAX, } pcnt_unit_t; @@ -55,8 +52,8 @@ typedef enum{ typedef enum { PCNT_EVT_L_LIM = 0, /*!< PCNT watch point event: Minimum counter value */ PCNT_EVT_H_LIM = 1, /*!< PCNT watch point event: Maximum counter value*/ - PCNT_EVT_THRES0 = 2, /*!< PCNT watch point event: threshold0 value event*/ - PCNT_EVT_THRES1 = 3, /*!< PCNT watch point event: threshold1 value event*/ + PCNT_EVT_THRES_0 = 2, /*!< PCNT watch point event: threshold0 value event*/ + PCNT_EVT_THRES_1 = 3, /*!< PCNT watch point event: threshold1 value event*/ PCNT_EVT_ZERO = 4, /*!< PCNT watch point event: counter value zero event*/ PCNT_EVT_MAX } pcnt_evt_type_t; @@ -268,6 +265,8 @@ esp_err_t pcnt_filter_disable(pcnt_unit_t unit); * @param unit PCNT unit number * @param filter_val PCNT signal filter value, counter in APB_CLK cycles. * Any pulses lasting shorter than this will be ignored when the filter is enabled. + * @note + * filter_val is a 10-bit value, so the maximum filter_val should be limited to 1023. * * @return * - ESP_OK Success @@ -301,13 +300,18 @@ esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val); * - ESP_OK Success * - ESP_ERR_INVALID_ARG Parameter error */ -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); +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); -/***************************EXAMPLE********************************** +/** + * @addtogroup pcnt-examples * + * @{ * - * ----------------EXAMPLE OF LEDC SETTING --------------------- + * EXAMPLE OF PCNT CONFIGURATION + * ============================== * @code{c} * //1. Config PCNT unit * pcnt_config_t pcnt_config = { @@ -324,15 +328,17 @@ esp_err_t pcnt_set_mode(pcnt_unit_t unit, pcnt_channel_t channel, pcnt_count_mod * pcnt_unit_config(&pcnt_config); //init unit * @endcode * + * EXAMPLE OF PCNT EVENT SETTING + * ============================== * @code{c} * //2. Configure PCNT watchpoint event. - * pcnt_set_event_value(PCNT_UNIT0, PCNT_EVT_THRES1, 5); //set thres1 value - * pcnt_event_enable(PCNT_UNIT0, PCNT_EVT_THRES1); //enable thres1 event + * pcnt_set_event_value(PCNT_UNIT_0, PCNT_EVT_THRES_1, 5); //set thres1 value + * pcnt_event_enable(PCNT_UNIT_0, PCNT_EVT_THRES_1); //enable thres1 event * @endcode * * For more examples please refer to PCNT example code in IDF_PATH/examples * - *--------------------------END OF EXAMPLE -------------------------- + * @} */ #ifdef __cplusplus diff --git a/components/driver/ledc.c b/components/driver/ledc.c index 41eb82cbdd..cd57d8fcc2 100644 --- a/components/driver/ledc.c +++ b/components/driver/ledc.c @@ -191,6 +191,7 @@ esp_err_t ledc_channel_config(ledc_channel_config_t* ledc_conf) LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG); LEDC_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "ledc GPIO output number error", ESP_ERR_INVALID_ARG); LEDC_CHECK(timer_select <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG); + periph_module_enable(PERIPH_LEDC_MODULE); esp_err_t ret = ESP_OK; /*set channel parameters*/ /* channel parameters decide how the waveform looks like in one period*/ diff --git a/components/driver/pcnt.c b/components/driver/pcnt.c index bfcdb7da1e..b39d53a800 100644 --- a/components/driver/pcnt.c +++ b/components/driver/pcnt.c @@ -22,6 +22,7 @@ #define PCNT_PARAM_ERR_STR "PCNT PARAM ERROR" #define PCNT_COUNT_MODE_ERR_STR "PCNT COUNTER MODE ERROR" #define PCNT_CTRL_MODE_ERR_STR "PCNT CTRL MODE ERROR" +#define PCNT_EVT_TYPE_ERR_STR "PCNT value type error" #define PCNT_CHECK(a,str,ret_val) if(!(a)) { \ ESP_LOGE(PCNT_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ return (ret_val); \ @@ -112,7 +113,7 @@ esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, i esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t* count) { PCNT_CHECK(pcnt_unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG); - PCNT_CHECK(count != NULL, "PCNT ADDRESS ERROR", ESP_ERR_INVALID_ARG); + PCNT_CHECK(count != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG); *count = (int16_t) PCNT.cnt_unit[pcnt_unit].cnt_val; return ESP_OK; } @@ -165,14 +166,14 @@ esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit) esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type) { PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG); - PCNT_CHECK(evt_type < PCNT_EVT_MAX, "PCNT value type error", ESP_ERR_INVALID_ARG); + PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG); if(evt_type == PCNT_EVT_L_LIM) { PCNT.conf_unit[unit].conf0.thr_l_lim_en = 1; } else if(evt_type == PCNT_EVT_H_LIM) { PCNT.conf_unit[unit].conf0.thr_h_lim_en = 1; - } else if(evt_type == PCNT_EVT_THRES0) { + } else if(evt_type == PCNT_EVT_THRES_0) { PCNT.conf_unit[unit].conf0.thr_thres0_en = 1; - } else if(evt_type == PCNT_EVT_THRES1) { + } else if(evt_type == PCNT_EVT_THRES_1) { PCNT.conf_unit[unit].conf0.thr_thres1_en = 1; } else if(evt_type == PCNT_EVT_ZERO) { PCNT.conf_unit[unit].conf0.thr_zero_en = 1; @@ -183,14 +184,14 @@ esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type) esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type) { PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG); - PCNT_CHECK(evt_type < PCNT_EVT_MAX, "PCNT value type error", ESP_ERR_INVALID_ARG); + PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG); if(evt_type == PCNT_EVT_L_LIM) { PCNT.conf_unit[unit].conf0.thr_l_lim_en = 0; } else if(evt_type == PCNT_EVT_H_LIM) { PCNT.conf_unit[unit].conf0.thr_h_lim_en = 0; - } else if(evt_type == PCNT_EVT_THRES0) { + } else if(evt_type == PCNT_EVT_THRES_0) { PCNT.conf_unit[unit].conf0.thr_thres0_en = 0; - } else if(evt_type == PCNT_EVT_THRES1) { + } else if(evt_type == PCNT_EVT_THRES_1) { PCNT.conf_unit[unit].conf0.thr_thres1_en = 0; } else if(evt_type == PCNT_EVT_ZERO) { PCNT.conf_unit[unit].conf0.thr_zero_en = 0; @@ -201,14 +202,14 @@ esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type) esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value) { PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG); - PCNT_CHECK(evt_type < PCNT_EVT_MAX, "PCNT value type error", ESP_ERR_INVALID_ARG); + PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG); if(evt_type == PCNT_EVT_L_LIM) { PCNT.conf_unit[unit].conf2.cnt_l_lim = value; } else if(evt_type == PCNT_EVT_H_LIM) { PCNT.conf_unit[unit].conf2.cnt_h_lim = value; - } else if(evt_type == PCNT_EVT_THRES0) { + } else if(evt_type == PCNT_EVT_THRES_0) { PCNT.conf_unit[unit].conf1.cnt_thres0 = value; - } else if(evt_type == PCNT_EVT_THRES1) { + } else if(evt_type == PCNT_EVT_THRES_1) { PCNT.conf_unit[unit].conf1.cnt_thres1 = value; } return ESP_OK; @@ -217,16 +218,16 @@ esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16 esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value) { PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG); - PCNT_CHECK(evt_type < PCNT_EVT_MAX, "PCNT value type error", ESP_ERR_INVALID_ARG); - PCNT_CHECK(value != NULL, "PCNT ADDRESS ERROR", ESP_ERR_INVALID_ARG); + PCNT_CHECK(evt_type < PCNT_EVT_MAX, PCNT_EVT_TYPE_ERR_STR, ESP_ERR_INVALID_ARG); + PCNT_CHECK(value != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG); if(evt_type == PCNT_EVT_L_LIM) { *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_l_lim; } else if(evt_type == PCNT_EVT_H_LIM) { *value = (int16_t) PCNT.conf_unit[unit].conf2.cnt_h_lim; - } else if(evt_type == PCNT_EVT_THRES0) { + } else if(evt_type == PCNT_EVT_THRES_0) { *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres0; - } else if(evt_type == PCNT_EVT_THRES1) { + } else if(evt_type == PCNT_EVT_THRES_1) { *value = (int16_t) PCNT.conf_unit[unit].conf1.cnt_thres1; } else { *value = 0; @@ -245,7 +246,7 @@ esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val) esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val) { PCNT_CHECK(unit < PCNT_UNIT_MAX, PCNT_UNIT_ERR_STR, ESP_ERR_INVALID_ARG); - PCNT_CHECK(filter_val != NULL, "PCNT ADDRESS ERROR", ESP_ERR_INVALID_ARG); + PCNT_CHECK(filter_val != NULL, PCNT_ADDRESS_ERR_STR, ESP_ERR_INVALID_ARG); *filter_val = PCNT.conf_unit[unit].conf0.filter_thres; return ESP_OK; diff --git a/docs/api/pcnt.rst b/docs/api/pcnt.rst new file mode 100644 index 0000000000..177027f62e --- /dev/null +++ b/docs/api/pcnt.rst @@ -0,0 +1,44 @@ +Macros +^^^^^^ + + +Type Definitions +^^^^^^^^^^^^^^^^ + + +Enumerations +^^^^^^^^^^^^ + +.. doxygenenum:: pcnt_ctrl_mode_t +.. doxygenenum:: pcnt_count_mode_t +.. doxygenenum:: pcnt_unit_t +.. doxygenenum:: pcnt_channel_t +.. doxygenenum:: pcnt_evt_type_t + +Structures +^^^^^^^^^^ + +.. doxygenstruct:: pcnt_config_t + +Functions +^^^^^^^^^ + +.. doxygenfunction:: pcnt_unit_config +.. doxygenfunction:: pcnt_get_counter_value +.. doxygenfunction:: pcnt_counter_pause +.. doxygenfunction:: pcnt_counter_resume +.. doxygenfunction:: pcnt_counter_clear +.. doxygenfunction:: pcnt_intr_enable +.. doxygenfunction:: pcnt_intr_disable +.. doxygenfunction:: pcnt_event_enable +.. doxygenfunction:: pcnt_event_disable +.. doxygenfunction:: pcnt_set_event_value +.. doxygenfunction:: pcnt_get_event_value +.. doxygenfunction:: pcnt_isr_register +.. doxygenfunction:: pcnt_set_pin +.. doxygenfunction:: pcnt_filter_enable +.. doxygenfunction:: pcnt_filter_disable +.. doxygenfunction:: pcnt_set_filter_value +.. doxygenfunction:: pcnt_get_filter_value +.. doxygenfunction:: pcnt_set_mode +