Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42e1d12ec4 | ||
|
|
5dfc8b9033 | ||
|
|
f02dc77b28 | ||
|
|
dbe7fce7ce | ||
|
|
0e9f663a8d | ||
|
|
f18ee9ba77 | ||
|
|
88907c3705 | ||
|
|
46762415d2 | ||
|
|
a14888aab0 | ||
|
|
54841bfd79 |
No files matched your search
+6
-1
@@ -1 +1,6 @@
|
||||
idf_component_register(SRCS "zh_ac_dimmer.c" INCLUDE_DIRS "include" REQUIRES driver esp_timer)
|
||||
if(${IDF_VERSION_MAJOR} STREQUAL 6)
|
||||
set(requires esp_driver_gpio esp_driver_gptimer esp_timer)
|
||||
else()
|
||||
set(requires driver esp_timer)
|
||||
endif()
|
||||
idf_component_register(SRCS "zh_ac_dimmer.c" INCLUDE_DIRS "include" REQUIRES ${requires})
|
||||
@@ -1,17 +1,15 @@
|
||||
# ESP32 ESP-IDF component for AC dimmer 115V/400Hz
|
||||
# ESP32 ESP-IDF component for AC dimmer for 115V/400Hz
|
||||
|
||||
## Tested on
|
||||
## Features
|
||||
|
||||
1. [ESP32 ESP-IDF v5.5.3](https://docs.espressif.com/projects/esp-idf/en/v5.5.3/esp32/index.html)
|
||||
1. Zero-crossing detection via GPIO interrupt for precise phase-angle control.
|
||||
2. TRIAC gate control through ESP-IDF gptimer with configurable dimming level (0–100%).
|
||||
3. IRAM-safe ISR and callback functions for reliable operation.
|
||||
4. Thread-safe start/stop/set/get API.
|
||||
|
||||
## SAST Tools
|
||||
## Note
|
||||
|
||||
[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.
|
||||
|
||||
## Attention
|
||||
|
||||
1. Supports frequency 400Hz only.
|
||||
2. For correct operation, please enable the following settings in the menuconfig:
|
||||
Enable the following settings in menuconfig:
|
||||
|
||||
```text
|
||||
GPIO_CTRL_FUNC_IN_IRAM
|
||||
@@ -22,9 +20,9 @@ CONFIG_GPTIMER_ISR_CACHE_SAFE
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the components:
|
||||
In an existing project, run the following command to install the component:
|
||||
|
||||
```text
|
||||
```bash
|
||||
cd ../your_project/components
|
||||
git clone https://github.com/aZholtikov/zh_ac_dimmer
|
||||
```
|
||||
@@ -35,35 +33,37 @@ In the application, add the component:
|
||||
#include "zh_ac_dimmer.h"
|
||||
```
|
||||
|
||||
## Examples
|
||||
## Example
|
||||
|
||||
```c
|
||||
#include "zh_ac_dimmer.h"
|
||||
|
||||
zh_ac_dimmer_handle_t *ac_dimmer_handle = NULL;
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
esp_log_level_set("zh_ac_dimmer", ESP_LOG_ERROR);
|
||||
zh_ac_dimmer_init_config_t config = ZH_AC_DIMMER_INIT_CONFIG_DEFAULT();
|
||||
config.zero_cross_gpio = GPIO_NUM_16;
|
||||
config.triac_gpio = GPIO_NUM_17;
|
||||
zh_ac_dimmer_init(&config);
|
||||
zh_ac_dimmer_start();
|
||||
zh_ac_dimmer_init(&config, &ac_dimmer_handle);
|
||||
zh_ac_dimmer_start(&ac_dimmer_handle);
|
||||
uint8_t value = 0;
|
||||
for (uint8_t i = 0; i <= 100; ++i)
|
||||
{
|
||||
zh_ac_dimmer_set(i);
|
||||
zh_ac_dimmer_get(&value);
|
||||
zh_ac_dimmer_set(&ac_dimmer_handle, i);
|
||||
zh_ac_dimmer_get(&ac_dimmer_handle, &value);
|
||||
printf("Dimmer value: %d.\n", value);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
for (uint8_t i = 100; i > 0; --i)
|
||||
{
|
||||
zh_ac_dimmer_set(i);
|
||||
zh_ac_dimmer_get(&value);
|
||||
zh_ac_dimmer_set(&ac_dimmer_handle, i);
|
||||
zh_ac_dimmer_get(&ac_dimmer_handle, &value);
|
||||
printf("Dimmer value: %d.\n", value);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
zh_ac_dimmer_stop();
|
||||
zh_ac_dimmer_deinit();
|
||||
zh_ac_dimmer_stop(&ac_dimmer_handle);
|
||||
zh_ac_dimmer_deinit(&ac_dimmer_handle);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,8 @@
|
||||
description: ESP32 ESP-IDF component for AC dimmer control
|
||||
url: https://github.com/aZholtikov/zh_ac_dimmer
|
||||
version: "3.0.0"
|
||||
license: "Apache-2.0"
|
||||
maintainers:
|
||||
- "Alexey Zholtikov <alexey.zholtikov@yandex.ru>"
|
||||
dependencies:
|
||||
idf: ">=5.0"
|
||||
+94
-30
@@ -1,5 +1,24 @@
|
||||
/**
|
||||
* @file zh_ac_dimmer.h
|
||||
*
|
||||
* @brief Header file for AC dimmer driver based on zero-crossing detection and TRIAC control.
|
||||
*
|
||||
* This module provides an interface for controlling AC power via phase-angle
|
||||
* dimming using ESP-IDF gptimer and GPIO peripherals. It detects the AC
|
||||
* mains zero-crossing point and fires the TRIAC with a programmable delay
|
||||
* to regulate the average power delivered to the load.
|
||||
*
|
||||
* Key features:
|
||||
* - Zero-crossing detection via GPIO interrupt
|
||||
* - TRIAC gate control through ESP-IDF gptimer
|
||||
* - Configurable dimming level (0–100 %)
|
||||
* - Thread-safe start/stop/set/get API
|
||||
*
|
||||
* @note The driver assumes a 400 Hz AC mains frequency (1250 µs period).
|
||||
* Adjust the timing constants in `zh_ac_dimmer.c` for other frequencies.
|
||||
* @warning This module interacts with mains voltage. Incorrect wiring or
|
||||
* insufficient isolation can cause electric shock or hardware damage.
|
||||
* Ensure proper opto-isolation and safety compliance before deployment.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@@ -11,9 +30,6 @@
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
|
||||
/**
|
||||
* @brief AC dimmer initial default values.
|
||||
*/
|
||||
#define ZH_AC_DIMMER_INIT_CONFIG_DEFAULT() \
|
||||
{ \
|
||||
.zero_cross_gpio = GPIO_NUM_MAX, \
|
||||
@@ -25,65 +41,113 @@ extern "C"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Structure for initial initialization of AC dimmer.
|
||||
* @brief Opaque handle representing an AC dimmer instance.
|
||||
*
|
||||
* Internal fields are defined in `zh_ac_dimmer.c` and must not be
|
||||
* accessed directly by user code.
|
||||
*/
|
||||
typedef struct _zh_ac_dimmer_handle_t zh_ac_dimmer_handle_t;
|
||||
|
||||
/**
|
||||
* @brief Configuration structure for AC dimmer initialization.
|
||||
*
|
||||
* Specifies the GPIO pins used for zero-crossing detection and TRIAC
|
||||
* gate control. Both pins must be valid and different from each other.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t zero_cross_gpio; /*!< Zero cross GPIO. */
|
||||
uint8_t triac_gpio; /*!< Triac GPIO. */
|
||||
gpio_num_t zero_cross_gpio; /*!< GPIO pin for zero-crossing detection input */
|
||||
gpio_num_t triac_gpio; /*!< GPIO pin for TRIAC gate control output */
|
||||
} zh_ac_dimmer_init_config_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize AC dimmer.
|
||||
* @brief Initialize the AC dimmer driver.
|
||||
*
|
||||
* @param[in] config Pointer to AC dimmer initialized configuration structure. Can point to a temporary variable.
|
||||
* Allocates memory for the dimmer handle, validates the configuration,
|
||||
* initializes GPIO pins (TRIAC as output, zero-crossing as input with
|
||||
* positive-edge interrupt), installs the ISR service, and creates the
|
||||
* gptimer instance.
|
||||
*
|
||||
* @note Before initialize the AC dimmer recommend initialize zh_ac_dimmer_init_config_t structure with default values.
|
||||
* @param[in] config Pointer to the initialization configuration structure.
|
||||
* May point to a temporary variable (not stored internally)
|
||||
* @param[out] handle Pointer to receive the created dimmer handle (must be NULL)
|
||||
*
|
||||
* @code zh_ac_dimmer_init_config_t config = ZH_AC_DIMMER_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 handle is NULL or GPIO pins are invalid
|
||||
* @return ESP_ERR_INVALID_STATE if a dimmer instance is already initialized
|
||||
* @return ESP_ERR_NO_MEM if memory allocation failed
|
||||
* @return ESP_FAIL if GPIO, ISR service, or timer initialization failed
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_init(const zh_ac_dimmer_init_config_t *config);
|
||||
esp_err_t zh_ac_dimmer_init(const zh_ac_dimmer_init_config_t *config, zh_ac_dimmer_handle_t **handle);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize AC dimmer.
|
||||
* @brief Deinitialize the AC dimmer driver.
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
* Stops and deletes the gptimer, removes the GPIO ISR handler, resets
|
||||
* the GPIO pins to input mode, frees the dimmer handle, and sets the
|
||||
* output handle to NULL.
|
||||
*
|
||||
* @param[in,out] handle Pointer to the dimmer handle (must not be NULL)
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_ERR_INVALID_ARG if handle or *handle is NULL
|
||||
* @return ESP_FAIL if timer or GPIO cleanup failed
|
||||
*
|
||||
* @warning After calling this function, the handle is invalid and must not
|
||||
* be used. Call `zh_ac_dimmer_init()` again to reinitialize.
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_deinit(void);
|
||||
esp_err_t zh_ac_dimmer_deinit(zh_ac_dimmer_handle_t **handle);
|
||||
|
||||
/**
|
||||
* @brief Start AC dimmer.
|
||||
* @brief Start the AC dimmer.
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
* Enables the dimmer so that zero-crossing events trigger TRIAC firing
|
||||
* according to the current dimming value.
|
||||
*
|
||||
* @param[in,out] handle Pointer to the dimmer handle (must not be NULL)
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_ERR_INVALID_ARG if handle or *handle is NULL
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_start(void);
|
||||
esp_err_t zh_ac_dimmer_start(zh_ac_dimmer_handle_t **handle);
|
||||
|
||||
/**
|
||||
* @brief Stop AC dimmer.
|
||||
* @brief Stop the AC dimmer.
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
* Disables TRIAC firing on zero-crossing events. The dimmer can be
|
||||
* restarted with `zh_ac_dimmer_start()`.
|
||||
*
|
||||
* @param[in,out] handle Pointer to the dimmer handle (must not be NULL)
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_ERR_INVALID_ARG if handle or *handle is NULL
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_stop(void);
|
||||
esp_err_t zh_ac_dimmer_stop(zh_ac_dimmer_handle_t **handle);
|
||||
|
||||
/**
|
||||
* @brief Set AC dimmer dimming value.
|
||||
* @brief Set the AC dimmer brightness level.
|
||||
*
|
||||
* @param[in] value Dimming value (0 to 100).
|
||||
* Sets the dimming percentage that will be applied on the next
|
||||
* zero-crossing event.
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
* @param[in] handle Pointer to the dimmer handle (must not be NULL)
|
||||
* @param[in] value Dimming level from 0 (off) to 100 (full brightness)
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_ERR_INVALID_ARG if handle, *handle, or value is invalid
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_set(uint8_t value);
|
||||
esp_err_t zh_ac_dimmer_set(zh_ac_dimmer_handle_t **handle, uint8_t value);
|
||||
|
||||
/**
|
||||
* @brief Get AC dimmer dimming value.
|
||||
* @brief Get the current AC dimmer brightness level.
|
||||
*
|
||||
* @param[out] value Pointer to dimming value.
|
||||
* @param[in] handle Pointer to the dimmer handle (must not be NULL)
|
||||
* @param[out] value Pointer to receive the current dimming level (0–100) (must not be NULL)
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_ERR_INVALID_ARG if handle, *handle, or value is NULL
|
||||
*/
|
||||
esp_err_t zh_ac_dimmer_get(uint8_t *value);
|
||||
esp_err_t zh_ac_dimmer_get(zh_ac_dimmer_handle_t **handle, uint8_t *value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
2.0.1
|
||||
3.0.0
|
||||
+167
-101
@@ -13,100 +13,166 @@ static const char *TAG = "zh_ac_dimmer";
|
||||
return err; \
|
||||
}
|
||||
|
||||
static gptimer_handle_t _dimmer_timer = NULL;
|
||||
static gptimer_alarm_config_t _alarm_config = {0};
|
||||
|
||||
static zh_ac_dimmer_init_config_t _init_config = {0};
|
||||
static volatile uint64_t _prev_us = 0;
|
||||
static volatile uint8_t _dimmer_value = 0;
|
||||
static volatile bool _is_dimmer_work = false;
|
||||
static bool _is_initialized = false;
|
||||
/**
|
||||
* @brief Internal handle for AC dimmer instance.
|
||||
*
|
||||
* Stores GPIO pin assignments, gptimer handle, timing state,
|
||||
* current dimming level, and running state.
|
||||
*/
|
||||
struct _zh_ac_dimmer_handle_t
|
||||
{
|
||||
gpio_num_t zero_cross_gpio; /*!< GPIO pin for zero-crossing detection input */
|
||||
gpio_num_t triac_gpio; /*!< GPIO pin for TRIAC gate control output */
|
||||
gptimer_handle_t dimmer_timer_handle; /*!< gptimer instance used for TRIAC firing delay */
|
||||
uint64_t prev_zero_crossing_us; /*!< Timestamp of the last zero-crossing event (µs) */
|
||||
uint8_t dimmer_value; /*!< Current dimming level (0 = off, 100 = full) */
|
||||
bool is_dimmer_work; /*!< `true` when the dimmer is actively running */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Validate the AC dimmer initialization configuration.
|
||||
*
|
||||
* Checks that both GPIO pins are valid (not `GPIO_NUM_MAX`),
|
||||
* are within the acceptable range, and are not the same pin.
|
||||
*
|
||||
* @param config Pointer to the initialization configuration structure
|
||||
*
|
||||
* @return ESP_OK if the configuration is valid
|
||||
* @return ESP_ERR_INVALID_ARG if any GPIO pin is invalid or both pins are the same
|
||||
*/
|
||||
static esp_err_t _zh_ac_dimmer_validate_config(const zh_ac_dimmer_init_config_t *config);
|
||||
static esp_err_t _zh_ac_dimmer_gpio_init(const zh_ac_dimmer_init_config_t *config);
|
||||
static esp_err_t _zh_ac_dimmer_timer_init(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize GPIO pins for TRIAC control and zero-crossing detection.
|
||||
*
|
||||
* Configures the TRIAC GPIO as an output (pulled low) and the zero-crossing
|
||||
* GPIO as an input with pull-down and positive-edge interrupt. Installs the
|
||||
* ESP-IDF ISR service (if not already installed) and registers the ISR
|
||||
* handler for the zero-crossing pin.
|
||||
*
|
||||
* @param config Pointer to the initialization configuration structure
|
||||
* @param handle Pointer to the dimmer handle to store GPIO pin numbers
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_FAIL if GPIO configuration, ISR service installation, or ISR
|
||||
* handler registration failed
|
||||
*
|
||||
* @note `ESP_ERR_INVALID_STATE` from `gpio_install_isr_service()` is treated
|
||||
* as success because the ISR service may have been installed previously.
|
||||
*/
|
||||
static esp_err_t _zh_ac_dimmer_gpio_init(const zh_ac_dimmer_init_config_t *config, zh_ac_dimmer_handle_t *handle);
|
||||
|
||||
/**
|
||||
* @brief Initialize the gptimer used for TRIAC firing delay.
|
||||
*
|
||||
* Creates a new gptimer with a 1 MHz resolution (1 µs steps), registers
|
||||
* the alarm callback that fires the TRIAC, enables the timer, and starts
|
||||
* it in auto-reload mode.
|
||||
*
|
||||
* @param handle Pointer to the dimmer handle (timer handle stored here)
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
* @return ESP_FAIL if timer creation, callback registration, or enable failed
|
||||
*/
|
||||
static esp_err_t _zh_ac_dimmer_timer_init(zh_ac_dimmer_handle_t *handle);
|
||||
|
||||
/**
|
||||
* @brief ISR handler for zero-crossing detection.
|
||||
*
|
||||
* Executed on every positive edge of the zero-crossing signal. Resets the
|
||||
* TRIAC gate low, updates the last zero-crossing timestamp, and schedules
|
||||
* a gptimer alarm to fire the TRIAC at the appropriate phase angle.
|
||||
*
|
||||
* - At dimming = 0: TRIAC stays off (gate held low).
|
||||
* - At dimming = 100: TRIAC fires immediately (gate set high).
|
||||
* - At dimming 1–99: gptimer alarm fires after a calculated delay.
|
||||
*
|
||||
* @param arg Pointer to the dimmer handle (passed from GPIO ISR)
|
||||
*/
|
||||
static void _zh_ac_dimmer_isr_handler(void *arg);
|
||||
|
||||
/**
|
||||
* @brief gptimer alarm callback that fires the TRIAC.
|
||||
*
|
||||
* Sets the TRIAC gate high to trigger conduction, then stops and resets
|
||||
* the timer for the next half-cycle.
|
||||
*
|
||||
* @param timer Handle to the gptimer that triggered the alarm
|
||||
* @param edata Event data (unused)
|
||||
* @param user_ctx Pointer to the dimmer handle
|
||||
*
|
||||
* @return `false` to indicate that the timer should not auto-restart
|
||||
*/
|
||||
static bool _zh_ac_dimmer_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx);
|
||||
|
||||
esp_err_t zh_ac_dimmer_init(const zh_ac_dimmer_init_config_t *config) // -V2008
|
||||
esp_err_t zh_ac_dimmer_init(const zh_ac_dimmer_init_config_t *config, zh_ac_dimmer_handle_t **handle)
|
||||
{
|
||||
ZH_LOGI("AC dimmer initialization started.");
|
||||
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer initialization failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(_is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "AC dimmer initialization failed. AC dimmer is already initialized.");
|
||||
esp_err_t err = _zh_ac_dimmer_validate_config(config);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer initialization failed. Initial configuration check failed.");
|
||||
err = _zh_ac_dimmer_gpio_init(config);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer initialization failed. GPIO initialization failed.");
|
||||
err = _zh_ac_dimmer_timer_init();
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, err = gpio_isr_handler_remove((gpio_num_t)config->zero_cross_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Remove GPIO isr handler failed."); err = gpio_reset_pin((gpio_num_t)config->triac_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Reset GPIO failed."); err = gpio_reset_pin((gpio_num_t)config->zero_cross_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Reset GPIO failed."), "AC dimmer initialization failed. Timer initialization failed.");
|
||||
_init_config = *config;
|
||||
_is_initialized = true;
|
||||
|
||||
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer initialization failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(*handle == NULL, ESP_ERR_INVALID_STATE, NULL, "AC dimmer initialization failed. AC dimmer is already initialized.");
|
||||
*handle = heap_caps_calloc(1, sizeof(zh_ac_dimmer_handle_t), MALLOC_CAP_8BIT);
|
||||
ZH_ERROR_CHECK(*handle != NULL, ESP_ERR_NO_MEM, NULL, "AC dimmer initialization failed. Failed to allocate 160X handle.");
|
||||
ZH_ERROR_CHECK(_zh_ac_dimmer_validate_config(config) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "AC dimmer initialization failed. Initial configuration check failed.");
|
||||
ZH_ERROR_CHECK(_zh_ac_dimmer_gpio_init(config, *handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "AC dimmer initialization failed. GPIO initialization failed.");
|
||||
ZH_ERROR_CHECK(_zh_ac_dimmer_timer_init(*handle) == ESP_OK, ESP_FAIL,
|
||||
{ZH_ERROR_CHECK(gpio_isr_handler_remove(config->zero_cross_gpio) == ESP_OK, ESP_FAIL, NULL, "Remove GPIO isr handler failed.")};
|
||||
{ZH_ERROR_CHECK(gpio_reset_pin(config->triac_gpio) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")};
|
||||
{ZH_ERROR_CHECK(gpio_reset_pin(config->zero_cross_gpio) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")};
|
||||
heap_caps_free(*handle); *handle = NULL, "AC dimmer initialization failed. Timer initialization failed.");
|
||||
ZH_LOGI("AC dimmer initialization completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_ac_dimmer_deinit(void) // -V2008
|
||||
esp_err_t zh_ac_dimmer_deinit(zh_ac_dimmer_handle_t **handle)
|
||||
{
|
||||
ZH_LOGI("AC dimmer deinitialization started.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "AC dimmer deinitialization failed. AC dimmer is not initialized.");
|
||||
esp_err_t err = gptimer_stop(_dimmer_timer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer deinitialization failed. Timer stop fail.");
|
||||
err = gptimer_disable(_dimmer_timer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer deinitialization failed. Timer disable fail.");
|
||||
err = gptimer_del_timer(_dimmer_timer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer deinitialization failed. Timer delete fail.");
|
||||
err = gpio_isr_handler_remove((gpio_num_t)_init_config.zero_cross_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer deinitialization failed. Remove GPIO isr handler failed.");
|
||||
err = gpio_reset_pin((gpio_num_t)_init_config.triac_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer deinitialization failed. Reset GPIO failed.")
|
||||
err = gpio_reset_pin((gpio_num_t)_init_config.zero_cross_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "AC dimmer deinitialization failed. Reset GPIO failed.")
|
||||
_is_dimmer_work = false;
|
||||
_is_initialized = false;
|
||||
_dimmer_timer = NULL;
|
||||
_dimmer_value = 0;
|
||||
_prev_us = 0;
|
||||
ZH_ERROR_CHECK(handle != NULL && *handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer deinitialization failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(gptimer_stop((*handle)->dimmer_timer_handle) == ESP_OK, ESP_FAIL, NULL, "AC dimmer deinitialization failed. Timer stop fail.");
|
||||
ZH_ERROR_CHECK(gptimer_disable((*handle)->dimmer_timer_handle) == ESP_OK, ESP_FAIL, NULL, "AC dimmer deinitialization failed. Timer disable fail.");
|
||||
ZH_ERROR_CHECK(gptimer_del_timer((*handle)->dimmer_timer_handle) == ESP_OK, ESP_FAIL, NULL, "AC dimmer deinitialization failed. Timer delete fail.");
|
||||
ZH_ERROR_CHECK(gpio_isr_handler_remove((*handle)->zero_cross_gpio) == ESP_OK, ESP_FAIL, NULL, "AC dimmer deinitialization failed. Remove GPIO isr handler failed.");
|
||||
ZH_ERROR_CHECK(gpio_reset_pin((*handle)->triac_gpio) == ESP_OK, ESP_FAIL, NULL, "AC dimmer deinitialization failed. Reset GPIO failed.");
|
||||
ZH_ERROR_CHECK(gpio_reset_pin((*handle)->zero_cross_gpio) == ESP_OK, ESP_FAIL, NULL, "AC dimmer deinitialization failed. Reset GPIO failed.");
|
||||
heap_caps_free(*handle);
|
||||
*handle = NULL;
|
||||
ZH_LOGI("AC dimmer deinitialization completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_ac_dimmer_start(void)
|
||||
esp_err_t zh_ac_dimmer_start(zh_ac_dimmer_handle_t **handle)
|
||||
{
|
||||
ZH_LOGI("AC dimmer start begin.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "AC dimmer start failed. AC dimmer is not initialized.");
|
||||
_is_dimmer_work = true;
|
||||
ZH_ERROR_CHECK(handle != NULL && *handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer start failed. Invalid argument.");
|
||||
(*handle)->is_dimmer_work = true;
|
||||
ZH_LOGI("AC dimmer start completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_ac_dimmer_stop(void)
|
||||
esp_err_t zh_ac_dimmer_stop(zh_ac_dimmer_handle_t **handle)
|
||||
{
|
||||
ZH_LOGI("AC dimmer stop begin.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "AC dimmer stop failed. AC dimmer is not initialized.");
|
||||
_is_dimmer_work = false;
|
||||
ZH_ERROR_CHECK(handle != NULL && *handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer stop failed. Invalid argument.");
|
||||
(*handle)->is_dimmer_work = false;
|
||||
ZH_LOGI("AC dimmer stop completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_ac_dimmer_set(uint8_t value)
|
||||
esp_err_t zh_ac_dimmer_set(zh_ac_dimmer_handle_t **handle, uint8_t value)
|
||||
{
|
||||
ZH_LOGI("AC dimmer setup begin.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "AC dimmer setup failed. AC dimmer is not initialized.");
|
||||
ZH_ERROR_CHECK(handle != NULL && *handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer setup failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(value <= 100, ESP_ERR_INVALID_ARG, NULL, "AC dimmer setup failed. Dimming value invalid.");
|
||||
_dimmer_value = value;
|
||||
(*handle)->dimmer_value = value;
|
||||
ZH_LOGI("AC dimmer setup completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_ac_dimmer_get(uint8_t *value)
|
||||
esp_err_t zh_ac_dimmer_get(zh_ac_dimmer_handle_t **handle, uint8_t *value)
|
||||
{
|
||||
ZH_LOGI("AC dimmer getting status begin.");
|
||||
ZH_ERROR_CHECK(value != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer getting status failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "AC dimmer getting status failed. AC dimmer is not initialized.");
|
||||
*value = _dimmer_value;
|
||||
ZH_ERROR_CHECK(handle != NULL && *handle != NULL && value != NULL, ESP_ERR_INVALID_ARG, NULL, "AC dimmer getting status failed. Invalid argument.");
|
||||
*value = (*handle)->dimmer_value;
|
||||
ZH_LOGI("AC dimmer getting status completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -119,7 +185,7 @@ static esp_err_t _zh_ac_dimmer_validate_config(const zh_ac_dimmer_init_config_t
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t _zh_ac_dimmer_gpio_init(const zh_ac_dimmer_init_config_t *config) // -V2008
|
||||
static esp_err_t _zh_ac_dimmer_gpio_init(const zh_ac_dimmer_init_config_t *config, zh_ac_dimmer_handle_t *handle)
|
||||
{
|
||||
gpio_config_t triac_gpio_config = {
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
@@ -128,10 +194,8 @@ static esp_err_t _zh_ac_dimmer_gpio_init(const zh_ac_dimmer_init_config_t *confi
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
};
|
||||
esp_err_t err = gpio_config(&triac_gpio_config);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Triac GPIO configuration failed.");
|
||||
err = gpio_set_level((gpio_num_t)config->triac_gpio, 0);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Triac GPIO set failed.");
|
||||
ZH_ERROR_CHECK(gpio_config(&triac_gpio_config) == ESP_OK, ESP_FAIL, NULL, "Triac GPIO configuration failed.");
|
||||
ZH_ERROR_CHECK(gpio_set_level(config->triac_gpio, 0) == ESP_OK, ESP_FAIL, NULL, "Triac GPIO set failed.");
|
||||
gpio_config_t zero_cross_gpio_config = {
|
||||
.intr_type = GPIO_INTR_POSEDGE,
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
@@ -139,72 +203,74 @@ static esp_err_t _zh_ac_dimmer_gpio_init(const zh_ac_dimmer_init_config_t *confi
|
||||
.pull_down_en = GPIO_PULLDOWN_ENABLE,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
};
|
||||
err = gpio_config(&zero_cross_gpio_config);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, err = gpio_reset_pin((gpio_num_t)config->triac_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Reset GPIO failed."), "Zero cross GPIO configuration failed.");
|
||||
err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
||||
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, err, err = gpio_reset_pin((gpio_num_t)config->triac_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Reset GPIO failed."); err = gpio_reset_pin((gpio_num_t)config->zero_cross_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Reset GPIO failed."), "Failed install isr service.")
|
||||
err = gpio_isr_handler_add((gpio_num_t)config->zero_cross_gpio, _zh_ac_dimmer_isr_handler, NULL);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, err = gpio_reset_pin((gpio_num_t)config->triac_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Reset GPIO failed."); err = gpio_reset_pin((gpio_num_t)config->zero_cross_gpio);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Reset GPIO failed."), "Failed add isr handler.");
|
||||
ZH_ERROR_CHECK(gpio_config(&zero_cross_gpio_config) == ESP_OK, ESP_FAIL,
|
||||
{ZH_ERROR_CHECK(gpio_reset_pin(config->triac_gpio) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")}, "Zero cross GPIO configuration failed.");
|
||||
esp_err_t err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
||||
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, ESP_FAIL,
|
||||
{ZH_ERROR_CHECK(gpio_reset_pin(config->triac_gpio) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")};
|
||||
{ZH_ERROR_CHECK(gpio_reset_pin(config->zero_cross_gpio) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")}, "Failed install isr service.")
|
||||
ZH_ERROR_CHECK(gpio_isr_handler_add(config->zero_cross_gpio, _zh_ac_dimmer_isr_handler, handle) == ESP_OK, ESP_FAIL,
|
||||
{ZH_ERROR_CHECK(gpio_reset_pin(config->triac_gpio) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")};
|
||||
{ZH_ERROR_CHECK(gpio_reset_pin(config->zero_cross_gpio) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")}, "Failed add isr handler.");
|
||||
handle->triac_gpio = config->triac_gpio;
|
||||
handle->zero_cross_gpio = config->zero_cross_gpio;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t _zh_ac_dimmer_timer_init(void)
|
||||
static esp_err_t _zh_ac_dimmer_timer_init(zh_ac_dimmer_handle_t *handle)
|
||||
{
|
||||
gptimer_config_t timer_config = {
|
||||
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
|
||||
.direction = GPTIMER_COUNT_UP,
|
||||
.resolution_hz = 1 * 1000 * 1000,
|
||||
};
|
||||
esp_err_t err = gptimer_new_timer(&timer_config, &_dimmer_timer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Failed create dimmer timer.");
|
||||
ZH_ERROR_CHECK(gptimer_new_timer(&timer_config, &handle->dimmer_timer_handle) == ESP_OK, ESP_FAIL, NULL, "Failed create dimmer timer.");
|
||||
gptimer_event_callbacks_t cbs = {
|
||||
.on_alarm = _zh_ac_dimmer_timer_on_alarm_cb,
|
||||
};
|
||||
err = gptimer_register_event_callbacks(_dimmer_timer, &cbs, NULL);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, err = gptimer_del_timer(_dimmer_timer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Timer delete fail."), "Failed register dimmer timer event callbacks.");
|
||||
err = gptimer_enable(_dimmer_timer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, err = gptimer_del_timer(_dimmer_timer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Timer delete fail."), "Failed enable dimmer timer.");
|
||||
ZH_ERROR_CHECK(gptimer_register_event_callbacks(handle->dimmer_timer_handle, &cbs, handle) == ESP_OK, ESP_FAIL,
|
||||
{ZH_ERROR_CHECK(gptimer_del_timer(handle->dimmer_timer_handle) == ESP_OK, ESP_FAIL, NULL, "Timer delete fail.")}, "Failed register dimmer timer event callbacks.");
|
||||
ZH_ERROR_CHECK(gptimer_enable(handle->dimmer_timer_handle) == ESP_OK, ESP_FAIL,
|
||||
{ZH_ERROR_CHECK(gptimer_del_timer(handle->dimmer_timer_handle) == ESP_OK, ESP_FAIL, NULL, "Timer delete fail.")}, "Failed enable dimmer timer.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void IRAM_ATTR _zh_ac_dimmer_isr_handler(void *arg)
|
||||
{
|
||||
zh_ac_dimmer_handle_t *handle = (zh_ac_dimmer_handle_t *)arg;
|
||||
uint64_t _current_us = esp_timer_get_time();
|
||||
if (_current_us - _prev_us <= (1250 * 0.9)) // 90% of zero crossing period (1250 µs) at 400 Hz.
|
||||
// Ignore spurious interrupts that arrive too soon after the previous one.
|
||||
if (_current_us - handle->prev_zero_crossing_us > (1250 * 0.9)) // 90% of zero-crossing period (1250 µs) at 400 Hz.
|
||||
{
|
||||
gpio_set_level(handle->triac_gpio, 0);
|
||||
handle->prev_zero_crossing_us = _current_us;
|
||||
if (handle->is_dimmer_work == true)
|
||||
{
|
||||
if (handle->dimmer_value != 0)
|
||||
{
|
||||
if (handle->dimmer_value == 100)
|
||||
{
|
||||
gpio_set_level(handle->triac_gpio, 1);
|
||||
return;
|
||||
}
|
||||
gpio_set_level((gpio_num_t)_init_config.triac_gpio, 0);
|
||||
_prev_us = _current_us;
|
||||
if (_is_dimmer_work == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (_dimmer_value != 0)
|
||||
{
|
||||
if (_dimmer_value == 100)
|
||||
{
|
||||
gpio_set_level((gpio_num_t)_init_config.triac_gpio, 1);
|
||||
return;
|
||||
}
|
||||
_alarm_config.alarm_count = (uint64_t)((((1250 - 330) / 100) * (100 - _dimmer_value)) + 330); // 330 is 50% of zero crossing time (by logic analyser).
|
||||
gptimer_alarm_config_t _alarm_config = {0};
|
||||
// Calculate alarm delay: maps dimming (1–99) to a µs value.
|
||||
// 330 µs is the base offset (50% of zero-crossing period, measured with a logic analyzer).
|
||||
_alarm_config.alarm_count = (uint64_t)((((1250 - 330) / 100) * (100 - handle->dimmer_value)) + 330);
|
||||
_alarm_config.flags.auto_reload_on_alarm = false;
|
||||
gptimer_set_alarm_action(_dimmer_timer, &_alarm_config);
|
||||
gptimer_start(_dimmer_timer);
|
||||
gptimer_set_alarm_action(handle->dimmer_timer_handle, &_alarm_config);
|
||||
gptimer_start(handle->dimmer_timer_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool IRAM_ATTR _zh_ac_dimmer_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
|
||||
{
|
||||
gpio_set_level((gpio_num_t)_init_config.triac_gpio, 1);
|
||||
gptimer_stop(_dimmer_timer);
|
||||
gptimer_set_raw_count(_dimmer_timer, 0);
|
||||
(void)edata;
|
||||
zh_ac_dimmer_handle_t *handle = (zh_ac_dimmer_handle_t *)user_ctx;
|
||||
gpio_set_level(handle->triac_gpio, 1);
|
||||
gptimer_stop(timer);
|
||||
gptimer_set_raw_count(timer, 0);
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user