Skip to content

Commit fba8c0c

Browse files
committed
Correct channels, modes and timer resolutions for all variants
1 parent 226597c commit fba8c0c

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/ledc.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ mrb_esp32_ledc_timer_config(mrb_state *mrb, mrb_value self) {
3030

3131
static mrb_value
3232
mrb_esp32_ledc_channel_config(mrb_state *mrb, mrb_value self) {
33-
// Only set pin and channel for now. Can add more config later.
3433
mrb_value pin, group, timer, ch;
3534

3635
mrb_get_args(mrb, "oooo", &pin, &group, &timer, &ch);
@@ -129,7 +128,7 @@ mrb_esp32_ledc_unset_pin(mrb_state *mrb, mrb_value self) {
129128
return mrb_nil_value();
130129
}
131130

132-
// PIN_FUNC_GPIO = 2. Normal GPIO.
131+
// PIN_FUNC_GPIO is integer 2, which returns the pin to normal GPIO mode.
133132
// Last param is output inversion.
134133
gpio_iomux_out(mrb_fixnum(pin), PIN_FUNC_GPIO, false);
135134

@@ -141,8 +140,10 @@ mrb_mruby_esp32_ledc_gem_init(mrb_state* mrb)
141140
{
142141
struct RClass *esp32, *ledc, *constants;
143142

143+
// ESP32 mruby module
144144
esp32 = mrb_define_module(mrb, "ESP32");
145145

146+
// ESP32::LEDC
146147
ledc = mrb_define_module_under(mrb, esp32, "LEDC");
147148
mrb_define_module_function(mrb, ledc, "timer_config", mrb_esp32_ledc_timer_config, MRB_ARGS_REQ(4));
148149
mrb_define_module_function(mrb, ledc, "channel_config", mrb_esp32_ledc_channel_config, MRB_ARGS_REQ(4));
@@ -152,37 +153,50 @@ mrb_mruby_esp32_ledc_gem_init(mrb_state* mrb)
152153
mrb_define_module_function(mrb, ledc, "set_pin", mrb_esp32_ledc_set_pin, MRB_ARGS_REQ(3));
153154
mrb_define_module_function(mrb, ledc, "unset_pin", mrb_esp32_ledc_unset_pin, MRB_ARGS_REQ(1));
154155

156+
// ESP32::Constants
155157
constants = mrb_define_module_under(mrb, esp32, "Constants");
156158

157-
#define define_const(SYM) \
159+
// Pass a C constant through to mruby, defined inside ESP32::Constants.
160+
#define define_const(SYM) \
158161
do { \
159162
mrb_define_const(mrb, constants, #SYM, mrb_fixnum_value(SYM)); \
160163
} while (0)
161164

162-
// LEDC channel groups.
163-
// High speed not available on some devices.
164-
define_const(LEDC_HIGH_SPEED_MODE);
165-
define_const(LEDC_LOW_SPEED_MODE);
165+
//
166+
// LEDC Speed Modes (Groups)
167+
// All chips define LEDC_LOW_SPEED_MODE, with either 6 or 8 channels.
168+
define_const(LEDC_LOW_SPEED_MODE);
169+
//
170+
// Only original ESP32 defines LEDC_HIGH_SPEED_MODE, second group with 8 more channels.
171+
#if defined(CONFIG_IDF_TARGET_ESP32)
172+
define_const(LEDC_HIGH_SPEED_MODE);
173+
#endif
166174

167-
// LEDC channel numbers. 8 channels per group.
175+
//
176+
// LEDC Channels
177+
// All chips define LEDC_CHANNEL_MAX and LEDC_CHANNEL_0..LEDC_CHANNEL_5.
178+
define_const(LEDC_CHANNEL_MAX);
168179
define_const(LEDC_CHANNEL_0);
169180
define_const(LEDC_CHANNEL_1);
170181
define_const(LEDC_CHANNEL_2);
171182
define_const(LEDC_CHANNEL_3);
172183
define_const(LEDC_CHANNEL_4);
173184
define_const(LEDC_CHANNEL_5);
174-
define_const(LEDC_CHANNEL_6);
175-
define_const(LEDC_CHANNEL_7);
176-
define_const(LEDC_CHANNEL_MAX);
177-
178-
// LEDC timer numbers. 4 timers per group of 8 channels.
185+
// Only original ESP32, S2 and S3 have 6,7.
186+
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
187+
define_const(LEDC_CHANNEL_6);
188+
define_const(LEDC_CHANNEL_7);
189+
#endif
190+
191+
// All chips have 4 LEDC timers.
192+
define_const(LEDC_TIMER_MAX);
179193
define_const(LEDC_TIMER_0);
180194
define_const(LEDC_TIMER_1);
181195
define_const(LEDC_TIMER_2);
182196
define_const(LEDC_TIMER_3);
183-
define_const(LEDC_TIMER_MAX);
184197

185198
// LEDC timer resolutions.
199+
define_const(LEDC_TIMER_BIT_MAX);
186200
define_const(LEDC_TIMER_1_BIT);
187201
define_const(LEDC_TIMER_2_BIT);
188202
define_const(LEDC_TIMER_3_BIT);
@@ -197,13 +211,15 @@ mrb_mruby_esp32_ledc_gem_init(mrb_state* mrb)
197211
define_const(LEDC_TIMER_12_BIT);
198212
define_const(LEDC_TIMER_13_BIT);
199213
define_const(LEDC_TIMER_14_BIT);
200-
define_const(LEDC_TIMER_15_BIT);
201-
define_const(LEDC_TIMER_16_BIT);
202-
define_const(LEDC_TIMER_17_BIT);
203-
define_const(LEDC_TIMER_18_BIT);
204-
define_const(LEDC_TIMER_19_BIT);
205-
define_const(LEDC_TIMER_20_BIT);
206-
define_const(LEDC_TIMER_BIT_MAX);
214+
// 15-bit+ timers available on original ESP32, C6 and H2.
215+
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32H2)
216+
define_const(LEDC_TIMER_15_BIT);
217+
define_const(LEDC_TIMER_16_BIT);
218+
define_const(LEDC_TIMER_17_BIT);
219+
define_const(LEDC_TIMER_18_BIT);
220+
define_const(LEDC_TIMER_19_BIT);
221+
define_const(LEDC_TIMER_20_BIT);
222+
#endif
207223
}
208224

209225
void

0 commit comments

Comments
 (0)