<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Learn Low-Level Embedded Systems on Medium]]></title>
        <description><![CDATA[Stories by Learn Low-Level Embedded Systems on Medium]]></description>
        <link>https://medium.com/@learnlowlevelembeddedsystems?source=rss-75e4c34adaf2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*T4izxKFN-9EpbokQ4nmzsQ.png</url>
            <title>Stories by Learn Low-Level Embedded Systems on Medium</title>
            <link>https://medium.com/@learnlowlevelembeddedsystems?source=rss-75e4c34adaf2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 20:36:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@learnlowlevelembeddedsystems/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Interrupts and Exceptions on ARM Cortex M4 CPUs and STM32 MCUs — Bare Metal Embedded C Approach]]></title>
            <link>https://medium.com/@learnlowlevelembeddedsystems/interrupts-and-exceptions-on-arm-cortex-m4-cpus-and-stm32-mcus-bare-metal-embedded-c-approach-868d331f24cc?source=rss-75e4c34adaf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/868d331f24cc</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[embedded-c-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[embedded-systems]]></category>
            <category><![CDATA[stm32]]></category>
            <dc:creator><![CDATA[Learn Low-Level Embedded Systems]]></dc:creator>
            <pubDate>Fri, 10 Jul 2026 01:38:53 GMT</pubDate>
            <atom:updated>2026-07-10T01:38:53.079Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*B3CyE5uc_8tl-jSBG_2fcg.jpeg" /><figcaption>Credits: <a href="https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors">https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors</a></figcaption></figure><p>Interrupts and exceptions are fundamentals concepts to master in any embedded programming domain, especially when dealing with low-level/bare-metal programming. This article will use C to explain some of the key concepts you need to know about interrupt and exception handling on the ARM Cortex M4 and STM32 MCUs.</p><p>This article assumes knowledge in embedded C.</p><h3>What are Interrupts and Exceptions?</h3><p>In general terms, an <strong>exception</strong> is any event, internal or external, that causes a CPU to pause its normal execution flow and tend to the said event. An <strong>interrupt</strong>, on the other hand, is the term usually used when referring to events that are external to the CPU that require its immediate attention. Every interrupt is an exception but not every exception is an interrupt.</p><p>These interrupts can be triggered from many different peripherals on STM32 MCUs, like GPIO pins, timers, communication protocols, etc…</p><p>A good analogy to understand an interrupt would be to treat the CPU as someone cooking in a kitchen, and an interrupt as the mailman ringing the doorbell to request a signature. The CPU, or the cook, was executing normally, until the doorbell rang (the interrupt), which forces the cook to answer the door and sign for the package before going back to cook.</p><p>An <strong>interrupt handler</strong>, referred to as an <strong>interrupt service routine (ISR)</strong>, is a function that, just judging by its name, handles an interrupt. These are the actual functions that do what you want when an interrupt happens. For example, if you had an interrupt every time a button was pressed, and wanted an LED to turn on when it was, the ISR would turn the LED on.</p><p>An <strong>unmasked interrupt</strong> is an interrupt that is eligible to preempt the CPU, and a <strong>masked interrupt</strong> is one that is currently blocked from preempting the CPU, even if it is set as pending.</p><h3>Interrupt Handling on the ARM Cortex M4</h3><p>The ARM Cortex M4 contains a built-in system called the <strong>Nested Vectored Interrupt Controller, NVIC</strong>, that controls interrupt lines coming into the CPU. An interrupt line is just a signal that corresponds to some interrupt. The NVIC handles everything from an interrupt’s priority to its current state. It also handles more complex tasks like tail-chaining and nesting.</p><p>Interrupts on the CPU have priorities, which determine what interrupts can and can’t preempt. Priorities in the NVIC are represented through a lower-is-higher system, meaning the lower the priority number, the higher priority it is.</p><h3>Interrupt Handling on STM32 MCUs</h3><p>STM32 MCUs include a peripheral called the <strong>External Interrupt/Event Controller, EXTI</strong>, which connects directly to GPIO pins to use to trigger interrupts. It can also be used to wake the system from deep sleep or power saving modes. The EXTI is the main method in which external interrupts from GPIO pins and certain internal signals are triggered.</p><p>The main method in which the EXTI is used to detect events on GPIO pins is through edge detection, like rising edge and falling edge detection. These two events occur when a signal goes from LOW to HIGH, and HIGH to LOW, respectively.</p><h3>NVIC and EXTI Cooperation</h3><p>The EXTI and NVIC are complementary. The EXTI is used to detect events on external peripheral signals, and the NVIC actually causes the CPU to tend to the interrupt.</p><p>Now, these two systems will be explained using an example of a push button connected to GPIO pin PA0. The button will be an active-HIGH button, meaning PA0 will be LOW when the button isn’t pressed, and HIGH when it is pressed.</p><p>A good way to think of the NVIC is as a middle man between the EXTI and the CPU. The EXTI detects the rising edge on PA0 when the button is pressed and sets the EXTI0 line to pending. Then, the NVIC detects the pending interrupt line on EXTI0, sets the respective EXTI0_IRQn line as pending, and checks if it is a high enough priority to preempt the currently running process.</p><p>Then, the CPU will save its state on the stack and switch execution to the ISR, called EXTI0_IRQHandler() on the STM32F411VE.</p><h3>Configuring and Handling Interrupts on the STM32F411VE</h3><p>This example will take the same information from the button connected to PA0 as stated above, and will assume the pin is correctly configured already. Please refer to the STM32F411xC/E reference manual and Armv7-M Architecture Reference Manual for the information used in this code.</p><p>The first thing that needs to be done is to enable the SYSCFG clock, which is used to map EXTI lines to GPIO pins.</p><pre>#define RCC_BASE_ADDR 0x40023800<br>#define RCC_APB2ENR *((volatile uint32_t *)(RCC_BASE_ADDR + 0x44))<br><br>// SYSCFG Clock Enable<br>RCC_APB2ENR |= (1 &lt;&lt; 14);</pre><p>Then, you need to the 4 least significant bits in the SYSCFG EXTICR1 register, to map the EXTI0 line to the PA0 pin.</p><pre>#define SYSCFG_BASE_ADDR 0x40013800<br>#define SYSCFG_EXTICR1 *((volatile uint32_t *)(SYSCFG_BASE_ADDR + 0x08))<br><br>// EXTI0 -&gt; PA0<br>SYSCFG_EXTICR1 &amp;= ~(15 &lt;&lt; 0);</pre><p>After that, the rest of the configuration is fairly straightforward. The EXTI registers are used to first unmask the interrupt line. Then, the rising edge detection for EXTI0 is enabled. Finally, the processor’s NVIC registers are used to set the priority (set as 1 here, for example), and then to enable the interrupt.</p><pre>#define EXTI_BASE_ADDR 0x40013C00<br>#define NVIC_IPR0_ADDR 0xE000E400<br>#define NVIC_ISER0_ADDR 0xE000E100<br>#define EXTI_IMR *((volatile uint32_t *)(EXTI_BASE_ADDR))<br>#define EXTI_RTSR *((volatile uint32_t *)(EXTI_BASE_ADDR + 0x08))<br>#define EXTI_PR *((volatile uint32_t *)(EXTI_BASE_ADDR + 0x14))<br>#define NVIC_IPR1 *((volatile uint32_t *) (NVIC_IPR0_ADDR + 4))<br>#define NVIC_ISER0 *((volatile uint32_t *)(NVIC_ISER0_ADDR))<br><br>void configureEXTI0()<br>{<br>  // Unmask Line 0<br>  EXTI_IMR |= (1 &lt;&lt; 0);<br><br>  // Trigger interupt on rising edge<br>  EXTI_RTSR |= (1 &lt;&lt; 0);<br><br>  // NVIC EXTI0 priority 1<br>  NVIC_IPR1 &amp;= ~(15 &lt;&lt; 20);<br>  NVIC_IPR1 |= (1 &lt;&lt; 20);<br><br>  // Enable NVIC EXTI0 line<br>  NVIC_ISER0 |= (1 &lt;&lt; 6);<br>}</pre><p>Finally, the ISR is implemented, where it enables the LED and removes the pending status from the interrupt. The remove pending function simply uses the line EXTI_PR = (1 &lt;&lt; 0); to clear the pending bit from EXTI0. Writing a one to the bit resets it.</p><pre>void EXTI0_IRQHandler()<br>{<br>  enableLED4();<br>  EXTI0RemovePend();<br>}</pre><p><strong>Note:</strong> The ISR name must match the name in the vector table, which is essentially an array placed at the start of flash with the addresses of all interrupt handlers that the CPU can branch to when needed.</p><p>That is all on simple interrupt handling on ARM Cortex M4 CPUs and STM32 MCUs!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=868d331f24cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>