Posts

Why Are Interrupts Better Than Polling?

Image
In embedded systems, peripherals constantly generate events. A UART receives a byte. A timer expires. A GPIO pin changes state. An ADC conversion completes. The processor needs to respond to these events. There are two common approaches: Polling Interrupts At first, polling looks simple. But interrupts usually provide a more efficient way for the CPU to respond to asynchronous events. What Is Polling? In polling, the CPU repeatedly checks whether an event has occurred. For example: while (1) { if (UART_RX_READY) { read_uart_data(); } } The processor keeps asking the UART: "Did data arrive?" If no data has arrived, the CPU checks again. And again. And again. Imagine UART data arrives only once every 100 ms. During those 100 ms, the CPU may check the same status flag thousands of times even though nothing has happened. That CPU time could have been used for something else. What Changes With Interrupts? With interrupt...

Why Does the Stack Usually Grow Downward?

Image
When debugging nested function calls, you may notice the Stack Pointer moving toward lower memory addresses as the call depth increases. For example: SP = 0x20001000 PUSH {R4} SP = 0x20000FFC But why does this happen? Is a downward-growing stack inherently better, or is it simply an architectural convention? 1. The Stack Does Not Have to Grow Downward A processor can support either: Descending stack: SP decreases as data is pushed. or: Ascending stack: SP increases as data is pushed. Both approaches can work efficiently. So the real question is: Why did descending stacks become so common? 2. It Fits Traditional Memory Layouts Well A simplified memory layout often looks like this: High Address +------------------+ | Stack | | ↓ | | | | Free RAM | | | | ↑ | | Heap | +------------------+ Low Address The stack can begin near the upper end of available memory and grow toward lower addr...

Why Do Microcontrollers Start with an Internal Oscillator?

Why Does a Microcontroller Start With an Internal Oscillator? When a microcontroller powers on, it needs one thing before it can execute even its first instruction: A clock. Most microcontrollers initially run from an internal oscillator. But if an external crystal is more accurate, why doesn't the microcontroller simply start with it? The answer comes down to startup speed, reliability, and initialization sequence. 1️⃣ Fast Startup Internal RC oscillators can become usable very quickly. External crystals need additional time for their oscillations to build and stabilize. Instead of waiting, the MCU can start executing firmware using its internal clock. Power ON → Internal Oscillator → CPU Starts Executing Meanwhile, the external clock source can be initialized and allowed to stabilize. 2️⃣ Guaranteed Availability The internal oscillator is built into the microcontroller. An external crystal depends on components outside the MCU, such as: • Crystal or resonator • Load capacitors • ...

🤔 Why Is RAM Access Slower Than CPU Registers in ARM Microcontrollers?

CPU registers are located inside the processor core . RAM is located outside the CPU core . So accessing RAM requires: address generation bus access memory read/write cycles But registers can be accessed almost instantly by the CPU. That is why CPUs first load data into registers before processing. 👉 Faster register access = better execution speed. This is also why: repeated memory access slows firmware efficient register usage improves performance compilers try to keep frequently used variables in registers Understanding this helps in: firmware optimization driver development assembly understanding performance tuning Small low-level concept. Huge embedded impact. #TheWhySeries #EmbeddedSystems #Firmware #ARM #Microcontrollers #EmbeddedC

🚀 Why Do MCUs Have Vector Tables?

When an interrupt or reset occurs, the CPU must quickly know which function to execute. Instead of searching through code, MCUs use a Vector Table — a fixed memory table that stores the addresses of interrupt handlers. Example (ARM Cortex-M): C void Reset_Handler(void); void UART_IRQHandler(void); void SysTick_Handler(void); The vector table stores pointers to these handlers: 0x00000000 → Initial Stack Pointer   0x00000004 → Reset_Handler   0x00000008 → NMI_Handler   0x0000000C → HardFault_Handler ... When an interrupt occurs, the CPU simply reads the handler address from the vector table and jumps to it. 📌 Why this design? • Instant interrupt response • Simple hardware implementation • Deterministic interrupt latency 💡 Key Insight The vector table acts like a hardware lookup table that maps interrupts to their handlers. #EmbeddedSystems #Firmware #Microcontrollers #ComputerArchitecture #WhySeries