2018年11月30日 星期五

DIY - ESP32:ESP32 PCNT API 函數結構件及其它(四十二)

DIY - ESP32:ESP32 PCNT API 函數結構件及其它(四十二):

除了 ESP32 PCNT(Pulse Counter / 脈衝計數器)API 函數模塊外,還提供了結構件(Structures)、巨集(Marco)、 泛型型別定義(Type Definitions)和枚舉(Enumerations)。

1. Structure
1.1. struct pcnt_config_t
Pulse Counter configuration for a single channel.
Public Members
Description
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
int ctrl_gpio_num
Control signal input GPIO number, a negative value will be ignored
pcnt_ctrl_mode_t lctrl_mode
PCNT low control mode
pcnt_ctrl_mode_t hctrl_mode
PCNT high control mode
pcnt_count_mode_t pos_mode
PCNT positive edge count mode
pcnt_count_mode_t neg_mode
PCNT negative edge count mode
int16_t counter_h_lim
Maximum counter value
int16_t counter_l_lim
Minimum counter value
pcnt_unit_t unit
PCNT unit number
pcnt_channel_t channel
the PCNT channel

範例:

pcnt_config_t pcnt_config = {

.pulse_gpio_num = 4,              //set gpio4 as pulse input gpio
.ctrl_gpio_num = 5,                 //set gpio5 as control gpio
.channel = PCNT_CHANNEL_0,                    //use unit 0 channel 0
.lctrl_mode = PCNT_MODE_REVERSE,        //when control signal is low, reverse the primary counter mode(inc->dec/dec->inc)
.hctrl_mode = PCNT_MODE_KEEP,   //when control signal is high, keep the primary counter mode
.pos_mode = PCNT_COUNT_INC,     //increment the counter
.neg_mode = PCNT_COUNT_DIS,     //keep the counter value
.counter_h_lim = 10,
.counter_l_lim = -10,
};

2. Macros
Macros
Description
PCNT_PIN_NOT_USED
When selected for a pin, this pin will not be used

3. Type Definitions
Type Definitions
Description
typedef intr_handle_t pcnt_isr_handle_t

4. Enumerations
4.1. enum pcnt_ctrl_mode_t
Selection of available modes that determine the counter’s action depending on the state of the control signal’s input GPIO.
NoteConfiguration covers two actions, one for high, and one for low level on the control input
Enumeration
Description
PCNT_MODE_KEEP = 0
Control mode: won’t change counter mode
PCNT_MODE_DISABLE = 1
Control mode: invert counter mode(increase -> decrease, decrease -> increase)
PCNT_MODE_DISABLE = 2
Control mode: Inhibit counter(counter value will not change in this condition)
PCNT_MODE_MAX

4.2. enum pcnt_count_mode_t
Selection of available modes that determine the counter’s action on the edge of the pulse signal’s input GPIO.
NoteConfiguration covers two actions, one for positive, and one for negative edge on the pulse input
Enumeration
Description
PCNT_COUNT_DIS = 0
Counter mode: Inhibit counter(counter value will not change in this condition)
PCNT_COUNT_INC = 1
Counter mode: Increase counter value
PCNT_COUNT_DEC = 2
Counter mode: Decrease counter value
PCNT_COUNT_MAX

4.3. enum pcnt_unit_t
Selection of all available PCNT units.
Enumeration
Description
PCNT_UNIT_0 = 0
PCNT unit 0
PCNT_UNIT_1 = 1
PCNT unit 1
PCNT_UNIT_2 = 2
PCNT unit 2
PCNT_UNIT_3 = 3
PCNT unit 3
PCNT_UNIT_4 = 4
PCNT unit 4
PCNT_UNIT_5 = 5
PCNT unit 5
PCNT_UNIT_6 = 6
PCNT unit 6
PCNT_UNIT_7 = 7
PCNT unit 7
PCNT_UNIT_MAX

4.4. enum pcnt_channel_t
Selection of channels available for a single PCNT unit.
Enumeration
Description
PCNT_CHANNEL_0 = 0x00
PCNT channel 0
PCNT_CHANNEL_1 = 0x01
PCNT channel 1
PCNT_CHANNEL_MAX

4.5. enum pcnt_evt_type_t
Selection of counter’s events the may trigger an interrupt.
Enumeration
Description
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_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 unit 5


相關網址:
※ DIY - ESP32:ESP32 PCNT 脈衝計數器電路(三十八)
※ DIY - ESP32:ESP32 PCNT 脈衝計數器程式(三十九)
※ DIY - ESP32:ESP32 PCNT API 函數模塊介紹(四十)
※ DIY - ESP32:ESP32 PCNT API 函數(四十一)

2018年 11月 30日 天氣報告
氣溫:22.1@ 22:00
相對濕度:百分之 64%
天氣:大致多雲

2018年11月29日 星期四

DIY - ESP32:ESP32 PCNT API 函數(四十一)

DIY - ESP32:ESP32 PCNT API 函數(四十一):

ESP32 的 IDF 內 PCNT (Pulse Counter / 脈衝計數器)API 函數模塊用於計算輸入信號的上升沿(Rising Edges)和下降沿(Falling Edges)的數量。函數模塊還可以設定輸入訊號的要求和中斷等等。
ESP32 PULSE_CNT 遞增 / 遞減計數圖
功能 - pcnt_unit_config
原型:esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config)
參數:pcnt_config: Pointer of Pulse Counter unit configure parameter
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Configure Pulse Counter unit. This function will disable three events: PCNT_EVT_L_LIM, PCNT_EVT_H_LIM, PCNT_EVT_ZERO.
範例:

功能 - pcnt_get_counter_value
原型:esp_err_t pcnt_get_counter_value(pcnt_unit_t pcnt_unit, int16_t *count)
參數:pcnt_unit: Pulse Counter unit number
            count: Pointer to accept counter value
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Get pulse counter value.
範例:

功能 - pcnt_counter_pause
原型:esp_err_t pcnt_counter_pause(pcnt_unit_t pcnt_unit)
參數:pcnt_unit: PCNT unit number
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Pause PCNT counter of PCNT unit.
範例:

功能 - pcnt_counter_resume
原型:esp_err_t pcnt_counter_resume(pcnt_unit_t pcnt_unit)
參數:pcnt_unit: PCNT unit number, select from pcnt_unit_t
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Resume counting for PCNT counter.
範例:

功能 - pcnt_counter_clear
原型:esp_err_t pcnt_counter_clear(pcnt_unit_t pcnt_unit)
參數:pcnt_unit: PCNT unit number, select from pcnt_unit_t
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Clear and reset PCNT counter value to zero.
範例:

功能 - pcnt_intr_enable
原型:esp_err_t pcnt_intr_enable(pcnt_unit_t pcnt_unit)
參數:pcnt_unit: PCNT unit number
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Enable PCNT interrupt for PCNT unit.
NoteEach Pulse counter unit has five watch point events that share the same interrupt. Configure events with pcnt_event_enable() and pcnt_event_disable()
範例:

功能 - pcnt_intr_disable
原型:esp_err_t pcnt_intr_disable(pcnt_unit_t pcnt_unit)
參數:pcnt_unit: PCNT unit number
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Disable PCNT interrupt for PCNT unit.
範例:

功能 - pcnt_event_enable
原型:esp_err_t pcnt_event_enable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
參數:unit: PCNT unit number
            evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Enable PCNT event of PCNT unit.
範例:

功能 - pcnt_event_disable _div
原型:esp_err_t pcnt_event_disable(pcnt_unit_t unit, pcnt_evt_type_t evt_type)
參數:unit: PCNT unit number
             evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Disable PCNT event of PCNT unit.
範例:

功能 - pcnt_set_event_value
原型:esp_err_t pcnt_set_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t value)
參數:unit: PCNT unit number
            evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).
            value: Counter value for PCNT event
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Set PCNT event value of PCNT unit.
範例:

功能 - pcnt_get_event_value
原型:esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16_t *value)
參數:unit: PCNT unit number
            evt_type: Watch point event type. All enabled events share the same interrupt (one interrupt per pulse counter unit).
            value: Pointer to accept counter value for PCNT event
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Get PCNT event value of PCNT unit.
範例:

功能 - pcnt_isr_register
原型:esp_err_t pcnt_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, pcnt_isr_handle_t *handle, )
參數:fn: Interrupt handler function.
            arg: Parameter for handler function
            intr_alloc_flags: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
            handle: Pointer to return handle. If non-NULL, a handle for the interrupt will be returned here. Calling esp_intr_free to unregister this ISR service if needed, but only if the handle is not NULL.
返回:ESP_OK Success
            ESP_ERR_NOT_FOUND Can not find the interrupt that matches the flags.
            ESP_ERR_INVALID_ARG Function pointer error.
說明:Register PCNT interrupt handler, the handler is an ISR. The handler will be attached to the same CPU core that this function is running on. Please do not use pcnt_isr_service_install if this function was called.
範例:

功能 - pcnt_set_pin
原型:esp_err_t pcnt_set_pin(pcnt_unit_t unit, pcnt_channel_t channel, int pulse_io, int ctrl_io)
參數:unit: PCNT unit number
            channel: PCNT channel number
            pulse_io: Pulse signal input GPIO
            ctrl_io: Control signal input GPIO
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Configure PCNT pulse signal input pin and control input pin.
NoteSet the signal input to PCNT_PIN_NOT_USED if unused.
範例:

功能 - pcnt_filter_enable
原型:esp_err_t pcnt_filter_enable(pcnt_unit_t unit)
參數:unit: PCNT unit number
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Enable PCNT input filter.
範例:

功能 - pcnt_filter_disable
原型:esp_err_t pcnt_filter_disable(pcnt_unit_t unit)
參數:unit: PCNT unit number
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Disable PCNT input filter.
範例:

功能 - pcnt_set_filter_value
原型:esp_err_t pcnt_set_filter_value(pcnt_unit_t unit, uint16_t filter_val)
參數:unit: PCNT unit number
            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.
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Set PCNT filter value.
Notefilter_val is a 10-bit value, so the maximum filter_val should be limited to 1023.
範例:

功能 - pcnt_get_filter_value
原型:esp_err_t pcnt_get_filter_value(pcnt_unit_t unit, uint16_t *filter_val)
參數:unit: PCNT unit number
            filter_val: Pointer to accept PCNT filter value.
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Get PCNT filter value.
範例:

功能 - pcnt_set_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)
參數:unit: PCNT unit number
            channel: PCNT channel number
            pos_mode: Counter mode when detecting positive edge
            neg_mode: Counter mode when detecting negative edge
            hctrl_mode: Counter mode when control signal is high level
            lctrl_mode: Counter mode when control signal is low level
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Set PCNT counter mode.
範例:

功能 - pcnt_isr_handler_add
原型:esp_err_t pcnt_isr_handler_add(pcnt_unit_t unit, void (*isr_handler)(void *), void *args, )
參數:unit: PCNT unit number
            isr_handler: Interrupt handler function.
            args: Parameter for handler function
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Add ISR handler for specified unit.
Call this function after using pcnt_isr_service_install() to install the PCNT driver’s ISR handler service.
The ISR handlers do not need to be declared with IRAM_ATTR, unless you pass the ESP_INTR_FLAG_IRAM flag when allocating the ISR in pcnt_isr_service_install().
This ISR handler will be called from an ISR. So there is a stack size limit (configurable as “ISR stack size” in menuconfig). This limit is smaller compared to a global PCNT interrupt handler due to the additional level of indirection.
範例:

功能 - pcnt_isr_service_install
原型:esp_err_t pcnt_isr_service_install(int intr_alloc_flags)
參數:intr_alloc_flags: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
返回:ESP_OK Success
            ESP_ERR_NO_MEM No memory to install this service
            ESP_ERR_INVALID_STATE ISR service already installed
說明:Install PCNT ISR service.
NoteWe can manage different interrupt service for each unit. This function will use the default ISR handle service, Calling pcnt_isr_service_uninstall to uninstall the default service if needed. Please do not use pcnt_isr_register if this function was called.
範例:

功能 - pcnt_isr_service_uninstall
原型:void pcnt_isr_service_uninstall(void)
參數:
返回:
說明:Uninstall PCNT ISR service, freeing related resources.
範例:

功能 - pcnt_isr_handler_remove
原型:esp_err_t pcnt_isr_handler_remove(pcnt_unit_t unit)
參數:unit: PCNT unit number
返回:ESP_OK Success
            ESP_ERR_INVALID_ARG Parameter error
說明:Delete ISR handler for specified unit.
範例:


相關網址:
※ DIY - ESP32:ESP32 PCNT 脈衝計數器電路(三十八)
※ DIY - ESP32:ESP32 PCNT 脈衝計數器程式(三十九)
※ DIY - ESP32:ESP32 PCNT API 函數模塊介紹(四十)
※ DIY - ESP32:ESP32 PCNT API 函數結構件及其它(四十二)

2018年 11月 29日 天氣報告
氣溫:21.5@ 19:40
相對濕度:百分之 68%
天氣:天色大致良好

2018年11月28日 星期三

DIY - ESP32:ESP32 PCNT API 函數模塊介紹(四十)

DIY - ESP32:ESP32 PCNT API 函數模塊介紹(四十):

ESP32 的 PCNT(Pulse Counter / 脈衝計數器)API 函數模塊用於計算輸入信號的上升沿(Rising Edges)和下降沿(Falling Edges)的數量。 ESP32 內有 8組脈衝計數器單元,每個脈衝計數器單元都有一個 16位有符號計數器寄存器(16-bit Signed Counter Register)和兩個通道(Two Channels),可配置為遞增(Increment)或遞減(Decrement)計數器。 每個通道都有一個接收待檢測信號邊沿的信號輸入,以及一個可用於啟用或禁用信號輸入的控制輸入。 輸入具有可選硬件濾波器(Filters),可用於丟棄信號中不需要的毛刺(Glitches)。
ESP32 PULSE_CNT 單元基本架構圖
功能概述:
此 API 的功能描述分為四個部分:
配置(Configuration) - 描述計數器的配置參數以及如何設置計數器。
操作計數器(Operating the Counter) - 提供有關暫停,測量和清除計數器的控制功能的信息。
濾波脈衝(Filtering Pulses) - 描述濾波脈沖和計數器控制信號的選項。
使用中斷(Using Interrupts) - 介紹如何在計數器的特定狀態上觸發中斷。

配置(Configuration):
PCNT 模塊有 8個獨立的計數“單位”,編號從 0 到 7.在 API 中,它們使用 pcnt_unit_t 引用。 每個單元有兩個獨立的通道,編號為 0 和 1,並用 pcnt_channel_t 指定。
使用 pcnt_config_t 為每個單元的通道單獨提供配置,並涵蓋:
此配置所指的單元和通道編號。
脈衝輸入和脈衝門輸入的 GPIO 編號。
兩對參數:pcnt_ctrl_mode_t 和 pcnt_count_mode_t,用於定義計數器如何根據控制信號的狀態作出反應以及如何計算脈衝的正 / 負邊緣。
當脈衝計數滿足特定限制時,用於建立觀察點和触發中斷的兩個限制值(最小 / 最大值)。
然後通過調用上面的 pcnt_config_t 作為輸入參數的函數 pcnt_unit_config()來完成特定通道的設置。
要在配置中禁用脈衝或控制輸入引腳,請提供 PCNT_PIN_NOT_USED 而不是 GPIO 編號。

操作計數器(Operating the Counter):
在使用 pcnt_unit_config()進行設置後,計數器立即開始運行。 可以通過調用 pcnt_get_counter_value()來檢查累積的脈衝計數。
有幾個函數可以控制計數器的操作:pcnt_counter_pause(),pcnt_counter_resume()和pcnt_counter_clear()
也可以通過調用 pcnt_set_mode()使用 pcnt_unit_config()動態更改先前設置的計數器模式。
如果需要,可以使用pcnt_set_pin()“動態”更改脈衝輸入引腳和控制輸入引腳。 要禁用特定輸入,請提供功能參數 PCNT_PIN_NOT_USED 而不是 GPIO 編號。

注意:
為了使計數器不會錯過任何脈衝,脈衝持續時間應該長於一個 APB_CLK 週期(12.5 ns)。 脈沖在 APB_CLK 時鐘的邊沿上採樣,如果在邊緣之間落下,則可能會丟失。 這適用於有或沒有文件管理器的計數器操作。

脈衝濾波(Filtering Pulses):
PCNT 單元在每個脈沖和控制輸入上都有濾波器,增加了忽略信號中短毛刺的選項。
通過調用 pcnt_set_filter_value()在 APB_CLK 時鐘週期中提供忽略脈衝的長度。 可以使用 pcnt_get_filter_value()檢查當前過濾器設置。 APB_CLK 時鐘以 80 MHz 運行。
通過調用 pcnt_filter_enable()暫停過濾器的操作。

使用中斷(Using Interrupts):
在 pcnt_evt_type_t 中定義的五個計數器狀態監視事件能夠觸發中斷。 事件發生在脈衝計數器

達到特定值:
最小或最大計數值:在配置中討論的 pcnt_config_t 中提供的 counter_l_lim 或 counter_h_lim
使用函數 pcnt_set_event_value()設置閾值 0 或閾值 1 值。
脈衝計數= 0

PCNT的中斷源呢一共有五個:
L_LIM - 最小計數值中斷,意思就是達到最小計數值的時候就會觸發該中斷,最小計數值在初始化的時候有配置
H_LIM - 最大計數值中斷,意思是達到最大計數值的時候回觸發該中斷
THRES_0 - 閾值 0 中斷,也就是自己設定一個值,當計數到達該值的時候就會觸發中斷
THRES_1 - 閾值 1中斷,和閾值 0 功能一樣,只是可以設定兩個閾值
ZERO - 計數為 0 中斷,當計數器值記到0時產生該中斷

要註冊,啟用或禁用中斷以服務上述事件,請調用 pcnt_isr_register(),pcnt_intr_enable()。 和 pcnt_intr_disable()。 要在達到閾值時啟用或禁用事件,您還需要調用函數 pcnt_event_enable()和 pcnt_event_disable()。
要檢查當前設置的閾值,請使用函數 pcnt_get_event_value()。

Header File:driver/include/driver/pcnt.h

相關網址:
※ DIY - ESP32:ESP32 PCNT 脈衝計數器電路(三十八)
※ DIY - ESP32:ESP32 PCNT 脈衝計數器程式(三十九)
※ DIY - ESP32:ESP32 PCNT API 函數(四十一)
※ DIY - ESP32:ESP32 PCNT API 函數結構件及其它(四十二)

2018年 11月 28日 天氣報告
氣溫:21.1@ 19:40
相對濕度:百分之 80%
天氣:多雲