Skip to content

stm: correct alternate functions in six pin tables - #11902

Merged
tannewt merged 1 commit into
adafruit:mainfrom
vegardw:stm-pin-table-alternate-functions
Nov 19, 2026
Merged

tannewt merged 1 commit into
adafruit:mainfrom
vegardw:stm-pin-table-alternate-functions

Conversation

@vegardw

@vegardw vegardw commented Nov 18, 2026 •

Copy link
Copy Markdown

Corrects 33 entries in six STM32 pin tables whose alternate function (AF) doesn't connect the signal to the pin, according to ST's pin data.

Problem

PWMOut and busio use the first usable table entry for a pin and switch the pin to that entry's AF. These entries have the wrong AF:

Table Entries AF: table → ST The table's AF selects Effect
F401, F411, F412Zx, F446 TIM10_CH1 on PB8, TIM11_CH1 on PB9 2 → 3 TIM4_CH3, TIM4_CH4 When TIM4 is busy at another frequency, PWMOut on PB8/PB9 gets TIM10/TIM11 and has no output
F401 SPI4_NSS on PB12 6 → none I2S2ext_WS None yet (the NSS list is unused); SPI4_NSS is on PB12 on the F411, not the F401
L433 USART2_RX on PA15 7 → 3 USART3_RTS/DE A UART with RX on PA15 receives nothing
L433 TIM15 on PA2, PA3, PB14, PB15; TIM16 on PA6, PB8 15 → 14 EVENTOUT PWMOut has no output with these timers. PA6, PB8, PB14 and PB15 have no other timer: D5, D9 and D12 on the Blues Cygnet never give PWM
L4R5 UART4 on PA0, PA1, PC10, PC11; UART5 on PC12, PD2 9 → 8 nothing, or TSC UART4 and UART5 don't work
L4R5 TIM15 on PA2, PA3, PB14, PB15, PF9, PF10, PG10, PG11; TIM16 on PA6, PB8, PE0 15 → 14 EVENTOUT As on the L433; MISO and MOSI on the Swan R5 never give PWM

Fix

Use ST's AF for each entry, and remove SPI4_NSS on PB12 from the F401 table. The values come from the GPIO files (mcu/IP/GPIO-*_Modes.xml) in ST's STM32_open_pin_data, the data CubeMX uses. The other tables agree: F405, F407 and F412Cx already have AF3 for TIM10/TIM11, and the L4R5 table has AF3 for USART2_RX on PA15.

Testing

Check Base This PR
The 1,688 I2C, SPI, UART and timer entries in the 13 F4, F7, H7 and L4 tables vs ST's pin data 33 differ, in 6 tables all match
Meowbit (en_US, ru), Espruino Pico, F411 BlackPill, F412ZG Discovery, Nucleo F446RE, Blues Cygnet, Swan R5 build build, same size; only the corrected AF bytes differ
STM32F412 die, TIM11 on PB9 with TIM4 busy the pin stays dark the pin follows the PWM

The table check compared each entry's AF with the one ST lists for that signal on that pin; I can share the script. The hardware check used the ELECFREAKS Retro Arcade (STM32F412RE; MCU support in #11896, the board isn't upstream yet), whose display backlight is on PB9, with the same AF2 → AF3 change in its tables; the script ran over USB and I watched the backlight. Not tested on hardware: the L4 changes, and boards with the F401, F411, F412Zx and F446 tables; I don't have them. Could someone with a Swan R5 or a Blues Cygnet run the test below?

How to run the test

Set the pins at the top of the script for your board (the comments list them), connect the jumper, copy the script as code.py and watch the serial console. Expect PASS with about 20 edges, and on the Swan R5 PASS for the UART. Without this PR: FAIL with 0 edges, and no data on UART4/UART5.

pin_table_test.py
# Copy to CIRCUITPY as code.py and watch the serial console. Checks pin-table entries whose
# alternate function was wrong, on STM32F401, F411, F412, F446, L433 or L4R5 boards.
# Set the pins below for your board first.
#
# PWM: jumper PWM_PIN to SENSE_PIN (any free pin). A 10 Hz PWM gives about 20 edges a
# second on SENSE_PIN. With a wrong alternate function the timer doesn't reach the pin: 0.
#   F401, F411, F412, F446: PWM_PIN = microcontroller.pin.PB08 (TIM10) or PB09 (TIM11),
#     HOLD_PIN = microcontroller.pin.PB06. The hold PWM takes TIM4, the first timer
#     PB08/PB09 list, so PWM_PIN gets TIM10 or TIM11.
#   Blues Cygnet (L433): PWM_PIN = board.D5 (TIM16), board.D9 or board.D12 (TIM15).
#   Swan R5 (L4R5): PWM_PIN = board.MISO or board.MOSI (TIM15).
# UART, Swan R5 only: jumper UART_TX = board.TX (USART1) to UART_RX = board.A1 (UART4) or
#   board.RTS3 (UART5). With a wrong alternate function UART_RX receives nothing.
import time

import board  # noqa: F401 (for the pins above)
import busio
import digitalio
import microcontroller  # noqa: F401 (for the pins above)
import pwmio

PWM_PIN = None
SENSE_PIN = None
HOLD_PIN = None
UART_TX = None
UART_RX = None


def check(name, ok):
    print("PASS" if ok else "FAIL", name)


def count_edges(pin, seconds):
    sense = digitalio.DigitalInOut(pin)
    sense.pull = digitalio.Pull.DOWN
    edges = 0
    last = sense.value
    end = time.monotonic() + seconds
    while time.monotonic() < end:
        value = sense.value
        if value != last:
            edges += 1
            last = value
    sense.deinit()
    return edges


if PWM_PIN is None and UART_TX is None:
    print("Set PWM_PIN and SENSE_PIN, or UART_TX and UART_RX, at the top of the script.")

if PWM_PIN is not None:
    hold = pwmio.PWMOut(HOLD_PIN, frequency=1000, duty_cycle=32768) if HOLD_PIN else None
    pwm = pwmio.PWMOut(PWM_PIN, frequency=10, duty_cycle=32768)
    edges = count_edges(SENSE_PIN, 1)
    check(f"PWM on {PWM_PIN}: {edges} edges in 1 s (expect about 20)", 15 <= edges <= 25)
    pwm.deinit()
    if hold:
        hold.deinit()

if UART_TX is not None:
    sender = busio.UART(UART_TX, None, baudrate=9600)
    receiver = busio.UART(None, UART_RX, baudrate=9600, timeout=0.5)
    receiver.reset_input_buffer()
    sender.write(b"pin table")
    data = receiver.read(9)
    check(f"UART receive on {UART_RX}: {data!r}", data == b"pin table")
    receiver.deinit()
    sender.deinit()

print("done")

Trade-offs and Alternatives

None: the change only corrects table data.

Generative AI

I used generative AI (Claude Code) to find and test this change. I have reviewed it, and I am responsible for the code and this description.

🤖 Generated with Claude Code

Checking every I2C, SPI, UART and timer entry in the STM pin tables
against ST's pin data (STM32_open_pin_data) found 33 entries in six
tables whose alternate function doesn't connect the signal to the pin:

- F401, F411, F412Zx, F446: TIM10_CH1 on PB8 and TIM11_CH1 on PB9 are
  AF3, not AF2, which selects TIM4.
- F401: SPI4_NSS isn't available on PB12 (it is on the F411); removed.
- L433: USART2_RX on PA15 is AF3, not AF7.
- L433, L4R5: TIM15 and TIM16 are AF14, not AF15 (EVENTOUT).
- L4R5: UART4 and UART5 are AF8, not AF9.

PWMOut on a pin that got one of these timers had no output, and UART4
and UART5 on the L4R5 didn't work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dhalbert

Copy link
Copy Markdown
Collaborator

Could someone with a Swan R5 or a Blues Cygnet run the test below?

@bablokb would you be interested in testing?

@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 merged commit d6be0e7 into adafruit:main Nov 19, 2026
39 checks passed
@vegardw
vegardw deleted the stm-pin-table-alternate-functions branch November 19, 2026 06:29
vegardw added a commit to vegardw/circuitpython that referenced this pull request Nov 19, 2026
Brings in the FSMC parallel display bus (adafruit#11908) and the pin-table
fixes (adafruit#11902), now merged upstream. The display-bus code was already
the same here, so the merge had no conflicts. The tree is now main
plus the board commit, the board README and Stage at the commit that
adds this board.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants