-1

I'm an application programmer without much knowledge in low-level programming. I've set up a basic toolchain for assembly programming with the ATMega328P on an Arduino Uno R3 board. One of my aims is to acquire both a conceptual and a pratical grasp of how interrupts work in general, as close as I can from a basic CS Computer Architecture course, without getting overwhelmed by too much complexity if possible.

However I've looked at some interrupt-enabling assembly code (ChatGPT FTW) and I've found it a bit too simple. I'm not sure the ATMega328P is the best platform for that because it (apparently) abstracts things away too much, even using assembly.

Alternatives I'm considering include Microchip PIC, ARM, RISC-V or pretty much whatever I can find that is not terribly expensive and which allows such an endeavor.

Am I committing any mistake in my approach? Are there learning platforms/boards which are more suited for that than others? Currently I'm favoring System-on-a-Chip, Arduino form factor boards.

Piovezan
  • 133
  • 7
  • 3
    Nothing stops you from programming that 328P in "normal" C or even assembly, as long as you're willing to leave the Arduino IDE. Arduino adds the abstractions, not the 328P itself. – Jonathan S. Mar 14 '23 at 21:50
  • @JonathanS Yes, as I mentioned it is an assembly programming toolchain. I've followed [these instructions](https://gist.github.com/mhitza/8a4608f4dfdec20d3879) and I'm writing assembly instructions on a text editor and sending the assembled code to the chip via `avra` and `avrdude` on Linux. However, the interrupt-handling code I've seen seems be pretty much about invoking an API of sorts in the chip, nothing too low level. – Piovezan Mar 14 '23 at 21:53
  • I learned interrupts on the MC68HC11, which turns out to have the simplest scheme possible. (No multilayer interrupt dispatch. Interrupt flag and interrupt mask for each internal peripheral. Global interrupt enable bit. Preemption only if the interrupt handler enables it.) Seems my professors knew what they were doing when they chose that architecture. Many other architectures have e.g. a vectored interrupt handler peripheral that has to be correctly configured. HC11 is just "unmask interrupt you care about" and go. – Ben Voigt Mar 14 '23 at 21:58
  • The entire documentation, fully explained, is only half a dozen pages. See section 5.5 of https://www.nxp.com/docs/en/reference-manual/M68HC11RM.pdf – Ben Voigt Mar 14 '23 at 22:02
  • 2
    @Piovezan That's not an "API in the chip", that's literally the chip's low-level control registers. There is no lower level, you're directly poking the transistors. Interrupt handling in an AVR just is that simple. The chip is hard-wired to jump to location 0x2 when that particular interrupt is enabled and fires. – Jonathan S. Mar 14 '23 at 22:06
  • @JonathanS Yes, the point is that it looks like I'm using an API of sorts. It does not give me much insight about how interrupts are implemented/work in general. – Piovezan Mar 14 '23 at 22:10
  • 1
    You might want to read the Atmega328P data sheet. It tells how they work. And even if it looks too simple, it is a good thing, as it lets you learn the newbie pitfalls in a simple environment which you can grasp easily. Try that on an ARM which are so complex you don't even be able to initialize in a week by reading the data sheet. – Justme Mar 14 '23 at 22:14
  • @Justme I see. That sounds a good idea. Thanks for pointing it out! – Piovezan Mar 14 '23 at 22:15
  • 2
    Is your goal to understand the circuits inside a CPU that _implement_ interrupts, or is your goal to understand the _behavior_ of interrupts and how to write software that uses them? If the latter, then I suspect that the reason that the interrupt-using code that you found seems "a bit too simple" is that interrupts really _are_ that simple. It sounds to me like maybe you already understand interrupts work perfectly well, and the reason you feel uncomfortable is that the concept is a lot simpler than you were expecting it to be. – Cassie Swett Mar 15 '23 at 00:08
  • @TannerSwett It's both actually. I was under the impression that, if I chose the right platform, the code dealing with interrupts would indirectly show me the innards of the microcontroller's interrupt control mechanisms. I guess I can read about particular interrupt mechanism implementations in a few MCU datasheets for that effect. – Piovezan Mar 15 '23 at 01:45
  • I think the "basic CS Computer Architecture course" explanation of how interrupts are implemented would be something like this: There's a part of the CPU—call it the "fetcher"—that is responsible for figuring out what instruction needs to be executed and giving that instruction to the execution unit. If the interrupt pin is not asserted, then the fetcher reads an instruction from memory and gives it to the execution unit. If the interrupt pin is asserted, then the fetcher gives a "call the interrupt handler" instruction to the execution unit, and there you have it. – Cassie Swett Mar 15 '23 at 03:10
  • @Piovezan that's how they are. Every time the CPU goes to the next instruction, it checks if there is an interrupt; if so, it goes to the interrupt handler instead. The x86 has layers of complexity on top, but that's an x86 problem not an interrupt problem per se – user253751 Apr 04 '23 at 14:24

1 Answers1

2

IMHO, the AVR-Architecture is well suited to get the basics correct.

  • What is an ISR?
  • What is a vector table?
  • What to know about context and registers?
  • How to use them in combination with a complex programm (Flags etc.)

I would recommend to start in C (Microchip Studio). If you want to deep dive into special topics, you can view the .lss output which allows to have a look at the actual instructions generated. You can learn from this basis and start implementing certain features to your liking.

But, as it is a simple and rather old implementation, many advanced features are not available.

To get a better understanding of advanced features, the ARM architecture (M0,M3,M4,M7) seems suiteable.

  • You can learn about nesting
  • You can learn about prioritys and priority groups
  • You can learn about multi-core ISR routing
  • And so on...

I would recommend giving it a start in C by using the CMSIS librarys. You can also look at the disassembly to learn and implement certain features yourself in assembly.

And now, as you know all about ISRs:

Get a FPGA board, download a simple Soft-Core and extend its architecture to use all ISR related features you learned about.

Now you learn even more!

On a side note:

  • Stay away from Arduino if you want to learn the deeper concepts
  • Start with CMSIS as ARM is much to complex to start from scratch
  • Feel free to "re-implement" certain CMSIS features/concepts yourself
ElectronicsStudent
  • 2,746
  • 5
  • 18