1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <string.h>
7 #include <esp_types.h>
8 #include "freertos/FreeRTOS.h"
9 #include "freertos/semphr.h"
10 #include "esp_log.h"
11 #include "esp_check.h"
12 #include "soc/gpio_periph.h"
13 #include "soc/ledc_periph.h"
14 #include "soc/rtc.h"
15 #include "soc/soc_caps.h"
16 #include "hal/ledc_hal.h"
17 #include "hal/gpio_hal.h"
18 #include "driver/ledc.h"
19 #include "esp_rom_gpio.h"
20 #include "esp_rom_sys.h"
21 #include "soc/clk_ctrl_os.h"
22
23 static const char* LEDC_TAG = "ledc";
24
25 #define LEDC_CHECK(a, str, ret_val) ESP_RETURN_ON_FALSE(a, ret_val, LEDC_TAG, "%s", str)
26 #define LEDC_ARG_CHECK(a, param) ESP_RETURN_ON_FALSE(a, ESP_ERR_INVALID_ARG, LEDC_TAG, param " argument is invalid")
27
28 typedef struct {
29 ledc_mode_t speed_mode;
30 ledc_duty_direction_t direction;
31 uint32_t target_duty;
32 int cycle_num;
33 int scale;
34 ledc_fade_mode_t mode;
35 xSemaphoreHandle ledc_fade_sem;
36 xSemaphoreHandle ledc_fade_mux;
37 #if CONFIG_SPIRAM_USE_MALLOC
38 StaticQueue_t ledc_fade_sem_storage;
39 #endif
40 ledc_cb_t ledc_fade_callback;
41 void *cb_user_arg;
42 } ledc_fade_t;
43
44 typedef struct {
45 ledc_hal_context_t ledc_hal; /*!< LEDC hal context*/
46 } ledc_obj_t;
47
48 static ledc_obj_t *p_ledc_obj[LEDC_SPEED_MODE_MAX] = {0};
49 static ledc_fade_t *s_ledc_fade_rec[LEDC_SPEED_MODE_MAX][LEDC_CHANNEL_MAX];
50 static ledc_isr_handle_t s_ledc_fade_isr_handle = NULL;
51 static portMUX_TYPE ledc_spinlock = portMUX_INITIALIZER_UNLOCKED;
52
53 #define LEDC_VAL_NO_CHANGE (-1)
54 #define LEDC_STEP_NUM_MAX (1023)
55 #define LEDC_DUTY_DECIMAL_BIT_NUM (4)
56 #define LEDC_TIMER_DIV_NUM_MAX (0x3FFFF)
57 #define LEDC_DUTY_NUM_MAX (LEDC_DUTY_NUM_LSCH0_V)
58 #define LEDC_DUTY_CYCLE_MAX (LEDC_DUTY_CYCLE_LSCH0_V)
59 #define LEDC_DUTY_SCALE_MAX (LEDC_DUTY_SCALE_LSCH0_V)
60 #define LEDC_HPOINT_VAL_MAX (LEDC_HPOINT_LSCH1_V)
61 #define DELAY_CLK8M_CLK_SWITCH (5)
62 #define SLOW_CLK_CYC_CALIBRATE (13)
63 #define LEDC_FADE_TOO_SLOW_STR "LEDC FADE TOO SLOW"
64 #define LEDC_FADE_TOO_FAST_STR "LEDC FADE TOO FAST"
65
66 static __attribute__((unused)) const char *LEDC_NOT_INIT = "LEDC is not initialized";
67 static __attribute__((unused)) const char *LEDC_FADE_SERVICE_ERR_STR = "LEDC fade service not installed";
68 static __attribute__((unused)) const char *LEDC_FADE_INIT_ERROR_STR = "LEDC fade channel init error, not enough memory or service not installed";
69
70 //This value will be calibrated when in use.
71 static uint32_t s_ledc_slow_clk_8M = 0;
72
ledc_ls_timer_update(ledc_mode_t speed_mode,ledc_timer_t timer_sel)73 static void ledc_ls_timer_update(ledc_mode_t speed_mode, ledc_timer_t timer_sel)
74 {
75 if (speed_mode == LEDC_LOW_SPEED_MODE) {
76 ledc_hal_ls_timer_update(&(p_ledc_obj[speed_mode]->ledc_hal), timer_sel);
77 }
78 }
79
ledc_ls_channel_update(ledc_mode_t speed_mode,ledc_channel_t channel)80 static IRAM_ATTR void ledc_ls_channel_update(ledc_mode_t speed_mode, ledc_channel_t channel)
81 {
82 if (speed_mode == LEDC_LOW_SPEED_MODE) {
83 ledc_hal_ls_channel_update(&(p_ledc_obj[speed_mode]->ledc_hal), channel);
84 }
85 }
86
87 //We know that CLK8M is about 8M, but don't know the actual value. So we need to do a calibration.
ledc_slow_clk_calibrate(void)88 static bool ledc_slow_clk_calibrate(void)
89 {
90 if (periph_rtc_dig_clk8m_enable()) {
91 s_ledc_slow_clk_8M = periph_rtc_dig_clk8m_get_freq();
92 ESP_LOGD(LEDC_TAG, "Calibrate CLK8M_CLK : %d Hz", s_ledc_slow_clk_8M);
93 return true;
94 }
95 ESP_LOGE(LEDC_TAG, "Calibrate CLK8M_CLK failed");
96 return false;
97 }
98
ledc_get_src_clk_freq(ledc_clk_cfg_t clk_cfg)99 static uint32_t ledc_get_src_clk_freq(ledc_clk_cfg_t clk_cfg)
100 {
101 uint32_t src_clk_freq = 0;
102 if (clk_cfg == LEDC_USE_APB_CLK) {
103 src_clk_freq = LEDC_APB_CLK_HZ;
104 } else if (clk_cfg == LEDC_USE_REF_TICK) {
105 src_clk_freq = LEDC_REF_CLK_HZ;
106 } else if (clk_cfg == LEDC_USE_RTC8M_CLK) {
107 src_clk_freq = s_ledc_slow_clk_8M;
108 #if SOC_LEDC_SUPPORT_XTAL_CLOCK
109 } else if (clk_cfg == LEDC_USE_XTAL_CLK) {
110 src_clk_freq = rtc_clk_xtal_freq_get() * 1000000;
111 #endif
112 }
113 return src_clk_freq;
114 }
115
ledc_enable_intr_type(ledc_mode_t speed_mode,ledc_channel_t channel,ledc_intr_type_t type)116 static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_intr_type_t type)
117 {
118 portENTER_CRITICAL(&ledc_spinlock);
119 if (type == LEDC_INTR_FADE_END) {
120 ledc_hal_set_fade_end_intr(&(p_ledc_obj[speed_mode]->ledc_hal), channel, true);
121 } else {
122 ledc_hal_set_fade_end_intr(&(p_ledc_obj[speed_mode]->ledc_hal), channel, false);
123 }
124 portEXIT_CRITICAL(&ledc_spinlock);
125 return ESP_OK;
126 }
127
_ledc_fade_hw_acquire(ledc_mode_t mode,ledc_channel_t channel)128 static void _ledc_fade_hw_acquire(ledc_mode_t mode, ledc_channel_t channel)
129 {
130 ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
131 if (fade) {
132 xSemaphoreTake(fade->ledc_fade_sem, portMAX_DELAY);
133 ledc_enable_intr_type(mode, channel, LEDC_INTR_DISABLE);
134 }
135 }
136
_ledc_fade_hw_release(ledc_mode_t mode,ledc_channel_t channel)137 static void _ledc_fade_hw_release(ledc_mode_t mode, ledc_channel_t channel)
138 {
139 ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
140 if (fade) {
141 xSemaphoreGive(fade->ledc_fade_sem);
142 }
143 }
144
_ledc_op_lock_acquire(ledc_mode_t mode,ledc_channel_t channel)145 static void _ledc_op_lock_acquire(ledc_mode_t mode, ledc_channel_t channel)
146 {
147 ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
148 if (fade) {
149 xSemaphoreTake(fade->ledc_fade_mux, portMAX_DELAY);
150 }
151 }
152
_ledc_op_lock_release(ledc_mode_t mode,ledc_channel_t channel)153 static void _ledc_op_lock_release(ledc_mode_t mode, ledc_channel_t channel)
154 {
155 ledc_fade_t* fade = s_ledc_fade_rec[mode][channel];
156 if (fade) {
157 xSemaphoreGive(fade->ledc_fade_mux);
158 }
159 }
160
ledc_get_max_duty(ledc_mode_t speed_mode,ledc_channel_t channel)161 static uint32_t ledc_get_max_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
162 {
163 // The arguments are checked before internally calling this function.
164 uint32_t max_duty;
165 ledc_hal_get_max_duty(&(p_ledc_obj[speed_mode]->ledc_hal), channel, &max_duty);
166 return max_duty;
167 }
168
ledc_timer_set(ledc_mode_t speed_mode,ledc_timer_t timer_sel,uint32_t clock_divider,uint32_t duty_resolution,ledc_clk_src_t clk_src)169 esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t clock_divider, uint32_t duty_resolution,
170 ledc_clk_src_t clk_src)
171 {
172 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
173 LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
174 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
175 portENTER_CRITICAL(&ledc_spinlock);
176 ledc_hal_set_clock_divider(&(p_ledc_obj[speed_mode]->ledc_hal), timer_sel, clock_divider);
177 ledc_hal_set_clock_source(&(p_ledc_obj[speed_mode]->ledc_hal), timer_sel, clk_src);
178 ledc_hal_set_duty_resolution(&(p_ledc_obj[speed_mode]->ledc_hal), timer_sel, duty_resolution);
179 ledc_ls_timer_update(speed_mode, timer_sel);
180 portEXIT_CRITICAL(&ledc_spinlock);
181 return ESP_OK;
182 }
183
ledc_duty_config(ledc_mode_t speed_mode,ledc_channel_t channel,int hpoint_val,int duty_val,ledc_duty_direction_t duty_direction,uint32_t duty_num,uint32_t duty_cycle,uint32_t duty_scale)184 static IRAM_ATTR esp_err_t ledc_duty_config(ledc_mode_t speed_mode, ledc_channel_t channel, int hpoint_val, int duty_val,
185 ledc_duty_direction_t duty_direction, uint32_t duty_num, uint32_t duty_cycle, uint32_t duty_scale)
186 {
187 portENTER_CRITICAL_SAFE(&ledc_spinlock);
188 if (hpoint_val >= 0) {
189 ledc_hal_set_hpoint(&(p_ledc_obj[speed_mode]->ledc_hal), channel, hpoint_val);
190 }
191 if (duty_val >= 0) {
192 ledc_hal_set_duty_int_part(&(p_ledc_obj[speed_mode]->ledc_hal), channel, duty_val);
193 }
194 ledc_hal_set_duty_direction(&(p_ledc_obj[speed_mode]->ledc_hal), channel, duty_direction);
195 ledc_hal_set_duty_num(&(p_ledc_obj[speed_mode]->ledc_hal), channel, duty_num);
196 ledc_hal_set_duty_cycle(&(p_ledc_obj[speed_mode]->ledc_hal), channel, duty_cycle);
197 ledc_hal_set_duty_scale(&(p_ledc_obj[speed_mode]->ledc_hal), channel, duty_scale);
198 ledc_ls_channel_update(speed_mode, channel);
199 portEXIT_CRITICAL_SAFE(&ledc_spinlock);
200 return ESP_OK;
201 }
202
ledc_bind_channel_timer(ledc_mode_t speed_mode,ledc_channel_t channel,ledc_timer_t timer_sel)203 esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_timer_t timer_sel)
204 {
205 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
206 LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
207 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
208 portENTER_CRITICAL(&ledc_spinlock);
209 ledc_hal_bind_channel_timer(&(p_ledc_obj[speed_mode]->ledc_hal), channel, timer_sel);
210 ledc_ls_channel_update(speed_mode, channel);
211 portEXIT_CRITICAL(&ledc_spinlock);
212 return ESP_OK;
213 }
214
ledc_timer_rst(ledc_mode_t speed_mode,ledc_timer_t timer_sel)215 esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, ledc_timer_t timer_sel)
216 {
217 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
218 LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
219 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
220 portENTER_CRITICAL(&ledc_spinlock);
221 ledc_hal_timer_rst(&(p_ledc_obj[speed_mode]->ledc_hal), timer_sel);
222 ledc_ls_timer_update(speed_mode, timer_sel);
223 portEXIT_CRITICAL(&ledc_spinlock);
224 return ESP_OK;
225 }
226
ledc_timer_pause(ledc_mode_t speed_mode,ledc_timer_t timer_sel)227 esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, ledc_timer_t timer_sel)
228 {
229 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
230 LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
231 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
232 portENTER_CRITICAL(&ledc_spinlock);
233 ledc_hal_timer_pause(&(p_ledc_obj[speed_mode]->ledc_hal), timer_sel);
234 ledc_ls_timer_update(speed_mode, timer_sel);
235 portEXIT_CRITICAL(&ledc_spinlock);
236 return ESP_OK;
237 }
238
ledc_timer_resume(ledc_mode_t speed_mode,ledc_timer_t timer_sel)239 esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, ledc_timer_t timer_sel)
240 {
241 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
242 LEDC_ARG_CHECK(timer_sel < LEDC_TIMER_MAX, "timer_select");
243 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
244 portENTER_CRITICAL(&ledc_spinlock);
245 ledc_hal_timer_resume(&(p_ledc_obj[speed_mode]->ledc_hal), timer_sel);
246 ledc_ls_timer_update(speed_mode, timer_sel);
247 portEXIT_CRITICAL(&ledc_spinlock);
248 return ESP_OK;
249 }
250
ledc_isr_register(void (* fn)(void *),void * arg,int intr_alloc_flags,ledc_isr_handle_t * handle)251 esp_err_t ledc_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, ledc_isr_handle_t *handle)
252 {
253 esp_err_t ret;
254 LEDC_ARG_CHECK(fn, "fn");
255 portENTER_CRITICAL(&ledc_spinlock);
256 ret = esp_intr_alloc(ETS_LEDC_INTR_SOURCE, intr_alloc_flags, fn, arg, handle);
257 portEXIT_CRITICAL(&ledc_spinlock);
258 return ret;
259 }
260
261 // Setting the LEDC timer divisor with the given source clock, frequency and resolution.
ledc_set_timer_div(ledc_mode_t speed_mode,ledc_timer_t timer_num,ledc_clk_cfg_t clk_cfg,int freq_hz,int duty_resolution)262 static esp_err_t ledc_set_timer_div(ledc_mode_t speed_mode, ledc_timer_t timer_num, ledc_clk_cfg_t clk_cfg, int freq_hz, int duty_resolution)
263 {
264 uint32_t div_param = 0;
265 uint32_t precision = ( 0x1 << duty_resolution );
266 ledc_clk_src_t timer_clk_src = LEDC_APB_CLK;
267 // Calculate the divisor
268 // User specified source clock(RTC8M_CLK) for low speed channel
269 if ((speed_mode == LEDC_LOW_SPEED_MODE) && (clk_cfg == LEDC_USE_RTC8M_CLK)) {
270 if(s_ledc_slow_clk_8M == 0) {
271 if (ledc_slow_clk_calibrate() == false) {
272 goto error;
273 }
274 }
275 div_param = ( (uint64_t) s_ledc_slow_clk_8M << 8 ) / freq_hz / precision;
276 } else {
277 // Automatically select APB or REF_TICK as the source clock.
278 if (clk_cfg == LEDC_AUTO_CLK) {
279 // Try calculating divisor based on LEDC_APB_CLK
280 div_param = ( (uint64_t) LEDC_APB_CLK_HZ << 8 ) / freq_hz / precision;
281 if (div_param > LEDC_TIMER_DIV_NUM_MAX) {
282 // APB_CLK results in divisor which too high. Try using REF_TICK as clock source.
283 timer_clk_src = LEDC_REF_TICK;
284 div_param = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
285 } else if (div_param < 256) {
286 // divisor is too low
287 goto error;
288 }
289 // User specified source clock(LEDC_APB_CLK_HZ or LEDC_REF_TICK)
290 } else {
291 timer_clk_src = (clk_cfg == LEDC_USE_REF_TICK) ? LEDC_REF_TICK : LEDC_APB_CLK;
292 uint32_t src_clk_freq = ledc_get_src_clk_freq(clk_cfg);
293 div_param = ( (uint64_t) src_clk_freq << 8 ) / freq_hz / precision;
294 }
295 }
296 if (div_param < 256 || div_param > LEDC_TIMER_DIV_NUM_MAX) {
297 goto error;
298 }
299 if (speed_mode == LEDC_LOW_SPEED_MODE) {
300 portENTER_CRITICAL(&ledc_spinlock);
301 ledc_hal_set_slow_clk(&(p_ledc_obj[speed_mode]->ledc_hal), clk_cfg);
302 portEXIT_CRITICAL(&ledc_spinlock);
303 }
304 //Set the divisor
305 ledc_timer_set(speed_mode, timer_num, div_param, duty_resolution, timer_clk_src);
306 // reset the timer
307 ledc_timer_rst(speed_mode, timer_num);
308 return ESP_OK;
309 error:
310 ESP_LOGE(LEDC_TAG, "requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution. div_param=%d",
311 (uint32_t ) div_param);
312 return ESP_FAIL;
313 }
314
ledc_timer_config(const ledc_timer_config_t * timer_conf)315 esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf)
316 {
317 LEDC_ARG_CHECK(timer_conf != NULL, "timer_conf");
318 uint32_t freq_hz = timer_conf->freq_hz;
319 uint32_t duty_resolution = timer_conf->duty_resolution;
320 uint32_t timer_num = timer_conf->timer_num;
321 uint32_t speed_mode = timer_conf->speed_mode;
322 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
323 LEDC_ARG_CHECK(!((timer_conf->clk_cfg == LEDC_USE_RTC8M_CLK) && (speed_mode != LEDC_LOW_SPEED_MODE)), "Only low speed channel support RTC8M_CLK");
324 periph_module_enable(PERIPH_LEDC_MODULE);
325 if (freq_hz == 0 || duty_resolution == 0 || duty_resolution >= LEDC_TIMER_BIT_MAX) {
326 ESP_LOGE(LEDC_TAG, "freq_hz=%u duty_resolution=%u", freq_hz, duty_resolution);
327 return ESP_ERR_INVALID_ARG;
328 }
329 if (timer_num > LEDC_TIMER_3) {
330 ESP_LOGE(LEDC_TAG, "invalid timer #%u", timer_num);
331 return ESP_ERR_INVALID_ARG;
332 }
333
334 if(p_ledc_obj[speed_mode] == NULL) {
335 p_ledc_obj[speed_mode] = (ledc_obj_t *) heap_caps_calloc(1, sizeof(ledc_obj_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
336 if (p_ledc_obj[speed_mode] == NULL) {
337 return ESP_ERR_NO_MEM;
338 }
339 ledc_hal_init(&(p_ledc_obj[speed_mode]->ledc_hal), speed_mode);
340 }
341
342 return ledc_set_timer_div(speed_mode, timer_num, timer_conf->clk_cfg, freq_hz, duty_resolution);
343 }
344
ledc_set_pin(int gpio_num,ledc_mode_t speed_mode,ledc_channel_t ledc_channel)345 esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel)
346 {
347 LEDC_ARG_CHECK(ledc_channel < LEDC_CHANNEL_MAX, "ledc_channel");
348 LEDC_ARG_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "gpio_num");
349 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
350 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
351 gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
352 esp_rom_gpio_connect_out_signal(gpio_num, ledc_periph_signal[speed_mode].sig_out0_idx + ledc_channel, 0, 0);
353 return ESP_OK;
354 }
355
ledc_channel_config(const ledc_channel_config_t * ledc_conf)356 esp_err_t ledc_channel_config(const ledc_channel_config_t* ledc_conf)
357 {
358 LEDC_ARG_CHECK(ledc_conf, "ledc_conf");
359 uint32_t speed_mode = ledc_conf->speed_mode;
360 uint32_t gpio_num = ledc_conf->gpio_num;
361 uint32_t ledc_channel = ledc_conf->channel;
362 uint32_t timer_select = ledc_conf->timer_sel;
363 uint32_t intr_type = ledc_conf->intr_type;
364 uint32_t duty = ledc_conf->duty;
365 uint32_t hpoint = ledc_conf->hpoint;
366 bool output_invert = ledc_conf->flags.output_invert;
367 LEDC_ARG_CHECK(ledc_channel < LEDC_CHANNEL_MAX, "ledc_channel");
368 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
369 LEDC_ARG_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "gpio_num");
370 LEDC_ARG_CHECK(timer_select < LEDC_TIMER_MAX, "timer_select");
371 LEDC_ARG_CHECK(intr_type < LEDC_INTR_MAX, "intr_type");
372
373 periph_module_enable(PERIPH_LEDC_MODULE);
374 esp_err_t ret = ESP_OK;
375
376 if(p_ledc_obj[speed_mode] == NULL) {
377 p_ledc_obj[speed_mode] = (ledc_obj_t *) heap_caps_calloc(1, sizeof(ledc_obj_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
378 if (p_ledc_obj[speed_mode] == NULL) {
379 return ESP_ERR_NO_MEM;
380 }
381 ledc_hal_init(&(p_ledc_obj[speed_mode]->ledc_hal), speed_mode);
382 }
383
384 /*set channel parameters*/
385 /* channel parameters decide how the waveform looks like in one period*/
386 /* set channel duty and hpoint value, duty range is (0 ~ ((2 ** duty_resolution) - 1)), max hpoint value is 0xfffff*/
387 ledc_set_duty_with_hpoint(speed_mode, ledc_channel, duty, hpoint);
388 /*update duty settings*/
389 ledc_update_duty(speed_mode, ledc_channel);
390 /*bind the channel with the timer*/
391 ledc_bind_channel_timer(speed_mode, ledc_channel, timer_select);
392 /*set interrupt type*/
393 ledc_enable_intr_type(speed_mode, ledc_channel, intr_type);
394 ESP_LOGD(LEDC_TAG, "LEDC_PWM CHANNEL %1u|GPIO %02u|Duty %04u|Time %01u",
395 ledc_channel, gpio_num, duty, timer_select
396 );
397 /*set LEDC signal in gpio matrix*/
398 gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
399 gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
400 esp_rom_gpio_connect_out_signal(gpio_num, ledc_periph_signal[speed_mode].sig_out0_idx + ledc_channel, output_invert, 0);
401
402 return ret;
403 }
404
ledc_update_duty(ledc_mode_t speed_mode,ledc_channel_t channel)405 esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
406 {
407 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
408 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
409 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
410 portENTER_CRITICAL(&ledc_spinlock);
411 ledc_hal_set_sig_out_en(&(p_ledc_obj[speed_mode]->ledc_hal), channel, true);
412 ledc_hal_set_duty_start(&(p_ledc_obj[speed_mode]->ledc_hal), channel, true);
413 ledc_ls_channel_update(speed_mode, channel);
414 portEXIT_CRITICAL(&ledc_spinlock);
415 return ESP_OK;
416 }
417
ledc_stop(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t idle_level)418 esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level)
419 {
420 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
421 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
422 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
423 portENTER_CRITICAL(&ledc_spinlock);
424 ledc_hal_set_idle_level(&(p_ledc_obj[speed_mode]->ledc_hal), channel, idle_level);
425 ledc_hal_set_sig_out_en(&(p_ledc_obj[speed_mode]->ledc_hal), channel, false);
426 ledc_hal_set_duty_start(&(p_ledc_obj[speed_mode]->ledc_hal), channel, false);
427 ledc_ls_channel_update(speed_mode, channel);
428 portEXIT_CRITICAL(&ledc_spinlock);
429 return ESP_OK;
430 }
431
ledc_set_fade(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t duty,ledc_duty_direction_t fade_direction,uint32_t step_num,uint32_t duty_cyle_num,uint32_t duty_scale)432 esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, ledc_duty_direction_t fade_direction,
433 uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale)
434 {
435 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
436 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
437 LEDC_ARG_CHECK(fade_direction < LEDC_DUTY_DIR_MAX, "fade_direction");
438 LEDC_ARG_CHECK(step_num <= LEDC_DUTY_NUM_MAX, "step_num");
439 LEDC_ARG_CHECK(duty_cyle_num <= LEDC_DUTY_CYCLE_MAX, "duty_cycle_num");
440 LEDC_ARG_CHECK(duty_scale <= LEDC_DUTY_SCALE_MAX, "duty_scale");
441 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
442 _ledc_fade_hw_acquire(speed_mode, channel);
443 ledc_duty_config(speed_mode,
444 channel, //uint32_t chan_num,
445 LEDC_VAL_NO_CHANGE,
446 duty, //uint32_t duty_val,
447 fade_direction, //uint32_t increase,
448 step_num, //uint32_t duty_num,
449 duty_cyle_num, //uint32_t duty_cycle,
450 duty_scale //uint32_t duty_scale
451 );
452 _ledc_fade_hw_release(speed_mode, channel);
453 return ESP_OK;
454 }
455
ledc_set_duty_with_hpoint(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t duty,uint32_t hpoint)456 esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint)
457 {
458 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
459 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
460 LEDC_ARG_CHECK(hpoint <= LEDC_HPOINT_VAL_MAX, "hpoint");
461 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
462 /* The channel configuration should not be changed before the fade operation is done. */
463 _ledc_fade_hw_acquire(speed_mode, channel);
464 ledc_duty_config(speed_mode,
465 channel, //uint32_t chan_num,
466 hpoint, //uint32_t hpoint_val,
467 duty, //uint32_t duty_val,
468 1, //uint32_t increase,
469 0, //uint32_t duty_num,
470 0, //uint32_t duty_cycle,
471 0 //uint32_t duty_scale
472 );
473 _ledc_fade_hw_release(speed_mode, channel);
474 return ESP_OK;
475 }
476
ledc_set_duty(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t duty)477 esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)
478 {
479 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
480 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
481 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
482 /* The channel configuration should not be changed before the fade operation is done. */
483 _ledc_fade_hw_acquire(speed_mode, channel);
484 ledc_duty_config(speed_mode,
485 channel, //uint32_t chan_num,
486 LEDC_VAL_NO_CHANGE,
487 duty, //uint32_t duty_val,
488 1, //uint32_t increase,
489 0, //uint32_t duty_num,
490 0, //uint32_t duty_cycle,
491 0 //uint32_t duty_scale
492 );
493 _ledc_fade_hw_release(speed_mode, channel);
494 return ESP_OK;
495 }
496
ledc_get_duty(ledc_mode_t speed_mode,ledc_channel_t channel)497 uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
498 {
499 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
500 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
501 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
502 uint32_t duty = 0;
503 ledc_hal_get_duty(&(p_ledc_obj[speed_mode]->ledc_hal), channel, &duty);
504 return duty;
505 }
506
ledc_get_hpoint(ledc_mode_t speed_mode,ledc_channel_t channel)507 int ledc_get_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel)
508 {
509 LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode argument is invalid", LEDC_ERR_VAL);
510 LEDC_CHECK(channel < LEDC_CHANNEL_MAX, "channel argument is invalid", LEDC_ERR_VAL);
511 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
512 uint32_t hpoint = 0;
513 ledc_hal_get_hpoint(&(p_ledc_obj[speed_mode]->ledc_hal), channel, &hpoint);
514 return hpoint;
515 }
516
ledc_set_freq(ledc_mode_t speed_mode,ledc_timer_t timer_num,uint32_t freq_hz)517 esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz)
518 {
519 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
520 LEDC_ARG_CHECK(timer_num < LEDC_TIMER_MAX, "timer_num");
521 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
522 ledc_clk_cfg_t clk_cfg = LEDC_USE_APB_CLK;
523 uint32_t duty_resolution = 0;
524 ledc_hal_get_clk_cfg(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &clk_cfg);
525 ledc_hal_get_duty_resolution(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &duty_resolution);
526 return ledc_set_timer_div(speed_mode, timer_num, clk_cfg, freq_hz, duty_resolution);
527 }
528
ledc_get_freq(ledc_mode_t speed_mode,ledc_timer_t timer_num)529 uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num)
530 {
531 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
532 LEDC_ARG_CHECK(timer_num < LEDC_TIMER_MAX, "timer_num");
533 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
534 portENTER_CRITICAL(&ledc_spinlock);
535 uint32_t clock_divider = 0;
536 uint32_t duty_resolution = 0;
537 ledc_clk_cfg_t clk_cfg = LEDC_USE_APB_CLK;
538 ledc_hal_get_clock_divider(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &clock_divider);
539 ledc_hal_get_duty_resolution(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &duty_resolution);
540 ledc_hal_get_clk_cfg(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &clk_cfg);
541 uint32_t precision = (0x1 << duty_resolution);
542 uint32_t src_clk_freq = ledc_get_src_clk_freq(clk_cfg);
543 portEXIT_CRITICAL(&ledc_spinlock);
544 return ((uint64_t) src_clk_freq << 8) / precision / clock_divider;
545 }
546
ledc_calc_fade_end_channel(uint32_t * fade_end_status,uint32_t * channel)547 static inline void ledc_calc_fade_end_channel(uint32_t *fade_end_status, uint32_t *channel)
548 {
549 uint32_t i = __builtin_ffs((*fade_end_status)) - 1;
550 (*fade_end_status) &= ~(1 << i);
551 *channel = i;
552 }
553
ledc_fade_isr(void * arg)554 void IRAM_ATTR ledc_fade_isr(void* arg)
555 {
556 bool cb_yield = false;
557 portBASE_TYPE HPTaskAwoken = pdFALSE;
558 uint32_t speed_mode = 0;
559 uint32_t channel = 0;
560 uint32_t intr_status = 0;
561
562 for (speed_mode = 0; speed_mode < LEDC_SPEED_MODE_MAX; speed_mode++) {
563 if (p_ledc_obj[speed_mode] == NULL) {
564 continue;
565 }
566 ledc_hal_get_fade_end_intr_status(&(p_ledc_obj[speed_mode]->ledc_hal), &intr_status);
567 while(intr_status) {
568 ledc_calc_fade_end_channel(&intr_status, &channel);
569
570 // clear interrupt
571 portENTER_CRITICAL_ISR(&ledc_spinlock);
572 ledc_hal_clear_fade_end_intr_status(&(p_ledc_obj[speed_mode]->ledc_hal), channel);
573 portEXIT_CRITICAL_ISR(&ledc_spinlock);
574
575 if (s_ledc_fade_rec[speed_mode][channel] == NULL) {
576 //fade object not initialized yet.
577 continue;
578 }
579
580 uint32_t duty_cur = 0;
581 ledc_hal_get_duty(&(p_ledc_obj[speed_mode]->ledc_hal), channel, &duty_cur);
582 uint32_t duty_tar = s_ledc_fade_rec[speed_mode][channel]->target_duty;
583 int scale = s_ledc_fade_rec[speed_mode][channel]->scale;
584 if (duty_cur == duty_tar || scale == 0) {
585 xSemaphoreGiveFromISR(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem, &HPTaskAwoken);
586
587 ledc_cb_param_t param = {
588 .event = LEDC_FADE_END_EVT,
589 .speed_mode = speed_mode,
590 .channel = channel,
591 .duty = duty_cur
592 };
593 ledc_cb_t fade_cb = s_ledc_fade_rec[speed_mode][channel]->ledc_fade_callback;
594 if (fade_cb) {
595 cb_yield |= fade_cb(¶m, s_ledc_fade_rec[speed_mode][channel]->cb_user_arg);
596 }
597 continue;
598 }
599 int cycle = s_ledc_fade_rec[speed_mode][channel]->cycle_num;
600 int delta = s_ledc_fade_rec[speed_mode][channel]->direction == LEDC_DUTY_DIR_DECREASE ? duty_cur - duty_tar : duty_tar - duty_cur;
601 int step = delta / scale > LEDC_STEP_NUM_MAX ? LEDC_STEP_NUM_MAX : delta / scale;
602 if (delta > scale) {
603 ledc_duty_config(
604 speed_mode,
605 channel,
606 LEDC_VAL_NO_CHANGE,
607 duty_cur,
608 s_ledc_fade_rec[speed_mode][channel]->direction,
609 step,
610 cycle,
611 scale);
612 } else {
613 ledc_duty_config(
614 speed_mode,
615 channel,
616 LEDC_VAL_NO_CHANGE,
617 duty_tar,
618 s_ledc_fade_rec[speed_mode][channel]->direction,
619 1,
620 1,
621 0);
622 }
623 portENTER_CRITICAL_ISR(&ledc_spinlock);
624 ledc_hal_set_duty_start(&(p_ledc_obj[speed_mode]->ledc_hal), channel, true);
625 portEXIT_CRITICAL_ISR(&ledc_spinlock);
626 }
627 }
628 if (HPTaskAwoken == pdTRUE || cb_yield) {
629 portYIELD_FROM_ISR();
630 }
631 }
632
ledc_fade_channel_deinit(ledc_mode_t speed_mode,ledc_channel_t channel)633 static esp_err_t ledc_fade_channel_deinit(ledc_mode_t speed_mode, ledc_channel_t channel)
634 {
635 if (s_ledc_fade_rec[speed_mode][channel]) {
636 if (s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux) {
637 vSemaphoreDelete(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux);
638 s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux = NULL;
639 }
640 if (s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem) {
641 vSemaphoreDelete(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem);
642 s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem = NULL;
643 }
644 free(s_ledc_fade_rec[speed_mode][channel]);
645 s_ledc_fade_rec[speed_mode][channel] = NULL;
646 }
647 return ESP_OK;
648 }
649
ledc_fade_channel_init_check(ledc_mode_t speed_mode,ledc_channel_t channel)650 static esp_err_t ledc_fade_channel_init_check(ledc_mode_t speed_mode, ledc_channel_t channel)
651 {
652 if (s_ledc_fade_isr_handle == NULL) {
653 ESP_LOGE(LEDC_TAG, "Fade service not installed, call ledc_fade_func_install");
654 return ESP_FAIL;
655 }
656 if (s_ledc_fade_rec[speed_mode][channel] == NULL) {
657 #if CONFIG_SPIRAM_USE_MALLOC
658 s_ledc_fade_rec[speed_mode][channel] = (ledc_fade_t *) heap_caps_calloc(1, sizeof(ledc_fade_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
659 if (!s_ledc_fade_rec[speed_mode][channel]) {
660 ledc_fade_channel_deinit(speed_mode, channel);
661 return ESP_FAIL;
662 }
663
664 memset(&s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem_storage, 0, sizeof(StaticQueue_t));
665 s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem = xSemaphoreCreateBinaryStatic(&s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem_storage);
666 #else
667 s_ledc_fade_rec[speed_mode][channel] = (ledc_fade_t *) calloc(1, sizeof(ledc_fade_t));
668 s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem = xSemaphoreCreateBinary();
669 #endif
670 s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux = xSemaphoreCreateMutex();
671 xSemaphoreGive(s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem);
672 }
673 if (s_ledc_fade_rec[speed_mode][channel]
674 && s_ledc_fade_rec[speed_mode][channel]->ledc_fade_mux
675 && s_ledc_fade_rec[speed_mode][channel]->ledc_fade_sem) {
676 return ESP_OK;
677 } else {
678 ledc_fade_channel_deinit(speed_mode, channel);
679 return ESP_FAIL;
680 }
681 }
682
_ledc_set_fade_with_step(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t target_duty,int scale,int cycle_num)683 static esp_err_t _ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int scale, int cycle_num)
684 {
685 portENTER_CRITICAL(&ledc_spinlock);
686 uint32_t duty_cur = 0;
687 ledc_hal_get_duty(&(p_ledc_obj[speed_mode]->ledc_hal), channel, &duty_cur);
688 // When duty == max_duty, meanwhile, if scale == 1 and fade_down == 1, counter would overflow.
689 if (duty_cur == ledc_get_max_duty(speed_mode, channel)) {
690 duty_cur -= 1;
691 }
692 s_ledc_fade_rec[speed_mode][channel]->speed_mode = speed_mode;
693 s_ledc_fade_rec[speed_mode][channel]->target_duty = target_duty;
694 s_ledc_fade_rec[speed_mode][channel]->cycle_num = cycle_num;
695 s_ledc_fade_rec[speed_mode][channel]->scale = scale;
696 int step_num = 0;
697 int dir = LEDC_DUTY_DIR_DECREASE;
698 if (scale > 0) {
699 if (duty_cur > target_duty) {
700 s_ledc_fade_rec[speed_mode][channel]->direction = LEDC_DUTY_DIR_DECREASE;
701 step_num = (duty_cur - target_duty) / scale;
702 step_num = step_num > LEDC_STEP_NUM_MAX ? LEDC_STEP_NUM_MAX : step_num;
703 } else {
704 s_ledc_fade_rec[speed_mode][channel]->direction = LEDC_DUTY_DIR_INCREASE;
705 dir = LEDC_DUTY_DIR_INCREASE;
706 step_num = (target_duty - duty_cur) / scale;
707 step_num = step_num > LEDC_STEP_NUM_MAX ? LEDC_STEP_NUM_MAX : step_num;
708 }
709 }
710
711 portEXIT_CRITICAL(&ledc_spinlock);
712 if (scale > 0 && step_num > 0) {
713 ledc_duty_config(speed_mode, channel, LEDC_VAL_NO_CHANGE, duty_cur, dir, step_num, cycle_num, scale);
714 ESP_LOGD(LEDC_TAG, "cur duty: %d; target: %d, step: %d, cycle: %d; scale: %d; dir: %d\n",
715 duty_cur, target_duty, step_num, cycle_num, scale, dir);
716 } else {
717 ledc_duty_config(speed_mode, channel, LEDC_VAL_NO_CHANGE, target_duty, dir, 0, 1, 0);
718 ESP_LOGD(LEDC_TAG, "Set to target duty: %d", target_duty);
719 }
720 return ESP_OK;
721 }
722
_ledc_set_fade_with_time(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t target_duty,int max_fade_time_ms)723 static esp_err_t _ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms)
724 {
725 ledc_timer_t timer_sel;
726 uint32_t duty_cur = 0;
727 ledc_hal_get_channel_timer(&(p_ledc_obj[speed_mode]->ledc_hal), channel, &timer_sel);
728 ledc_hal_get_duty(&(p_ledc_obj[speed_mode]->ledc_hal), channel, &duty_cur);
729 uint32_t freq = ledc_get_freq(speed_mode, timer_sel);
730 uint32_t duty_delta = target_duty > duty_cur ? target_duty - duty_cur : duty_cur - target_duty;
731
732 if (duty_delta == 0) {
733 return _ledc_set_fade_with_step(speed_mode, channel, target_duty, 0, 0);
734 }
735 uint32_t total_cycles = max_fade_time_ms * freq / 1000;
736 if (total_cycles == 0) {
737 ESP_LOGW(LEDC_TAG, LEDC_FADE_TOO_FAST_STR);
738 return _ledc_set_fade_with_step(speed_mode, channel, target_duty, 0, 0);
739 }
740 int scale, cycle_num;
741 if (total_cycles > duty_delta) {
742 scale = 1;
743 cycle_num = total_cycles / duty_delta;
744 if (cycle_num > LEDC_DUTY_NUM_MAX) {
745 ESP_LOGW(LEDC_TAG, LEDC_FADE_TOO_SLOW_STR);
746 cycle_num = LEDC_DUTY_NUM_MAX;
747 }
748 } else {
749 cycle_num = 1;
750 scale = duty_delta / total_cycles;
751 if (scale > LEDC_DUTY_SCALE_MAX) {
752 ESP_LOGW(LEDC_TAG, LEDC_FADE_TOO_FAST_STR);
753 scale = LEDC_DUTY_SCALE_MAX;
754 }
755 }
756 return _ledc_set_fade_with_step(speed_mode, channel, target_duty, scale, cycle_num);
757 }
758
_ledc_fade_start(ledc_mode_t speed_mode,ledc_channel_t channel,ledc_fade_mode_t fade_mode)759 static void _ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode)
760 {
761 s_ledc_fade_rec[speed_mode][channel]->mode = fade_mode;
762 // Clear interrupt status of channel
763 ledc_hal_clear_fade_end_intr_status(&(p_ledc_obj[speed_mode]->ledc_hal), channel);
764 // Enable interrupt for channel
765 ledc_enable_intr_type(speed_mode, channel, LEDC_INTR_FADE_END);
766 ledc_update_duty(speed_mode, channel);
767 if (fade_mode == LEDC_FADE_WAIT_DONE) {
768 // Waiting for fade done
769 _ledc_fade_hw_acquire(speed_mode, channel);
770 // Release hardware to support next time fade configure
771 _ledc_fade_hw_release(speed_mode, channel);
772 }
773 }
774
ledc_set_fade_with_time(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t target_duty,int max_fade_time_ms)775 esp_err_t ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms)
776 {
777 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
778 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
779 LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
780 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
781 LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
782
783 _ledc_fade_hw_acquire(speed_mode, channel);
784 _ledc_set_fade_with_time(speed_mode, channel, target_duty, max_fade_time_ms);
785 _ledc_fade_hw_release(speed_mode, channel);
786 return ESP_OK;
787 }
788
ledc_set_fade_with_step(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t target_duty,uint32_t scale,uint32_t cycle_num)789 esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num)
790 {
791 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
792 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
793 LEDC_ARG_CHECK((scale > 0) && (scale <= LEDC_DUTY_SCALE_MAX), "fade scale");
794 LEDC_ARG_CHECK((cycle_num > 0) && (cycle_num <= LEDC_DUTY_CYCLE_MAX), "cycle_num");
795 LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
796 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
797 LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
798
799 _ledc_fade_hw_acquire(speed_mode, channel);
800 _ledc_set_fade_with_step(speed_mode, channel, target_duty, scale, cycle_num);
801 _ledc_fade_hw_release(speed_mode, channel);
802 return ESP_OK;
803 }
804
ledc_fade_start(ledc_mode_t speed_mode,ledc_channel_t channel,ledc_fade_mode_t fade_mode)805 esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode)
806 {
807 LEDC_CHECK(s_ledc_fade_rec != NULL, LEDC_FADE_SERVICE_ERR_STR, ESP_ERR_INVALID_STATE);
808 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
809 LEDC_ARG_CHECK(fade_mode < LEDC_FADE_MAX, "fade_mode");
810 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
811 _ledc_fade_hw_acquire(speed_mode, channel);
812 _ledc_fade_start(speed_mode, channel, fade_mode);
813 return ESP_OK;
814 }
815
ledc_fade_func_install(int intr_alloc_flags)816 esp_err_t ledc_fade_func_install(int intr_alloc_flags)
817 {
818 //OR intr_alloc_flags with ESP_INTR_FLAG_IRAM because the fade isr is in IRAM
819 return ledc_isr_register(ledc_fade_isr, NULL, intr_alloc_flags | ESP_INTR_FLAG_IRAM, &s_ledc_fade_isr_handle);
820 }
821
ledc_fade_func_uninstall(void)822 void ledc_fade_func_uninstall(void)
823 {
824 if (s_ledc_fade_rec == NULL) {
825 return;
826 }
827 if (s_ledc_fade_isr_handle) {
828 esp_intr_free(s_ledc_fade_isr_handle);
829 s_ledc_fade_isr_handle = NULL;
830 }
831 int channel, mode;
832 for (mode = 0; mode < LEDC_SPEED_MODE_MAX; mode++) {
833 for (channel = 0; channel < LEDC_CHANNEL_MAX; channel++) {
834 ledc_fade_channel_deinit(mode, channel);
835 }
836 }
837 return;
838 }
839
ledc_cb_register(ledc_mode_t speed_mode,ledc_channel_t channel,ledc_cbs_t * cbs,void * user_arg)840 esp_err_t ledc_cb_register(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_cbs_t *cbs, void *user_arg)
841 {
842 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
843 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
844 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
845 LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
846 s_ledc_fade_rec[speed_mode][channel]->ledc_fade_callback = cbs->fade_cb;
847 s_ledc_fade_rec[speed_mode][channel]->cb_user_arg = user_arg;
848 return ESP_OK;
849 }
850
851 /*
852 * The functions below are thread-safe version of APIs for duty and fade control.
853 * These APIs can be called from different tasks.
854 */
ledc_set_duty_and_update(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t duty,uint32_t hpoint)855 esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint)
856 {
857 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
858 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
859 LEDC_ARG_CHECK(duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
860 LEDC_ARG_CHECK(hpoint <= LEDC_HPOINT_VAL_MAX, "hpoint");
861 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
862 LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK, LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
863 _ledc_fade_hw_acquire(speed_mode, channel);
864 ledc_duty_config(speed_mode, channel, hpoint, duty, 1, 0, 0, 0);
865 ledc_update_duty(speed_mode, channel);
866 _ledc_fade_hw_release(speed_mode, channel);
867 return ESP_OK;
868 }
869
ledc_set_fade_time_and_start(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t target_duty,uint32_t max_fade_time_ms,ledc_fade_mode_t fade_mode)870 esp_err_t ledc_set_fade_time_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t max_fade_time_ms, ledc_fade_mode_t fade_mode)
871 {
872 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
873 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
874 LEDC_ARG_CHECK(fade_mode < LEDC_FADE_MAX, "fade_mode");
875 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
876 LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
877 LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
878 _ledc_op_lock_acquire(speed_mode, channel);
879 _ledc_fade_hw_acquire(speed_mode, channel);
880 _ledc_set_fade_with_time(speed_mode, channel, target_duty, max_fade_time_ms);
881 _ledc_fade_start(speed_mode, channel, fade_mode);
882 _ledc_op_lock_release(speed_mode, channel);
883 return ESP_OK;
884 }
885
ledc_set_fade_step_and_start(ledc_mode_t speed_mode,ledc_channel_t channel,uint32_t target_duty,uint32_t scale,uint32_t cycle_num,ledc_fade_mode_t fade_mode)886 esp_err_t ledc_set_fade_step_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num, ledc_fade_mode_t fade_mode)
887 {
888 LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
889 LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
890 LEDC_ARG_CHECK(fade_mode < LEDC_FADE_MAX, "fade_mode");
891 LEDC_CHECK(p_ledc_obj[speed_mode] != NULL, LEDC_NOT_INIT, ESP_ERR_INVALID_STATE);
892 LEDC_CHECK(ledc_fade_channel_init_check(speed_mode, channel) == ESP_OK , LEDC_FADE_INIT_ERROR_STR, ESP_FAIL);
893 LEDC_ARG_CHECK((scale > 0) && (scale <= LEDC_DUTY_SCALE_MAX), "fade scale");
894 LEDC_ARG_CHECK((cycle_num > 0) && (cycle_num <= LEDC_DUTY_CYCLE_MAX), "cycle_num");
895 LEDC_ARG_CHECK(target_duty <= ledc_get_max_duty(speed_mode, channel), "target_duty");
896 _ledc_op_lock_acquire(speed_mode, channel);
897 _ledc_fade_hw_acquire(speed_mode, channel);
898 _ledc_set_fade_with_step(speed_mode, channel, target_duty, scale, cycle_num);
899 _ledc_fade_start(speed_mode, channel, fade_mode);
900 _ledc_op_lock_release(speed_mode, channel);
901 return ESP_OK;
902 }
903