How can RTC.wakeup() change global variables?

Hi guys

There seems to be some bugs concerning the RTC.wakeup() code.

The simple example here works for me: Use of callback function in sleep mode - OpenMV Products - OpenMV Forums

But when I initially tried to get the callback routine to set a value that I could access outside the callback routine, it behaved as though this is not possible, as shown in the code below: I would expect “triggered” would be set True by the callback at line 32 so the red LED would toggle. However the green LED toggles.

Then I discovered that if I specify the callback with a dummy argument it works as expected - see the two alternatives at lines 40 and 41.

But if I call rtc.wakeup() once, outside the “while (True)” loop (at line 37 instead of line 41), the code behaves as though “triggered” is set True the first time the callback is called but not subsequently.

There is further confusion regarding reading the values of globals: the callback reads the value of the global “choose_blue” with both of the line 40 and 41 invocations, as can be seen if you set that True at line 9. But if the invocation is done just once, then the invocation without the dummy variable at line 36 works, but the invation with the dummy variable at line 37 shows that “choose_blue” is read the first time the callback is called but not subsequently.

This looks like a bug rather than a feature… how can I use globals with the callback reliably?
(I am using an H7 with firmware 3.9.6 and IDE 2.6.7)

import pyb, machine, time

global triggered
global choose_blue

triggered = False

choose_blue = False
#choose_blue = True

red_led = pyb.LED(1)
red_led.off()
green_led = pyb.LED(2)
green_led.off()
blue_led = pyb.LED(3)
blue_led.off()

def toggleRed():
    red_led.toggle()

def toggleGreen():
    green_led.toggle()

def toggleBlue():
    blue_led.toggle()

def test(arg):
    global triggered
    global choose_blue
    if(choose_blue):
        toggleBlue()
    triggered = True

rtc = pyb.RTC()

#rtc.wakeup(1000, test)
#rtc.wakeup(1000, test(1))

while (True):
    rtc.wakeup(1000, test)
    #rtc.wakeup(1000, test(1))
    machine.sleep()

    if (triggered == True):
        toggleRed()
    else:
        toggleGreen()

    triggered = False

Hi, there’s no 3.9.6 firmware maybe you mean 3.6.9, in that case you should update to 3.8.0 the latest and then test again, if the issue is not fixed please report a bug on github with the smallest code that shows the issue.

The results about were using an H7 with firmware 3.6.9 and IDE 2.6.7
I updated to IDE 2.6.8 and firmware 3.8.0 - same results.
As requested I will file a bug report on github.

A github issue was raised here: RTC.wakeup() shows unreliable accessing of global variables · Issue #1032 · openmv/openmv · GitHub

A explanation and a resolution are documented there.