Library 38- External interrupts for STM32F4
A new library is here. 38th are external interrupts. This library allows you to very easly use external interrupts for your needs with just one function and function handler. I have written everything you need to start.
For more informations on how external interrupts works on STM32F4 with pins and lines and so on, you should look at my external interrupts tutorial. I tried explain there everything as best as I can. I think it’s quite understandable.
I will just say here, that you can connect only one pin to one line at a time. For more, look at tutorial.
So, my library is below.
Library
Features
- Attach external interrupt on specific pin
- Detach external interrupt on specific pin
- Selectable trigger for interrupt
- Rising edge
- Falling edge
- Both edges
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- STM32F4xx EXTI
- STM32F4xx SYSCFG
- misc.h
- TM
- TM GPIO
- defines.h
- attributes.h
- TM GPIO
EXTI LINE | GPIOA | GPIOB | GPIOC | GPIOD | GPIOE | GPIOF | GPIOG | GPIOH | GPIOI | GPIOJ | GPIOK |
---|---|---|---|---|---|---|---|---|---|---|---|
Line 0 | PA0 | PB0 | PC0 | PD0 | PE0 | PF0 | PG0 | PH0 | PI0 | PJ0 | PK0 |
Line 1 | PA1 | PB1 | PC1 | PD1 | PE1 | PF1 | PG1 | PH1 | PI1 | PJ1 | PK1 |
Line 2 | PA2 | PB2 | PC2 | PD2 | PE2 | PF2 | PG2 | PH2 | PI2 | PJ2 | PK2 |
Line 3 | PA3 | PB3 | PC3 | PD3 | PE3 | PF3 | PG3 | PH3 | PI3 | PJ3 | PK3 |
Line 4 | PA4 | PB4 | PC4 | PD4 | PE4 | PF4 | PG4 | PH4 | PI4 | PJ4 | PK4 |
Line 5 | PA5 | PB5 | PC5 | PD5 | PE5 | PF5 | PG5 | PH5 | PI5 | PJ5 | PK5 |
Line 6 | PA6 | PB6 | PC6 | PD6 | PE6 | PF6 | PG6 | PH6 | PI6 | PJ6 | PK6 |
Line 7 | PA7 | PB7 | PC7 | PD7 | PE7 | PF7 | PG7 | PH7 | PI7 | PJ7 | PK7 |
Line 8 | PA8 | PB8 | PC8 | PD8 | PE8 | PF8 | PG8 | PH8 | PI8 | PJ8 | PK8 |
Line 9 | PA9 | PB9 | PC9 | PD9 | PE9 | PF9 | PG9 | PH9 | PI9 | PJ9 | PK9 |
Line 10 | PA10 | PB10 | PC10 | PD10 | PE10 | PF10 | PG10 | PH10 | PI10 | PJ10 | PK10 |
Line 11 | PA11 | PB11 | PC11 | PD11 | PE11 | PF11 | PG11 | PH11 | PI11 | PJ11 | PK11 |
Line 12 | PA12 | PB12 | PC12 | PD12 | PE12 | PF12 | PG12 | PH12 | PI12 | PJ12 | PK12 |
Line 13 | PA13 | PB13 | PC13 | PD13 | PE13 | PF13 | PG13 | PH13 | PI13 | PJ13 | PK13 |
Line 14 | PA14 | PB14 | PC14 | PD14 | PE14 | PF14 | PG14 | PH14 | PI14 | PJ14 | PK14 |
Line 15 | PA15 | PB15 | PC15 | PD15 | PE15 | PF15 | PG15 | PH15 | PI15 | PJ15 | PK15 |
Yeah, you are right. There are many GPIOxs available on STM32F4 devices. So, this table describes you, which pins are used for specific line. So let’s say Line 11 has pins PA11 to PK11.
I had to implement default interrupt handlers if I want to use interrupts. So 7 functions with default names in my library. If you need any of this functions (names below) then you can disable this functionality in library by adding some defines to defines.h file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Disable EXTI0_IRQHandler function #define TM_EXTI_DISABLE_DEFAULT_HANDLER_0 //Disable EXTI1_IRQHandler function #define TM_EXTI_DISABLE_DEFAULT_HANDLER_1 //Disable EXTI2_IRQHandler function #define TM_EXTI_DISABLE_DEFAULT_HANDLER_2 //Disable EXTI3_IRQHandler function #define TM_EXTI_DISABLE_DEFAULT_HANDLER_3 //Disable EXTI4_IRQHandler function #define TM_EXTI_DISABLE_DEFAULT_HANDLER_4 //Disable EXTI9_5_IRQHandler function #define TM_EXTI_DISABLE_DEFAULT_HANDLER_9_5 //Disable EXTI15_10_IRQHandler function #define TM_EXTI_DISABLE_DEFAULT_HANDLER_15_10 |
By default, external interrupts have priority in NVIC set to 0x0A. If you want higher priority, add line below in defines.h file and edit it. Lower number means higher priority. Sub priorities are set like pins. Px0 has highest priority, Px15 lowest.
1 2 |
//Change external interrupts main priority for NVIC #define EXTI_NVIC_PRIORITY 0x0A |
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/** * Result enumeration * * Parameters: * - TM_EXTI_Result_Ok: * Everything ok * - TM_EXTI_Result_Error: * An error has occured */ typedef enum { TM_EXTI_Result_Ok = 0, TM_EXTI_Result_Error } TM_EXTI_Result_t; /** * Interrupt trigger enumeration * * Parameters: * - TM_EXTI_Trigger_Rising: * Trigger interrupt on rising edge on line * - TM_EXTI_Trigger_Falling: * Trigger interrupt on falling edge on line * - TM_EXTI_Trigger_Rising_Falling: * Trigger interrupt on any edge on line */ typedef enum { TM_EXTI_Trigger_Rising = 0x08, TM_EXTI_Trigger_Falling = 0x0C, TM_EXTI_Trigger_Rising_Falling = 0x10 } TM_EXTI_Trigger_t; /** * Attach interrupt to specific pin. * This function automatically enables the clock for GPIO peripheral and also sets pull resistors * depending on trigger you use. * - Falling edge: pull up is enabled * - Rising edge: pull down is enabled * - Any edge: no pull activated * * Also, you can attach only one GPIOx to specific GPIO_Pin. * In other words, GPIO_Pin_5 can not be attached to GPIOA and GPIOB at the same time. * * Parameters: * - GPIO_TypeDef* GPIOx: * GPIOx to be used. Valid x is between A and K * - uint16_t GPIO_Pin: * GPIO pin in use. Valid GPIO is GPIO_Pin_0 to GPIO_Pin_15 * - TM_EXTI_Trigger_t trigger: * Trigger for interrupt * TM_EXTI_Trigger_t member * * Returns OK if attached OK or error if previous gio with same number was attached */ extern TM_EXTI_Result_t TM_EXTI_Attach(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_EXTI_Trigger_t trigger); /** * Detach gpio pin from interrupt lines * * Parameters: * - uint16_t GPIO_Pin: * GPIO pin in use. Valid GPIO is GPIO_Pin_0 to GPIO_Pin_15 * * Returns OK if detached or error if already detached */ extern TM_EXTI_Result_t TM_EXTI_Detach(uint16_t GPIO_Pin); /* Handler for all lines. This function is called anytime interrupt occured on any pin */ __weak void TM_EXTI_Handler(uint16_t GPIO_Pin); |
Example
Example is set to work with both, discovery boards and nucleo boards.
- Discovery boards
- Button connected to PA0
- Rising edge when you press button
- Line0 selected
- Nucleo boards
- Button connected to PC13
- Falling edge when you press button
- Line13 selected
- Every click on button will toggle led
- After 10 clicks on button, interrupt for specific line is detached
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/** * Keil project for external interrupts library * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_exti.h" volatile uint8_t counter = 0; int main(void) { /* Initialize system */ SystemInit(); /* Initialize LEDS */ TM_DISCO_LedInit(); /* Attach interrupt on pin PA0 = External line 0 */ /* Button connected on discovery boards */ if (TM_EXTI_Attach(GPIOA, GPIO_Pin_0, TM_EXTI_Trigger_Rising) == TM_EXTI_Result_Ok) { TM_DISCO_LedOn(LED_RED); } /* Attach interrupt on pin PC13 = External line 13 */ /* Button connected on nucleo boards */ if (TM_EXTI_Attach(GPIOC, GPIO_Pin_13, TM_EXTI_Trigger_Falling) == TM_EXTI_Result_Ok) { TM_DISCO_LedOn(LED_GREEN); } while (1) { } } void TM_EXTI_Handler(uint16_t GPIO_Pin) { /* Handle external line 0 interrupts */ if (GPIO_Pin == GPIO_Pin_0) { /* Toggle RED led */ TM_DISCO_LedToggle(LED_RED); /* Check counter */ if (++counter >= 10) { /* Detach external interrupt for GPIO_Pin_0 no matter on which GPIOx is connected */ TM_EXTI_Detach(GPIO_Pin_0); } } /* Handle external line 13 interrupts */ if (GPIO_Pin == GPIO_Pin_13) { /* Toggle GREEN led */ TM_DISCO_LedToggle(LED_GREEN); /* Check counter */ if (++counter >= 10) { /* Detach external interrupt for GPIO_Pin_0 no matter on which GPIOx is connected */ TM_EXTI_Detach(GPIO_Pin_13); } } } |
Project is available on Github, download library here.
Recent comments