STM32 External Interrupts
Moderators: RoccoMarco, lbednarz, tfAteba
-
BlackNecro
- Posts: 2
- Joined: Mon Dec 13, 2010 5:49 pm
STM32 External Interrupts
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
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
- 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
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
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
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.
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.
- 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
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
Giovanni
Re: STM32 External Interrupts
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).
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).
Re: STM32 External Interrupts
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
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
- 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
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
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
Re: STM32 External Interrupts
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.
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.
- 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
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
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