RTC callback

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
hlovatt
Posts: 59
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

RTC callback

Post by hlovatt » Wed Jul 01, 2020 5:54 am

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?

hlovatt
Posts: 59
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: RTC callback

Post by hlovatt » Wed Jul 01, 2020 6:00 am

More information, it really does take an argument. However you can't do anything with the argument!

Therefore is this an RTC bug?

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

Re: RTC callback

Post by dhylands » Wed Jul 01, 2020 5:27 pm

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.

hlovatt
Posts: 59
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: RTC callback

Post by hlovatt » Wed Jul 01, 2020 8:31 pm

There must be a bug though. Because you have to give a callback with an argument, but the argument is uninitialised.

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

Re: RTC callback

Post by dhylands » Wed Jul 01, 2020 11:20 pm

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

hlovatt
Posts: 59
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: RTC callback

Post by hlovatt » Thu Jul 02, 2020 12:43 am

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?

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

Re: RTC callback

Post by dhylands » Thu Jul 02, 2020 5:19 am

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.

hlovatt
Posts: 59
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: RTC callback

Post by hlovatt » Thu Jul 02, 2020 11:37 pm

Switch's callback has no argument, presumably because there is only one switch. By the same argument RTC could have no argument.

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

Re: RTC callback

Post by dhylands » Fri Jul 03, 2020 4:32 pm

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.

Post Reply