Skip to content

Commit 93ee54a

Browse files
committed
Fix PWM status LED never_reset
It doesn't need never reset because the status LED is only active when user code isn't. This also fixes PWM never reset on espressif so that deinit will undo it. Fixes #6223
1 parent ef34378 commit 93ee54a

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

ports/espressif/common-hal/pwmio/PWMOut.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,20 @@ void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) {
166166

167167
void common_hal_pwmio_pwmout_reset_ok(pwmio_pwmout_obj_t *self) {
168168
never_reset_tim[self->tim_handle.timer_num] = false;
169-
never_reset_chan[self->chan_handle.channel] = false;
169+
// Search if any other channel is using the timer and is never reset.
170+
// Otherwise, we clear never_reset for the timer as well.
171+
bool other_never_reset = false;
172+
for (size_t i = 0; i < LEDC_CHANNEL_MAX; i++) {
173+
if (i != self->tim_handle.timer_num &&
174+
reserved_channels[i] == self->tim_handle.timer_num &&
175+
never_reset_chan[i]) {
176+
other_never_reset = true;
177+
break;
178+
}
179+
}
180+
if (!other_never_reset) {
181+
never_reset_chan[self->chan_handle.channel] = false;
182+
}
170183
}
171184

172185
bool common_hal_pwmio_pwmout_deinited(pwmio_pwmout_obj_t *self) {
@@ -182,11 +195,13 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) {
182195
ledc_stop(LEDC_LOW_SPEED_MODE, self->chan_handle.channel, 0);
183196
}
184197
reserved_channels[self->chan_handle.channel] = INDEX_EMPTY;
198+
never_reset_chan[self->chan_handle.channel] = false;
185199
// Search if any other channel is using the timer
186200
bool taken = false;
187201
for (size_t i = 0; i < LEDC_CHANNEL_MAX; i++) {
188202
if (reserved_channels[i] == self->tim_handle.timer_num) {
189203
taken = true;
204+
break;
190205
}
191206
}
192207
// Variable frequency means there's only one channel on the timer
@@ -195,6 +210,7 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) {
195210
reserved_timer_freq[self->tim_handle.timer_num] = 0;
196211
// if timer isn't varfreq this will be off aleady
197212
varfreq_timers[self->tim_handle.timer_num] = false;
213+
never_reset_tim[self->tim_handle.timer_num] = false;
198214
}
199215
common_hal_reset_pin(self->pin);
200216
self->deinited = true;

supervisor/shared/status_leds.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,15 @@ void status_led_init() {
179179

180180
#elif CIRCUITPY_PWM_RGB_LED
181181
if (common_hal_mcu_pin_is_free(CIRCUITPY_RGB_STATUS_R)) {
182-
pwmout_result_t red_result = common_hal_pwmio_pwmout_construct(&rgb_status_r, CIRCUITPY_RGB_STATUS_R, 0, 50000, false);
183-
184-
if (PWMOUT_OK == red_result) {
185-
common_hal_pwmio_pwmout_never_reset(&rgb_status_r);
186-
}
182+
common_hal_pwmio_pwmout_construct(&rgb_status_r, CIRCUITPY_RGB_STATUS_R, 0, 50000, false);
187183
}
188184

189185
if (common_hal_mcu_pin_is_free(CIRCUITPY_RGB_STATUS_G)) {
190-
pwmout_result_t green_result = common_hal_pwmio_pwmout_construct(&rgb_status_g, CIRCUITPY_RGB_STATUS_G, 0, 50000, false);
191-
192-
if (PWMOUT_OK == green_result) {
193-
common_hal_pwmio_pwmout_never_reset(&rgb_status_g);
194-
}
186+
common_hal_pwmio_pwmout_construct(&rgb_status_g, CIRCUITPY_RGB_STATUS_G, 0, 50000, false);
195187
}
196188

197189
if (common_hal_mcu_pin_is_free(CIRCUITPY_RGB_STATUS_B)) {
198-
pwmout_result_t blue_result = common_hal_pwmio_pwmout_construct(&rgb_status_b, CIRCUITPY_RGB_STATUS_B, 0, 50000, false);
199-
200-
if (PWMOUT_OK == blue_result) {
201-
common_hal_pwmio_pwmout_never_reset(&rgb_status_b);
202-
}
190+
common_hal_pwmio_pwmout_construct(&rgb_status_b, CIRCUITPY_RGB_STATUS_B, 0, 50000, false);
203191
}
204192

205193
#elif defined(MICROPY_HW_LED_STATUS)

0 commit comments

Comments
 (0)