Skip to content

Commit b442088

Browse files
authored
Merge pull request #7 from pmarchini/feat/porting-to-component
Feat/porting to component
2 parents 9450ada + 6e9cb63 commit b442088

File tree

6 files changed

+80
-46
lines changed

6 files changed

+80
-46
lines changed

README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# ESP32IDFDimmer
1+
# ESP32 Triac Dimmer Driver
22

3-
This library provides an API to control dimmer devices using the ESP32IDF. It supports both toggle and normal modes, and allows you to set the power levels of the dimmer.
3+
This library provides an API to control dimmer devices using ESP-IDF 5.x.
4+
**For ESP-IDF 4.x projects, please use the v1.0.0 release of this library.**
5+
It supports both toggle and normal modes, and allows you to set the power levels of the dimmer.
46

57
### Prerequisites
6-
- ESP32 board with ESP-IDF v4.1 or higher
7-
- A dimmable AC load
8+
- ESP32 board with ESP-IDF v5.0 or higher
9+
- A dimmable AC load
810

911
### Installation
10-
Clone the project from the repository and add the library to your project.
12+
Clone the component into your project components directory.
1113

1214
### Usage
1315
1. Include the library header in your program
@@ -61,6 +63,14 @@ The library provides the following API methods:
6163

6264
![image](https://user-images.githubusercontent.com/49943249/194775053-0badd3f8-0c23-4a86-8843-abe2f994f5b3.png)
6365

66+
## Migrated to ESP-IDF 5.x and Component
67+
This library has been migrated to ESP-IDF 5.x and is no longer compatible with previous versions.
68+
It has also been transformed into an ESP-IDF component for easier integration.
69+
70+
To use the basic example, add the component to your project's components directory and replace the main file with the code from examples/base/main.c.
71+
72+
If you are using the library in a project that is not using ESP-IDF 5.x, you can still use the old version of the library (v1.0.0) which is compatible with ESP-IDF 4.x.
73+
6474
## Contributing
6575

6676
We welcome contributions to this library. Please open a pull request or an issue to get started.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
idf_component_register(
2+
SRCS "esp32-triac-dimmer-driver.c"
3+
INCLUDE_DIRS "include"
4+
REQUIRES driver
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
COMPONENT_ADD_INCLUDEDIRS = .
2+
COMPONENT_DEPENDS = driver

src/lib/esp32idfDimmer.c src/components/esp32-triac-dimmer-driver/esp32-triac-dimmer-driver.c

+54-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#include "esp32idfDimmer.h"
2+
#include "esp32-triac-dimmer-driver.h"
33

44
static const char *TAG = "Esp32idfDimmer";
55

@@ -31,7 +31,12 @@ volatile uint16_t togMin[ALL_DIMMERS];
3131
volatile bool togDir[ALL_DIMMERS];
3232

3333
/* timer configurations */
34-
timer_config_t m_timer_config;
34+
gptimer_config_t m_timer_config;
35+
gptimer_handle_t gptimer = NULL;
36+
typedef struct {
37+
uint64_t event_count;
38+
} example_queue_element_t;
39+
3540

3641
dimmertyp *createDimmer(gpio_num_t user_dimmer_pin, gpio_num_t zc_dimmer_pin)
3742
{
@@ -56,8 +61,35 @@ dimmertyp *createDimmer(gpio_num_t user_dimmer_pin, gpio_num_t zc_dimmer_pin)
5661
return dimmer[current_dim - 1];
5762
}
5863

59-
#define TIMER_DIVIDER 80 // Hardware timer clock divider
60-
#define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) // convert counter value to seconds
64+
#define TIMER_BASE_CLK 1 * 1000 * 1000, // 1MHz, 1 tick = 1us
65+
66+
/**
67+
* @brief Configure the timer alarm
68+
*/
69+
void config_alarm(gptimer_handle_t *timer, int ACfreq)
70+
{
71+
/*self regulation 50/60 Hz*/
72+
double m_calculated_interval = (1 / (double)(ACfreq * 2)) / 100;
73+
ESP_LOGI(TAG, "Interval between wave calculated for frequency : %3dHz = %5f", ACfreq, m_calculated_interval);
74+
75+
ESP_LOGI(TAG, "Timer configuration - configure interrupt and timer");
76+
ESP_LOGI(TAG, "Timer configuration - configure alarm");
77+
gptimer_alarm_config_t alarm_config = {
78+
.reload_count = 0, // counter will reload with 0 on alarm event
79+
.alarm_count = (1000000 * m_calculated_interval),
80+
.flags.auto_reload_on_alarm = true, // enable auto-reload
81+
};
82+
ESP_LOGI(TAG, "Timer configuration - set alarm action");
83+
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
84+
85+
gptimer_event_callbacks_t cbs = {
86+
.on_alarm = onTimerISR, // register user callback
87+
};
88+
ESP_LOGI(TAG, "Timer configuration - register event callbacks");
89+
ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
90+
91+
ESP_LOGI(TAG, "Timer configuration - configuration completed");
92+
}
6193

6294
void config_timer(int ACfreq)
6395
{
@@ -72,28 +104,22 @@ void config_timer(int ACfreq)
72104
memset(&m_timer_config, 0, sizeof(m_timer_config));
73105

74106
/* Prepare configuration */
75-
timer_config_t m_timer_config =
76-
{
77-
.alarm_en = TIMER_ALARM_DIS,
78-
.counter_en = TIMER_PAUSE,
79-
.counter_dir = TIMER_COUNT_UP,
80-
.auto_reload = TIMER_AUTORELOAD_EN,
81-
.divider = TIMER_DIVIDER,
82-
};
83-
84-
/*self regulation 50/60 Hz*/
85-
double m_calculated_interval = (1 / (double)(ACfreq * 2)) / 100;
86-
ESP_LOGI(TAG, "Interval between wave calculated for frequency : %3dHz = %5f", ACfreq, m_calculated_interval);
107+
gptimer_config_t m_timer_config = {
108+
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
109+
.direction = GPTIMER_COUNT_UP,
110+
.resolution_hz = TIMER_BASE_CLK
111+
};
112+
87113
ESP_LOGI(TAG, "Timer configuration - configure interrupt and timer");
88114
/* Configure the alarm value and the interrupt on alarm. */
89-
timer_init(TIMER_GROUP_0, TIMER_0, &m_timer_config);
90-
timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, TIMER_SCALE * m_calculated_interval);
91-
timer_enable_intr(TIMER_GROUP_0, TIMER_0);
92-
timer_isr_register(TIMER_GROUP_0, TIMER_0, onTimerISR, (void *)TIMER_0, ESP_INTR_FLAG_IRAM, NULL);
115+
ESP_ERROR_CHECK(gptimer_new_timer(&m_timer_config, &gptimer));
116+
117+
/* Configure the alarm value and the interrupt on alarm. */
118+
config_alarm(gptimer, ACfreq);
93119
/* start timer */
94120
ESP_LOGI(TAG, "Timer configuration - start timer");
95-
timer_start(TIMER_GROUP_0, TIMER_0);
96-
timer_set_alarm(TIMER_GROUP_0, TIMER_0, TIMER_ALARM_EN);
121+
ESP_ERROR_CHECK(gptimer_enable(gptimer));
122+
ESP_ERROR_CHECK(gptimer_start(gptimer));
97123
ESP_LOGI(TAG, "Timer configuration - completed");
98124
}
99125

@@ -132,9 +158,9 @@ void ext_int_init(dimmertyp *ptr)
132158
}
133159

134160
/*ISR debug region*/
135-
#if DEBUG_ISR_DIMMER == ISR_DEBUG_ON
161+
#if DEBUG_ZERO_CROSS_SIGNAL == ISR_DEBUG_ON
136162

137-
static xQueueHandle gpio_zero_cross_evt_queue = NULL;
163+
static QueueHandle_t gpio_zero_cross_evt_queue = NULL;
138164

139165
static void gpio_isr_zerocross_debug(void *arg)
140166
{
@@ -153,7 +179,7 @@ static void gpio_isr_zerocross_debug(void *arg)
153179
/*ISR timer debug region*/
154180
#if DEBUG_ISR_TIMER == ISR_DEBUG_ON
155181

156-
static xQueueHandle timer_event_queue = NULL;
182+
static QueueHandle_t timer_event_queue = NULL;
157183

158184
static void timer_isr_debug(void *arg)
159185
{
@@ -162,7 +188,7 @@ static void timer_isr_debug(void *arg)
162188
{
163189
if (xQueueReceive(timer_event_queue, &io_num, portMAX_DELAY))
164190
{
165-
printf("Timer interrupt event , counter = %5d \n", io_num);
191+
printf("Timer interrupt event , counter = %5lu \n", io_num);
166192
}
167193
}
168194
}
@@ -175,7 +201,7 @@ void begin(dimmertyp *ptr, DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OF
175201
dimMode[ptr->current_num] = DIMMER_MODE;
176202
dimState[ptr->current_num] = ON_OFF;
177203

178-
#if DEBUG_ISR_DIMMER == ISR_DEBUG_ON
204+
#if DEBUG_ZERO_CROSS_SIGNAL == ISR_DEBUG_ON
179205
if (!_initDone)
180206
{
181207
// create a queue to handle gpio event from isr
@@ -276,7 +302,7 @@ void toggleSettings(dimmertyp *ptr, int minValue, int maxValue)
276302
static void IRAM_ATTR isr_ext(void *arg)
277303
{
278304

279-
#if DEBUG_ISR_DIMMER == ISR_DEBUG_ON
305+
#if DEBUG_ZERO_CROSS_SIGNAL == ISR_DEBUG_ON
280306
uint32_t gpio_num = (uint32_t)arg;
281307
xQueueSendFromISR(gpio_zero_cross_evt_queue, &gpio_num, NULL);
282308
#endif
@@ -295,10 +321,6 @@ static int counter = 0;
295321
/* Execution on timer event */
296322
static void IRAM_ATTR onTimerISR(void *para)
297323
{
298-
/*Block needed to handle timer ISR*/
299-
timer_spinlock_take(TIMER_GROUP_0);
300-
timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
301-
/*Give back spinlock at the end of the method*/
302324
/**********************************/
303325
#if DEBUG_ISR_TIMER == ISR_DEBUG_ON
304326
counter++;
@@ -355,9 +377,4 @@ static void IRAM_ATTR onTimerISR(void *para)
355377
}
356378
if (toggleCounter >= toggleReload)
357379
toggleCounter = 1;
358-
359-
/* After the alarm has been triggered
360-
we need enable it again, so it is triggered the next time */
361-
timer_group_enable_alarm_in_isr(TIMER_GROUP_0, TIMER_0);
362-
timer_spinlock_give(TIMER_GROUP_0);
363380
}

src/lib/esp32idfDimmer.h src/components/esp32-triac-dimmer-driver/include/esp32-triac-dimmer-driver.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "freertos/queue.h"
1010
#include "driver/gpio.h"
1111
#include "driver/periph_ctrl.h"
12-
#include "driver/timer.h"
12+
#include "driver/gptimer.h"
1313
#include "freertos/task.h"
1414
#include "math.h"
1515
#include "esp_log.h"

src/examples/base/example.c src/examples/base/main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "esp32idfDimmer.h"
1+
#include "esp32-triac-dimmer-driver.h"
22

33
#include "esp_log.h"
44
#include "esp_err.h"
@@ -34,10 +34,10 @@ void app_main()
3434
while (1)
3535
{
3636
// change the output power
37-
getPower(ptr_dimmer) < 60 ? setPower(ptr_dimmer, (getPower(ptr_dimmer) + 5)) : setPower(ptr_dimmer, 1);
37+
getPower(ptr_dimmer) < 50 ? setPower(ptr_dimmer, (getPower(ptr_dimmer) + 1)) : setPower(ptr_dimmer, 20);
3838
setPower(ptr_dimmer_2, getPower(ptr_dimmer));
3939
// wait
40-
vTaskDelay(100 / portTICK_PERIOD_MS);
40+
vTaskDelay(1000 / portTICK_PERIOD_MS);
4141
}
4242
}
4343

0 commit comments

Comments
 (0)