Skip to content

Fix pad assignments on atmel-samd UART #7616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2023

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Feb 20, 2023

  • Fixes UART write does not work on QT Py SAMD21 with CircuitPython 8.x #7612.

  • SERCOM USART TXPO computation was not correct on SAMD21 for TX pins using pad 2, due to changes introduced in Implement hardware flow control on SAMD busio.UART #6434. SAMD21 and SAMx5x are different: SAMx5x does not allow pad 2 for TX. In addition, checking for allowed pins based on their pads was not as rigorous as it should have been. Code is somewhat restructured.

  • Signature for busio.UART() was missing several arguments and did not account for None values.

  • Did more pin validation in shared-bindings busio.UART(). Ensure pins are distinct, and that both TX and RX are not None. Removed now-redundant checks for TX and RX both being none in ports/*/common-hal/busio/UART.c.

Tested that original bug (#7612) is now fixed. Did not test that flow control is working. Did verify that TXPO and RXPO are correct for a variety of pin choices, with and without RTS/CTS. It's not clear whether TXPO 0x2 is OK on SAMD51 if RTS and CTS are not used, but did the safe thing.

Also tested on a Metro M4, confirming that the standard TX and RX still work in both directions.

@stonehippo You may want to re-test with these changes.

@dhalbert dhalbert force-pushed the 8.0.x-fix-atmel-uart branch from 2a1d19a to 6d51356 Compare February 20, 2023 01:42
@dhalbert dhalbert linked an issue Feb 20, 2023 that may be closed by this pull request
@stonehippo
Copy link

Thanks @dhalbert. I'll try this out out SAMD21 and SAM5x boards I have which expose the right pins for flow control.

@dhalbert
Copy link
Collaborator Author

dhalbert commented Feb 20, 2023

Test program I used to find working combinations, a variation on https://learn.adafruit.com/circuitpython-essentials/circuitpython-uart-serial#wheres-my-uart-2985036. Generates thousands of combinations, and takes a while to run. Includes cases where pins are None. I checked a few of these against the pin mux tables in the datasheets.

import board
import busio
from microcontroller import Pin

def is_hardware_uart(tx, rx, rts, cts):
    try:
        p = busio.UART(tx, rx, rts=rts, cts=cts)
        p.deinit()
        return True
    except ValueError:
        return False

def get_unique_pins():
    exclude = ['NEOPIXEL', 'APA102_MOSI', 'APA102_SCK']
    pins = [pin for pin in [
        getattr(board, p) for p in dir(board) if p not in exclude]
            if isinstance(pin, Pin)]
    unique = []
    for p in pins:
        if p not in unique:
            unique.append(p)
    return [None] + unique

for tx_pin in get_unique_pins():
    for rx_pin in get_unique_pins():
        if is_hardware_uart(tx_pin, rx_pin, None, None):
            print(f"TX: {tx_pin}\tRX: {rx_pin}")
            for rts_pin in get_unique_pins():
                for cts_pin in get_unique_pins():
                    if is_hardware_uart(tx_pin, rx_pin, rts_pin, cts_pin):
                        print(f"\tRTS: {rts_pin}\tCTS: {cts_pin}")

@dhalbert dhalbert force-pushed the 8.0.x-fix-atmel-uart branch from a3e7fab to 2684aeb Compare February 21, 2023 00:11
@dhalbert dhalbert added this to the 8.0.x milestone Feb 21, 2023
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you for the quick fix

@tannewt
Copy link
Member

tannewt commented Feb 21, 2023

Will wait for @stonehippo to test before merging.

@dhalbert
Copy link
Collaborator Author

Will merge for now since this is an 8.0.0 regression on a number of boards, breaking UART. @stonehippo we are still interested in your test results, of course!

@dhalbert dhalbert merged commit 460dda0 into adafruit:8.0.x Feb 22, 2023
@dhalbert dhalbert deleted the 8.0.x-fix-atmel-uart branch February 22, 2023 22:25
@stonehippo
Copy link

@dhalbert I finally got around to testing this out and RTS/CTS flow control seems to work as expected on both SAMD21 and SAMD51 boards where I could get at the correct pins for hardware flow control on a given board (e.g. Feather M0 Adalogger, MicroMod SAMD51).

@dhalbert
Copy link
Collaborator Author

dhalbert commented Apr 8, 2023

@stonehippo glad to hear it's working for you

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.

UART write does not work on QT Py SAMD21 with CircuitPython 8.x
3 participants