← index #6814Issue #10226
Related · high · value 1.242
QUERY · ISSUE

SPI2 on STM32WB55 pin error

openby H-Grobbenopened 2021-01-29updated 2025-10-06
port-stm32needs-info

Trying to use pin D3 as SPI2_MISO alternate function does not work, the pin D3 switches to the previous alternate funtion (SPI2_SCK).

This can be fixed by removing the AF3 declaration out of the stm32wb55_af.csv list:
PortD,PD3,,,,SPI2_SCK,,SPI2_MISO,,,,,QUADSPI_BK1_NCS,,,,,EVENTOUT, (Original)
PortD,PD3,,,,,,SPI2_MISO,,,,,QUADSPI_BK1_NCS,,,,,EVENTOUT, (fix)

But this causes the Pin D3 incapable of switching to SCK mode.

This is caused by the definition of a pin list in spi.c (line 294), not a function list. In the pin list, the actual output pin is defined, but the information what the pin should be doing is not remembered.
A sollution could be the sequnce of the list, it can be defined to hold the function. The function mp_hal_pin_config_alt then should be told what the pin function should be.

CANDIDATE · ISSUE

RP2: 1us glitch on SCK pin when switching from SPI function to Pin.OUT

closedby pdg137opened 2022-12-15updated 2023-01-12
bugport-rp2

On the RP2040, several sets of pins can be selected for each of the two SPI modules. Sometimes we need to disable the SPI function of a pin, while leaving it in the state it was in. For example the following code should leave pin 14 (SCK) low between write() and sck(1):

from machine import SPI, Pin

sck = Pin(14)
mosi = Pin(15)
spi = SPI(id=1, baudrate=100000, polarity=0, phase=0, sck=sck, mosi=mosi)
spi.write(bytearray([0xAA]))
sck.init(mode=Pin.OUT, value=0)
sck(1)

However, the first time it runs after reset, there's a ~1 microsecond glitch during which pin 14 floats upward. The glitch causes unreliable behavior with SPI devices if you don't take measures to avoid it (the workaround is to run sck.init(mode=Pin.OUT, value=0) at least once before using SPI.)

I tested this on the latest master branch, MicroPython v1.19.1-716-g227ea47f4, with a Raspberry Pi Pico having a 1k pull-up on pin 14. Here is a trace of the output:

<img src="https://user-images.githubusercontent.com/466760/207778158-b1f8e8e6-0f4a-4a2b-8287-acf0e205ab19.png" width=500>

We can zoom in on it:
<img src="https://user-images.githubusercontent.com/466760/207778540-79950859-ed3a-4106-ad30-06a1f04f2e57.png" width=500>

I believe the source of the problem is here:

https://github.com/micropython/micropython/blob/master/ports/rp2/mphalport.h#L99-L103

static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
    gpio_set_function(pin, GPIO_FUNC_SIO);
    gpio_set_dir(pin, GPIO_OUT);
    machine_pin_open_drain_mask &= ~(1 << pin);
}

The SPI alternate function is disabled before the pin is set to an output.

I don't quite understand what's going on here, but I believe that switching the order of these operations would let it work as intended. Is there a reason to do them in this order? Or is there some other problem?

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied