Skip to content

Commit f54c7ad

Browse files
committed
Must treat NO_PIN cast to uint8_t the same as NO_PIN
Otherwise, deinitializing a camera with e.g., the powerdown pin unspecified results in an assertion failure in a debug build.
1 parent 1d1e139 commit f54c7ad

File tree

1 file changed

+9
-3
lines changed
  • ports/espressif/common-hal/microcontroller

1 file changed

+9
-3
lines changed

ports/espressif/common-hal/microcontroller/Pin.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ static const uint64_t pin_mask_reset_forbidden =
100100

101101

102102
void never_reset_pin_number(gpio_num_t pin_number) {
103-
if (pin_number == NO_PIN) {
103+
// Some CircuitPython APIs deal in uint8_t pin numbers, but NO_PIN is -1.
104+
// Also allow pin 255 to be treated as NO_PIN to avoid crashes
105+
if (pin_number == NO_PIN || pin_number == (uint8_t)NO_PIN) {
104106
return;
105107
}
106108
never_reset_pins |= PIN_BIT(pin_number);
@@ -141,7 +143,9 @@ STATIC void _reset_pin(gpio_num_t pin_number) {
141143

142144
// Mark pin as free and return it to a quiescent state.
143145
void reset_pin_number(gpio_num_t pin_number) {
144-
if (pin_number == NO_PIN) {
146+
// Some CircuitPython APIs deal in uint8_t pin numbers, but NO_PIN is -1.
147+
// Also allow pin 255 to be treated as NO_PIN to avoid crashes
148+
if (pin_number == NO_PIN || pin_number == (uint8_t)NO_PIN) {
145149
return;
146150
}
147151
never_reset_pins &= ~PIN_BIT(pin_number);
@@ -174,7 +178,9 @@ void reset_all_pins(void) {
174178
}
175179

176180
void claim_pin_number(gpio_num_t pin_number) {
177-
if (pin_number == NO_PIN) {
181+
// Some CircuitPython APIs deal in uint8_t pin numbers, but NO_PIN is -1.
182+
// Also allow pin 255 to be treated as NO_PIN to avoid crashes
183+
if (pin_number == NO_PIN || pin_number == (uint8_t)NO_PIN) {
178184
return;
179185
}
180186
in_use |= PIN_BIT(pin_number);

0 commit comments

Comments
 (0)