Difference between Software and Hardware Interrupts
Asked Answered
U

4

5

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?

Ulrika answered 15/11, 2017 at 13:21 Comment(3)
Possible DuplicateShelby
A software interrupt is an instruction in a program. A hardware interrupt is generated by the processor, or some connected external device.Wreck
You will have an interrupt service routine which has to do a certain action, depending on a certain event. The event itself can be an instruction (i.e. triggered from your own code), or a hardware event. That's the only difference, as @BoPersson wrote. And once you trigger it, if the priority level of this newly triggered interrupt is higher that the code currently being executed, it will suspend current code and jump to the higher priority interrupt routine. If not, a flag will be raised and the routine will start executing when all higher priority interrupts are done executing.Detent
S
10

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!

Scabies answered 16/11, 2017 at 14:3 Comment(0)
L
3

Hardware interrupts are generated by physical signals, either from within the microcontroller itself (e.g. as part of a bus controller) or from an external GPIO configured as an interrupt. These interrupts are usually related to interactions with hardware external to the microcontroller, e.g. SPI or I2C bus interrupts generated when an event occurs on the bus. These hardware interrupts are usually configured via a combination of control registers, which specify hardware behaviour, and interrupt masking, which allows certain interrupts to be enabled or disabled at any point in time. Internal hardware events such as power events, timers, etc. also trigger interrupts.

Software interrupts are generated by instructions executed by the microcontroller. For example, on x86 platforms you can use an INT3 instruction to raise a TRAP interrupt for debugging purposes. Software interrupts are commonly used as a way to switch privilege levels, e.g. with syscalls, since code running in a low-privilege mode can trigger an interrupt which executes in a high-privilege mode, which can then dispatch the syscall request appropriately.

In both cases, interrupt handlers are generally configured using an interrupt vector table. Interrupt vectors are simply pointers to functions that handle each interrupt. The table usually exists at a fixed memory location in the microcontroller's flash or ROM space, but in many situations the interrupt table location can be configured as part of the programming process. Generally, each interrupt type is given a number which corresponds to its index in the interrupt table, so you know where to put each individual handler's vector.

Lilylilyan answered 15/11, 2017 at 13:34 Comment(0)
B
3

they are all interrupts it just has to do with the source. there are some interrupts (these are signals too) that are generated by instructions or faults, etc (undefined, unaligned, etc). And some that are available to the chip vendor. ARM makes IP not chips, the chip vendor is provided with a number of interrupts they can choose to use however they wish. Often they are tied to peripherals like usb, gpio, uart, spi, etc.

ARM has one called swi software interrupt or svc. And like the number of soft interrupts in x86, this is for example so that an application can make a service call. On a full sized arm this can be executed at the lower/est execution levels but is serviced by a higher more privileged mode or execution level. basically allowing you to have an operating system. Can use this feature on the cortex-m but doesnt have the protection layers it provides a common interface for example when you have downloaded an rtos and want to write applications for it, the rtos is generic-ish and the applications are yours doing what you want or need to get done and can make calls into the rtos (defined by the rtos) using this interface. be it arm or x86 the calls are really arbitrary, logic doesnt dictate, the software dictates, traditionally x86 has bios handlers for specific interrupts, but that is arbitrary, you can write your own bios/handler that is incompatible with tradition and it will work just fine so long as both sides agree on the interface, likewise swi/svc calls for arm are not necessarily a standard, linux might have one but freertos doesnt have to conform to that if they dont want to.

another difference in the case of the cortex-m but is not necessarily a universal differences nor required, is that the so called external interrupts which are external to the core but generally tied to items provided by the chip vendor, have or may have an extra layer of masking, so that you can disable them using an interrupt controller, where the event type interrupts like undefined, systick, etc although are just signals do not go through a masked layer. not universally true for all architectures from all vendors (arm, intel, mips, etc), but if I remember right in the case of the cortex-m this is how they implemented it. granted there continue to be new cores coming out and there is no reason whatsoever for them to design all of them the same way.

Budgie answered 15/11, 2017 at 15:1 Comment(0)
P
0

Hardware interrupt is triggered by hardware like soundchip while software interrupt is triggered by software since it is a program instruction

Puss answered 16/12, 2018 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.