Page 1 of 1

Callback fuzziness???

Posted: Fri Feb 11, 2022 6:56 pm
by srogers
I am trying to understand the meaning of the following 'tip' for the building of interrupt handling code. The following tip can be found at:

https://docs.micropython.org/en/latest/ ... rules.html

  • Where an ISR returns multiple bytes use a pre-allocated bytearray. If multiple integers are to be shared between an ISR and the main program consider an array (array.array).
When I looked at how to set up an interrupt handler (for a Timer class),I've seen examples like this:

Code: Select all

# -----------------------------------------
def mycallback(t):
    pass.

tim = machine.Timer()
tim.init(mode = Timer.PERIODIC, period = 1000, callback = mycallback)
# -----------------------------------------------
What I do not understand is how does a callback (interrupt handler) like mycallback(t) above return anything, much less a pre-allocated buffer?

Did the hint mean to say something other than return'? Did the hint mean to say that the ISR should update a pre-allocated memory buffer like a pre-allocated bytearray or a pre-allocated array.array? If this is not the case, how does one construct an ISR that 'returns' a pre-allocated buffer with the 'return' reserved word, and how does it get passed back via the callback function return?

Re: Callback fuzziness???

Posted: Fri Feb 11, 2022 11:01 pm
by fivdi
srogers wrote:
Fri Feb 11, 2022 6:56 pm
Did the hint mean to say something other than return'?
I guess the word return is overloaded. In the context of an ISR the returned data doesn't mean the value sent back to the ISR caller with a return statement. It means the data that the ISR knows about that it needs to share with (or return to) the main program.
srogers wrote:
Fri Feb 11, 2022 6:56 pm
Did the hint mean to say that the ISR should update a pre-allocated memory buffer like a pre-allocated bytearray or a pre-allocated array.array?
Yes, this is what it means.

The "Writing interrupt handlers" documentation has a section titled Critical sections showing how this can be done.