Compare commits
8 Commits
d5c94158c2
...
c4d6bf7dec
| Author | SHA1 | Date | |
|---|---|---|---|
| c4d6bf7dec | |||
| d8c6e2679f | |||
| 6b2ed520d0 | |||
| 232933650d | |||
| 37ae299426 | |||
| 2060a56b55 | |||
| deb882beb6 | |||
| 5087b9b743 |
42
README.md
42
README.md
@@ -1,25 +1,20 @@
|
|||||||
# 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.1](https://docs.espressif.com/projects/esp-idf/en/v5.5.1/esp32/index.html)
|
||||||
2. [ESP32 ESP-IDF v5.4](https://docs.espressif.com/projects/esp-idf/en/release-v5.4/esp32/index.html)
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
1. Support some encoders on one device.
|
1. Support some encoders on one device.
|
||||||
|
|
||||||
## Note
|
|
||||||
|
|
||||||
1. Encoder pins must be pull up to the VCC via 0.1 µf capacitors.
|
|
||||||
|
|
||||||
## Using
|
## Using
|
||||||
|
|
||||||
In an existing project, run the following command to install the components:
|
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:
|
||||||
@@ -41,31 +36,38 @@ void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t ev
|
|||||||
|
|
||||||
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.task_priority = 5;
|
||||||
|
encoder_init_config.stack_size = configMINIMAL_STACK_SIZE;
|
||||||
|
encoder_init_config.queue_size = 3;
|
||||||
encoder_init_config.a_gpio_number = GPIO_NUM_27;
|
encoder_init_config.a_gpio_number = GPIO_NUM_27;
|
||||||
encoder_init_config.b_gpio_number = GPIO_NUM_26;
|
encoder_init_config.b_gpio_number = GPIO_NUM_26;
|
||||||
// encoder_init_config.encoder_min_value = -10; // Just for example.
|
encoder_init_config.encoder_min_value = -10;
|
||||||
// encoder_init_config.encoder_max_value = 20; // Just for example.
|
encoder_init_config.encoder_max_value = 20;
|
||||||
// encoder_init_config.encoder_step = 0.1; // Just for example.
|
encoder_init_config.encoder_step = 0.1;
|
||||||
encoder_init_config.encoder_number = 1;
|
encoder_init_config.encoder_number = 1;
|
||||||
zh_encoder_init(&encoder_init_config, &encoder_handle);
|
zh_encoder_init(&encoder_init_config, &encoder_handle);
|
||||||
double position = 0;
|
double position = 0;
|
||||||
zh_encoder_get(&encoder_handle, &position);
|
zh_encoder_get(&encoder_handle, &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.2f.\n", position);
|
||||||
// zh_encoder_set(&encoder_handle, 5); // Just for example.
|
zh_encoder_set(&encoder_handle, 5);
|
||||||
// zh_encoder_reset(&encoder_handle); // Just for example.
|
zh_encoder_reset(&encoder_handle);
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
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;
|
zh_encoder_event_on_isr_t *event = event_data;
|
||||||
printf("Encoder number %d position %0.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.
|
printf("Encoder number %d position %0.2f.\n", event->encoder_number, event->encoder_position);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -13,16 +13,16 @@
|
|||||||
/**
|
/**
|
||||||
* @brief Encoder initial default values.
|
* @brief Encoder initial default values.
|
||||||
*/
|
*/
|
||||||
#define ZH_ENCODER_INIT_CONFIG_DEFAULT() \
|
#define ZH_ENCODER_INIT_CONFIG_DEFAULT() \
|
||||||
{ \
|
{ \
|
||||||
.task_priority = 10, \
|
.task_priority = 1, \
|
||||||
.stack_size = 3072, \
|
.stack_size = configMINIMAL_STACK_SIZE, \
|
||||||
.queue_size = 10, \
|
.queue_size = 1, \
|
||||||
.a_gpio_number = 0, \
|
.a_gpio_number = GPIO_NUM_MAX, \
|
||||||
.b_gpio_number = 0, \
|
.b_gpio_number = GPIO_NUM_MAX, \
|
||||||
.encoder_min_value = -100, \
|
.encoder_min_value = -100, \
|
||||||
.encoder_max_value = 100, \
|
.encoder_max_value = 100, \
|
||||||
.encoder_step = 1, \
|
.encoder_step = 1, \
|
||||||
.encoder_number = 0}
|
.encoder_number = 0}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@@ -37,9 +37,9 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
typedef struct
|
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. */
|
uint8_t task_priority; /*!< Task priority for the encoder isr processing. @note Minimum value is 1. */
|
||||||
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is 3072 bytes. */
|
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
|
||||||
uint8_t queue_size; /*!< Queue size for task for the encoder processing. @note It is not recommended to set a value less than 10. */
|
uint8_t queue_size; /*!< Queue size for task for the encoder processing. @note Minimum value is 1. */
|
||||||
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
|
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
|
||||||
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
|
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
|
||||||
int32_t encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */
|
int32_t encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */
|
||||||
@@ -64,6 +64,16 @@ extern "C"
|
|||||||
bool is_initialized; /*!< Encoder initialization flag. */
|
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;
|
||||||
|
|
||||||
ESP_EVENT_DECLARE_BASE(ZH_ENCODER);
|
ESP_EVENT_DECLARE_BASE(ZH_ENCODER);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,6 +103,13 @@ 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 Set encoder position.
|
* @brief Set encoder position.
|
||||||
*
|
*
|
||||||
@@ -124,6 +141,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
|
1.2.0
|
||||||
186
zh_encoder.c
186
zh_encoder.c
@@ -27,13 +27,14 @@ static const uint8_t _encoder_matrix[7][4] = {
|
|||||||
|
|
||||||
TaskHandle_t zh_encoder = NULL;
|
TaskHandle_t zh_encoder = NULL;
|
||||||
static QueueHandle_t _queue_handle = NULL;
|
static QueueHandle_t _queue_handle = NULL;
|
||||||
static bool _is_initialized = false;
|
static uint8_t _encoder_counter = 0;
|
||||||
|
static bool _is_prev_gpio_isr_handler = false;
|
||||||
|
static zh_encoder_stats_t _stats = {0};
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config);
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config);
|
||||||
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config);
|
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_configure_interrupts(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_init_resources(const zh_encoder_init_config_t *config);
|
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config);
|
||||||
static esp_err_t _zh_encoder_create_task(const zh_encoder_init_config_t *config);
|
|
||||||
static void _zh_encoder_isr_handler(void *arg);
|
static void _zh_encoder_isr_handler(void *arg);
|
||||||
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
||||||
|
|
||||||
@@ -42,37 +43,74 @@ 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)
|
||||||
{
|
{
|
||||||
ZH_LOGI("Encoder initialization started.");
|
ZH_LOGI("Encoder initialization started.");
|
||||||
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument.");
|
||||||
esp_err_t err = _zh_encoder_validate_config(config);
|
esp_err_t err = _zh_encoder_validate_config(config);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Initial configuration check failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Initial configuration check failed.");
|
||||||
ZH_LOGI("Encoder initial configuration check completed successfully.");
|
err = _zh_encoder_gpio_init(config, handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. GPIO initialization failed.");
|
||||||
|
err = _zh_encoder_resources_init(config);
|
||||||
|
if (_is_prev_gpio_isr_handler == true)
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number); gpio_reset_pin((gpio_num_t)config->a_gpio_number);
|
||||||
|
gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number); gpio_uninstall_isr_service();
|
||||||
|
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
||||||
|
}
|
||||||
|
err = _zh_encoder_task_init(config);
|
||||||
|
if (_is_prev_gpio_isr_handler == true)
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL; gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number);
|
||||||
|
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Processing task initialization failed.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL; gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number);
|
||||||
|
gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Processing task initialization failed.");
|
||||||
|
}
|
||||||
|
handle->a_gpio_number = config->a_gpio_number;
|
||||||
|
handle->b_gpio_number = config->b_gpio_number;
|
||||||
handle->encoder_number = config->encoder_number;
|
handle->encoder_number = config->encoder_number;
|
||||||
handle->encoder_min_value = config->encoder_min_value;
|
handle->encoder_min_value = config->encoder_min_value;
|
||||||
handle->encoder_max_value = config->encoder_max_value;
|
handle->encoder_max_value = config->encoder_max_value;
|
||||||
handle->encoder_step = config->encoder_step;
|
handle->encoder_step = config->encoder_step;
|
||||||
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
||||||
err = _zh_encoder_gpio_init(config);
|
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. GPIO initialization failed.");
|
|
||||||
ZH_LOGI("Encoder GPIO initialization completed successfully.");
|
|
||||||
handle->a_gpio_number = config->a_gpio_number;
|
|
||||||
handle->b_gpio_number = config->b_gpio_number;
|
|
||||||
err = _zh_encoder_configure_interrupts(config, handle);
|
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Interrupt initialization failed.");
|
|
||||||
ZH_LOGI("Encoder interrupt initialization completed successfully.");
|
|
||||||
err = _zh_encoder_init_resources(config);
|
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Resources initialization failed.");
|
|
||||||
ZH_LOGI("Encoder resources initialization completed successfully.");
|
|
||||||
err = _zh_encoder_create_task(config);
|
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Processing task initialization failed.");
|
|
||||||
ZH_LOGI("Encoder processing task initialization completed successfully.");
|
|
||||||
handle->is_initialized = true;
|
handle->is_initialized = true;
|
||||||
_is_initialized = true;
|
++_encoder_counter;
|
||||||
ZH_LOGI("Encoder initialization completed successfully.");
|
ZH_LOGI("Encoder initialization completed successfully.");
|
||||||
return ESP_OK;
|
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.");
|
||||||
|
gpio_isr_handler_remove((gpio_num_t)handle->a_gpio_number);
|
||||||
|
gpio_isr_handler_remove((gpio_num_t)handle->b_gpio_number);
|
||||||
|
gpio_reset_pin((gpio_num_t)handle->a_gpio_number);
|
||||||
|
gpio_reset_pin((gpio_num_t)handle->b_gpio_number);
|
||||||
|
if (_encoder_counter == 1)
|
||||||
|
{
|
||||||
|
vQueueDelete(_queue_handle);
|
||||||
|
vTaskDelete(zh_encoder);
|
||||||
|
if (_is_prev_gpio_isr_handler == false)
|
||||||
|
{
|
||||||
|
gpio_uninstall_isr_service();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
handle->is_initialized = false;
|
||||||
|
--_encoder_counter;
|
||||||
|
ZH_LOGI("Encoder deinitialization completed successfully.");
|
||||||
|
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_LOGI("Encoder set position started.");
|
ZH_LOGI("Encoder set position started.");
|
||||||
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "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(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.");
|
ZH_ERROR_CHECK(position <= handle->encoder_max_value && position >= handle->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Encoder set position failed. Invalid argument.");
|
||||||
handle->encoder_position = position;
|
handle->encoder_position = position;
|
||||||
@@ -83,6 +121,7 @@ esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, double position)
|
|||||||
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_LOGI("Encoder get position started.");
|
ZH_LOGI("Encoder get position started.");
|
||||||
|
ZH_ERROR_CHECK(handle != 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.");
|
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_LOGI("Encoder get position completed successfully.");
|
ZH_LOGI("Encoder get position completed successfully.");
|
||||||
@@ -92,26 +131,41 @@ esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, double *position)
|
|||||||
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle)
|
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle)
|
||||||
{
|
{
|
||||||
ZH_LOGI("Encoder reset started.");
|
ZH_LOGI("Encoder reset started.");
|
||||||
|
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.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder reset failed. Encoder not initialized.");
|
||||||
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_LOGI("Encoder reset completed successfully.");
|
ZH_LOGI("Encoder reset completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const zh_encoder_stats_t *zh_encoder_get_stats(void)
|
||||||
|
{
|
||||||
|
return &_stats;
|
||||||
|
}
|
||||||
|
|
||||||
|
void zh_encoder_reset_stats(void)
|
||||||
|
{
|
||||||
|
ZH_LOGI("Error statistic reset started.");
|
||||||
|
_stats.event_post_error = 0;
|
||||||
|
_stats.queue_overflow_error = 0;
|
||||||
|
_stats.min_stack_size = 0;
|
||||||
|
ZH_LOGI("Error statistic reset successfully.");
|
||||||
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config)
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config)
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid configuration.");
|
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid configuration.");
|
||||||
ZH_ERROR_CHECK(config->task_priority >= 10 && config->stack_size >= 3072, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
|
ZH_ERROR_CHECK(config->task_priority >= 1 && config->stack_size >= configMINIMAL_STACK_SIZE, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
|
||||||
ZH_ERROR_CHECK(config->queue_size >= 10, ESP_ERR_INVALID_ARG, NULL, "Invalid queue size.");
|
ZH_ERROR_CHECK(config->queue_size >= 1, ESP_ERR_INVALID_ARG, NULL, "Invalid queue size.");
|
||||||
ZH_ERROR_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder min/max value.");
|
ZH_ERROR_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder min/max value.");
|
||||||
ZH_ERROR_CHECK(config->encoder_step > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder step.");
|
ZH_ERROR_CHECK(config->encoder_step > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder step.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config)
|
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle)
|
||||||
{
|
{
|
||||||
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 < 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, "Invalid GPIO number.")
|
ZH_ERROR_CHECK(config->a_gpio_number != config->b_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Both GPIO is same.")
|
||||||
gpio_config_t pin_config = {
|
gpio_config_t pin_config = {
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
.pin_bit_mask = (1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number),
|
.pin_bit_mask = (1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number),
|
||||||
@@ -119,42 +173,45 @@ static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config)
|
|||||||
.intr_type = GPIO_INTR_ANYEDGE};
|
.intr_type = GPIO_INTR_ANYEDGE};
|
||||||
esp_err_t err = gpio_config(&pin_config);
|
esp_err_t err = gpio_config(&pin_config);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "GPIO initialization failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "GPIO initialization failed.");
|
||||||
return ESP_OK;
|
if (_encoder_counter == 0)
|
||||||
}
|
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_configure_interrupts(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle)
|
|
||||||
{
|
|
||||||
gpio_install_isr_service(0);
|
|
||||||
esp_err_t err = gpio_isr_handler_add(config->a_gpio_number, _zh_encoder_isr_handler, handle);
|
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Interrupt initialization failed.");
|
|
||||||
err = gpio_isr_handler_add(config->b_gpio_number, _zh_encoder_isr_handler, handle);
|
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Interrupt initialization failed.");
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
||||||
ZH_ERROR_CHECK(_queue_handle != NULL, ESP_FAIL, NULL, "Queue creation failed.");
|
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, err, gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Failed install isr service.");
|
||||||
|
if (err == ESP_ERR_INVALID_STATE)
|
||||||
|
{
|
||||||
|
_is_prev_gpio_isr_handler = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = gpio_isr_handler_add(config->a_gpio_number, _zh_encoder_isr_handler, handle);
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
|
||||||
|
err = gpio_isr_handler_add(config->b_gpio_number, _zh_encoder_isr_handler, handle);
|
||||||
|
if (_is_prev_gpio_isr_handler == true)
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
|
||||||
}
|
}
|
||||||
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_handle_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,
|
return ESP_OK;
|
||||||
NULL,
|
}
|
||||||
config->task_priority,
|
|
||||||
&zh_encoder,
|
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config)
|
||||||
tskNO_AFFINITY);
|
{
|
||||||
ZH_ERROR_CHECK(err == pdPASS, ESP_FAIL, NULL, "Task creation failed.");
|
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;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -163,8 +220,7 @@ static void IRAM_ATTR _zh_encoder_isr_handler(void *arg)
|
|||||||
{
|
{
|
||||||
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)arg;
|
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]
|
encoder_handle->encoder_state = _encoder_matrix[encoder_handle->encoder_state & 0x0F][(gpio_get_level(encoder_handle->b_gpio_number) << 1) | gpio_get_level(encoder_handle->a_gpio_number)];
|
||||||
[(gpio_get_level(encoder_handle->b_gpio_number) << 1) | gpio_get_level(encoder_handle->a_gpio_number)];
|
|
||||||
switch (encoder_handle->encoder_state & 0x30)
|
switch (encoder_handle->encoder_state & 0x30)
|
||||||
{
|
{
|
||||||
case ZH_ENCODER_DIRECTION_CW:
|
case ZH_ENCODER_DIRECTION_CW:
|
||||||
@@ -175,7 +231,10 @@ 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);
|
if (xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken) != pdTRUE)
|
||||||
|
{
|
||||||
|
++_stats.queue_overflow_error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ZH_ENCODER_DIRECTION_CCW:
|
case ZH_ENCODER_DIRECTION_CCW:
|
||||||
@@ -186,7 +245,10 @@ 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);
|
if (xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken) != pdTRUE)
|
||||||
|
{
|
||||||
|
++_stats.queue_overflow_error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -204,15 +266,15 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
|||||||
zh_encoder_event_on_isr_t encoder_data = {0};
|
zh_encoder_event_on_isr_t encoder_data = {0};
|
||||||
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
|
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
|
||||||
{
|
{
|
||||||
ZH_LOGI("Encoder isr processing begin.");
|
|
||||||
encoder_data.encoder_number = queue.encoder_number;
|
encoder_data.encoder_number = queue.encoder_number;
|
||||||
encoder_data.encoder_position = queue.encoder_position;
|
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);
|
esp_err_t err = esp_event_post(ZH_ENCODER, 0, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
|
||||||
if (err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
|
++_stats.event_post_error;
|
||||||
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
||||||
}
|
}
|
||||||
ZH_LOGI("Encoder isr processing completed successfully.");
|
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
||||||
}
|
}
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user