'How to set up interrupt registers on the stm32 microcontrollers?
I wanted to toggle a led status by reading a push button status's using interrupt instead of polling. I'm using a Nucleo board F411 and a register based programming instead of that no sense HAL libraries(lol I'm just joking)
After reading the reference manual and the programming manual for the corresponding micro controller I thought that this code would work fine for the purpose of implementing interrupts...turns out it was not the case. I took at look at the debugging session to see how the corresponding registers changes and it does exactly as I need (the NVIC->ISERx, the priority bits, the unmasking bits, the rising or falling edge trigger, etc) so I actually have no idea what is wrong with my way of thinking about this problem.
By the way, I'm using a custom made (for me) class in order to easily set up any GPIO, I've tried this separately and I can say that the problem is not related to my library. You may think of this as just a configuration function for the GPIO as system_clock_config might be.
The thing is, whenever I push the button, nothing happens.
I get no compilation errors nor warnings.
Here is the code:
#include <main.h>
#include <GPIO_PORT.h>
#include <config.h>
#include <stm32f4xx.h>
#include <core_cm4.h>
/*GPIO_PORT is a custom made class that allows me to easily set any GPIO port as well
*as changing its attributes like pull up or pull down input mode, push-pull or open
*drain output, change its mode (input or output), read and write to it, etc
*/
#define IO GPIO_PORT
/*interrupt handler function for external interrupts from lines 10 to 15.
* It's name goes according to the ones in the vector table generated in the startup.s file
*/
void EXTI15_10_IRQHandler (void){
EXTI->PR = (1 << 13); //clears the pending interrupt from line 13. this is done by writing a 1 to the register.
GPIOA->ODR ^= (1 << 5); //toggle PORTA5 output (this is where the LED is connected to)
}
int main(void){
/*system_clock_settings():
* function that selects whether to use internal or external oscillator as clock source
*/
system_clock_settings('I');
/*HAL_Init():
* I wanted to use a little delay
*/
HAL_Init();
IO led('A', 5 , 0); //creates an IO class object which resembles a LED connected to PORTA0(output GPIO)
IO boton1('C', 13, 1); //creates an IO class object which resembles a push button connected to PORTC13
boton1.pupd_selection(-1); //method of the class IO that selects no pull up neither pull down mode for this input.
RCC->APB2ENR |= (1<<14); //enables system configuration controller.
SYSCFG->EXTICR[3] = (1<<5); //bits [4:7] 0010 selects PORTC13 as EXTI source for line 13.
EXTI->IMR |= (1<<13); //unmasking line 13 for the corresponding interrupt to be able to be triggered.
EXTI->RTSR |= (1<<13); //rising edge triggers interrupt on line 13.
NVIC_SetPriority(EXTI15_10_IRQn, 1); //sets the priority for the EXTI15_10 lines of EXTI
NVIC_EnableIRQ(EXTI15_10_IRQn); //enables external interrupts coming from line 13
while(1){
}
}
Solution 1:[1]
Add :
NVIC_SetVector(EXTI15_10_IRQn, (uint32_t)&EXTI15_10_IRQHandler);
before your NVIC_Enable.
Best of luck to you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sympathic_Cone |