Skip to main content
waaizkhan
Associate II
December 19, 2019
Question

how to use extern "C"

  • December 19, 2019
  • 4 replies
  • 3123 views

hi everyone want to use interrupt in main.c i am using extern "C" for this purpose but its couldn't compile

/* USER CODE BEGIN 4 */

extern "C" void TIM2_IRQHandler(void) {

// Handle update interrupts (update interrupt flag is set)

if (TIM2->SR & TIM_SR_UIF) {

// Reset the update interrupt flag

TIM2->SR &= ~(TIM_SR_UIF);

// Toggle the LED

HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);

Flag_Angle = 1;

}

}

Error: L6200E: Symbol TIM2_IRQHandler multiply defined (by stm32f4xx_it.o and main.o).

also infinite while is executed just one time .

This topic has been closed for replies.

4 replies

Tesla DeLorean
Guru
December 19, 2019

Don't think the issue is related to extern "C" usage.

The linker is complaining that you have two copies of the IRQ Handler's body function, both in stm32f4xx_it.c and main.c decide which one you want, and comment out the other.

Don't write TIM->SR with a RMW instruction, just WRITE the bit mask to clear the interrupt sources. See Reference Manual, read specific functionality of the register described there.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waaizkhan
waaizkhanAuthor
Associate II
December 19, 2019

thanks a lot can i use IRQ Handler in main.c instead of stm32f4xx_it.c if i comment it in stm32f4xx_it.c is it a right approach and why while infinite loop execute just one time .

Tesla DeLorean
Guru
December 19, 2019

You can put it in whatever source file you choose.

>>.. why while infinite loop execute just one time .

No idea what you're talking about, lack any real context, see no code presented with a while() loop

Code lacking appropriate handlers may dump into Default_Handler(), HardFault_Handler(), or Error_Handler()

If the interrupt rate is too high (say >400 KHz) the system may saturate and continually reenter the IRQHandler, and prevent foreground execution.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
turboscrew
Senior III
December 19, 2019

"extern C" is for C++. It tells the compiler that the function is extern and C instead of C++ (calling convention and name mangling).