2026-06-01 8:13 PM
Hello there, as a way to study embedded programming I decided to perform various tasks on the board, including firing an interrupt on button pressing to light up the 3 user LEDs.
I got stuck, the interrupt always fires.
In the beginning I tried debugging it myself, using the reference manual, user manual, arm architecture reference manual and the schematic, in the end I got desperate and tried debugging with chatgpt.
As a way of research, I didn't use HAL and wrote it pretty much myself.
The LEDs light up alright on their own, it's just the interrupt that always fires, maybe because I use the debug option and the step in and step over?
Thank you.
Here is the code:
#define RCC_AHB1ENR1 (int*)(0x40023830)
#define RCC_BASE ((int*)(0x40023800))
#define EXTI_BASE ((int*)(0x40013C00))
#define SYSCFG_BASE ((int*)(0x40013800))
#define GPIOB_BASE ((int*)(0x40020400))
#define GPIOC_BASE ((int*)(0x40020800))
#define NVIC_ISER1 ((int*)(0xE000E104))
typedef struct {
unsigned int RCC_CR;
unsigned int RCC_PLLCFGR;
unsigned int RCC_CFGR;
unsigned int RCC_CIR;
unsigned int RCC_AHB1RSTR;
unsigned int RCC_AHB2RSTR;
unsigned int RCC_AHB3RSTR;
unsigned int RES1;
unsigned int RCC_APB1RSTR;
unsigned int RCC_APB2RSTR;
unsigned int RES2;
unsigned int RES3;
unsigned int RCC_AHB1ENR;
unsigned int RCC_AHB2ENR;
unsigned int RCC_AHB3ENR;
unsigned int RES4;
unsigned int RCC_APB1ENR;
unsigned int RCC_APB2ENR;
unsigned int RES5;
unsigned int RES6;
unsigned int RCC_AHB1LPENR;
unsigned int RCC_AHB2LPENR;
unsigned int RCC_AHB3LPENR;
unsigned int RES7;
unsigned int RCC_APB1LPENR;
unsigned int RCC_APB2LPENR;
unsigned int RES8;
unsigned int RES9;
unsigned int RCC_BDCR;
unsigned int RCC_CSR;
unsigned int RES10;
unsigned int RES11;
unsigned int RCC_SSCGR;
unsigned int RCC_PLLI2SCFGR;
unsigned int RCC_PLLSAICFGR;
unsigned int RCC_DCKCFGR1;
unsigned int DCKCFGR2;
} RCC;
typedef struct {
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
volatile unsigned int LCKR;
volatile unsigned int AFRL;
volatile unsigned int AFRH;
} UGPIO;
typedef struct {
volatile unsigned int EXTI_IMR;
volatile unsigned int EXTI_EMR;
volatile unsigned int EXTI_RTSR;
volatile unsigned int EXTI_FTSR;
volatile unsigned int EXTI_SWIER;
volatile unsigned int EXTI_PR;
} EXTI;
typedef struct {
volatile unsigned int SYSCFG_MEMRMP;
volatile unsigned int SYSCFG_PMC;
volatile unsigned int SYSCFG_EXTICR1;
volatile unsigned int SYSCFG_EXTICR2;
volatile unsigned int SYSCFG_EXTICR3;
volatile unsigned int SYSCFG_EXTICR4;
volatile unsigned int SYSCFG_CMPCR;
} SYSCFG;
RCC* rcc = (RCC*)RCC_BASE;
EXTI* exti = (EXTI*)EXTI_BASE;
SYSCFG* syscfg = (SYSCFG*)SYSCFG_BASE;
UGPIO* gpiob = (UGPIO*)GPIOB_BASE;
UGPIO* gpioc = (UGPIO*)GPIOC_BASE;
void EXTI15_10_IRQHandler(){
if (exti->EXTI_PR & (1 << 13))
{
exti->EXTI_PR = (1 << 13); // clear pending
gpiob->BSRR |= (1 << 7);
gpiob->BSRR |= (1 << 14);
gpiob->BSRR |= 1;
}
}
int main(void)
{
//PC13 -> EXTI15
rcc->RCC_AHB1ENR |= (1 << 1); //GPIOB CLOCK GATING
rcc->RCC_AHB1ENR |= (1 << 2); //GPIOC CLOCK GATING
rcc->RCC_APB2ENR |= (1 << 14); //SYSCFG CLOCK GATING
gpiob->MODER &= ~(3 << 14); // clear
gpiob->MODER |= (1 << 14); // set
gpiob->MODER &= ~(3 << 28); // clear
gpiob->MODER |= (1 << 28); // set
gpiob->MODER &= ~2; // clear
gpiob->MODER |= 1; // set
//-------
gpioc->MODER &= ~(3 << (13 * 2));
// 0000 0000 0000 0000 0000 0000 0000 0011
// 0000 1100 0000 0000 0000 0000 0000 0000
// NOT = 1111 0011 1111 1111 1111 1111 1111 1111
gpioc->PUPDR &= ~(3 << (13 * 2));
//gpioc->PUPDR |= (1 << (13 * 2));
//gpioc->PUPDR |= (2 << (13*2));
syscfg->SYSCFG_EXTICR4 &= ~(0xF << 12); // clear
syscfg->SYSCFG_EXTICR4 |= (0x2 << 12); // port C
//gpioc->PUPDR |= (1 << (15 * 2)); // pull-up
exti->EXTI_IMR |= (1 << 13);
exti->EXTI_FTSR |= (1 << 13);
exti->EXTI_RTSR &= ~(1 << 13);
exti->EXTI_PR = (1 << 13);
*NVIC_ISER1 |= (1 << 8);
for (int i = 0; i < 10; i++)
{
int a = gpioc->IDR & (1 << 15);
int x = 1;
int y=2;
}
//40 47 settable EXTI15_10 EXTI Line[15:10] interrupts 0x0000 00E0
/* Loop forever */
for(;;)
{
//gpiob->BSRR |= (1 << 23);
//gpiob->BSRR &= ~(1 << 7);
//for(int i =0; i < 1000000; i++);
}
}
2026-06-02 10:36 AM
Welcome to the forum
Please see How to write your question to maximize your chances to find a solution for best results.
@Sagiv_Alia wrote:I decided to perform various tasks on the board,
You haven't told us what board that is.
Before venturing into interrupts, I would suggest that you first get polling working.
@Sagiv_Alia wrote:As a way of research, I didn't use HAL and wrote it pretty much myself.
Have you tried using one of the provided ST examples?
That would give you a known-good reference ...
We’re moving the ST Community to a new platform to give you a better and more reliable community experience.