rtc.wakeup() behaves differently depending on sleep mode

This topic is a further example of this bug https://forums.openmv.io/viewtopic.php?f=6&t=2200 but separated to reduce clutter.

I find the RTC.wakeup() clock works differently depending on whether I Invoke machine.sleep(). To get correct behaviour I must call either:
rtc.wakeup(timerInterval, timerISR) if not sleeping or rtc.wakeup(timerInterval, timerISR(1)) if sleeping - i.e. with or without the dummy argument.

The code below should wake the processor every 4s, and I can specify whether or not to sleep by one of two options at lines 10 or 11.

With sleep disabled it works correctly if I call rtc.wakeup(timerInterval, timerISR). But if I call rtc.wakeup(timerInterval, timerISR(1)) then the callback appears to happen immediately (rather than after 4s) and then never again.

With sleep enabled it works correctly only if I call rtc.wakeup(timerInterval, timerISR(1)) at both lines 44 and 59. Other combinations give errors, such as the “timerTriggered” flag not being set. If I enable rtc.wakeup(timerInterval, timerISR) at line 44 and leave lines 59 and 60 commented out, the 4s interrupt happnes but “timerTriggered” flag is set every second time!

import pyb, machine, time, utime

global timerTriggered

timerTriggered = False

timerInterval = 4000

# Choose one or other:
enableSleep = True
#enableSleep = False

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

def flashGreen():
    green_led.on()
    utime.sleep_ms(200)
    green_led.off()
    utime.sleep_ms(200)

def flashBlue():
    blue_led.on()
    utime.sleep_ms(200)
    blue_led.off()
    utime.sleep_ms(200)

def timerISR(arg):
    global timerTriggered
    timerTriggered = True

if enableSleep:
    print("Testing wakeup ISR (while sleeping)")
else:
    print("Testing wakeup ISR (while NOT sleeping)")

rtc = pyb.RTC()

utime.sleep_ms(200)    # delay to ensure the print output happens before we sleep

# Experimentally, I find I have to use this when machine.sleep() is enabled:
#rtc.wakeup(timerInterval, timerISR(1))
# and I have to use this when machine.sleep() is not enabled
rtc.wakeup(timerInterval, timerISR)

count = 0

while (True):

    if enableSleep:
        # We enable sleep and wait for a timerinterrupt
        machine.sleep()

        if timerTriggered:
            flashBlue()
            timerTriggered = False
            #rtc.wakeup(timerInterval, timerISR(1))
            #rtc.wakeup(timerInterval, timerISR)
        else:
            flashGreen()
    else:
        # We don't enable sleep, so we should loop through here with no LED activity
        # UNTIL the timer interrupt sets a flag
        if timerTriggered:
            count = count + 1
            print("Timer " + str(count))
            flashBlue()
            timerTriggered = False
        utime.sleep_ms(100) # so we don't run flat out....

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.