Interrupts: potential concurrency issue

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Interrupts: potential concurrency issue

Post by pythoncoder » Thu Aug 14, 2014 8:25 am

When I worked in hardware and firmware development we had a well-formed fear and loathing of edge triggered interrupts, the reason being that it was hard to avoid occasional situations in which an interrupt was missed. A debugging nightmare. Doubtless the technology has moved on in the last quarter of a century but perhaps someone could advise me how to implement this:

I need to maintain an accurate count of interrupts. To simplify this, let's pretend the count is a global. My interrupt handler has
count += 1
My main program (running in its own context, not that of the interrupt) needs to read the counter and reset it to zero. Ideally we would use an indivisible read-modify-write instruction. Lacking this, I disable the interrupt, read and modify the value, and re-enable the interrupt.

If an edge occurs while the relevant interrupt is disabled, will the hardware/interpreter call the handler when it is re-enabled? Even this isn't ideal because theoretically muliple dges might occur but I'm not planning to accommodate hardware that fast...

Or is there a better way? I've not yet delved into ARM assembler but perhaps someone can advise.

Regards, Pete
Peter Hinch
Index to my micropython libraries.

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

Re: Interrupts: potential concurrency issue

Post by dhylands » Thu Aug 14, 2014 1:45 pm

I'm not aware of any atomic increment at the hardware level.

So you'll need to disable/enable irqs around your accesses to the counter in the main thread. I put together some code which does just that over here:
http://forum.micropython.org/viewtopic. ... tomic#p872

As far as edge triggered interrupts go, what are the edges being used for?

The hardware will buffer a single edge. So if an edge happens while interrupts are enabled, your irq handler will still get called as soon as interrupts are re-enabled. However, if more than one edge comes along, you'll only get called once.

The timer has some interesting modes for dealing with certain types of operations. There is an Input Capture mode which can be used to measure pulse widths. There is even an Encoder mode where the HW will increment/decrement a counter using quadrature input.

I've just started on PWM output using the timers, and I'm planning on adding Input Capture, Output Compare, and Encoder support.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Interrupts: potential concurrency issue

Post by pythoncoder » Thu Aug 14, 2014 4:51 pm

Thanks for that, Dave. I am currently disabling interrupts when I read the counter - if the hardware buffers them there should be no risk of missing an edge. I've no particular application in mind - just trying to avoid potential bear traps in my scheduler.

The timer related stuff you're doing sounds excellent.

Regards, Pete
Peter Hinch
Index to my micropython libraries.

Post Reply