Thanks to visit codestin.com
Credit goes to github.com

Skip to content

rp2: Change order of pin operations to prevent glitches. #10337

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 1 commit into from
Jan 12, 2023

Conversation

pdg137
Copy link
Contributor

@pdg137 pdg137 commented Dec 27, 2022

Fixes #10226. When switching from a special function like SPI to an input or output, there was a brief period after the function was disabled but before the pin's I/O state was configured, in which the state would be poorly defined. This fixes the problem by switching off the special function after fully configuring the I/O state.

Test code:

from machine import SPI, Pin

sck = Pin(14)
mosi = Pin(15)

sck.init(mode=Pin.IN)
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)

Before/after this change (with 1k pull-up):

from machine import SPI, Pin

sck = Pin(14)
mosi = Pin(15)

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

sck.init(mode=Pin.IN, pull=Pin.PULL_DOWN)
sck.init(mode=Pin.OUT, value=1)

Before/after this change (with no pull-up):

from machine import SPI, Pin

sck = Pin(14)
mosi = Pin(15)

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

sck.init(mode=Pin.OPEN_DRAIN, value=0)

Before/after this change (with 10k pull-up):

As I noted in the issue, the last one seems weird since init apparently disobeys its value argument in open-drain mode.

@dpgeorge
Copy link
Member

As I noted in the issue, the last one seems weird since init apparently disobeys its value argument in open-drain mode.

Yes that looks wrong. We can fix that in a separate PR.

When switching from a special function like SPI to an input or output,
there was a brief period after the function was disabled but before the
pin's I/O state was configured, in which the state would be poorly defined.
This fixes the problem by switching off the special function after fully
configuring the I/O state.

Fixes micropython#10226.

Signed-off-by: Paul Grayson <[email protected]>
@dpgeorge dpgeorge merged commit b208cf2 into micropython:master Jan 12, 2023
@dpgeorge
Copy link
Member

I rebased this on master and force pushed to your branch (so it checks CI, and so github can show it as merged), then merged.

tannewt added a commit to tannewt/circuitpython that referenced this pull request May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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