Skip to content

digitalio, pwmio, keypad, bitbangio: subclasses and arguments taken o… - #11381

Merged
tannewt merged 1 commit into
adafruit:mainfrom
peterbay:pin-apis-subclasses-and-arguments
Sep 15, 2026
Merged

tannewt merged 1 commit into
adafruit:mainfrom
peterbay:pin-apis-subclasses-and-arguments

Conversation

@peterbay

@peterbay peterbay commented Sep 14, 2026

Copy link
Copy Markdown

Code written by Claude Code, guided and corrected by @peterbay.

The problem

Five defects in the pin and bit-banged bus bindings: three arguments narrowed or compared before being checked, one accepted without being checked at all, and a bus error read as an acknowledgement.

The changes

  • drive_mode accepted anything. Anything that was not OPEN_DRAIN silently became PUSH_PULL, so switch_to_output(drive_mode=42) and pin.drive_mode = 42 configured a push-pull output rather than raising.

  • PWMOut narrowed duty_cycle before validating it. The constructor put it into a uint16_t and checked afterwards, so duty_cycle=65536 silently became 0 and -1 became 65535. The setter next door already validated the value first.

  • keypad's range checks admitted one past the last index. key_count, row_count and column_count were passed to mp_arg_validate_int_range as the maximum, and it is inclusive at both ends. On a two by three matrix, key_number_to_row_column(6) returned (2, 0) and row_column_to_key_number(2, 0) returned 6.

  • bitbangio did not range-check its rate. Neither the I2C frequency nor the SPI baudrate went through any validation, and both are divisors when the module works out its bit delay. The first version of this also added a maximum of 500000; that was wrong and only the lower bound remains.

  • bitbangio.I2C read a bus error as an acknowledgement. write_byte returns -1 for a bus error, 0 for a NAK and 1 for an ACK, and both the data loop in i2c_write and the address byte in i2c_read tested !write_byte(...), which is true only for the NAK.

Testing

Seeed XIAO nRF52840 Sense, on two builds differing only by these changes. Nothing is wired to the pins.

before after
switch_to_output(drive_mode=42) accepted, gives PUSH_PULL ValueError: Invalid drive_mode
pin.drive_mode = 42 accepted, gives PUSH_PULL ValueError: Invalid drive_mode
PWMOut(pin, duty_cycle=65536) accepted, reads back 0 ValueError: duty_cycle must be 0-65535
PWMOut(pin, duty_cycle=-1) accepted, reads back 65535 ValueError
pwm.duty_cycle = 65536 ValueError, unchanged — the setter always checked ValueError
key_number_to_row_column(6) on a 2x3 matrix returns (2, 0) ValueError: key_number must be 0-5
row_column_to_key_number(2, 0) with two rows returns 6 ValueError: row must be 0-1
SPI.configure(baudrate=0) accepted ValueError: baudrate must be >= 1
SPI.configure(baudrate=1000000) accepted, unchanged accepted
I2C(scl, sda, frequency=0) accepted ValueError: frequency must be >= 1

The bus-error row is not covered: it needs a bus fault to produce, which nothing here can arrange.

No new translatable strings.

@peterbay

Copy link
Copy Markdown
Author

Testing and diagnostic script.
pin_apis_subclasses_and_arguments.py

@tannewt

tannewt commented Sep 14, 2026

Copy link
Copy Markdown
Member
  • Subclassing DigitalInOut in Python and using it worked on the wrong object. Every binding in the file cast self_in straight to digitalio_digitalinout_obj_t *, so for an instance of a Python subclass the pin pointer was read out of the instance's member map. It is reached through mp_obj_cast_to_native_base now, in all twenty bindings — the two that switch_to_output and switch_to_input use were the last that still cast directly.

I don't believe anyone does this. Sometimes we allow it but it is spotty. I wouldn't add it here because it bloats the code size on the samd1.

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No digitalinout subclass support is needed. Folks shouldn't do this.

digitalio's drive_mode took anything that was not OPEN_DRAIN to mean PUSH_PULL,
so switch_to_output(drive_mode=42) configured a push-pull output instead of
raising.

pwmio.PWMOut put duty_cycle into a uint16_t and validated it afterwards, so
65536 became 0 and -1 became 65535. The setter next to it already checked first.

keypad and keypad_demux passed their counts to mp_arg_validate_int_range as the
maximum, which is inclusive, so one index past the last was accepted: on a two
by three matrix key_number_to_row_column(6) returned (2, 0).

bitbangio range-checked neither the I2C frequency nor the SPI baudrate, and both
are divisors when the bit delay is worked out.

bitbangio.I2C read a bus error as an acknowledgement: write_byte returns -1 for
a bus error, 0 for a NAK and 1 for an ACK, and the callers tested !write_byte(),
which is true only for the NAK.
@peterbay
peterbay force-pushed the pin-apis-subclasses-and-arguments branch from 63d84f7 to d48973e Compare September 15, 2026 04:30
@peterbay

Copy link
Copy Markdown
Author

Dropped it.

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@tannewt
tannewt added this pull request to the merge queue Sep 15, 2026
@tannewt
tannewt removed this pull request from the merge queue due to the queue being cleared Sep 15, 2026
@tannewt
tannewt merged commit 158e09f into adafruit:main Sep 15, 2026
688 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants