STM32 External Interrupts

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

BlackNecro
Posts: 2
Joined: Mon Dec 13, 2010 5:49 pm

STM32 External Interrupts

Post by BlackNecro »

Hi there
I'm having some problems trying to get external interrupts and interrupt handlers to work.
I have to admit I got no idea where to start, tried including stm32lib and using the Exti functionset this half worked ending up in probably unassigned interrupt vectors.
Anyone able to give me a small example on how to implement a simple external interrupt on rising/falling edge?
Thanks in advance
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 External Interrupts

Post by Giovanni »

Hi,

Some generic suggestions:
- Make sure to use the appropriate OS macros for declaring the ISR (there is an article in the documentation about this).
- Make sure to give the ISR the correct name or it will not be linked to the vectors table the vector names are declared in hal_lld_fxx.h.
- Enable the IRQ using NVICEnableVector, this function also assigns the priority.
- Make sure to give the IRQ a priority not greater than CORTEX_BASEPRI_KERNEL (an higher number is a lower priority).
- Make sure to reset the IRQ source before exiting the ISR or the interrupt will be re-triggered immediately.

If you could post your code it would be easier to give more specific suggestions.

Giovanni
BlackNecro
Posts: 2
Joined: Mon Dec 13, 2010 5:49 pm

Re: STM32 External Interrupts

Post by BlackNecro »

So dug a bit through the platform files and found the EXTI definitions, which is what I'm still missing.
Is there any documentation on how to setup the EXTI Registers using your macros?
Thanks.

EDIT:
Well solved it by just writing the EXTI Registers using the EXTI TypeDef.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 External Interrupts

Post by Giovanni »

What you did is correct, EXTI is a platform-specific feature so it is correct to handle it using vendor libraries or by accessing directly the registers.

Giovanni
joaquin
Posts: 50
Joined: Sat Jan 22, 2011 8:44 pm
Has thanked: 1 time
Been thanked: 3 times

Re: STM32 External Interrupts

Post by joaquin »

I have used external interrupts on a infrared receiver. I recommend reading STM32 Reference manual, chapters on EXTI, and AFIO registers.
What I did was:
- Some definitions
#define RC5_EXTI_LINE ((uint32_t)0x04000) /*!< External interrupt line 14 */
- Activation of interrupt. In my case PB14, that goes to interrupt line EXTI14
void extLinIntInit(void) {
EXTI->IMR = EXTI_IMR_MR14;
EXTI->EMR = EXTI_EMR_MR14;
EXTI->RTSR = EXTI_RTSR_TR14; // interrupt on rising
EXTI->FTSR = EXTI_FTSR_TR14; // and descending
AFIO->EXTICR[3] |= (1<<8); // Assign AFIO to line EXTI14 -> PB14
NVICEnableVector(EXTI15_10_IRQn,STM32_EXTLINES_PRIORITY);
}
- Interrupt handler
CH_IRQ_HANDLER(ExtLineVector) {
CH_IRQ_PROLOGUE();
if (EXTI->PR == RC5_EXTI_LINE) // check activation of PB14
{
codeToExecuteOnInterrupt();
EXTI->PR = RC5_EXTI_LINE; // reset interrupt
}
CH_IRQ_EPILOGUE();
}
- Substitute interrupt vector on vectors_md.s (PORTS/GCC/ARMCMx/STM32). Wich vector file depends on your CPU (mine is an Olimex STM32-H103)
.word Tim2Vector // VectorB0

I hope I don't have forgotten some code (I have had to browsed my code).
asfarley
Posts: 8
Joined: Thu Dec 13, 2012 7:19 pm

Re: STM32 External Interrupts

Post by asfarley »

Hi,

In this line:
NVICEnableVector(EXTI15_10_IRQn,STM32_EXTLINES_PRIORITY);

Why is it EXT15 instead of EXT14?
What does the 10 mean?
Where is STM32_EXTLINES_PRIORITY defined?

When creating the interrupt handler CH_IRQ_HANDLER(ExtLineVector), where does ExtLineVector come from? I mean, where is this name defined? Does the name CH_IRQ_HANDLER ever need to be changed?

When compiling, I get AFIO undefined and STM32_EXTLINES_PRIORITY undefined.

I've read the STM32 ref manual but I'm a bit confused about which parts are ChibiOS vs STM32-specific.

Thanks,

Alexander
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 External Interrupts

Post by Giovanni »

Hi,

EXTI15_10_IRQn comes from the ST header stm32f10x.h, it is a shared interrupt handler for all EXTI lines from 10 to 15. Not sure why you ask for EXTI14.

About STM32_EXTLINES_PRIORITY, I am unable to find it, where did you see it? please specify file name and OS version.

ExtLineVector is a vector name as defined in ./os/ports/...../vectors.c, you need to consult the RM of your device for a list of vectors. hal_lld.h contains definitions for readable vector names which can be used as parameters for the CH_IRQ_HANDLER() macro.

Giovanni
asfarley
Posts: 8
Joined: Thu Dec 13, 2012 7:19 pm

Re: STM32 External Interrupts

Post by asfarley »

Thanks,

I was looking at Joaquin's post above, I've realized that STM32_EXTLINES_PRIORITY is probably defined by him in his code.

Are the alternate function mapping registers defined by ChibiOS anywhere? I get AFIO undefined.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 External Interrupts

Post by Giovanni »

AFIO is specific of F1 devices, are you using an F1?

Giovanni
asfarley
Posts: 8
Joined: Thu Dec 13, 2012 7:19 pm

Re: STM32 External Interrupts

Post by asfarley »

No, I'm using an F415 - does it have an equivalent register?

I've tried this since SYSCFG_EXTICR3 appears in the STM32F4 reference manual, but it appears that SYSCFG_EXTICR3 is also undefined:
SYSCFG_EXTICR3 = (1<<9);

Looking at stm32f4xx.h, I see in the section *CHIBIOS FIX* a definition for AFR[] but it's disabled by an #if 0 directive.

I hate to be asking for so much hand-holding, but I'm not sure which set of documents I need to read to understand the interrupt configuration. I've looked at the STM32 reference manual, what other documents should I be checking?

Just to clarify, I'm trying to enable external interrupts on pin PC10. Thank you,

Alexander
Post Reply