8000 GitHub · Where software is built
[go: up one dir, main page]

Skip to content
with statement in loop takes cyclically increasing time #2602
Closed
@dhalbert

Description

@dhalbert
import board, digitalio
class WithTest:
    def __init__(self):
        pass
    def __enter__(self):
        pass
    def __exit__(self, *exc):
        pass

d13 = digitalio.DigitalInOut(board.D13)
d13.switch_to_output()

# CYCLICALLY INCREASING INTERVALS
wt = WithTest()
while True:
    with wt as wtx:
        d13.value = not d13.value

#OK: CONSTANT INTERVALS
while True:
    with WithTest() as wt:
        d13.value = not d13.value

Putting a scope on D13 will show a square wave whose period gets longer and longer and then snaps back and starts over. On an M0 the period is increasing from very roughly 0.5 msec to 5 msec (eyeballing it on the scope).

The upper loop shows the problem. The lower loop (comment out the upper one) shows constant intervals.

This was originally discovered by @ladyada when monitoring an I2C transaction that used adafruit_bus_device.I2CDevice. But it has nothing to do with I2C.

This is easiest to see on an oscilloscope:
https://youtu.be/tGB2uVwig_4 (unlisted; use the link)

Screen shots from Saleae:
first loop (taken at gap: starts fast after a gap and slows down):
Selection_129

second loop (constant before and after gap):
Selection_130

Also filed as micropython#5622.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0