@@ -30,7 +30,6 @@ mrb_esp32_ledc_timer_config(mrb_state *mrb, mrb_value self) {
30
30
31
31
static mrb_value
32
32
mrb_esp32_ledc_channel_config (mrb_state * mrb , mrb_value self ) {
33
- // Only set pin and channel for now. Can add more config later.
34
33
mrb_value pin , group , timer , ch ;
35
34
36
35
mrb_get_args (mrb , "oooo" , & pin , & group , & timer , & ch );
@@ -129,7 +128,7 @@ mrb_esp32_ledc_unset_pin(mrb_state *mrb, mrb_value self) {
129
128
return mrb_nil_value ();
130
129
}
131
130
132
- // PIN_FUNC_GPIO = 2. Normal GPIO.
131
+ // PIN_FUNC_GPIO is integer 2, which returns the pin to normal GPIO mode .
133
132
// Last param is output inversion.
134
133
gpio_iomux_out (mrb_fixnum (pin ), PIN_FUNC_GPIO , false);
135
134
@@ -141,8 +140,10 @@ mrb_mruby_esp32_ledc_gem_init(mrb_state* mrb)
141
140
{
142
141
struct RClass * esp32 , * ledc , * constants ;
143
142
143
+ // ESP32 mruby module
144
144
esp32 = mrb_define_module (mrb , "ESP32" );
145
145
146
+ // ESP32::LEDC
146
147
ledc = mrb_define_module_under (mrb , esp32 , "LEDC" );
147
148
mrb_define_module_function (mrb , ledc , "timer_config" , mrb_esp32_ledc_timer_config , MRB_ARGS_REQ (4 ));
148
149
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)
152
153
mrb_define_module_function (mrb , ledc , "set_pin" , mrb_esp32_ledc_set_pin , MRB_ARGS_REQ (3 ));
153
154
mrb_define_module_function (mrb , ledc , "unset_pin" , mrb_esp32_ledc_unset_pin , MRB_ARGS_REQ (1 ));
154
155
156
+ // ESP32::Constants
155
157
constants = mrb_define_module_under (mrb , esp32 , "Constants" );
156
158
157
- #define define_const (SYM ) \
159
+ // Pass a C constant through to mruby, defined inside ESP32::Constants.
160
+ #define define_const (SYM ) \
158
161
do { \
159
162
mrb_define_const(mrb, constants, #SYM, mrb_fixnum_value(SYM)); \
160
163
} while (0)
161
164
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
166
174
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 );
168
179
define_const (LEDC_CHANNEL_0 );
169
180
define_const (LEDC_CHANNEL_1 );
170
181
define_const (LEDC_CHANNEL_2 );
171
182
define_const (LEDC_CHANNEL_3 );
172
183
define_const (LEDC_CHANNEL_4 );
173
184
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 );
179
193
define_const (LEDC_TIMER_0 );
180
194
define_const (LEDC_TIMER_1 );
181
195
define_const (LEDC_TIMER_2 );
182
196
define_const (LEDC_TIMER_3 );
183
- define_const (LEDC_TIMER_MAX );
184
197
185
198
// LEDC timer resolutions.
199
+ define_const (LEDC_TIMER_BIT_MAX );
186
200
define_const (LEDC_TIMER_1_BIT );
187
201
define_const (LEDC_TIMER_2_BIT );
188
202
define_const (LEDC_TIMER_3_BIT );
@@ -197,13 +211,15 @@ mrb_mruby_esp32_ledc_gem_init(mrb_state* mrb)
197
211
define_const (LEDC_TIMER_12_BIT );
198
212
define_const (LEDC_TIMER_13_BIT );
199
213
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
207
223
}
208
224
209
225
void
0 commit comments