Page 1 of 1

STM32f103xx External Interrupt using register - not working

Posted: Fri Mar 24, 2023 6:16 pm
by Phoenix
Hi everyone,

I am using STM32F103C8 blue pill board with arduino ide - stm32duino official core.

I am trying to configure the external interrupt on pin PB4, according to the datasheet.
However, seems like i have missed something or it doesn't work at all.

This is the piece of code I am using for testing purposes.

Code: Select all

void setup() {
  pinMode(PB4, INPUT_PULLDOWN);
  RCC->APB2ENR |= (1 << 0);
  AFIO->EXTICR[2] |= (1 << 0);
  EXTI->IMR |= (1 << 4);
  EXTI->RTSR |= (1 << 4);
  EXTI->FTSR &= ~(1 << 4);
  NVIC_SetPriority(EXTI4_IRQn, 0);
  NVIC_EnableIRQ(EXTI4_IRQn);
}

Code: Select all

void loop() {
  // put your main code here, to run repeatedly:
}

Code: Select all

void EXTI4_IRQHandler(void) {
  Serial.println("Trigger_Signal");
}
if i use

Code: Select all

attachInterrupt(PB4, EXTI4_IRQHandler, RISING);
, everything works but I can't get it to work through register manipulation.
Any Idea on how to do so?

Regards,

Re: STM32f103xx External Interrupt using register - not working

Posted: Fri Mar 24, 2023 11:11 pm
by dannyf
you need to unmask the line?

Re: STM32f103xx External Interrupt using register - not working

Posted: Sat Mar 25, 2023 3:02 am
by Phoenix
dannyf wrote: Fri Mar 24, 2023 11:11 pm you need to unmask the line?
This line:

Code: Select all

EXTI->IMR |= (1 << 4);
does the unmasking.

Re: STM32f103xx External Interrupt using register - not working

Posted: Sat Mar 25, 2023 6:46 am
by fpiSTM
EXTI4_IRQHandler is alread defined by the core interrupt part
https://github.com/stm32duino/Arduino_C ... t.cpp#L371

So if you want use it you have to disabled it by defining: HAL_EXTI_MODULE_DISABLED

https://github.com/stm32duino/Arduino_C ... figuration

You can use build_opt.h feature to do that:
https://github.com/stm32duino/Arduino_C ... uild_opt.h
with

Code: Select all

-DHAL_EXTI_MODULE_DISABLED
Finally, you have to declare the handler as extern "C":

Code: Select all

extern "C" void EXTI4_IRQHandler(void) {

Re: STM32f103xx External Interrupt using register - not working

Posted: Sat Mar 25, 2023 9:35 am
by Phoenix
fpiSTM wrote: Sat Mar 25, 2023 6:46 am EXTI4_IRQHandler is alread defined by the core interrupt part
https://github.com/stm32duino/Arduino_C ... t.cpp#L371

So if you want use it you have to disabled it by defining: HAL_EXTI_MODULE_DISABLED

https://github.com/stm32duino/Arduino_C ... figuration

You can use build_opt.h feature to do that:
https://github.com/stm32duino/Arduino_C ... uild_opt.h
with

Code: Select all

-DHAL_EXTI_MODULE_DISABLED
Finally, you have to declare the handler as extern "C":

Code: Select all

extern "C" void EXTI4_IRQHandler(void) {
Well, when i compile the sketch with extern "C", i get the following error:

Code: Select all

exit status 1

Compilation error: conflicting declaration of 'void EXTI4_IRQHandler()' with 'C' linkage

Re: STM32f103xx External Interrupt using register - not working

Posted: Sat Mar 25, 2023 9:39 am
by fpiSTM
Maybe a cache issue linked to arduino IDE. I got several with new version 2.x

Re: STM32f103xx External Interrupt using register - not working

Posted: Sat Mar 25, 2023 10:10 am
by Phoenix
fpiSTM wrote: Sat Mar 25, 2023 9:39 am Maybe a cache issue linked to arduino IDE. I got several with new version 2.x
So, how do we slove this issue?
What is the correct way of using external interrupt through register manipulation?
And how to use the build_opt.h file correctly?
Do I create header file called "build_opt" because arduino ide only accepts .h, .c, .cpp, .ino.
and then put this line in it

Code: Select all

-DHAL_EXTI_MODULE_DISABLED
and then drop that in the sketechbook folder directory which is located on C:\Users\"PC-USER"\Documents\Arduino

Am I configuring the registers correctly?

Re: STM32f103xx External Interrupt using register - not working

Posted: Sat Mar 25, 2023 10:51 am
by fpiSTM
Simply create build_opt.h file and add -D..... inside. About register Ididn't check.