cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] How to set up a GPIO interrupt on STM32L031

sm23
Associate II
Posted on January 21, 2018 at 14:02

Please can somebody point me to an example of setting up an interrupt and service routine for a GPIO input for an STM32L031? I have read UM1749 but it maybe it's because I'm not familiar with HAL (4 years of using Standard Peripheral Library for several STM32F20x projects), but although there is a code snippet for configuring the GPIO pin as an interrupt input there isn't any corresponding code for enabling it or for the ISR. None of the ST examples for this chip seem to use a GPIO interrupt. 

#stm32l0-hal-gpio-interrupt
7 REPLIES 7
Posted on January 21, 2018 at 14:23

Look at the example in the appendix, or in L0Snippets:

/**

  * Brief   This function enables the peripheral clocks on GPIO port A,

  *         configures the EXTI register and NVIC IRQ.

  *         PA0 is set in input,no pull-up, no pull-down

  *         SYSCFG_EXTICR1 is kept at its reset value to select Port A for EXTI0

  * Param   None

  * Retval  None

  */

__INLINE void  ConfigureExtendedIT(void)

{  

  /* (1) Enable the peripheral clock of GPIOA */

  /* (2) Select input mode (00) on GPIOA pin 0 */  

  /* (3) Select Port A for pin 0 extended interrupt by writing 0000

         in EXTI0 (reset value)*/

  /* (4) Configure the corresponding mask bit in the EXTI_IMR register */

  /* (5) Configure the Trigger Selection bits of the Interrupt line

         on rising edge*/

  /* (6) Configure the Trigger Selection bits of the Interrupt line

         on falling edge*/

  RCC->IOPENR |= RCC_IOPENR_GPIOAEN; /* (1) */

  GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODE0)); /* (2) */  

  //SYSCFG->EXTICR[0] &= (uint16_t)~SYSCFG_EXTICR1_EXTI0_PA; /* (3) */

  EXTI->IMR |= 0x0001; /* (4) */

  EXTI->RTSR |= 0x0001; /* (5) */

  //EXTI->FTSR |= 0x0001; /* (6) */

 

  /* Configure NVIC for Extended Interrupt */

  /* (6) Enable Interrupt on EXTI0_1 */

  /* (7) Set priority for EXTI0_1 */

  NVIC_EnableIRQ(EXTI0_1_IRQn); /* (6) */

  NVIC_SetPriority(EXTI0_1_IRQn,0); /* (7) */

}

There's an flaw in this example as it does not enable SYSCFG by setting RCC_APB2ENR.SYSCFGEN  - the example will work as it leaves the selector in SYSCFG at its reset value, but any other pin than PAx would need to have this changed.

JW

Posted on January 21, 2018 at 15:21

Review HAL examples provided in the Cube trees.

STM32Cube_FW_L0_V1.10.0\Projects\STM32L053C8-Discovery\Examples\GPIO\GPIO_EXTI\Src\main.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Szymon PANECKI
Senior III
Posted on January 21, 2018 at 15:22

Hello Simon,

Guidance how to configure a pin in EXTI (external intterupt) mode you can find in my answer provided in this thread:

https://community.st.com/0D50X00009XkWoPSAV

. This approach uses STM32CubeMX, which could be alternative solution proposed above by Jan.

Additionally you can use an example application fromSTM32Cube L0 package. For example please refer to this path:

...\STM32Cube_FW_L0_V1.0\Projects\STM32L053R8-Nucleo\Examples\GPIO\GPIO_EXTI

This example is prepared for STM32L053, but it will work also for your device (STM32L031).

Regards

Szymon

Posted on January 22, 2018 at 09:48

Thanks. Can't click through to your link though 'Unexpected error'. I found the STML053 example though :)

Posted on January 22, 2018 at 09:50

Thanks, got it. Didn't realise that there would be different examples for the different variants :)

Posted on January 22, 2018 at 10:07

Hello Simon,

Thank you for your feedback. I am gladto hear that example application was helpful to you.

I am sorry for the

'Unexpected error'. ST forum/community experiences this problem for some weeks already and it generates a lot of troubles for users... In case you would like to take a look on the thread, which I mentioned, I attach it here in form of pdf file.

Regards

Szymon

________________

Attachments :

CubeMX interrupt.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hy7R&d=%2Fa%2F0X0000000b4C%2Flhs3uAfUCl6RTBED4IJt6fmQCn2labOlpM8Fkl_mVlw&asPdf=false
Posted on January 22, 2018 at 11:13

Thanks, but you forgot to send the PDF :)