1
|
#include <stdio.h>
|
2
|
#include "freertos/FreeRTOS.h"
|
3
|
#include "freertos/task.h"
|
4
|
#include "driver/ledc.h"
|
5
|
#include "esp_err.h"
|
6
|
|
7
|
/*
|
8
|
* CONFIG_2TIM_3CH :- configuration when 3 channels are configured using two timers
|
9
|
* CONFIG_1TIM_2CH :- configuration when 2 channels are configured using one timer
|
10
|
* CONFIG_1TIM_3CH :- configuration when 3 channels are configured using one timer
|
11
|
*/
|
12
|
#define CONFIG_1TIM_3CH
|
13
|
/*
|
14
|
* CONFIG_THREADSAFE_API :- configure using ledc_set_duty_and_update() function
|
15
|
* CONFIG_NONTHREADSAFE_API :- configure using ledc_set_duty() and ledc_update_duty() functions
|
16
|
*/
|
17
|
#define CONFIG_NONTHREADSAFE_API
|
18
|
|
19
|
/* TIMER and CHENNEL configurations */
|
20
|
#define LEDC_TIMER0 LEDC_TIMER_0
|
21
|
#define LEDC_TIMER1 LEDC_TIMER_1
|
22
|
#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
|
23
|
#define LEDC_HS_CH0_GPIO (18)
|
24
|
#define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
|
25
|
#define LEDC_HS_CH1_GPIO (19)
|
26
|
#define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
|
27
|
#define LEDC_HS_CH2_GPIO (21)
|
28
|
#define LEDC_HS_CH2_CHANNEL LEDC_CHANNEL_2
|
29
|
#define LEDC_TIMER_RESOLUTION LEDC_TIMER_10_BIT
|
30
|
#define LEDC_TIMER_FREQ 1024
|
31
|
|
32
|
#ifdef CONFIG_1TIM_2CH
|
33
|
#define LEDC_TEST_CH_NUM (2)
|
34
|
#else
|
35
|
#define LEDC_TEST_CH_NUM (3)
|
36
|
#endif
|
37
|
|
38
|
#define LEDC_TEST_DUTY (1023)
|
39
|
|
40
|
/* A digital signal is toggled to measure the sweep time */
|
41
|
#define GPIO_OUTPUT_IO 5
|
42
|
int gpio_level = 0;
|
43
|
|
44
|
ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
|
45
|
{
|
46
|
.channel = LEDC_HS_CH0_CHANNEL,
|
47
|
.duty = 0,
|
48
|
.gpio_num = LEDC_HS_CH0_GPIO,
|
49
|
.speed_mode = LEDC_HS_MODE,
|
50
|
.hpoint = 0,
|
51
|
.timer_sel = LEDC_TIMER0
|
52
|
},
|
53
|
{
|
54
|
.channel = LEDC_HS_CH1_CHANNEL,
|
55
|
.duty = 0,
|
56
|
.gpio_num = LEDC_HS_CH1_GPIO,
|
57
|
.speed_mode = LEDC_HS_MODE,
|
58
|
.hpoint = 0,
|
59
|
.timer_sel = LEDC_TIMER0
|
60
|
},
|
61
|
#ifndef CONFIG_1TIM_2CH
|
62
|
{
|
63
|
.channel = LEDC_HS_CH2_CHANNEL,
|
64
|
.duty = 0,
|
65
|
.gpio_num = LEDC_HS_CH2_GPIO,
|
66
|
.speed_mode = LEDC_HS_MODE,
|
67
|
.hpoint = 0,
|
68
|
#ifdef CONFIG_2TIM_3CH
|
69
|
.timer_sel = LEDC_TIMER1
|
70
|
#else
|
71
|
.timer_sel = LEDC_TIMER0
|
72
|
#endif
|
73
|
},
|
74
|
#endif
|
75
|
};
|
76
|
|
77
|
void test_ledc_api (void *arg){
|
78
|
|
79
|
int duty = 0;
|
80
|
while (1) {
|
81
|
|
82
|
if (gpio_level == 1)
|
83
|
{
|
84
|
gpio_level = 0;
|
85
|
}
|
86
|
else
|
87
|
{
|
88
|
gpio_level = 1;
|
89
|
}
|
90
|
gpio_set_level(GPIO_OUTPUT_IO, gpio_level);
|
91
|
|
92
|
for (int ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
|
93
|
#ifdef CONFIG_THREADSAFE_API
|
94
|
ledc_set_duty_and_update(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, duty, 0);
|
95
|
#else
|
96
|
ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, duty);
|
97
|
ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
|
98
|
#endif
|
99
|
}
|
100
|
|
101
|
duty = duty + 1;
|
102
|
if (duty == 1024)
|
103
|
{
|
104
|
duty = 0;
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
void config_gpio(){
|
110
|
gpio_pad_select_gpio(GPIO_OUTPUT_IO);
|
111
|
gpio_set_direction(GPIO_OUTPUT_IO, GPIO_MODE_OUTPUT);
|
112
|
gpio_set_drive_capability(GPIO_OUTPUT_IO, GPIO_DRIVE_CAP_2);
|
113
|
}
|
114
|
|
115
|
void app_main(void)
|
116
|
{
|
117
|
ledc_timer_config_t ledc_timer0 = {
|
118
|
.duty_resolution = LEDC_TIMER_RESOLUTION, // resolution of PWM duty
|
119
|
.freq_hz = LEDC_TIMER_FREQ, // frequency of PWM signal
|
120
|
.speed_mode = LEDC_HS_MODE, // timer mode
|
121
|
.timer_num = LEDC_TIMER0, // timer index
|
122
|
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
|
123
|
};
|
124
|
ledc_timer_config(&ledc_timer0);
|
125
|
#ifdef CONFIG_2TIM_3CH
|
126
|
ledc_timer_config_t ledc_timer1 = {
|
127
|
.duty_resolution = LEDC_TIMER_RESOLUTION, // resolution of PWM duty
|
128
|
.freq_hz = 1024, // frequency of PWM signal
|
129
|
.speed_mode = LEDC_HS_MODE, // timer mode
|
130
|
.timer_num = LEDC_TIMER1, // timer index
|
131
|
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
|
132
|
};
|
133
|
ledc_timer_config(&ledc_timer1);
|
134
|
#endif
|
135
|
|
136
|
for (int ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
|
137
|
ledc_channel_config(&ledc_channel[ch]);
|
138
|
}
|
139
|
|
140
|
ledc_fade_func_install(0);
|
141
|
|
142
|
config_gpio();
|
143
|
|
144
|
xTaskCreate(test_ledc_api, "ledcapi", 4096, NULL, tskIDLE_PRIORITY, NULL);
|
145
|
}
|