Can anyone explain this?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Can anyone explain this?

Post by pythoncoder » Fri Sep 04, 2015 10:34 am

The behaviour of the following script varies depending on the delay in the last but one line. With a delay of 10mS or above it runs as expected with the LED coming on for one second, then going off for five seconds. With a 1mS delay the LED stays on for 55 secs and goes off for 5. With the delay commented out the LED stays on continuously but the current consumption of the board spikes to 22mA every 6 seconds indicating that the RTC low power delay code is running as expected.

Code: Select all

import pyb
rtc = pyb.RTC()
rtc.datetime((2015, 8, 6, 4, 13, 0, 0, 0)) # ensure it's initialised (?)
usb_connected = pyb.Pin.board.USB_VBUS.value() == 1

def lpdelay(ms):
    rtc.wakeup(ms)
    pyb.stop()
    rtc.wakeup(None)

if not usb_connected:
    led = pyb.LED(4)
    while True:
        led.on()
        pyb.delay(1000) # use pyb version to keep weirdness in one place
        led.off()
#        pyb.delay(1)
        lpdelay(5000)
Peter Hinch
Index to my micropython libraries.

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: Can anyone explain this?

Post by manitou » Sun Sep 13, 2015 9:40 pm

Have you discovered anything?

I know I should try this myself ... but what happens if you use udelay() instead of delay()? delay() uses sleep mode (WFI) to pass the time, udelay() is a spin-wait ...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Can anyone explain this?

Post by dhylands » Sun Sep 13, 2015 11:14 pm

WFI stands for wait for interrupt.

The way the systick timer is setup it generates an interrupt every millisecond. Since delay is waiting for msecs we mightas well wait for an interrupt since the tick.counter won't get incremented until the next systick interrupt.

When waiting for a fed microseconds there is assurance that any interrupt will occur in the next few microseconds, so we can't call WFI. If the wait were longer than 1000 microseconds then we could probably put a WFI call in.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Can anyone explain this?

Post by pythoncoder » Mon Sep 14, 2015 6:29 am

This was resolved here https://github.com/micropython/micropython/issues/1455
@Damien:
LED(4) is PWM so the timer might not be getting a chance to clock over and turn the PWM off before you go to sleep (pyb.stop() will stop the timer counting). Try LED 1,2,3.
It resulted from my arbitrary choice of LED 4 for testing and it works as expected with other LED's.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Can anyone explain this?

Post by dhylands » Mon Sep 14, 2015 6:48 am

Doh - that's what I get for answering on my phone...

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: Can anyone explain this?

Post by manitou » Mon Sep 14, 2015 5:35 pm

pythoncoder wrote:It resulted from my arbitrary choice of LED 4 for testing and it works as expected with other LED's.
Ahhhh, interesting. LED4's PWM period is 10ms. QED

Post Reply