Skip to main content
OHaza.1
Associate III
January 12, 2022
Question

HAL_GPIO_EXTI_Rising_Callback or EXTI0_1_IRQHandler

  • January 12, 2022
  • 2 replies
  • 2982 views

Hi, is there a reason to implement the HAL function over simply running code inside the IRQ handler.

In my current case I only am incrementing a counter and do not need to demux the line, so I presume not this time, but if I was doing something more involved.

Thanks Oliver

This topic has been closed for replies.

2 replies

TDK
January 12, 2022

Convenience.

If you have multiple interrupts, HAL will call HAL_GPIO_EXTI_Rising_Callback for all of them and you can locate all of your code in the same place. Whereas if you put your code in the actual IRQ handler, you would need to duplicate code in each handler.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Piranha
Principal III
January 13, 2022

Merging together all of the EXTI lines in a single function is not a convenience, but a problem because in that common callback function one has to sort out all of them and call the next level handlers from other software modules anyway.

I've found this approach to provide a good balance of convenience and performance:

void EXTI3_IRQHandler(void)
{
	if (EXTI->PR1 & EXTI_PR1_PIF3) {
		EXTI->PR1 = EXTI_PR1_PIF3;
 
		EXTIHandler_BTNx(BSP_BTN_LEFT);
	}
}
 
void EXTI9_5_IRQHandler(void)
{
	uint32_t rPR1 = EXTI->PR1;
	EXTI->PR1 = rPR1 & (EXTI_PR1_PIF6 | EXTI_PR1_PIF5);
 
	if (rPR1 & EXTI_PR1_PIF6) {
		EXTIHandler_MOTOR_DIAG();
	}
	if (rPR1 & EXTI_PR1_PIF5) {
		EXTIHandler_BTNx(BSP_BTN_RIGHT);
	}
}

Put this code in a BSP for the specific board and call from it the functionally specific handlers from other software modules. The "inconvenient and excess" code is just the lines 4 and 12-13, but those are trivial.

Tesla DeLorean
Guru
January 13, 2022

Looks like it has the potential to clear and miss an interrupt source

This might be safer

EXTI->PR1 = (EXTI_PR1_PIF6 | EXTI_PR1_PIF5) & rPR1;

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
January 12, 2022

Cube/HAL will check and clear the interrupt source for you.

JW

Tesla DeLorean
Guru
January 13, 2022

But that's not exactly difficult, and doing all the processing in the IRQ Handler is probably clearer and more transparent. And can be limited to the actual sources rather than the kitchen sink approach HAL/Cube example frequently employ.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
OHaza.1
OHaza.1Author
Associate III
January 13, 2022

Right, I figure ideally it's best to do the functionality manually, i've gone with HAL_GPIO_EXTI_Rising_Callback in the end, kind of need rapid and reliable right now, although this means I now have to demux in the function whereas in the IRQ handler I wouldn't have to as the two interrupts would have their own.. somewhat unfortunate side effect of HAL.