Skip to content

Conversation

@graugans
Copy link
Contributor

@graugans graugans commented Nov 1, 2023

During the discussion of: tinygo-org/tinygo#3981 I brought up that there is some room for improving the interrupt documentation. This is my take on this.

Signed-off-by: Christian Ege <ch@ege.io>
Copy link
Contributor

@soypat soypat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, merging! Thank you graugans for your contribution!

@soypat soypat merged commit 1173359 into tinygo-org:dev Jan 6, 2024
soypat added a commit that referenced this pull request Jan 6, 2024
deadprogram pushed a commit that referenced this pull request Feb 26, 2024
Comment on lines +83 to +86
When writing an interrupt handler you have to take care that your code does not block. This is important because the interrupt execution typically has higher priority than your regular code. This means in case your interrupt handler code needs to wait for anything it will wait forever. In general, it is good advice to avoid the following:

* Memory allocation, for details see [heap allocation]({{<ref "heap-allocation.md">}}).
* Blocking on channels, better use a select with a default clause to implement non-blocking send and receives.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's stronger than this.

typically has higher priority than your regular code

This is always the case. An interrupt always runs instead of regular code, and unless you use very weird hacks you can't continue normal code. (This is true for all architectures I've looked at: Cortex-M, AVR, Xtensa, RISC-V). In fact, the runtime now checks for these things and if it is inside an interrupt handler when any of these things occur (blocking operations, memory allocations) it will simply panic.

The reason memory allocations are not allowed is a bit more subtle: if an interrupt happens while the GC is running, it will mess with the internal state of the heap. (I've looked for possible ways to fix this but couldn't find anything that was acceptable for TinyGo).

In general, it is good advice to avoid

I'd rather word this as something like "never use these things".

fmt.Printf

That falls under the general category of "too big for an interrupt". LEDs are a way to indicate things, and on some chips println will also work - but not on all (it will usually work when using UART, but USB-CDC is generally too complex to use inside an interrupt handler).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I ALWAYS avoid interrupts. Inconditionally. Unless you are writing real-time critical code down to the nanosecond, interrupts are a needless source of breakage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Now realizing this has been merged by no one other than yours truly 🤦)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interrupts can also be useful to avoid polling and conserve battery life :)
But yeah, most users should not use interrupts. Instead, all that functionality should be hidden inside packages like the machine package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants