I will attempt to answer this fairly vague question by listing the high level steps to set up a timer interrupt, to help point you in the right direction.
- Turn on the timer peripheral by enabling it's clock. This is done via the RCC subsystem. You will find this kind of register manipulation in any STM32 code example. If you are going to use the timer for e.g. PWM waveform generation, or to measure external signals, you will also need to enable the clock for the GPIO port you are using. I assume you are past the general "blinking LED" stage and will not go into more detail about GPIO ports :)
- Configure the timer peripheral to generate interrupts in response to specific timer events. This is not covered in the NVIC section of the manual, but in the section about timers. For the STM32F103 and probably other STM32 chips, the register is called
DIER. You will likely want to set theUIEbit in this register, however don't take my word for it. You should read the whole section of the manual to understand how the timer works and how to set it up. - Enable the correct
IRQin theNVIC. This hardware (the NVIC) is not specific to STM32, but is common to all ARM microcontrollers. Each interrupt line on the chip (there may be one dedicated to the timer you are using, or it might be shared by several peripherals) is referred to by an integer. I usually look in the device header file for this information, and I recommend getting comfortable with looking through the vendor code for answers about the chip you're working with. - Implement your interrupt handler function, often referred to as an ISR (Interrupt Service Routine). In C code, you declare a function which is already named elsewhere in the vendor's initialization code. When you declare a function with that name, the compiler/linker will know to place the address of that function into a known location (the vector table, another good thing to look up). This is the part of the code where you will toggle your LED. It is important to note that each peripheral that can generate an interrupt has a status bit that your ISR should clear, to acknowledge that the ISR has been executed. This becomes important when you have an IRQ shared by multiple peripherals, or if you want to handle more than one event generated by one peripheral. For example, if you enable the "timer update" interrupt via the
UIEbit, you will need to then clear the correspondingUIFbit in the status register. This is the only way the peripheral knows that the interrupt has been handled. If you don't do this, the chip will lock up because it is continuously entering the interrupt handler.
Other general advice: the book "The Definitive Guide To ARM Cortex-M3 and Cortex-M4 Processors" helped me understand a lot of this stuff, but not as much as closely reading the chip manual and vendor-provided code.
Answer from jrsa on Stack Exchangeembedded - STM32F103 blue pill interrupts from scratch - Stack Overflow
[SOLVED] setting up timer interrupt on blue pill without a library - Arduino for STM32
what timer/interupt library to match
c - Timer 1 on STM32 not outputting onto GPIO Pin (Bluepill) - Stack Overflow
Videos
I wrote a Bare metal code to generate interrupt (toggle on-Board LED) whenever external button is pressed. when i run the code, no toggling if i press the button. But, If I just touch the reset button (making contact), the LED Toggles. This is kinda weird. Is this due to electrical issue, cause i enabled pull-up/pull down mode in Registers but still get this error. Do anyone have any idea to solve this issue??
I use STM32F103C8T6. I think there is no problem with board, cause other code works. and Note: i think it's a clone board because i can't run in debug mode even with stlink-V2. and is that have anything to do with the issue?
Edit: Sorry for delay guys, as suggested I tried using HAL library and the code worked. So, problem was with my code. And i also share the link to my code:
For main function: https://github.com/ggurusaran/EMbedded_code/blob/main/stm32f103xx_drivers/Src/E01_ledtoggle.c
Drivers->Inc contains the header files i created for this code and Drivers->Src contains function prototypes. Sorry, if path is confusing, im quite new in using GitHub. thanks
