Difference between Software and Hardware Interrupts

anandamu16 picture anandamu16 · Nov 15, 2017 · Viewed 10.1k times · Source

I have recently started working on ARM Cortex Microcontrollers. While reading different articles over Internet, I usually found 2 common terms as Software Interrupt and Hardware Interrupt. What is the actual difference in both? Can you explain with an example?

Answer

jedzej picture jedzej · Nov 16, 2017

I think you're trying to figure out what are software interrupts needed for and how to use them rather than the difference.

Let's start with what's common for software and hardware interrupt: they're both used to switch from main execution context to low-level interrupt handler in order to perform some low level operations - mainly on peripheral register.

The purpose of this switch for hardware interrupts is that the hardware wants to pass some data to the program (GPIO toggled, new chars on UART arrived, etc). It's easy to understand, since when you're writing your program, all you need to do is to implement the handlers - whenever HW needs your action, the handler is called.

The purpose of this switch for software interrupts is that the program wants to pass some data to the hardware. More specifically, it wants to access some resources which cannot be accessed from current context. It applies to a common design, where we don't want high level applications to mess with the hardware, e.g. write directly to flash or change USB control registers, so we block it from upper layers and delegate this task to the OS core (like linux kernel). The core receives requests via software interrupt from upper layers, performs some HW-related operations and returns responses. Software interrupts are also be used to trigger OS maintenance tasks - e.g. context switch when currently executed task blocks on mutex.

It's harder to understand, since in contrary to HW interrupts the programmer must implement both - handlers and callers.


Of course my explanation is a big simplification, but I hope it should help you to understand the general idea. If you are not implementing you OS or very complex bare metal app, you probably don't need this at all. And you should rather avoid modifying software interrupts related code when using OS, since the system may rely on them.

Good luck!