cancel
Showing results for 
Search instead for 
Did you mean: 

Help to add EXTI to a port already in use as ETR input for counter/timer (PA0).

PSolw.1
Associate II

I'm struggling to find a solution to this by searching.

I have an existing system running using PA1 as an EXTI set up through Cube MX. I just need to add an exrernal interrupt to PA0

As far as I can see the following code is all I should need to add:

HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0); // IRQ Line0 for new IRQ (Port PA0)
HAL_NVIC_EnableIRQ(EXTI0_IRQn);  //Enable IRQ
EXTI->RTSR |= 1; // Enable Rising IRQ Line 0
EXTI->IMR |= 1; //Unmask IRQ Line 0.

  My existing callback should suffice for the new IRQ but it doesn't get triggered because the HAL_GPIO_EXTI_IRQHandler() in stm32f1xx_hal_gpio.c doen't run

Could someone let me know what I'm missing?

9 REPLIES 9
TDK
Guru

That should call EXTI0_IRQHandler. You'll need to call HAL_GPIO_EXTI_IRQHandler from within there.

If you feel a post has answered your question, please click "Accept as Solution".

Also, is PA selected for EXTI0 in SYSCFG?

JW

Thanks for that. I see that I am missing the EXTI0_IRQHandler. Should I create this function in stm32f1xx_it.c/h files next to its brother EXTI1_IRQHandler which was generated by MX, or should I put it in main.c or similar? I worry that it may be overwritten or removed by future use of MX.

PSolw.1
Associate II

Thanks Jan.

I don't know what SYSCFG is. There doesn't seem to be a file of that name in my project...

TDK
Guru

> Should I create this function in stm32f1xx_it.c/h files next to its brother EXTI1_IRQHandler which was generated by MX, or should I put it in main.c or similar? I worry that it may be overwritten or removed by future use of MX.

CubeMX should generate this for you in stm32f1xx_it.c, if you've enabled the right checkboxes. If not, you can put it and its declaration somewhere within a USER CODE section. Those should be preserved on code regeneration.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for that. As far as I can see it is not possible to just add an EXTI to an existing input using MX. In this case the pin is already used as ETR for a counter timer. If I use MX I think it will force the pin to be a simple input/rising edge EXTI OR an ETR. (I'd love to be wrong.)

CubeMX is limiting in this regard, this is often seen here. Any "library" and even more a clicky interface inevitably implement only a fraction of possible functionality of the mcu, the "most usual" functions. If you want anything less usual, you have to program.

Also, you should've been more explicit in saying which family are you using, in the initial post. In the 'F1, this detail is different from all other families, instead of SYSCFG the EXTI selection is in AFIO.

0693W000001pTRIQA2.png

JW

PSolw.1
Associate II

Help to add EXTI to a port already in use as ETR input for counter/timer (PA0). CubeIDE on STM31F103

Thanks to waclawek.jan and TDK for your help with this.

The crucial bits of code that worked for me are here.

  // Initialise Ext IRQ on PA0 (which is currently in use as external trigger for something else.
 	HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0); // IRQ Line0 for clock start IRQ (Port PA0)
	HAL_NVIC_EnableIRQ(EXTI0_IRQn);		//		
 	EXTI->RTSR |= 1; 			// Enable Rising IRQ Line 0
	EXTI->IMR |= 1;  			// Unmask IRQ Line 0.
	/* AFIO_EXTICR[1-4] IRQ Config Registers need to know GPIO Port (A,B,C etc) and Pin.
		No action required here because Reset value just happens to be correct for PA0 on LINE0 */
 
 
// This is my callback routine
 
void EXTI0_IRQHandler(void)
{
	__HAL_GPIO_EXTI_CLEAR_IT(1);  	// "1"  (Bit0) is the position in the register for Line0
	debug("\n IRQ Detected!\n");	// (debug is my routine to write to a serial port)
	// Your code here!
}

I realise all this is very untidy and hope someone will tidy it up for posterity. Running the callback through the same path as the MX geneated interrupts would be more consistent but this way leaves the MX generated include files unaltered

> /* AFIO_EXTICR[1-4] IRQ Config Registers need to know GPIO Port (A,B,C etc) and Pin.

> No action required here because Reset value just happens to be correct for PA0 on LINE0 */

Heh, indeed, I didn't realize that... 🙂

JW