Page 1 of 1

Can anyone explain this?

Posted: Fri Sep 04, 2015 10:34 am
by pythoncoder
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)

Re: Can anyone explain this?

Posted: Sun Sep 13, 2015 9:40 pm
by manitou
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 ...

Re: Can anyone explain this?

Posted: Sun Sep 13, 2015 11:14 pm
by dhylands
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.

Re: Can anyone explain this?

Posted: Mon Sep 14, 2015 6:29 am
by pythoncoder
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.

Re: Can anyone explain this?

Posted: Mon Sep 14, 2015 6:48 am
by dhylands
Doh - that's what I get for answering on my phone...

Re: Can anyone explain this?

Posted: Mon Sep 14, 2015 5:35 pm
by manitou
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