feat!: main structure of handle changed to double pointer
This commit is contained in:
1 parent
73a2b00c2a
commit
be6f843c9a
5 files changed
+319
-160
No files matched your search
+89
-60
@@ -1,5 +1,22 @@
|
||||
/**
|
||||
* @file zh_ttp229.h
|
||||
*
|
||||
* @brief Driver for TTP229 capacitive touch pad controller via SPI-like
|
||||
* interface (SCL/SDO) using ESP-IDF RMT peripheral.
|
||||
*
|
||||
* This module provides a FreeRTOS-based driver for the TTP229 touch
|
||||
* controller. It uses the RMT transmitter to generate SCL clock pulses
|
||||
* and the RMT receiver to capture SDO data, enabling reading of touch
|
||||
* status from 8 or 16 pad configurations. Each touch event triggers an
|
||||
* ISR that initiates RMT transfer and posts an event with the touched
|
||||
* pad number.
|
||||
*
|
||||
* Key features:
|
||||
* - Supports 8-pad and 16-pad TTP229 configurations
|
||||
* - RMT-based SPI-like communication (no GPIO bit-banging)
|
||||
* - FreeRTOS task for debouncing and event dispatching
|
||||
* - Per-device unique numbering for multi-device support
|
||||
* - Error statistics tracking (RMT, queue, stack)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@@ -11,10 +28,8 @@
|
||||
#include "driver/rmt_rx.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_event.h"
|
||||
#include "zh_vector.h"
|
||||
|
||||
/**
|
||||
* @brief TTP229 touch pad initial default values.
|
||||
*/
|
||||
#define ZH_TTP229_INIT_CONFIG_DEFAULT() \
|
||||
{ \
|
||||
.task_priority = 1, \
|
||||
@@ -32,108 +47,122 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern TaskHandle_t zh_ttp229; /*!< Unique task handle. */
|
||||
/**
|
||||
* @brief External task handle for the TTP229 ISR processing task.
|
||||
*/
|
||||
extern TaskHandle_t zh_ttp229;
|
||||
|
||||
/**
|
||||
* @brief Enumeration of TTP229 touch pad work mode.
|
||||
* @brief Opaque handle returned by zh_ttp229_init().
|
||||
*
|
||||
* Internal fields are defined in zh_ttp229.c. Users must not access
|
||||
* the handle's members directly.
|
||||
*/
|
||||
typedef struct _zh_ttp229_handle_t zh_ttp229_handle_t;
|
||||
|
||||
/**
|
||||
* @brief TTP229 touch pad work mode (number of pads).
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ZH_TTP229_8_PAD = 8,
|
||||
ZH_TTP229_16_PAD = 16,
|
||||
ZH_TTP229_8_PAD = 8, /*!< 8-pad TTP229 configuration */
|
||||
ZH_TTP229_16_PAD = 16, /*!< 16-pad TTP229 configuration */
|
||||
} zh_ttp229_work_mode_t;
|
||||
|
||||
/**
|
||||
* @brief Structure for initial initialization of TTP229 touch pad.
|
||||
* @brief Configuration structure for TTP229 initialization.
|
||||
*
|
||||
* Use ZH_TTP229_INIT_CONFIG_DEFAULT() to obtain sensible defaults
|
||||
* and override only the fields that differ from defaults.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t stack_size; /*!< Stack size for task for the TTP229 touch pad isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
|
||||
uint16_t debounce_time; /*!< Touch pad button debounce time. @note Minimum value is 10 (in milliseconds). */
|
||||
uint8_t task_priority; /*!< Task priority for the TTP229 touch pad isr processing. @note Minimum value is 1. */
|
||||
uint8_t queue_size; /*!< Queue size for task for the touch pad processing. @note Minimum value is 1. */
|
||||
uint8_t device_number; /*!< Touch pad unique number. @note Must be greater than 0. */
|
||||
uint8_t rmt_tx_start_delay; /*!< The time between the low-level pulse in the interrupt and the actual start time of the RMT signal generator (in microseconds). @note Minimum value is 15. */
|
||||
gpio_num_t sdo_gpio; /*!< SDO GPIO. */
|
||||
gpio_num_t scl_gpio; /*!< SCL GPIO. */
|
||||
zh_ttp229_work_mode_t work_mode; /*!< Touch pad work mode. */
|
||||
uint16_t stack_size; /*!< Task stack size in bytes. @note Minimum: configMINIMAL_STACK_SIZE */
|
||||
uint16_t debounce_time; /*!< Button debounce time in milliseconds. @note Minimum: 10 ms */
|
||||
uint8_t task_priority; /*!< FreeRTOS task priority. @note Minimum: 1 */
|
||||
uint8_t queue_size; /*!< Event queue depth. @note Minimum: 1 */
|
||||
uint8_t device_number; /*!< Unique device identifier. @note Must be > 0 */
|
||||
uint8_t rmt_tx_start_delay; /*!< RMT TX delay in microseconds after SDO interrupt. @note Minimum: 15 us */
|
||||
gpio_num_t sdo_gpio; /*!< SDO (Serial Data Out) GPIO pin */
|
||||
gpio_num_t scl_gpio; /*!< SCL (Serial Clock) GPIO pin */
|
||||
zh_ttp229_work_mode_t work_mode; /*!< Touch pad configuration (8 or 16 pads) */
|
||||
} zh_ttp229_init_config_t;
|
||||
|
||||
/**
|
||||
* @brief TTP229 touch pad handle.
|
||||
* @brief Error and runtime statistics for the TTP229 driver.
|
||||
*
|
||||
* Retrieved via zh_ttp229_get_stats(). Reset with zh_ttp229_reset_stats().
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
bool is_initialized; /*!< Touch pad initialization flag. */
|
||||
gpio_num_t sdo_gpio; /*!< SDO GPIO. */
|
||||
uint8_t device_number; /*!< Touch pad unique number. */
|
||||
uint8_t rmt_tx_start_delay; /*!< The time between the low-level pulse in the interrupt and the actual start time of the RMT signal generator (in microseconds). */
|
||||
uint16_t debounce_time; /*!< Touch pad button debounce_time. */
|
||||
zh_ttp229_work_mode_t work_mode; /*!< Touch pad work mode. */
|
||||
rmt_symbol_word_t rx_symbols; /*!< Received symbols. */
|
||||
rmt_channel_handle_t tx_channel; /*!< Unique RMT TX device handle. */
|
||||
rmt_channel_handle_t rx_channel; /*!< Unique RMT RX device handle. */
|
||||
rmt_encoder_handle_t copy_encoder; /*!< Unique copy encoder handle. */
|
||||
} zh_ttp229_handle_t;
|
||||
|
||||
/**
|
||||
* @brief Structure for error statistics storage.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t rmt_driver_error; /*!< Number of RMT driver error. */
|
||||
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. */
|
||||
uint32_t rmt_driver_error; /*!< Count of RMT driver errors */
|
||||
uint32_t event_post_error; /*!< Count of esp_event_post failures */
|
||||
uint32_t queue_overflow_error; /*!< Count of event queue overflows */
|
||||
uint32_t min_stack_size; /*!< Minimum free stack (bytes) observed */
|
||||
} zh_ttp229_stats_t;
|
||||
|
||||
ESP_EVENT_DECLARE_BASE(ZH_TTP229);
|
||||
|
||||
/**
|
||||
* @brief Structure for sending data to the event handler when cause an interrupt.
|
||||
* @brief Event payload delivered on touch detection.
|
||||
*
|
||||
* @note Should be used with ZH_TTP229 event base.
|
||||
* Should be used with the ZH_TTP229 event base.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t pad_number; /*!< The pad that caused the interrupt. */
|
||||
uint8_t device_number; /*!< Touch pad unique number. */
|
||||
uint8_t pad_number; /*!< Number of the pad that triggered the interrupt (1-based) */
|
||||
uint8_t device_number; /*!< Unique device identifier matching the init config */
|
||||
} zh_ttp229_event_on_isr_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize TTP229 touch pad.
|
||||
* @brief Initialize the TTP229 touch pad driver.
|
||||
*
|
||||
* @param[in] config Pointer to TTP229 initialized configuration structure. Can point to a temporary variable.
|
||||
* @param[out] handle Pointer to unique TTP229 handle.
|
||||
* Allocates a handle, validates the configuration, initializes RMT
|
||||
* TX/RMT channels, installs the GPIO ISR handler, and creates the
|
||||
* processing task. Supports multiple devices via unique device
|
||||
* numbers.
|
||||
*
|
||||
* @attention I2C driver must be initialized first.
|
||||
* @param[in] config Pointer to initialization configuration (must not be NULL)
|
||||
* @param[out] handle Pointer to unique touch pad handle (must not be NULL)
|
||||
*
|
||||
* @note Before initialize the expander recommend initialize zh_ttp229_init_config_t structure with default values.
|
||||
*
|
||||
* @code zh_ttp229_init_config_t config = ZH_TTP229_INIT_CONFIG_DEFAULT() @endcode
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_ERR_INVALID_ARG if config or handle is NULL, or if any configuration value is out of range
|
||||
* @return ESP_ERR_INVALID_STATE if the device is already initialized
|
||||
* @return ESP_FAIL on resource allocation or peripheral failure
|
||||
*/
|
||||
esp_err_t zh_ttp229_init(const zh_ttp229_init_config_t *config, zh_ttp229_handle_t *handle);
|
||||
esp_err_t zh_ttp229_init(const zh_ttp229_init_config_t *config, zh_ttp229_handle_t **handle);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize TTP229 touch pad.
|
||||
* @brief Deinitialize a TTP229 touch pad instance.
|
||||
*
|
||||
* @param[in] handle Pointer to unique TTP229 handle.
|
||||
* Disables RMT channels, removes the GPIO ISR handler, frees the
|
||||
* handle, and removes the device from the internal device list.
|
||||
* The processing task and shared resources are deleted only when
|
||||
* the last device is deinitialized.
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
* @param[in,out] handle Pointer to unique touch pad handle (must not be NULL)
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_ERR_INVALID_ARG if handle is NULL or points to a device not found in the internal list
|
||||
*/
|
||||
esp_err_t zh_ttp229_deinit(zh_ttp229_handle_t *handle);
|
||||
esp_err_t zh_ttp229_deinit(zh_ttp229_handle_t **handle);
|
||||
|
||||
/**
|
||||
* @brief Get error statistics.
|
||||
* @brief Retrieve a pointer to the current error statistics.
|
||||
*
|
||||
* @return Pointer to the statistics structure.
|
||||
* The returned pointer remains valid until zh_ttp229_reset_stats()
|
||||
* is called or the module is deinitialized.
|
||||
*
|
||||
* @return Pointer to the static zh_ttp229_stats_t structure
|
||||
*/
|
||||
const zh_ttp229_stats_t *zh_ttp229_get_stats(void);
|
||||
|
||||
/**
|
||||
* @brief Reset error statistics.
|
||||
* @brief Reset all error and runtime statistics to zero.
|
||||
*
|
||||
* Call this function to clear accumulated counters. The
|
||||
* min_stack_size field is also cleared and will be updated on the
|
||||
* next processing cycle.
|
||||
*/
|
||||
void zh_ttp229_reset_stats(void);
|
||||
|
||||
|
||||
Reference in new issue
Block a user