Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 121ba14a49 | |||
| db86633155 | |||
| 6787373e82 | |||
| fca6da29cc | |||
| 624a6b4661 | |||
| 020194431b | |||
| 4efbe96de1 | |||
| 7d2bd7fd2e | |||
| b70d625a05 | |||
| 28d067eb98 | |||
| be29ff31f7 | |||
| 5ef3913eb9 | |||
| c4d6bf7dec | |||
| d8c6e2679f | |||
| 6b2ed520d0 | |||
| 232933650d | |||
| 37ae299426 | |||
| 2060a56b55 | |||
| deb882beb6 | |||
| 5087b9b743 | |||
| d5c94158c2 | |||
| e133e5a807 | |||
| 13e04b2cd3 | |||
| b4c8220284 |
@@ -1,6 +1 @@
|
|||||||
if(${IDF_TARGET} STREQUAL esp8266)
|
idf_component_register(SRCS "zh_encoder.c" INCLUDE_DIRS "include" REQUIRES esp_event driver esp_timer)
|
||||||
set(requires driver)
|
|
||||||
else()
|
|
||||||
set(requires driver esp_event)
|
|
||||||
endif()
|
|
||||||
idf_component_register(SRCS "zh_encoder.c" INCLUDE_DIRS "include" REQUIRES ${requires})
|
|
||||||
78
README.md
78
README.md
@@ -1,17 +1,26 @@
|
|||||||
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for rotary encoder
|
# ESP32 ESP-IDF component for rotary encoder
|
||||||
|
|
||||||
## Tested on
|
## Tested on
|
||||||
|
|
||||||
1. [ESP8266 RTOS_SDK v3.4](https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/index.html#)
|
1. [ESP32 ESP-IDF v5.5.2](https://docs.espressif.com/projects/esp-idf/en/v5.5.2/esp32/index.html)
|
||||||
2. [ESP32 ESP-IDF v5.4](https://docs.espressif.com/projects/esp-idf/en/release-v5.4/esp32/index.html)
|
|
||||||
|
## SAST Tools
|
||||||
|
|
||||||
|
[PVS-Studio](https://pvs-studio.com/pvs-studio/?utm_source=website&utm_medium=github&utm_campaign=open_source) - static analyzer for C, C++, C#, and Java code.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
1. Support some encoders on one device.
|
1. Support some encoders on one device.
|
||||||
|
|
||||||
## Note
|
## Attention
|
||||||
|
|
||||||
1. Encoder pins must be pull up to the VCC via 0.1 µf capacitors.
|
1. For correct operation, please enable the following settings in the menuconfig:
|
||||||
|
|
||||||
|
```text
|
||||||
|
GPIO_CTRL_FUNC_IN_IRAM
|
||||||
|
PCNT_CTRL_FUNC_IN_IRAM
|
||||||
|
PCNT_ISR_IRAM_SAFE
|
||||||
|
```
|
||||||
|
|
||||||
## Using
|
## Using
|
||||||
|
|
||||||
@@ -19,7 +28,7 @@ In an existing project, run the following command to install the components:
|
|||||||
|
|
||||||
```text
|
```text
|
||||||
cd ../your_project/components
|
cd ../your_project/components
|
||||||
git clone http://git.zh.com.ru/alexey.zholtikov/zh_encoder
|
git clone http://git.zh.com.ru/esp_components/zh_encoder
|
||||||
```
|
```
|
||||||
|
|
||||||
In the application, add the component:
|
In the application, add the component:
|
||||||
@@ -30,42 +39,61 @@ In the application, add the component:
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
One encoder on device:
|
One encoder with button on device:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include "zh_encoder.h"
|
#include "zh_encoder.h"
|
||||||
|
|
||||||
|
#define ENCODER_NUMBER 0x01
|
||||||
|
|
||||||
zh_encoder_handle_t encoder_handle = {0};
|
zh_encoder_handle_t encoder_handle = {0};
|
||||||
|
|
||||||
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
esp_log_level_set("zh_encoder", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
|
esp_log_level_set("zh_encoder", ESP_LOG_ERROR);
|
||||||
esp_event_loop_create_default();
|
esp_event_loop_create_default();
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
|
||||||
esp_event_handler_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &zh_encoder_event_handler, NULL);
|
|
||||||
#else
|
|
||||||
esp_event_handler_instance_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &zh_encoder_event_handler, NULL, NULL);
|
esp_event_handler_instance_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &zh_encoder_event_handler, NULL, NULL);
|
||||||
#endif
|
|
||||||
zh_encoder_init_config_t encoder_init_config = ZH_ENCODER_INIT_CONFIG_DEFAULT();
|
zh_encoder_init_config_t encoder_init_config = ZH_ENCODER_INIT_CONFIG_DEFAULT();
|
||||||
encoder_init_config.a_gpio_number = GPIO_NUM_27;
|
encoder_init_config.task_priority = 5;
|
||||||
encoder_init_config.b_gpio_number = GPIO_NUM_26;
|
encoder_init_config.stack_size = configMINIMAL_STACK_SIZE;
|
||||||
// encoder_init_config.encoder_min_value = -10; // Just for example.
|
encoder_init_config.queue_size = 5;
|
||||||
// encoder_init_config.encoder_max_value = 20; // Just for example.
|
encoder_init_config.a_gpio_number = GPIO_NUM_4;
|
||||||
// encoder_init_config.encoder_step = 0.1; // Just for example.
|
encoder_init_config.b_gpio_number = GPIO_NUM_16;
|
||||||
encoder_init_config.encoder_number = 1;
|
encoder_init_config.s_gpio_number = GPIO_NUM_15;
|
||||||
|
encoder_init_config.encoder_min_value = -10;
|
||||||
|
encoder_init_config.encoder_max_value = 10;
|
||||||
|
encoder_init_config.encoder_step = 0.001;
|
||||||
|
encoder_init_config.encoder_number = ENCODER_NUMBER;
|
||||||
zh_encoder_init(&encoder_init_config, &encoder_handle);
|
zh_encoder_init(&encoder_init_config, &encoder_handle);
|
||||||
double position = 0;
|
double encoder_position = 0;
|
||||||
zh_encoder_get(&encoder_handle, &position);
|
zh_encoder_get(&encoder_handle, &encoder_position);
|
||||||
printf("Encoder position %0.2f.\n", position); // For ESP8266 first disable "Component config -> Newlib -> Enable ‘nano’ formatting options for printf/scanf family" via menuconfig.
|
printf("Encoder position %0.3f.\n", encoder_position);
|
||||||
// zh_encoder_set(&encoder_handle, 5); // Just for example.
|
for (;;)
|
||||||
// zh_encoder_reset(&encoder_handle); // Just for example.
|
{
|
||||||
|
const zh_encoder_stats_t *stats = zh_encoder_get_stats();
|
||||||
|
printf("Number of event post error: %ld.\n", stats->event_post_error);
|
||||||
|
printf("Number of queue overflow error: %ld.\n", stats->queue_overflow_error);
|
||||||
|
printf("Minimum free stack size: %ld.\n", stats->min_stack_size);
|
||||||
|
vTaskDelay(60000 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||||
{
|
{
|
||||||
zh_encoder_event_on_isr_t *event = event_data;
|
switch (event_id)
|
||||||
printf("Encoder number %d position %.2f.\n", event->encoder_number, event->encoder_position); // For ESP8266 first disable "Component config -> Newlib -> Enable ‘nano’ formatting options for printf/scanf family" via menuconfig.
|
{
|
||||||
|
case ZH_BUTTON_EVENT:
|
||||||
|
zh_encoder_button_event_on_isr_t *button_event = event_data;
|
||||||
|
printf("Encoder number %d button %s.\n", button_event->encoder_number, button_event->button_status == 1 ? "released" : "pressed");
|
||||||
|
break;
|
||||||
|
case ZH_ENCODER_EVENT:
|
||||||
|
zh_encoder_event_on_isr_t *encoder_event = event_data;
|
||||||
|
printf("Encoder number %d position %0.3f.\n", encoder_event->encoder_number, encoder_event->encoder_position);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
2981
Schematic.pdf
Executable file
2981
Schematic.pdf
Executable file
File diff suppressed because it is too large
Load Diff
@@ -1,21 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* @file zh_encoder.h
|
||||||
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
|
#include "driver/pulse_cnt.h"
|
||||||
|
#include "esp_timer.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
|
|
||||||
#define ZH_ENCODER_INIT_CONFIG_DEFAULT() \
|
/**
|
||||||
{ \
|
* @brief Encoder initial default values.
|
||||||
.task_priority = 10, \
|
*/
|
||||||
.stack_size = 3072, \
|
#define ZH_ENCODER_INIT_CONFIG_DEFAULT() \
|
||||||
.queue_size = 10, \
|
{ \
|
||||||
.a_gpio_number = 0, \
|
.task_priority = 1, \
|
||||||
.b_gpio_number = 0, \
|
.stack_size = configMINIMAL_STACK_SIZE, \
|
||||||
.encoder_min_value = -100, \
|
.queue_size = 1, \
|
||||||
.encoder_max_value = 100, \
|
.a_gpio_number = GPIO_NUM_MAX, \
|
||||||
.encoder_step = 1, \
|
.b_gpio_number = GPIO_NUM_MAX, \
|
||||||
|
.s_gpio_number = GPIO_NUM_MAX, \
|
||||||
|
.s_gpio_debounce_time = 10, \
|
||||||
|
.encoder_min_value = -10, \
|
||||||
|
.encoder_max_value = 10, \
|
||||||
|
.encoder_step = 1, \
|
||||||
.encoder_number = 0}
|
.encoder_number = 0}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@@ -23,40 +34,89 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct // Structure for initial initialization of encoder.
|
extern TaskHandle_t zh_encoder; /*!< Encoder Task Handle. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Structure for initial initialization of encoder.
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t task_priority; // Task priority for the encoder isr processing. @note It is not recommended to set a value less than 10.
|
double encoder_step; /*!< Encoder step. @note Must be greater than 0. */
|
||||||
uint16_t stack_size; // Stack size for task for the encoder isr processing processing. @note The minimum size is 3072 bytes.
|
double encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */
|
||||||
uint8_t queue_size; // Queue size for task for the encoder processing. @note It is not recommended to set a value less than 10.
|
double encoder_max_value; /*!< Encoder max value. @note Must be greater than encoder_min_value. */
|
||||||
uint8_t a_gpio_number; // Encoder A GPIO number.
|
uint8_t task_priority; /*!< Task priority for the encoder isr processing. @note Minimum value is 1. */
|
||||||
uint8_t b_gpio_number; // Encoder B GPIO number.
|
uint8_t queue_size; /*!< Queue size for task for the encoder processing. @note Minimum value is 1. */
|
||||||
int32_t encoder_min_value; // Encoder min value. @note Must be less than encoder_max_value.
|
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
|
||||||
int32_t encoder_max_value; // Encoder max value. @note Must be greater than encoder_min_value.
|
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
|
||||||
double encoder_step; // Encoder step. @note Must be greater than 0.
|
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
|
||||||
uint8_t encoder_number; // Unique encoder number.
|
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. @note In microseconds. */
|
||||||
|
uint8_t encoder_number; /*!< Unique encoder number. @note Must be greater than 0. */
|
||||||
|
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
|
||||||
} zh_encoder_init_config_t;
|
} zh_encoder_init_config_t;
|
||||||
|
|
||||||
typedef struct // Encoder handle.
|
/**
|
||||||
|
* @brief Encoder handle.
|
||||||
|
*/
|
||||||
|
typedef struct // -V802
|
||||||
{
|
{
|
||||||
uint8_t a_gpio_number; // Encoder A GPIO number.
|
pcnt_unit_handle_t pcnt_unit_handle; /*!< Encoder unique pcnt unit handle. */
|
||||||
uint8_t b_gpio_number; // Encoder B GPIO number.
|
pcnt_channel_handle_t pcnt_channel_a_handle; /*!< Encoder unique pcnt channel handle. */
|
||||||
int32_t encoder_min_value; // Encoder min value.
|
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Encoder unique pcnt channel handle. */
|
||||||
int32_t encoder_max_value; // Encoder max value.
|
double encoder_step; /*!< Encoder step. */
|
||||||
double encoder_step; // Encoder step.
|
double encoder_position; /*!< Encoder position. */
|
||||||
double encoder_position; // Encoder position.
|
double encoder_min_value; /*!< Encoder min value. */
|
||||||
uint8_t encoder_number; // Encoder unique number.
|
double encoder_max_value; /*!< Encoder max value. */
|
||||||
uint8_t encoder_state; // Encoder internal state.
|
uint8_t encoder_number; /*!< Encoder unique number. */
|
||||||
bool is_initialized; // Encoder initialization flag.
|
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
|
||||||
|
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. */
|
||||||
|
uint64_t s_gpio_prev_time; /*!< Encoder button prev interrupt time. */
|
||||||
|
bool s_gpio_status; /*!< Encoder button status. */
|
||||||
|
bool is_initialized; /*!< Encoder initialization flag. */
|
||||||
} zh_encoder_handle_t;
|
} zh_encoder_handle_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Structure for error statistics storage.
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint32_t event_post_error; /*!< Number of event post error. */
|
||||||
|
uint32_t queue_overflow_error; /*!< Number of queue overflow error. */
|
||||||
|
uint32_t min_stack_size; /*!< Minimum free stack size. */
|
||||||
|
} zh_encoder_stats_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Enumeration of encoder event ID.
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
ZH_BUTTON_EVENT, /*!< Button event. */
|
||||||
|
ZH_ENCODER_EVENT /*!< Encoder event. */
|
||||||
|
} zh_encoder_event_id_t;
|
||||||
|
|
||||||
ESP_EVENT_DECLARE_BASE(ZH_ENCODER);
|
ESP_EVENT_DECLARE_BASE(ZH_ENCODER);
|
||||||
|
|
||||||
typedef struct // Structure for sending data to the event handler when cause an interrupt. @note Should be used with ZH_ENCODER event base.
|
/**
|
||||||
|
* @brief Structure for sending data to the event handler when cause encoder interrupt.
|
||||||
|
*
|
||||||
|
* @note Should be used with ZH_ENCODER event base and ZH_ENCODER_EVENT event ID.
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t encoder_number; // Encoder unique number.
|
double encoder_position; /*!< Encoder current position. */
|
||||||
double encoder_position; // Encoder current position.
|
uint8_t encoder_number; /*!< Encoder unique number. */
|
||||||
} zh_encoder_event_on_isr_t;
|
} zh_encoder_event_on_isr_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Structure for sending data to the event handler when cause encoder button interrupt.
|
||||||
|
*
|
||||||
|
* @note Should be used with ZH_ENCODER event base and ZH_BUTTON_EVENT event ID.
|
||||||
|
*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
uint8_t encoder_number; /*!< Encoder unique number. */
|
||||||
|
bool button_status; /*!< Encoder button status. */
|
||||||
|
} zh_encoder_button_event_on_isr_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize encoder.
|
* @brief Initialize encoder.
|
||||||
*
|
*
|
||||||
@@ -73,6 +133,27 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deinitialize encoder.
|
||||||
|
*
|
||||||
|
* @return ESP_OK if success or an error code otherwise.
|
||||||
|
*/
|
||||||
|
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reinitialize encoder (change min, max and step values).
|
||||||
|
*
|
||||||
|
* @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2.
|
||||||
|
*
|
||||||
|
* @param[in, out] handle Pointer to unique encoder handle.
|
||||||
|
* @param[in] min Encoder min value. @note Must be less than encoder_max_value.
|
||||||
|
* @param[in] max Encoder max value. @note Must be greater than encoder_min_value.
|
||||||
|
* @param[in] step Encoder step. @note Must be greater than 0.
|
||||||
|
*
|
||||||
|
* @return ESP_OK if success or an error code otherwise.
|
||||||
|
*/
|
||||||
|
esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, double min, double max, double step);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set encoder position.
|
* @brief Set encoder position.
|
||||||
*
|
*
|
||||||
@@ -104,6 +185,18 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle);
|
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get error statistics.
|
||||||
|
*
|
||||||
|
* @return Pointer to the statistics structure.
|
||||||
|
*/
|
||||||
|
const zh_encoder_stats_t *zh_encoder_get_stats(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reset error statistics.
|
||||||
|
*/
|
||||||
|
void zh_encoder_reset_stats(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -1 +1 @@
|
|||||||
1.0.0
|
2.0.0
|
||||||
434
zh_encoder.c
434
zh_encoder.c
@@ -2,170 +2,308 @@
|
|||||||
|
|
||||||
#define TAG "zh_encoder"
|
#define TAG "zh_encoder"
|
||||||
|
|
||||||
#define ZH_ENCODER_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
|
#define ZH_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
|
||||||
#define ZH_ENCODER_LOGW(msg, ...) ESP_LOGW(TAG, msg, ##__VA_ARGS__)
|
#define ZH_LOGE(msg, err, ...) ESP_LOGE(TAG, "[%s:%d:%s] " msg, __FILE__, __LINE__, esp_err_to_name(err), ##__VA_ARGS__)
|
||||||
#define ZH_ENCODER_LOGE(msg, ...) ESP_LOGE(TAG, msg, ##__VA_ARGS__)
|
|
||||||
#define ZH_ENCODER_LOGE_ERR(msg, err, ...) ESP_LOGE(TAG, "[%s:%d:%s] " msg, __FILE__, __LINE__, esp_err_to_name(err), ##__VA_ARGS__)
|
|
||||||
|
|
||||||
#define ZH_ENCODER_CHECK(cond, err, msg, ...) \
|
#define ZH_ERROR_CHECK(cond, err, cleanup, msg, ...) \
|
||||||
if (!(cond)) \
|
if (!(cond)) \
|
||||||
{ \
|
{ \
|
||||||
ZH_ENCODER_LOGE_ERR(msg, err); \
|
ZH_LOGE(msg, err, ##__VA_ARGS__); \
|
||||||
return err; \
|
cleanup; \
|
||||||
|
return err; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ZH_ENCODER_DIRECTION_CW 0x10
|
#define ZH_ENCODER_DIRECTION_CW 1
|
||||||
#define ZH_ENCODER_DIRECTION_CCW 0x20
|
#define ZH_ENCODER_DIRECTION_CCW -1
|
||||||
|
|
||||||
static const uint8_t _encoder_matrix[7][4] = {
|
|
||||||
{0x03, 0x02, 0x01, 0x00},
|
|
||||||
{0x23, 0x00, 0x01, 0x00},
|
|
||||||
{0x13, 0x02, 0x00, 0x00},
|
|
||||||
{0x03, 0x05, 0x04, 0x00},
|
|
||||||
{0x03, 0x03, 0x04, 0x00},
|
|
||||||
{0x03, 0x05, 0x03, 0x00},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
TaskHandle_t zh_encoder = NULL;
|
||||||
static QueueHandle_t _queue_handle = NULL;
|
static QueueHandle_t _queue_handle = NULL;
|
||||||
static bool _is_initialized = false;
|
static portMUX_TYPE _spinlock = portMUX_INITIALIZER_UNLOCKED;
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config);
|
static uint8_t _encoder_counter = 0;
|
||||||
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config);
|
static zh_encoder_stats_t _stats = {0};
|
||||||
static esp_err_t _zh_encoder_configure_interrupts(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
static uint8_t _encoder_number_matrix[CONFIG_SOC_PCNT_UNITS_PER_GROUP] = {0};
|
||||||
static esp_err_t _zh_encoder_init_resources(const zh_encoder_init_config_t *config);
|
|
||||||
static esp_err_t _zh_encoder_create_task(const zh_encoder_init_config_t *config);
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
||||||
static void _zh_encoder_isr_handler(void *arg);
|
static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
||||||
|
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
||||||
|
static esp_err_t _zh_encoder_resources_init(const zh_encoder_init_config_t *config);
|
||||||
|
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config);
|
||||||
|
static bool _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx);
|
||||||
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
||||||
|
static void _zh_encoder_button_isr_handler(void *arg);
|
||||||
|
|
||||||
ESP_EVENT_DEFINE_BASE(ZH_ENCODER);
|
ESP_EVENT_DEFINE_BASE(ZH_ENCODER);
|
||||||
|
|
||||||
esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle)
|
esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
||||||
{
|
{
|
||||||
ZH_ENCODER_LOGI("Encoder initialization started.");
|
ZH_LOGI("Encoder initialization started.");
|
||||||
esp_err_t err = _zh_encoder_validate_config(config);
|
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument.");
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "Encoder initialization failed. Initial configuration check failed.");
|
ZH_ERROR_CHECK(_encoder_counter < sizeof(_encoder_number_matrix), ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Maximum quantity reached.");
|
||||||
ZH_ENCODER_LOGI("Encoder initial configuration check completed successfully.");
|
esp_err_t err = _zh_encoder_validate_config(config, handle);
|
||||||
handle->encoder_number = config->encoder_number;
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Initial configuration check failed.");
|
||||||
handle->encoder_min_value = config->encoder_min_value;
|
err = _zh_encoder_resources_init(config);
|
||||||
handle->encoder_max_value = config->encoder_max_value;
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Resources initialization failed.");
|
||||||
handle->encoder_step = config->encoder_step;
|
err = _zh_encoder_task_init(config);
|
||||||
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL, "Encoder initialization failed. Processing task initialization failed.");
|
||||||
err = _zh_encoder_gpio_init(config);
|
err = _zh_encoder_pcnt_init(config, handle);
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "Encoder initialization failed. GPIO initialization failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL; vTaskDelete(zh_encoder); zh_encoder = NULL,
|
||||||
ZH_ENCODER_LOGI("Encoder GPIO initialization completed successfully.");
|
"Encoder initialization failed. PCNT initialization failed.");
|
||||||
handle->a_gpio_number = config->a_gpio_number;
|
err = _zh_encoder_gpio_init(config, handle);
|
||||||
handle->b_gpio_number = config->b_gpio_number;
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_stop(handle->pcnt_unit_handle); pcnt_unit_disable(handle->pcnt_unit_handle); pcnt_del_channel(handle->pcnt_channel_a_handle);
|
||||||
err = _zh_encoder_configure_interrupts(config, handle);
|
pcnt_del_channel(handle->pcnt_channel_b_handle); pcnt_del_unit(handle->pcnt_unit_handle); vQueueDelete(_queue_handle); _queue_handle = NULL;
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "Encoder initialization failed. Interrupt initialization failed.");
|
vTaskDelete(zh_encoder); zh_encoder = NULL, "Encoder initialization failed. GPIO initialization failed.");
|
||||||
ZH_ENCODER_LOGI("Encoder interrupt initialization completed successfully.");
|
|
||||||
err = _zh_encoder_init_resources(config);
|
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "Encoder initialization failed. Resources initialization failed.");
|
|
||||||
ZH_ENCODER_LOGI("Encoder resources initialization completed successfully.");
|
|
||||||
err = _zh_encoder_create_task(config);
|
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "Encoder initialization failed. Processing task initialization failed.");
|
|
||||||
ZH_ENCODER_LOGI("Encoder processing task initialization completed successfully.");
|
|
||||||
handle->is_initialized = true;
|
handle->is_initialized = true;
|
||||||
_is_initialized = true;
|
++_encoder_counter;
|
||||||
ZH_ENCODER_LOGI("Encoder initialization completed successfully.");
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
|
{
|
||||||
|
if (_encoder_number_matrix[i] == 0)
|
||||||
|
{
|
||||||
|
_encoder_number_matrix[i] = handle->encoder_number;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ZH_LOGI("Encoder initialization completed successfully.");
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle)
|
||||||
|
{
|
||||||
|
ZH_LOGI("Encoder deinitialization started.");
|
||||||
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder deinitialization failed. Invalid argument.");
|
||||||
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder deinitialization failed. Encoder not initialized.");
|
||||||
|
pcnt_unit_stop(handle->pcnt_unit_handle);
|
||||||
|
pcnt_unit_disable(handle->pcnt_unit_handle);
|
||||||
|
pcnt_del_channel(handle->pcnt_channel_a_handle);
|
||||||
|
pcnt_del_channel(handle->pcnt_channel_b_handle);
|
||||||
|
pcnt_del_unit(handle->pcnt_unit_handle);
|
||||||
|
if (handle->s_gpio_number != GPIO_NUM_MAX)
|
||||||
|
{
|
||||||
|
gpio_isr_handler_remove((gpio_num_t)handle->s_gpio_number);
|
||||||
|
gpio_reset_pin((gpio_num_t)handle->s_gpio_number);
|
||||||
|
}
|
||||||
|
if (_encoder_counter == 1)
|
||||||
|
{
|
||||||
|
vQueueDelete(_queue_handle);
|
||||||
|
_queue_handle = NULL;
|
||||||
|
vTaskDelete(zh_encoder);
|
||||||
|
zh_encoder = NULL;
|
||||||
|
}
|
||||||
|
handle->is_initialized = false;
|
||||||
|
--_encoder_counter;
|
||||||
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
|
{
|
||||||
|
if (_encoder_number_matrix[i] == handle->encoder_number)
|
||||||
|
{
|
||||||
|
_encoder_number_matrix[i] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ZH_LOGI("Encoder deinitialization completed successfully.");
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, double min, double max, double step) // -V2008
|
||||||
|
{
|
||||||
|
ZH_LOGI("Encoder reinitialization started.");
|
||||||
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid argument.");
|
||||||
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder reinitialization failed. Encoder not initialized.");
|
||||||
|
ZH_ERROR_CHECK(max > min, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid encoder min/max value.");
|
||||||
|
ZH_ERROR_CHECK(step > 0, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid encoder step.");
|
||||||
|
taskENTER_CRITICAL(&_spinlock);
|
||||||
|
handle->encoder_min_value = min;
|
||||||
|
handle->encoder_max_value = max;
|
||||||
|
handle->encoder_step = step;
|
||||||
|
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
||||||
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
|
ZH_LOGI("Encoder reinitialization completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, double position)
|
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, double position)
|
||||||
{
|
{
|
||||||
ZH_ENCODER_LOGI("Encoder set position started.");
|
ZH_LOGI("Encoder set position started.");
|
||||||
ZH_ENCODER_CHECK(handle->is_initialized == true, ESP_FAIL, "Encoder set position failed. Encoder not initialized.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder set position failed. Invalid argument.");
|
||||||
ZH_ENCODER_CHECK(position <= handle->encoder_max_value && position >= handle->encoder_min_value, ESP_ERR_INVALID_ARG, "Encoder set position failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder set position failed. Encoder not initialized.");
|
||||||
|
ZH_ERROR_CHECK(position <= handle->encoder_max_value && position >= handle->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Encoder set position failed. Invalid argument.");
|
||||||
|
taskENTER_CRITICAL(&_spinlock);
|
||||||
handle->encoder_position = position;
|
handle->encoder_position = position;
|
||||||
ZH_ENCODER_LOGI("Encoder set position completed successfully.");
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
|
ZH_LOGI("Encoder set position completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, double *position)
|
esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, double *position)
|
||||||
{
|
{
|
||||||
ZH_ENCODER_LOGI("Encoder get position started.");
|
ZH_LOGI("Encoder get position started.");
|
||||||
ZH_ENCODER_CHECK(handle->is_initialized == true, ESP_FAIL, "Encoder get position failed. Encoder not initialized.");
|
ZH_ERROR_CHECK(handle != NULL && position != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder get position failed. Invalid argument.");
|
||||||
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder get position failed. Encoder not initialized.");
|
||||||
*position = handle->encoder_position;
|
*position = handle->encoder_position;
|
||||||
ZH_ENCODER_LOGI("Encoder get position completed successfully.");
|
ZH_LOGI("Encoder get position completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle)
|
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle)
|
||||||
{
|
{
|
||||||
ZH_ENCODER_LOGI("Encoder reset started.");
|
ZH_LOGI("Encoder reset started.");
|
||||||
ZH_ENCODER_CHECK(handle->is_initialized == true, ESP_FAIL, "Encoder reset failed. Encoder not initialized.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder reset failed. Invalid argument.");
|
||||||
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder reset failed. Encoder not initialized.");
|
||||||
|
taskENTER_CRITICAL(&_spinlock);
|
||||||
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
||||||
ZH_ENCODER_LOGI("Encoder reset completed successfully.");
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
|
ZH_LOGI("Encoder reset completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config)
|
const zh_encoder_stats_t *zh_encoder_get_stats(void)
|
||||||
{
|
{
|
||||||
ZH_ENCODER_CHECK(config != NULL, ESP_ERR_INVALID_ARG, "Invalid configuration.");
|
return &_stats;
|
||||||
ZH_ENCODER_CHECK(config->task_priority >= 10 && config->stack_size >= 3072, ESP_ERR_INVALID_ARG, "Invalid task settings.");
|
|
||||||
ZH_ENCODER_CHECK(config->queue_size >= 10, ESP_ERR_INVALID_ARG, "Invalid queue size.");
|
|
||||||
ZH_ENCODER_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, "Invalid encoder min/max value.");
|
|
||||||
ZH_ENCODER_CHECK(config->encoder_step > 0, ESP_ERR_INVALID_ARG, "Invalid encoder step.");
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config)
|
void zh_encoder_reset_stats(void)
|
||||||
{
|
{
|
||||||
ZH_ENCODER_CHECK(config->a_gpio_number < GPIO_NUM_MAX || config->b_gpio_number < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, "Invalid GPIO number.")
|
ZH_LOGI("Error statistic reset started.");
|
||||||
ZH_ENCODER_CHECK(config->a_gpio_number != config->b_gpio_number, ESP_ERR_INVALID_ARG, "Invalid GPIO number.")
|
_stats.event_post_error = 0;
|
||||||
gpio_config_t pin_config = {
|
_stats.queue_overflow_error = 0;
|
||||||
.mode = GPIO_MODE_INPUT,
|
_stats.min_stack_size = 0;
|
||||||
.pin_bit_mask = (1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number),
|
ZH_LOGI("Error statistic reset successfully.");
|
||||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
|
||||||
.intr_type = GPIO_INTR_ANYEDGE};
|
|
||||||
esp_err_t err = gpio_config(&pin_config);
|
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "GPIO initialization failed.");
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_configure_interrupts(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle)
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
||||||
{
|
{
|
||||||
gpio_install_isr_service(0);
|
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
|
||||||
esp_err_t err = gpio_isr_handler_add(config->a_gpio_number, _zh_encoder_isr_handler, handle);
|
ZH_ERROR_CHECK(config->task_priority >= 1 && config->stack_size >= configMINIMAL_STACK_SIZE, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "Interrupt initialization failed.");
|
ZH_ERROR_CHECK(config->queue_size >= 1, ESP_ERR_INVALID_ARG, NULL, "Invalid queue size.");
|
||||||
err = gpio_isr_handler_add(config->b_gpio_number, _zh_encoder_isr_handler, handle);
|
ZH_ERROR_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder min/max value.");
|
||||||
ZH_ENCODER_CHECK(err == ESP_OK, err, "Interrupt initialization failed.");
|
ZH_ERROR_CHECK(config->encoder_step > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder step.");
|
||||||
return ESP_OK;
|
ZH_ERROR_CHECK(config->encoder_number > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder number.");
|
||||||
}
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_init_resources(const zh_encoder_init_config_t *config)
|
|
||||||
{
|
|
||||||
if (_is_initialized == false)
|
|
||||||
{
|
{
|
||||||
_queue_handle = xQueueCreate(config->queue_size, sizeof(zh_encoder_handle_t));
|
ZH_ERROR_CHECK(config->encoder_number != _encoder_number_matrix[i], ESP_ERR_INVALID_ARG, NULL, "Encoder number already present.");
|
||||||
ZH_ENCODER_CHECK(_queue_handle != NULL, ESP_FAIL, "Queue creation failed.");
|
}
|
||||||
|
handle->encoder_number = config->encoder_number;
|
||||||
|
handle->encoder_min_value = config->encoder_min_value;
|
||||||
|
handle->encoder_max_value = config->encoder_max_value;
|
||||||
|
handle->encoder_step = config->encoder_step;
|
||||||
|
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
|
||||||
|
ZH_ERROR_CHECK(config->a_gpio_number < GPIO_NUM_MAX && config->b_gpio_number < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.")
|
||||||
|
ZH_ERROR_CHECK(config->a_gpio_number != config->b_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder A and B GPIO is same.")
|
||||||
|
pcnt_unit_config_t pcnt_unit_config = {
|
||||||
|
.high_limit = 10,
|
||||||
|
.low_limit = -10,
|
||||||
|
};
|
||||||
|
pcnt_unit_handle_t pcnt_unit_handle = NULL;
|
||||||
|
esp_err_t err = pcnt_new_unit(&pcnt_unit_config, &pcnt_unit_handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCNT initialization failed.");
|
||||||
|
pcnt_glitch_filter_config_t pcnt_glitch_filter_config = {
|
||||||
|
.max_glitch_ns = 1000,
|
||||||
|
};
|
||||||
|
err = pcnt_unit_set_glitch_filter(pcnt_unit_handle, &pcnt_glitch_filter_config);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
pcnt_chan_config_t pcnt_chan_a_config = {
|
||||||
|
.edge_gpio_num = config->a_gpio_number,
|
||||||
|
.level_gpio_num = config->b_gpio_number,
|
||||||
|
};
|
||||||
|
pcnt_channel_handle_t pcnt_channel_a_handle = NULL;
|
||||||
|
err = pcnt_new_channel(pcnt_unit_handle, &pcnt_chan_a_config, &pcnt_channel_a_handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
pcnt_chan_config_t pcnt_chan_b_config = {
|
||||||
|
.edge_gpio_num = config->b_gpio_number,
|
||||||
|
.level_gpio_num = config->a_gpio_number,
|
||||||
|
};
|
||||||
|
pcnt_channel_handle_t pcnt_channel_b_handle = NULL;
|
||||||
|
err = pcnt_new_channel(pcnt_unit_handle, &pcnt_chan_b_config, &pcnt_channel_b_handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
err = pcnt_channel_set_edge_action(pcnt_channel_a_handle, PCNT_CHANNEL_EDGE_ACTION_DECREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
err = pcnt_channel_set_level_action(pcnt_channel_a_handle, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_HOLD);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
err = pcnt_channel_set_edge_action(pcnt_channel_b_handle, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
err = pcnt_channel_set_level_action(pcnt_channel_b_handle, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_HOLD);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
err = pcnt_unit_add_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
err = pcnt_unit_add_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
pcnt_event_callbacks_t cbs = {
|
||||||
|
.on_reach = _zh_encoder_isr_handler,
|
||||||
|
};
|
||||||
|
err = pcnt_unit_register_event_callbacks(pcnt_unit_handle, &cbs, handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle),
|
||||||
|
"PCNT initialization failed.");
|
||||||
|
err = pcnt_unit_enable(pcnt_unit_handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle),
|
||||||
|
"PCNT initialization failed.");
|
||||||
|
err = pcnt_unit_clear_count(pcnt_unit_handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_disable(pcnt_unit_handle); pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle);
|
||||||
|
pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
err = pcnt_unit_start(pcnt_unit_handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_disable(pcnt_unit_handle); pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle);
|
||||||
|
pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
handle->pcnt_unit_handle = pcnt_unit_handle;
|
||||||
|
handle->pcnt_channel_a_handle = pcnt_channel_a_handle;
|
||||||
|
handle->pcnt_channel_b_handle = pcnt_channel_b_handle;
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
|
||||||
|
ZH_ERROR_CHECK(config->s_gpio_number <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.")
|
||||||
|
ZH_ERROR_CHECK(config->a_gpio_number != config->s_gpio_number && config->b_gpio_number != config->s_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder GPIO and button GPIO is same.")
|
||||||
|
if (config->s_gpio_number != GPIO_NUM_MAX)
|
||||||
|
{
|
||||||
|
gpio_config_t pin_config = {
|
||||||
|
.mode = GPIO_MODE_INPUT,
|
||||||
|
.pin_bit_mask = (1ULL << config->s_gpio_number),
|
||||||
|
.intr_type = GPIO_INTR_ANYEDGE};
|
||||||
|
esp_err_t err = gpio_config(&pin_config);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "GPIO initialization failed.");
|
||||||
|
err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, err, gpio_reset_pin((gpio_num_t)config->s_gpio_number), "Failed install isr service.");
|
||||||
|
err = gpio_isr_handler_add((gpio_num_t)config->s_gpio_number, _zh_encoder_button_isr_handler, handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_reset_pin((gpio_num_t)config->s_gpio_number), "Interrupt initialization failed.");
|
||||||
|
handle->s_gpio_number = config->s_gpio_number;
|
||||||
|
handle->s_gpio_debounce_time = config->s_gpio_debounce_time;
|
||||||
|
handle->s_gpio_status = gpio_get_level((gpio_num_t)config->s_gpio_number);
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_create_task(const zh_encoder_init_config_t *config)
|
static esp_err_t _zh_encoder_resources_init(const zh_encoder_init_config_t *config)
|
||||||
{
|
{
|
||||||
if (_is_initialized == false)
|
if (_encoder_counter == 0)
|
||||||
{
|
{
|
||||||
BaseType_t err = xTaskCreatePinnedToCore(
|
_queue_handle = xQueueCreate(config->queue_size, sizeof(zh_encoder_event_on_isr_t));
|
||||||
&_zh_encoder_isr_processing_task,
|
ZH_ERROR_CHECK(_queue_handle != NULL, ESP_FAIL, NULL, "Failed to create queue.");
|
||||||
"zh_encoder_isr_processing",
|
|
||||||
config->stack_size,
|
|
||||||
NULL,
|
|
||||||
config->task_priority,
|
|
||||||
NULL,
|
|
||||||
tskNO_AFFINITY);
|
|
||||||
ZH_ENCODER_CHECK(err == pdPASS, ESP_FAIL, "Task creation failed.");
|
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void IRAM_ATTR _zh_encoder_isr_handler(void *arg)
|
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config)
|
||||||
|
{
|
||||||
|
if (_encoder_counter == 0)
|
||||||
|
{
|
||||||
|
BaseType_t err = xTaskCreatePinnedToCore(&_zh_encoder_isr_processing_task, "zh_encoder_isr_processing", config->stack_size, NULL, config->task_priority,
|
||||||
|
&zh_encoder, tskNO_AFFINITY);
|
||||||
|
ZH_ERROR_CHECK(err == pdPASS, ESP_FAIL, NULL, "Failed to create isr processing task.");
|
||||||
|
}
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool IRAM_ATTR _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx)
|
||||||
{
|
{
|
||||||
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)arg;
|
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
encoder_handle->encoder_state = _encoder_matrix[encoder_handle->encoder_state & 0x0F]
|
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)user_ctx;
|
||||||
[(gpio_get_level(encoder_handle->b_gpio_number) << 1) | gpio_get_level(encoder_handle->a_gpio_number)];
|
pcnt_unit_clear_count(unit);
|
||||||
switch (encoder_handle->encoder_state & 0x30)
|
switch (edata->watch_point_value)
|
||||||
{
|
{
|
||||||
case ZH_ENCODER_DIRECTION_CW:
|
case ZH_ENCODER_DIRECTION_CW:
|
||||||
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
|
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
|
||||||
@@ -175,7 +313,6 @@ static void IRAM_ATTR _zh_encoder_isr_handler(void *arg)
|
|||||||
{
|
{
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_max_value;
|
encoder_handle->encoder_position = encoder_handle->encoder_max_value;
|
||||||
}
|
}
|
||||||
xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ZH_ENCODER_DIRECTION_CCW:
|
case ZH_ENCODER_DIRECTION_CCW:
|
||||||
@@ -186,33 +323,66 @@ static void IRAM_ATTR _zh_encoder_isr_handler(void *arg)
|
|||||||
{
|
{
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_min_value;
|
encoder_handle->encoder_position = encoder_handle->encoder_min_value;
|
||||||
}
|
}
|
||||||
xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
return false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
zh_encoder_event_on_isr_t encoder_data = {0};
|
||||||
|
encoder_data.encoder_number = encoder_handle->encoder_number;
|
||||||
|
encoder_data.encoder_position = encoder_handle->encoder_position;
|
||||||
|
if (xQueueSendFromISR(_queue_handle, &encoder_data, &xHigherPriorityTaskWoken) != pdTRUE)
|
||||||
|
{
|
||||||
|
++_stats.queue_overflow_error;
|
||||||
|
}
|
||||||
|
if (xHigherPriorityTaskWoken == pdTRUE)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
||||||
|
{
|
||||||
|
zh_encoder_event_on_isr_t encoder_data = {0};
|
||||||
|
while (xQueueReceive(_queue_handle, &encoder_data, portMAX_DELAY) == pdTRUE)
|
||||||
|
{
|
||||||
|
esp_err_t err = esp_event_post(ZH_ENCODER, ZH_ENCODER_EVENT, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
|
||||||
|
if (err != ESP_OK)
|
||||||
|
{
|
||||||
|
++_stats.event_post_error;
|
||||||
|
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
||||||
|
}
|
||||||
|
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
||||||
|
}
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void IRAM_ATTR _zh_encoder_button_isr_handler(void *arg)
|
||||||
|
{
|
||||||
|
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)arg;
|
||||||
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
|
uint64_t _current_us = esp_timer_get_time();
|
||||||
|
if (_current_us - encoder_handle->s_gpio_prev_time >= encoder_handle->s_gpio_debounce_time)
|
||||||
|
{
|
||||||
|
bool s_gpio_status = gpio_get_level((gpio_num_t)encoder_handle->s_gpio_number);
|
||||||
|
if (encoder_handle->s_gpio_status != s_gpio_status)
|
||||||
|
{
|
||||||
|
encoder_handle->s_gpio_status = s_gpio_status;
|
||||||
|
zh_encoder_button_event_on_isr_t encoder_data = {0};
|
||||||
|
encoder_data.encoder_number = encoder_handle->encoder_number;
|
||||||
|
encoder_data.button_status = encoder_handle->s_gpio_status;
|
||||||
|
esp_err_t err = esp_event_isr_post(ZH_ENCODER, ZH_BUTTON_EVENT, &encoder_data, sizeof(zh_encoder_button_event_on_isr_t), &xHigherPriorityTaskWoken);
|
||||||
|
if (err != ESP_OK)
|
||||||
|
{
|
||||||
|
++_stats.event_post_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
encoder_handle->s_gpio_prev_time = _current_us;
|
||||||
if (xHigherPriorityTaskWoken == pdTRUE)
|
if (xHigherPriorityTaskWoken == pdTRUE)
|
||||||
{
|
{
|
||||||
portYIELD_FROM_ISR();
|
portYIELD_FROM_ISR();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
|
||||||
{
|
|
||||||
zh_encoder_handle_t queue = {0};
|
|
||||||
zh_encoder_event_on_isr_t encoder_data = {0};
|
|
||||||
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
|
|
||||||
{
|
|
||||||
ZH_ENCODER_LOGI("Encoder isr processing begin.");
|
|
||||||
encoder_data.encoder_number = queue.encoder_number;
|
|
||||||
encoder_data.encoder_position = queue.encoder_position;
|
|
||||||
esp_err_t err = esp_event_post(ZH_ENCODER, 0, &encoder_data, sizeof(zh_encoder_event_on_isr_t), portTICK_PERIOD_MS);
|
|
||||||
if (err != ESP_OK)
|
|
||||||
{
|
|
||||||
ZH_ENCODER_LOGE_ERR("Encoder isr processing failed. Failed to post interrupt event.", err);
|
|
||||||
}
|
|
||||||
ZH_ENCODER_LOGI("Encoder isr processing completed successfully.");
|
|
||||||
}
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user