5#include <MycilaDimmerThyristor.h>
8#include <freertos/FreeRTOS.h>
11#include <driver/gpio.h>
12#include <driver/gptimer_types.h>
13#include <esp32-hal-gpio.h>
14#include <hal/gpio_ll.h>
15#include <soc/gpio_struct.h>
18#include <esp32-hal-log.h>
21#include "priv/inlined_gptimer.h"
23#ifndef GPIO_IS_VALID_OUTPUT_GPIO
24 #define GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) ((gpio_num >= 0) && \
25 (((1ULL << (gpio_num)) & SOC_GPIO_VALID_OUTPUT_GPIO_MASK) != 0))
28#ifndef GPIO_IS_VALID_GPIO
29 #define GPIO_IS_VALID_GPIO(gpio_num) ((gpio_num >= 0) && \
30 (((1ULL << (gpio_num)) & SOC_GPIO_VALID_GPIO_MASK) != 0))
36#define PHASE_DELAY_MIN_US (90)
38#define TAG "Thyristor"
45 uint16_t alarm_count = UINT16_MAX;
49static gptimer_handle_t fire_timer =
nullptr;
51#ifndef MYCILA_DIMMER_NO_LOCK
52static portMUX_TYPE dimmers_spinlock = portMUX_INITIALIZER_UNLOCKED;
59 if (!GPIO_IS_VALID_OUTPUT_GPIO(_pin)) {
60 ESP_LOGE(TAG,
"Invalid pin: %" PRId8, _pin);
64 ESP_LOGI(TAG,
"Enable dimmer on pin %" PRId8, _pin);
66 pinMode(_pin, OUTPUT);
67 digitalWrite(_pin, LOW);
68 _registerDimmer(
this);
80 ESP_LOGI(TAG,
"Disable dimmer on pin %" PRId8, _pin);
82 _unregisterDimmer(
this);
83 digitalWrite(_pin, LOW);
88 gptimer_alarm_config_t fire_timer_alarm_cfg = {.alarm_count = UINT16_MAX, .reload_count = 0, .flags = {.auto_reload_on_alarm =
false}};
91 if (inlined_gptimer_set_raw_count(fire_timer, 0) != ESP_OK) {
96#ifndef MYCILA_DIMMER_NO_LOCK
98 portENTER_CRITICAL_SAFE(&dimmers_spinlock);
103 while (current !=
nullptr) {
105 if (current->dimmer->_delay)
106 gpio_ll_set_level(&GPIO, current->dimmer->_pin, LOW);
111 current->alarm_count = current->dimmer->_delay < PHASE_DELAY_MIN_US ? PHASE_DELAY_MIN_US : current->dimmer->_delay;
113 if (current->alarm_count < fire_timer_alarm_cfg.alarm_count)
114 fire_timer_alarm_cfg.alarm_count = current->alarm_count;
115 current = current->next;
118#ifndef MYCILA_DIMMER_NO_LOCK
120 portEXIT_CRITICAL_SAFE(&dimmers_spinlock);
124 uint64_t fire_timer_count_value;
125 if (inlined_gptimer_get_raw_count(fire_timer, &fire_timer_count_value) != ESP_OK) {
131 if (fire_timer_count_value >= delayUntilZero) {
132 fire_timer_count_value -= delayUntilZero;
135 if (fire_timer_count_value <= fire_timer_alarm_cfg.alarm_count) {
137 if (inlined_gptimer_set_raw_count(fire_timer, fire_timer_count_value) == ESP_OK) {
138 _fireTimerISR(fire_timer,
nullptr,
nullptr);
146 if (inlined_gptimer_set_raw_count(fire_timer, -
static_cast<uint64_t
>(delayUntilZero) + fire_timer_count_value) == ESP_OK) {
148 inlined_gptimer_set_alarm_action(fire_timer, &fire_timer_alarm_cfg);
154bool ARDUINO_ISR_ATTR Mycila::ThyristorDimmer::_fireTimerISR(gptimer_handle_t timer,
const gptimer_alarm_event_data_t* event,
void* arg) {
156 gptimer_alarm_config_t fire_timer_alarm_cfg = {.alarm_count = UINT16_MAX, .reload_count = 0, .flags = {.auto_reload_on_alarm =
false}};
159 uint64_t fire_timer_count_value;
160 if (inlined_gptimer_get_raw_count(fire_timer, &fire_timer_count_value) != ESP_OK) {
166 fire_timer_alarm_cfg.alarm_count = UINT16_MAX;
168#ifndef MYCILA_DIMMER_NO_LOCK
170 portENTER_CRITICAL_SAFE(&dimmers_spinlock);
174 struct RegisteredDimmer* current = dimmers;
175 while (current !=
nullptr) {
176 if (current->alarm_count != UINT16_MAX) {
178 if (current->alarm_count <= fire_timer_count_value) {
180 gpio_ll_set_level(&GPIO, current->dimmer->_pin, HIGH);
182 current->alarm_count = UINT16_MAX;
185 if (current->alarm_count < fire_timer_alarm_cfg.alarm_count)
186 fire_timer_alarm_cfg.alarm_count = current->alarm_count;
189 current = current->next;
192#ifndef MYCILA_DIMMER_NO_LOCK
194 portEXIT_CRITICAL_SAFE(&dimmers_spinlock);
198 inlined_gptimer_get_raw_count(fire_timer, &fire_timer_count_value);
199 }
while (fire_timer_alarm_cfg.alarm_count != UINT16_MAX && fire_timer_alarm_cfg.alarm_count <= fire_timer_count_value);
202 if (fire_timer_alarm_cfg.alarm_count != UINT16_MAX)
203 inlined_gptimer_set_alarm_action(fire_timer, &fire_timer_alarm_cfg);
209void Mycila::ThyristorDimmer::_registerDimmer(Mycila::ThyristorDimmer* dimmer) {
210 if (dimmers ==
nullptr) {
211 ESP_LOGI(TAG,
"Starting dimmer firing ISR");
213 gptimer_config_t timer_config;
214 timer_config.clk_src = GPTIMER_CLK_SRC_DEFAULT;
215 timer_config.direction = GPTIMER_COUNT_UP;
216 timer_config.resolution_hz = 1000000;
217 timer_config.flags.intr_shared =
true;
218 timer_config.intr_priority = 0;
219#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
220 timer_config.flags.backup_before_sleep =
false;
222#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
223 timer_config.flags.allow_pd =
false;
226 ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &fire_timer));
227 gptimer_event_callbacks_t callbacks_config;
228 callbacks_config.on_alarm = _fireTimerISR;
229 ESP_ERROR_CHECK(gptimer_register_event_callbacks(fire_timer, &callbacks_config,
nullptr));
230 ESP_ERROR_CHECK(gptimer_enable(fire_timer));
231 ESP_ERROR_CHECK(gptimer_start(fire_timer));
234 ESP_LOGD(TAG,
"Register new dimmer %p on pin %d", dimmer, dimmer->
getPin());
236#ifndef MYCILA_DIMMER_NO_LOCK
237 portENTER_CRITICAL_SAFE(&dimmers_spinlock);
240 if (dimmers ==
nullptr) {
241 dimmers =
new RegisteredDimmer();
242 dimmers->dimmer = dimmer;
244 struct RegisteredDimmer* first =
new RegisteredDimmer();
245 first->next = dimmers;
246 dimmers->prev = first;
250#ifndef MYCILA_DIMMER_NO_LOCK
251 portEXIT_CRITICAL_SAFE(&dimmers_spinlock);
256void Mycila::ThyristorDimmer::_unregisterDimmer(Mycila::ThyristorDimmer* dimmer) {
257 ESP_LOGD(TAG,
"Unregister dimmer %p on pin %d", dimmer, dimmer->
getPin());
259#ifndef MYCILA_DIMMER_NO_LOCK
260 portENTER_CRITICAL_SAFE(&dimmers_spinlock);
263 struct RegisteredDimmer* current = dimmers;
264 while (current !=
nullptr) {
265 if (current->dimmer == dimmer) {
266 if (current->prev !=
nullptr) {
267 current->prev->next = current->next;
269 dimmers = current->next;
271 if (current->next !=
nullptr) {
272 current->next->prev = current->prev;
277 current = current->next;
280#ifndef MYCILA_DIMMER_NO_LOCK
281 portEXIT_CRITICAL_SAFE(&dimmers_spinlock);
284 if (dimmers ==
nullptr) {
285 ESP_LOGI(TAG,
"Stopping dimmer firing ISR");
286 gptimer_stop(fire_timer);
287 ESP_ERROR_CHECK(gptimer_disable(fire_timer));
288 ESP_ERROR_CHECK(gptimer_del_timer(fire_timer));
289 fire_timer =
nullptr;
bool setDutyCycle(float dutyCycle)
Set the power duty.
Thyristor (TRIAC) based dimmer implementation for TRIAC and Random SSR dimmers.
gpio_num_t getPin() const
Get the GPIO pin used for the dimmer.
static void onZeroCross(int16_t delayUntilZero, void *args)
void begin() override
Enable a dimmer on a specific GPIO pin.
void end() override
Disable the dimmer.