Page 1 of 1

RTC callback

Posted: Wed Jul 01, 2020 5:54 am
by hlovatt
Hi,

Trying to work out what the callback signature for the RTC is.

The documentation says:
If ``callback`` is given then it is executed at every trigger of the wakeup timer. ``callback`` must take exactly one argument.
However:

Code: Select all

>>> def callback(line):
...     print('Callback')
...     print('line = ', line, ' type = ', type(line))
...
>>> rtc.wakeup(2000, callback)
Gives:

Code: Select all

>>> Callback
Traceback (most recent call last):
  File "<stdin>", line 3, in callback
NameError: local variable referenced before assignment
Callback
Traceback (most recent call last):
  File "<stdin>", line 3, in callback
NameError: local variable referenced before assignment
...
Does the callback really take an argument?

Re: RTC callback

Posted: Wed Jul 01, 2020 6:00 am
by hlovatt
More information, it really does take an argument. However you can't do anything with the argument!

Therefore is this an RTC bug?

Re: RTC callback

Posted: Wed Jul 01, 2020 5:27 pm
by dhylands
On the STM32, the callback is actually an ExtInt callback and the argument is the line that caused the interrupt.

For the RTC on the STM32F405, there are 3 different reasons (i.e. values of line) the RTC interrupt can be called.

17 = RTC Alarm
21 = RTC Tamper/RTC Timestamp
22 = RTC Wakeup

In theory, you could setup handlers for the other sources and have them all go to the same routine. The line parameter would then allow it to distinguish the reason for the interrupt. If you have a different handler for each interrupt type then the line parameter is redundant and should just be ignored.

Re: RTC callback

Posted: Wed Jul 01, 2020 8:31 pm
by hlovatt
There must be a bug though. Because you have to give a callback with an argument, but the argument is uninitialised.

Re: RTC callback

Posted: Wed Jul 01, 2020 11:20 pm
by dhylands
I think you're right.

It looks like adding this line:

Code: Select all

pyb_extint_callback_arg[i] = MP_OBJ_NEW_SMALL_INT(i);
into the for loop in the extint_init0 function would fix things.
https://github.com/micropython/micropyt ... int.c#L635

Re: RTC callback

Posted: Thu Jul 02, 2020 12:43 am
by hlovatt
Bear in mind that I have no idea how MP works!

But wouldn't the above fix return an int, not an RTC or Timer object?

Note documentation for Timer says the callbacks get the timer itself:
Timer.callback(fun)

Set the function to be called when the timer triggers. fun is passed 1 argument, the timer object.
Which sort of implies the RTC callback should get the RTC?

Re: RTC callback

Posted: Thu Jul 02, 2020 5:19 am
by dhylands
With timers it makes sense to get the timer object since there may be multiple timers.
With the RTC there is only one, so padding that as an argument doesn’t really help.
It probably makes sense to use whatever is most consistent with the other ports, but currently the RTC is fairly inconsistent.

Re: RTC callback

Posted: Thu Jul 02, 2020 11:37 pm
by hlovatt
Switch's callback has no argument, presumably because there is only one switch. By the same argument RTC could have no argument.

Re: RTC callback

Posted: Fri Jul 03, 2020 4:32 pm
by dhylands
It would be possible to make the RTC callback not have an argument.

I think it takes an argument just because it’s actually an EXTI interrupt and they all take arguments.