2018-01-21 05:02 AM
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-interrupt2018-01-21 05:23 AM
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
2018-01-21 06:21 AM
Review HAL examples provided in the Cube trees.
STM32Cube_FW_L0_V1.10.0\Projects\STM32L053C8-Discovery\Examples\GPIO\GPIO_EXTI\Src\main.c
2018-01-21 06:22 AM
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
2018-01-22 01:48 AM
Thanks. Can't click through to your link though 'Unexpected error'. I found the STML053 example though :)
2018-01-22 01:50 AM
Thanks, got it. Didn't realise that there would be different examples for the different variants :)
2018-01-22 02:07 AM
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=false2018-01-22 03:13 AM
Thanks, but you forgot to send the PDF :)