-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
pwm_led_esp32.c
376 lines (307 loc) · 10.8 KB
/
pwm_led_esp32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (c) 2017 Vitor Massaru Iha <vitor@massaru.org>
* Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT espressif_esp32_ledc
/* Include esp-idf headers first to avoid redefining BIT() macro */
#include <hal/ledc_hal.h>
#include <hal/ledc_types.h>
#include <soc.h>
#include <errno.h>
#include <string.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(pwm_ledc_esp32, CONFIG_PWM_LOG_LEVEL);
struct pwm_ledc_esp32_data {
ledc_hal_context_t hal;
struct k_sem cmd_sem;
};
struct pwm_ledc_esp32_channel_config {
const uint8_t idx;
const uint8_t channel_num;
const uint8_t timer_num;
uint32_t freq;
const ledc_mode_t speed_mode;
uint8_t resolution;
ledc_clk_src_t clock_src;
uint32_t duty_val;
};
struct pwm_ledc_esp32_config {
const struct pinctrl_dev_config *pincfg;
const struct device *clock_dev;
const clock_control_subsys_t clock_subsys;
struct pwm_ledc_esp32_channel_config *channel_config;
const int channel_len;
};
static struct pwm_ledc_esp32_channel_config *get_channel_config(const struct device *dev,
int channel_id)
{
struct pwm_ledc_esp32_config *config =
(struct pwm_ledc_esp32_config *) dev->config;
for (uint8_t i = 0; i < config->channel_len; i++) {
if (config->channel_config[i].idx == channel_id) {
return &config->channel_config[i];
}
}
return NULL;
}
static void pwm_led_esp32_low_speed_update(const struct device *dev, int speed_mode, int channel)
{
uint32_t reg_addr;
struct pwm_ledc_esp32_data *data = (struct pwm_ledc_esp32_data *const)(dev)->data;
if (speed_mode == LEDC_LOW_SPEED_MODE) {
ledc_hal_ls_channel_update(&data->hal, channel);
}
}
static void pwm_led_esp32_update_duty(const struct device *dev, int speed_mode, int channel)
{
struct pwm_ledc_esp32_data *data = (struct pwm_ledc_esp32_data *const)(dev)->data;
ledc_hal_set_sig_out_en(&data->hal, channel, true);
ledc_hal_set_duty_start(&data->hal, channel, true);
pwm_led_esp32_low_speed_update(dev, speed_mode, channel);
}
static void pwm_led_esp32_duty_set(const struct device *dev,
struct pwm_ledc_esp32_channel_config *channel)
{
struct pwm_ledc_esp32_data *data = (struct pwm_ledc_esp32_data *const)(dev)->data;
ledc_hal_set_hpoint(&data->hal, channel->channel_num, 0);
ledc_hal_set_duty_int_part(&data->hal, channel->channel_num, channel->duty_val);
ledc_hal_set_duty_direction(&data->hal, channel->channel_num, 1);
ledc_hal_set_duty_num(&data->hal, channel->channel_num, 1);
ledc_hal_set_duty_cycle(&data->hal, channel->channel_num, 1);
ledc_hal_set_duty_scale(&data->hal, channel->channel_num, 0);
pwm_led_esp32_low_speed_update(dev, channel->speed_mode, channel->channel_num);
pwm_led_esp32_update_duty(dev, channel->speed_mode, channel->channel_num);
}
static int pwm_led_esp32_configure_pinctrl(const struct device *dev)
{
int ret;
struct pwm_ledc_esp32_config *config = (struct pwm_ledc_esp32_config *) dev->config;
ret = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
if (ret < 0) {
LOG_ERR("PWM pinctrl setup failed (%d)", ret);
return ret;
}
return 0;
}
static void pwm_led_esp32_bind_channel_timer(const struct device *dev,
struct pwm_ledc_esp32_channel_config *channel)
{
struct pwm_ledc_esp32_data *data = (struct pwm_ledc_esp32_data *const)(dev)->data;
ledc_hal_bind_channel_timer(&data->hal, channel->channel_num, channel->timer_num);
pwm_led_esp32_low_speed_update(dev, channel->speed_mode, channel->channel_num);
}
static int pwm_led_esp32_calculate_max_resolution(struct pwm_ledc_esp32_channel_config *channel)
{
/**
* Max duty resolution can be obtained with
* max_res = log2(CLK_FREQ/FREQ)
*/
uint64_t clock_freq = channel->clock_src == LEDC_APB_CLK ? APB_CLK_FREQ : REF_CLK_FREQ;
uint32_t max_precision_n = clock_freq/channel->freq;
for (uint8_t i = 0; i <= SOC_LEDC_TIMER_BIT_WIDTH; i++) {
max_precision_n /= 2;
if (!max_precision_n) {
channel->resolution = i;
return 0;
}
}
return -EINVAL;
}
static int pwm_led_esp32_timer_config(struct pwm_ledc_esp32_channel_config *channel)
{
/**
* Calculate max resolution based on the given frequency and the pwm clock.
*
* There are 2 clock resources for PWM:
*
* 1. APB_CLK (80MHz)
* 2. REF_TICK (1MHz)
*
* The low speed timers can be sourced from:
*
* 1. APB_CLK (80MHz)
* 2. RTC_CLK (8Mhz)
*
* The APB_CLK is mostly used
*
* First we try to find the largest resolution using the APB_CLK source.
* If the given frequency doesn't support it, we move to the next clock source.
*/
channel->clock_src = LEDC_APB_CLK;
if (!pwm_led_esp32_calculate_max_resolution(channel)) {
return 0;
}
#if SOC_LEDC_SUPPORT_REF_TICK
channel->clock_src = LEDC_REF_TICK;
if (!pwm_led_esp32_calculate_max_resolution(channel)) {
return 0;
}
#endif
/**
* ESP32 - S2,S3 and C3 variants have only 14 bits counter.
* where as the plain ESP32 variant has 20 bits counter.
* application failed to set low frequency(1Hz) in S2, S3 and C3 variants.
* to get very low frequencies on these variants,
* frequency needs to be tuned with 18 bits clock divider.
* so select the slow clock source (1MHz) with highest counter resolution.
* this can be handled on the func 'pwm_led_esp32_timer_set' with 'prescaler'.
*/
channel->resolution = SOC_LEDC_TIMER_BIT_WIDTH;
return 0;
}
static int pwm_led_esp32_timer_set(const struct device *dev,
struct pwm_ledc_esp32_channel_config *channel)
{
int prescaler = 0;
uint32_t precision = (0x1 << channel->resolution);
struct pwm_ledc_esp32_data *data = (struct pwm_ledc_esp32_data *const)(dev)->data;
__ASSERT_NO_MSG(channel->freq > 0);
switch (channel->clock_src) {
case LEDC_APB_CLK:
/** This expression comes from ESP32 Espressif's Technical Reference
* Manual chapter 13.2.2 Timers.
* div_num is a fixed point value (Q10.8).
*/
prescaler = ((uint64_t) APB_CLK_FREQ << 8) / channel->freq / precision;
break;
#if SOC_LEDC_SUPPORT_REF_TICK
case LEDC_REF_TICK:
prescaler = ((uint64_t) REF_CLK_FREQ << 8) / channel->freq / precision;
break;
#endif
default:
LOG_ERR("Invalid clock source (%d)", channel->clock_src);
return -EINVAL;
}
if (prescaler < 0x100 || prescaler > 0x3FFFF) {
LOG_ERR("Prescaler out of range: %#X", prescaler);
return -EINVAL;
}
if (channel->speed_mode == LEDC_LOW_SPEED_MODE) {
ledc_hal_set_slow_clk_sel(&data->hal, channel->clock_src);
}
ledc_hal_set_clock_divider(&data->hal, channel->timer_num, prescaler);
ledc_hal_set_duty_resolution(&data->hal, channel->timer_num, channel->resolution);
ledc_hal_set_clock_source(&data->hal, channel->timer_num, channel->clock_src);
if (channel->speed_mode == LEDC_LOW_SPEED_MODE) {
ledc_hal_ls_timer_update(&data->hal, channel->timer_num);
}
/* reset low speed timer */
ledc_hal_timer_rst(&data->hal, channel->timer_num);
return 0;
}
static int pwm_led_esp32_get_cycles_per_sec(const struct device *dev,
uint32_t channel_idx, uint64_t *cycles)
{
struct pwm_ledc_esp32_config *config = (struct pwm_ledc_esp32_config *) dev->config;
struct pwm_ledc_esp32_channel_config *channel = get_channel_config(dev, channel_idx);
if (!channel) {
LOG_ERR("Error getting channel %d", channel_idx);
return -EINVAL;
}
*cycles = channel->clock_src == LEDC_APB_CLK ? APB_CLK_FREQ : REF_CLK_FREQ;
return 0;
}
static int pwm_led_esp32_set_cycles(const struct device *dev, uint32_t channel_idx,
uint32_t period_cycles,
uint32_t pulse_cycles, pwm_flags_t flags)
{
int ret;
uint64_t clk_freq;
struct pwm_ledc_esp32_config *config = (struct pwm_ledc_esp32_config *) dev->config;
struct pwm_ledc_esp32_data *data = (struct pwm_ledc_esp32_data *const)(dev)->data;
struct pwm_ledc_esp32_channel_config *channel = get_channel_config(dev, channel_idx);
if (!channel) {
LOG_ERR("Error getting channel %d", channel_idx);
return -EINVAL;
}
/* Update PWM frequency according to period_cycles */
ret = pwm_led_esp32_get_cycles_per_sec(dev, channel_idx, &clk_freq);
if (ret < 0) {
return ret;
}
channel->freq = (uint32_t) (clk_freq/period_cycles);
if (!channel->freq) {
channel->freq = 1;
}
k_sem_take(&data->cmd_sem, K_FOREVER);
ledc_hal_init(&data->hal, channel->speed_mode);
ret = pwm_led_esp32_timer_config(channel);
if (ret < 0) {
k_sem_give(&data->cmd_sem);
return ret;
}
ret = pwm_led_esp32_timer_set(dev, channel);
if (ret < 0) {
k_sem_give(&data->cmd_sem);
return ret;
}
pwm_led_esp32_bind_channel_timer(dev, channel);
/* Update PWM duty */
double duty_cycle = (double) pulse_cycles / (double) period_cycles;
channel->duty_val = (uint32_t)((double) (1 << channel->resolution) * duty_cycle);
pwm_led_esp32_duty_set(dev, channel);
ret = pwm_led_esp32_configure_pinctrl(dev);
if (ret < 0) {
k_sem_give(&data->cmd_sem);
return ret;
}
k_sem_give(&data->cmd_sem);
return ret;
}
int pwm_led_esp32_init(const struct device *dev)
{
int ret;
const struct pwm_ledc_esp32_config *config = dev->config;
struct pwm_ledc_esp32_data *data = (struct pwm_ledc_esp32_data *const)(dev)->data;
if (!device_is_ready(config->clock_dev)) {
LOG_ERR("clock control device not ready");
return -ENODEV;
}
/* Enable peripheral */
clock_control_on(config->clock_dev, config->clock_subsys);
return 0;
}
static const struct pwm_driver_api pwm_led_esp32_api = {
.set_cycles = pwm_led_esp32_set_cycles,
.get_cycles_per_sec = pwm_led_esp32_get_cycles_per_sec,
};
PINCTRL_DT_INST_DEFINE(0);
#define CHANNEL_CONFIG(node_id) \
{ \
.idx = DT_REG_ADDR(node_id), \
.channel_num = DT_REG_ADDR(node_id) % 8, \
.timer_num = DT_PROP(node_id, timer), \
.speed_mode = DT_REG_ADDR(node_id) < SOC_LEDC_CHANNEL_NUM \
? LEDC_LOW_SPEED_MODE \
: !LEDC_LOW_SPEED_MODE, \
.clock_src = LEDC_APB_CLK, \
},
static struct pwm_ledc_esp32_channel_config channel_config[] = {
DT_INST_FOREACH_CHILD(0, CHANNEL_CONFIG)
};
static struct pwm_ledc_esp32_config pwm_ledc_esp32_config = {
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0),
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(0)),
.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(0, offset),
.channel_config = channel_config,
.channel_len = ARRAY_SIZE(channel_config),
};
static struct pwm_ledc_esp32_data pwm_ledc_esp32_data = {
.hal = {
.dev = (ledc_dev_t *) DT_INST_REG_ADDR(0),
},
.cmd_sem = Z_SEM_INITIALIZER(pwm_ledc_esp32_data.cmd_sem, 1, 1),
};
DEVICE_DT_INST_DEFINE(0, &pwm_led_esp32_init, NULL,
&pwm_ledc_esp32_data,
&pwm_ledc_esp32_config,
POST_KERNEL,
CONFIG_PWM_INIT_PRIORITY,
&pwm_led_esp32_api);