Author Topic: Does Arduino have software interrupts?  (Read 3559 times)

0 Members and 1 Guest are viewing this topic.

Offline soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3540
  • Country: es
Does Arduino have software interrupts?
« on: May 19, 2024, 07:40:06 pm »
I got myself an Arduino and plan to do something with it ... as soon as I can resolve a few things first.

Many decades ago I used to program 6502 in assembler and the 6502 had interrupts which were HW pins which would interrupt the program and launch a separate routine. This was useful for dealing with infrequent and irregular events like keypresses, etc.

Do modern microcontrollers, especially Arduino, have something similar?

Suppose I want to use a keyboard for input; can I use interrupts or do I have to poll the keyboard all the time?

The way I understand it is that the "sketch" has an initialization part, which runs once at startup, and a main part which runs and repeats endlessly after that.

If there exist interrupts, where would the code go?

All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 8134
  • Country: de
  • A qualified hobbyist ;)
Re: Does Arduino have software interrupts?
« Reply #1 on: May 19, 2024, 08:15:01 pm »
For ATmega328:
- hardware interrupts: yes
- software interrupts: no
Interrupts are handled by ISRs (function tied to a specific interrupt source).
 

Offline agehall

  • Frequent Contributor
  • **
  • Posts: 389
  • Country: se
Re: Does Arduino have software interrupts?
« Reply #2 on: May 19, 2024, 08:15:40 pm »
Arduino is not a specific thing - it is an ecosystem of things. When it comes to hardware, there are multiple boards with very different MCUs, so you have to be specific.

Their sketch solution is based on two callbacks that are called from the initialization code added by the framework. You can implement other methods that you then call and if you want to implement an ISR, you will need to implement that in a separate function.

Now, the bigger question is why you want software interrupts in your Arduino code. I fail to see the point of using such things in that ecosystem.
 

Offline soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3540
  • Country: es
Re: Does Arduino have software interrupts?
« Reply #3 on: May 19, 2024, 09:36:28 pm »
OK, thanks. I have the Elegoo UNO R3 which I understand is an exact clone of the Arduino Uno and totally compatible.

ELEGOO UNO R3 Board ATmega328P with USB Cable(Arduino-Compatible) for Arduino
https://www.amazon.com/ELEGOO-Board-ATmega328P-ATMEGA16U2-Compliant/dp/B01EWOE0UU/

and also
ELEGOO Nano Board CH 340/ATmega+328P Without USB Cable, Compatible with Arduino Nano V3.0 (Nano x 3 Without Cable)
https://www.amazon.com/ELEGOO-Arduino-ATmega328P-Without-Compatible/dp/B0713XK923/

I gather the answer is that it does have HW interrupts but I guess my question is more about where the interrupt handling routine goes.

We have the main loop repeating over and over so we do not want to put the interrupt routine there. Or is it that I finish the main program by placing a jump to the beginning and after that I place the interrupt routine(s)? that way the interrupt routine(s) would never execute in the normal flow of the program.

How is the Interrupt routine identified as such?

Sorry but I am just not familiar with how the sketch program is structured.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1679
  • Country: gb
Re: Does Arduino have software interrupts?
« Reply #4 on: May 19, 2024, 09:43:12 pm »
A good start would be the datasheet for the microcontroller that whatever type of Arduino hardware you're using has.

When using the Arduinio IDE its a runtime thing that will use up a few resources (like timer0, and by default the USART on the Arduino uno - Atmega328).  But you can still just write bare-metal C in the IDE, and using the interrupts as you would in any other GCC application.  I often do kind of half in half - use the IDE, the bootloader, and a few built-in functions, but for the most part just write my own drivers for other hardware.  For just testing things or one-shot set ups, the built-in libraries are very handy - albeit can be unoptimised and buggy - but they are free so if it doesn't do something you want, write you're own, or fork then make a pull request to the libraries git.

There are of course many libraries that can help, but that all sits ontop of GCC so you don't have to use nay of the built-in libraries if you don't want to. 

 

Online xrunner

  • Super Contributor
  • ***
  • Posts: 7638
  • Country: us
  • hp>Agilent>Keysight>???
Re: Does Arduino have software interrupts?
« Reply #5 on: May 19, 2024, 09:47:48 pm »
I gather the answer is that it does have HW interrupts but I guess my question is more about where the interrupt handling routine goes.

We have the main loop repeating over and over so we do not want to put the interrupt routine there. Or is it that I finish the main program by placing a jump to the beginning and after that I place the interrupt routine(s)? that way the interrupt routine(s) would never execute in the normal flow of the program.

How is the Interrupt routine identified as such?

You just write the ISR and put it outside of the main loop, just listed after it along with other functions you may have. It's just like another function that's compiled. It's identified as an ISR when the interrupt is defined, for example if you want to use attachinterrupt() it's defined there as the routine that gets executed when the interrupt is triggered -

https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
I told my friends I could teach them to be funny, but they all just laughed at me.
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1679
  • Country: gb
Re: Does Arduino have software interrupts?
« Reply #6 on: May 19, 2024, 09:53:02 pm »

I gather the answer is that it does have HW interrupts but I guess my question is more about where the interrupt handling routine goes.

We have the main loop repeating over and over so we do not want to put the interrupt routine there. Or is it that I finish the main program by placing a jump to the beginning and after that I place the interrupt routine(s)? that way the interrupt routine(s) would never execute in the normal flow of the program.

How is the Interrupt routine identified as such?

Sorry but I am just not familiar with how the sketch program is structured.

I missed this reply when I was posting my previous one.

I use ISR(<isr vector> { }

eg:
Code: [Select]
ISR(TIMER1_CAPT_vect) {
  TCNT1 = 0x0000;
  doneflag = 1;
}

I can't remember where that list of vectors is, but I'm sure its in iom328p.h, but theres also here:
https://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html

and a great resource for all things Atmega Arduino:
https://www.gammon.com.au/interrupts

You will have to enable the interrupt in the appropriate peripherals register, I believe global interrupts are enabled by default, but Arduino provides a few macros that lets you disable them for timing critical or non-atomic operations with nointerrupts() and enable them with interrupts()
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6844
  • Country: fi
    • My home page and email address
Re: Does Arduino have software interrupts?
« Reply #7 on: May 19, 2024, 10:04:37 pm »
Suppose I want to use a keyboard for input; can I use interrupts or do I have to poll the keyboard all the time?
Two most common interrupts are pin change interrupts and timer interrupts.

If there exist interrupts, where would the code go?
For AVR microcontrollers in the Arduino environment, you write it as a function (that does not return anything), and in the setup part of your code, attach it to the desired interrupt.

Timer interrupts vary a bit depending on the board and microcontroller you're using, but the simplest interface is to declare a timer object as a global variable –– you can think of it as creating the "attachable" interrupt source –– and at setup (or any other time), start the interrupts by calling a specific member function of that object, naming the interrupt handler function and the interval.

As many here well know, I often recommend Teensy 4.0 and Teensy 4.1 microcontrollers for us hobbyists.  They're incredibly powerful, affordable, have a forum where the developer participates, and the Arduino environment support (Teensyduino) is pretty well written.  Teensy boards have a proprietary bootloader chip (that you can buy separately), but the rest is pretty much open source, including the Arduino cores.  PJRC has also contributed quite a lot to Arduino environment and libraries.

For both Arduino-branded microcontroller boards and Teensy boards, if you have a pin, say
    constexpr uint8_t  indicator_pin = 6;
and some function you want to be called whenever the pin state changes, say
    void indicator_changed_state(void) { ... }
and then in setup() you set that pin as an input and attach the function to the state change interrupt using
        pinMode(indicator_pin, INPUT);
        attachInterrupt(digitalPinToInterrupt(indicator_pin), indicator_changed_state);

For Teensy, to create a timer (IntervalTimer) that calls your timerfunc() say 100 times a second, you'll need to declare the timer object as a global variable,
    IntervalTimer  mytimer;
and then in setup(), you attach your function to it and set the interval in microseconds (1,000,000 µs = 1 second; 1/100th of a second is 10,000 µs):
        mytimer.begin(timerfunc, 10000);
The Delay and Timing Functions – IntervalTimer page describes it in more detail.

As Teensys have a hardware USB interface, they can be any kind of an USB device.  One of the built-in devices is an USB keyboard, which means the Teensy will look like a perfectly standard USB keyboard to any computer –– so won't need special drivers or anything ––, except that you yourself are in full control of the "presses" and "releases", via the code running on the Teensy.  There is even a USB Keyboard example.  (Built-in examples includes Keyboard + Mouse + Joystick and Serial + Keyboard + Mouse + Joystick, which allows you to create very complicated USB Human Interface Devices with very little code.)
Because there is not much "other work" to do for USB HID devices, the keyboard and other inputs are typically polled in the main loop.

If you use a timer interrupt or pin change interrupt, the only problem is atomicity: that the main loop code sees the input event correctly.  (Even on 6502 assembler, you did have to make sure that larger than single-byte values read or written from an interrupt, were accessed with interrupts disabled, or you might see half-updated values.)  This means you have to implement some kind of an interrupt-safe event queue (or keypress/release queue).  For Teensys, the simplest way to do this is to write accessor functions, where you use __disable_irq() before and __enable_irq() after the actual access; both in the interrupt, and in the main loop code.  (What it does is slightly more complicated than SEI/CLI, because priorities are involved –– only equal or lower priority interrupts are disabled/enabled –– but I'm sure it should feel familiar to you.)

I use ISR(<isr vector>) { }
That is the AVR-specific interrupt service routine, provided by avr-libc's <avr/interrupts.h>.  There is nothing wrong in using that, but it is limited to AVR microcontrollers.
The attachInterrupt() I show above is the "new", easier to use and portable Arduino environment equivalent.
 

Offline soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3540
  • Country: es
Re: Does Arduino have software interrupts?
« Reply #8 on: May 19, 2024, 10:07:35 pm »
Many thanks to all. That answers my question.

I still have a steep hill to climb ahead of me but I am beginning to get my bearings.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 17117
  • Country: us
  • DavidH
Re: Does Arduino have software interrupts?
« Reply #9 on: May 19, 2024, 10:45:32 pm »
Do modern microcontrollers, especially Arduino, have something similar?

Modern microcontrollers almost always support hardware interrupts, usually some form of interrupt on change which monitors an input port.

Quote
Suppose I want to use a keyboard for input; can I use interrupts or do I have to poll the keyboard all the time?

Interrupts could be used, but there are good reasons to use polling instead.

If the keyboard is connected through an I/O expander, then there will be no signal for the microcontroller to detect without accessing the I/O expander first, so polling is more suitable.  Using an I/O expander has the advantage of limiting the number of microcontroller I/O pins needed for the keyboard and makes it easier to locate the keyboard remotely.

 

Offline pqass

  • Frequent Contributor
  • **
  • Posts: 910
  • Country: ca
Re: Does Arduino have software interrupts?
« Reply #10 on: May 20, 2024, 03:10:16 am »
Quote
Suppose I want to use a keyboard for input; can I use interrupts or do I have to poll the keyboard all the time?

You can make an interrupt only (no poll) keyboard interface but you'll need a bit of hardware support; like NPN & PNP transistors, resistors, and some diodes. See attached (adapted from here).

It can be directly attached to the MCU ports or accessed via simple SPI I/O expander (like the 74HC165).  The active-high interrupt line (pull-down resistor not shown) can be shared with other devices too.  If an active-low interrupt is desired just move the diodes and interrupt PNP to the column lines replacing with an NPN and reversing the diodes.

However, there are more sophisticated I/O expanders (like the MCP23S17) that can generate the interrupt signal automatically but they're complicated to configure.

Parts count can be reduced by using SIP resistor arrays.
 
The following users thanked this post: Dazed_N_Confused

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 4305
  • Country: nl
Re: Does Arduino have software interrupts?
« Reply #11 on: May 20, 2024, 06:32:17 am »
Maybe this site will help you see things more clearly.

And don't forget about the Arduino reference site: https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

Offline soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3540
  • Country: es
Re: Does Arduino have software interrupts?
« Reply #12 on: May 20, 2024, 07:32:14 am »
I mentioned a keypad just as an example but decades ago I used to buy keypads which included a rows, columns and a third pin for interrupt. When any key was pressed the interrupt signal was activated. No electronics. When pressing a key the row and column would be shorted and also to the interrupt.  I guess it could be replicated with keys with switches with two contacts.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Offline kripton2035

  • Super Contributor
  • ***
  • Posts: 2679
  • Country: fr
    • kripton2035 schematics repository
Re: Does Arduino have software interrupts?
« Reply #13 on: May 20, 2024, 07:32:49 am »
and this web page is also about arduino interrupts and very well explained.
https://www.gammon.com.au/interrupts

and his mother page
https://www.gammon.com.au/forum/index.php?bbtopic_id=123
 

Offline soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3540
  • Country: es
Re: Does Arduino have software interrupts?
« Reply #14 on: May 20, 2024, 08:01:08 am »
Thanks. I am only beginning with this and have a lot of studying ahead of me.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22381
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Does Arduino have software interrupts?
« Reply #15 on: May 20, 2024, 09:17:27 am »
Software interrupts are... Idunno, rather passe nowadays, anyway?

Like, in the x86 days, there were 256 slots to use, and the BIOS and system only used the first couple dozen, leaving everything else open for software use.  No way someone would build a system that actually uses all of them in hardware, but you could if you wanted to, and anything leftover is usable by software interrupt.  This was also in analogy to earlier (x80) systems of the time, where zero-page memory ($00 to $FF) might've been used to store important data like BIOS entry points and system variables.  For example, MS-DOS loaded executables such that a JMP 0 in the default segment (initial CS value) would terminate the program. See: https://en.wikipedia.org/wiki/Program_Segment_Prefix

I'm not familiar with 6502, but I expect they used similar things, so this will all be familiar; in which case, this serves as a reminder of where we're coming from, and a more concrete example of how things might've been done.

AFAIK, modern systems largely link API calls into the executable -- the EXE contains a table of symbols, and locations they're called from, that the OS needs to patch with its call addresses.  (Which may be stubs in user space -- 32-bit Windows for example only allocates 3.5GB to the application. What the rest is mapped to--you're, I think, not supposed to know?  Or they may be task gates or whatever, but I think those were mostly frowned upon for performance reasons?)  There is still SYSCALL on *nix, which, I think differs in x64 because it's been improved and just kinda works better?  (And maybe Windows uses it too?)  But the executor supports all the usual load-time and dynamic linking that's been used over supported history, so you can still do whatever, give or take x86 and x64 conventions.  Anyway, I'm hardly an expert on modern systems, even 386 era, so, just for flavor, more examples.

But if you're linking a binary blob, and that's all that's ever loaded onto the chip, it really doesn't matter how you jump to a particular piece of code, just call it from wherever: the linker has the address, ready to go, no indirection necessary.

And it's so easy to compile/link and program a new blob onto a chip of this size, that it's hardly worth doing anything, or more than a basic bootloader for example.

Then, if you are actually going to the trouble of crafting dynamic code updates, and need to load a symbol/jump table and entry points and all that, or want to write a full-blown operating system of sorts -- yeah, you may want to craft something like that.

If nothing else, you could override and extend the IVT and just append your own dummy entries, and then a
Code: [Select]
(void (*)(void))(interrupt_number * 2)();will simply jump to and execute any one.  (For ATMEGA, the IVT is traditionally populated with JMP $(ISR) instructions, which occupy two words.  This will show as *4 in the disassembler.)  But, beware the extraneous RETI at the end of the ISR may acknowledge the next pending interrupt, causing inconsistent behavior.

You also can't declare your own ISRs beyond what are defined for the platform, so you're kind of on your own there.  You're just making more named functions, in this case not overriding the default stub but creating new ones, and you'll want to review the header files, and a bit of the C Runtime library, to see how it's done.

So, I would strongly recommend just using interrupts the way they're intended, and not playing with it in software.  At worst, use something like a spare timer set to single-cycle countdown, or a pin change interrupt triggered by a port pin toggle, if you really must.

Anyway, you can craft your own jump table, and even assign it a fixed address across projects, if you really like.  (It can be forced to a fixed base address in the same way the built-in IVT is.)  There's no standard here, nor any expectation for where one should be placed, like, you won't find libraries that rely on this (well, probably not?).

This is of course very different on ARM, for example the IVT is variable on some platforms.  I forget which all (Cortex-M0? M4? A-anything? etc.) Arduino is on, or most details of any of these platforms really, but suffice it to say there are various ways one could approach this.  There is more/better support of dynamic objects on ARM, for example because Flash is often shadowed into RAM for performance reasons, and the program and linker need to be aware of both spaces.  Or you can go so far as loading Linux entirely, on supported devices (usually M4 and up, with MMU, something like that?), and work from there.

Anyway, as for stuff like keyboards: if it's polled, you control the timing.  Simple as that.  Run a heartbeat timer interrupt, scan the key matrix, debounce algorithm, and update key state in a global array.  This can be turned into a proper BIOS function if you like (translating scan codes to key codes, ASCII or ANSI, buffering inputs), or left as is.  Or sent through a callback table for event-driven (e.g. onpress(KEY_NUM0), etc.) logic if you like.

Or if it's asynchronous, simply use the USART interrupt to fill a buffer, and poll for bytes from time to time in the main() loop.  Or parse it during the interrupt, and translate into above functionality, also fine.

A heartbeat interrupt is useful in general, for a wide variety of applications.  If you want to implement a task system, it's a good way to trigger context switching.  Most embedded applications, that you'd use one of these MCUs for in the first place, will have some manner of housekeeping activity in main(), which can be triggered by waiting (whether sleep() or spin looping) until a trigger (such as heartbeat setting a flag which is then cleared by main(): perhaps the simplest realization of the generator-consumer design pattern) and then state updates are made (e.g. polling high-latency devices, filtering variables, updating display, queuing serial data, etc.).

Tim
« Last Edit: May 20, 2024, 09:30:37 am by T3sl4co1l »
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 4305
  • Country: nl
Re: Does Arduino have software interrupts?
« Reply #16 on: May 20, 2024, 10:17:56 am »
Judging on the OP the title is a bit misleading.

Many decades ago I used to program 6502 in assembler and the 6502 had interrupts which were HW pins which would interrupt the program and launch a separate routine. This was useful for dealing with infrequent and irregular events like keypresses, etc.

Do modern microcontrollers, especially Arduino, have something similar?

soldar refers to the 6502 having interrupts and that they were HardWare pins that generate an interrupt.

The answer to the question if modern microcontrollers have these too, is yes.

A better title would be "How to setup interrupts in Arduino" or "Does Arduino have interrupts and how to set them up in software".

The 6502 actually does have an interrupt instruction (BRK) to trigger an interrupt from software.

Offline madires

  • Super Contributor
  • ***
  • Posts: 8134
  • Country: de
  • A qualified hobbyist ;)
Re: Does Arduino have software interrupts?
« Reply #17 on: May 20, 2024, 11:53:48 am »
Software interrupts are... Idunno, rather passe nowadays, anyway?

Still in use. Things like 'division by zero' are usually handled via software interrupts.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8760
  • Country: fi
Re: Does Arduino have software interrupts?
« Reply #18 on: May 20, 2024, 05:45:18 pm »
Software interrupts are... Idunno, rather passe nowadays, anyway?

Hugely important. I use them all the time on Cortex-M microcontrollers. They are really the only way to demote interrupt priority and continue slower processing while accepting further low-latency interrupts; enabling event-driven programming. Great alternative to flags/FIFOs and loops that check them (which require more work to get right, and have worse guarantees about timing as no one really implement their own pre-emption logic, so they tend to run one large function completely before others), or full-fledged operating systems with schedulers. Interrupts are still the simplest way to make the CPU hardware itself slice the linear code flow, and software interrupts allow one to assign priorities freely to such functions.

I think this is the middle ground. The other two extremes are writing the whole program in very small state transitions which are small enough to be processed as atomic chunks, in which case you can write a pretty simple "scheduler" yourself; and the other is using time-slicing OS which can run linear code flow in threads, making copy-pasting existing code very easy - especially appealing in complex networking code. My favorite middle ground is writing event-driven state machines, but with larger event functions than in the first extreme by utilizing the pre-emption possibilities of modern MCUs which have interrupts with configurable priority.
« Last Edit: May 20, 2024, 05:50:24 pm by Siwastaja »
 

Offline soldarTopic starter

  • Super Contributor
  • ***
  • Posts: 3540
  • Country: es
Re: Does Arduino have software interrupts?
« Reply #19 on: May 20, 2024, 07:41:12 pm »
Judging on the OP the title is a bit misleading.

Many decades ago I used to program 6502 in assembler and the 6502 had interrupts which were HW pins which would interrupt the program and launch a separate routine. This was useful for dealing with infrequent and irregular events like keypresses, etc.

Do modern microcontrollers, especially Arduino, have something similar?

soldar refers to the 6502 having interrupts and that they were HardWare pins that generate an interrupt.

The answer to the question if modern microcontrollers have these too, is yes.

A better title would be "How to setup interrupts in Arduino" or "Does Arduino have interrupts and how to set them up in software".

The 6502 actually does have an interrupt instruction (BRK) to trigger an interrupt from software.
Yes. Thanks.
All my posts are made with 100% recycled electrons and bare traces of grey matter.
 

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22381
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Does Arduino have software interrupts?
« Reply #20 on: May 20, 2024, 07:43:58 pm »
Hugely important. I use them all the time on Cortex-M microcontrollers. They are really the only way to demote interrupt priority and continue slower processing while accepting further low-latency interrupts; enabling event-driven programming. Great alternative to flags/FIFOs and loops that check them (which require more work to get right, and have worse guarantees about timing as no one really implement their own pre-emption logic, so they tend to run one large function completely before others), or full-fledged operating systems with schedulers. Interrupts are still the simplest way to make the CPU hardware itself slice the linear code flow, and software interrupts allow one to assign priorities freely to such functions.

Ahh, cool example!

I've thought about that on AVR before, but it seems kind of silly to even need that on such a platform, and I certainly don't write enough programs to have had the actual need.  Doing it there, I think would basically be, pushing the "wrong" address and doing a tail call out of the interrupt proper (thus acknowledging the source (XMEGA+)).  Calling into a function that still puts back all the registers on the stack, of course..!

Well sort of.  I kinda wrote my first large project that way, actually.  My senior design (≈thesis) project was a digital control power supply, and I had put all the housekeeping functionality in the timer interrupt itself.  On ATMEGA, this is practically trivial, as you can just sei() at will -- RETI does nothing special -- and that's your priority system, in software.  So, after counting a few ticks (heartbeat timer interrupt), it entered the big processing clause, and enabled interrupts; I don't think it ever actually happened (but I never checked--!), but it could if need be, interrupt the same function while it's still running and do the lower-level repetitive stuff without re-entering the clause (protected by a volatile bool inClause flag, in addition to running less frequently), thus "mostly" avoiding stack overflow.  But, early projects being what they are, I never used that architecture again, and anyway, the display (HD44780 style) never was quite correct, there was some rare consistency error that I didn't have time to debug.  Doubtless some concurrency or buffer consistency (display was buffered, I think I sent it a char every interrupt (1ms) or something like that?) problem.  The accompanying paper made lip service to it being some manner of hard-coded cooperative multitasking system, which seemed to impress/amuse the instructor well enough.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4701
  • Country: dk
Re: Does Arduino have software interrupts?
« Reply #21 on: May 20, 2024, 09:39:32 pm »
Hugely important. I use them all the time on Cortex-M microcontrollers. They are really the only way to demote interrupt priority and continue slower processing while accepting further low-latency interrupts; enabling event-driven programming. Great alternative to flags/FIFOs and loops that check them (which require more work to get right, and have worse guarantees about timing as no one really implement their own pre-emption logic, so they tend to run one large function completely before others), or full-fledged operating systems with schedulers. Interrupts are still the simplest way to make the CPU hardware itself slice the linear code flow, and software interrupts allow one to assign priorities freely to such functions.

Ahh, cool example!

I've thought about that on AVR before, but it seems kind of silly to even need that on such a platform, and I certainly don't write enough programs to have had the actual need.  Doing it there, I think would basically be, pushing the "wrong" address and doing a tail call out of the interrupt proper (thus acknowledging the source (XMEGA+)).  Calling into a function that still puts back all the registers on the stack, of course..!

yeh basically add a stack frame or what ever is needed to do an RTI to you function, which will then later return to main

I never wrapped my head around how to do it "properly" on an cortex-m using using the software interrupt, so I usually do it the easy way, SW trigger an otherwise unused interrupt set at the right priority and do an rti, cortex-m does tail-chaining so RTI while an interrupt is pending is just a jump without unstacking/stacking

 
 

Online westfw

  • Super Contributor
  • ***
  • Posts: 4306
  • Country: us
Re: Does Arduino have software interrupts?
« Reply #22 on: May 21, 2024, 04:48:41 am »
On an Arduino, there are essentially two types of interrupts.

The first type is the various hardware interrupts implemented by the hardware. (25 different sources on the ATmega328p used on an Arduino Uno.)  These are vectored through a table at location 0 of flash, and you can implement your own Service Routines via the "ISR" macro as several people have mentioned.  Since the vectors are in flash, they are NOT changeable at runtime.  If the Arduino Core is already using the interrupt, you can be in trouble.  (however, by default I think the Uno core only uses the Timer0 interrupt.)

The second type is via the Arduino core and assorted libraries.  This provide an ISR for some hardware interrupt (notably the "external interrupts" on two pins, and the port-wide "Pin Change Interrupts" on each port), and provide further dispatching and an API, like attachInterrupt() and attachPCINT(), so that a user provided function (just a C function, in this case) can be provided by the user sketch (and changed at runtime, as desired.)  It's probably worth noting that this extra level of indirection is significantly "expensive" compared to a simple ISR.

"Software Interrupts" are a feature that the AVR doesn't have.  They're useful if you have a separately compiled "operating system" (regardless of how trivial), since the usual vectoring and context preservation permits the "user program" from needing to know the actual function addresses used in the OS.  Thus the "int 21" for accessing MSDOS functions, and assorted other SW interrupts for accessing PC BIOS functions.  Since most Arduinos don't have any OS, and are just a monolithic compiled and linked executable, SW interrupts aren't really very relevant anyway.  (Also, AVR interrupts don't really save any more context than a "call" instruction.  Unlike ARMs.)

Different chips behave differently.  Most ARM chips let you relocate the interrupt vectors to RAM, have more vectors, have a SW interrupt function, and DO save significant context.
The circumstances under which a pin can interrupt also vary significantly.  The "external interrupts" on an AVR can be configured to trigger on a rising or falling edge, but the "Pin Change" interrupts are limited to "one of the pin configured for interrupts has changed.  The SAMD (ARM) chips used on some Arduinos can usually have the more complex triggering on any pin, but not on all pins (usually ~16 possible pin interrupts distributed across however many IO pins are on the chip.)
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8760
  • Country: fi
Re: Does Arduino have software interrupts?
« Reply #23 on: May 21, 2024, 09:05:21 am »
I never wrapped my head around how to do it "properly" on an cortex-m using using the software interrupt, so I usually do it the easy way, SW trigger an otherwise unused interrupt set at the right priority and do an rti, cortex-m does tail-chaining so RTI while an interrupt is pending is just a jump without unstacking/stacking

That's how it's done. Very simple and easy. But you might be right, maybe there is a more difficult and as such, more proper way. Tail-chaining makes the whole thing very efficient and elegant. When the fast interrupt finishes, the processing continues right away from whatever pending ISR has the highest priority; if there is something more urgent pending, it will be processed before your software IRQ. But in my opinion priorities are quite easy to get right for the application needs, as one-dimensional continuum.
 

Offline aliarifat794

  • Regular Contributor
  • *
  • !
  • Posts: 138
  • Country: bd
Re: Does Arduino have software interrupts?
« Reply #24 on: May 26, 2024, 08:41:04 pm »
Arduino boards does not support software interrupt. However, ESP32 does. This board is also Arduino compatible. So, you can program it the same way you program an Arduino Board. Unfortunately, I could not find a solid example tutorial. However, here are some contents that may help you.

https://docs.espressif.com/projects/esp-idf/en/v5.1/esp32/api-reference/system/intr_alloc.html
https://www.theengineeringprojects.com/2021/12/esp32-interrupts.html
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf