cancel
Showing results for 
Search instead for 
Did you mean: 

How to change GPIO pin mode efficiently

Pavel A.
Evangelist III
Posted on February 17, 2018 at 19:38

Dear experts,

I need to change mode of a GPIO pin of STM32 for some 1-wire protocol.

In one state, the pin must be EXTI interrupt, in other state the same pin must be output - at least the MCU should be able to drive it high or low. These states need to switch often, quickly, without glitches.

In the Cube the pin is configured as EXTI interrupt, it generated HAL-style init code and interrupt handlers, and it seems to work.

Now the question is, to drive the pin high or low - do I need to call HAL_GPIO_DeInit and HAL_GPIO_Init with new settings? If I only set pullup/pulldown using LL GPIO calls while the pin is in EXTI mode, should it work?

Thanks,

-- Pavel

#stm32-gpio #stm32cube-hal #stm32-exti
5 REPLIES 5
Posted on February 17, 2018 at 20:54

Here you can find some info about mixing HAL & LL:

http://empa.com/dokumanlar/st2016/12_Low-Layer-Library-theory.pdf

 

I try to work with LL where I can. It's better from the performance perspective to switch to LL or to the register level, particularly here where the GPIO is not very complex pheripheral.

S.Ma
Principal
Posted on February 17, 2018 at 21:37

Unless I'm wrong, EXTI works based on the pin level, being input, output or alternate function.

So it maybe possible to keep EXTI enabled, and hop with interrupt cleared/enabled/disabled...

henry.dick
Senior II
Posted on February 17, 2018 at 21:50

'

Now the question is...'

Now, the real question is: what does that datasheet say about configurations for a pin to be EXTI or output?

Look them up in the datasheet and see the differences. So when you change the pin's mode, just change those bits / registers.

that's the most efficient way, bar none.

Jan Waclawek
Senior II
Posted on February 18, 2018 at 01:41

EXTI, and Input/Output mode of GPIO are two completely separate functionalities, bound together only by the 'logic' of Cube/CubeMX. Read: EXTI continues to work even if you change pin to output, it is set separately - as KIC said, in the EXTI module, by setting/clearing the respective bit in EXTI's rising/falling edge register, whichever (or both) you were using.

Changing pin from GPIO input to output is to change one bit in respective GPIO_MODER (unless you use 'F1 - you neglected to tell us the STM32 model you are using).

Note, that you may meet atomicity problems if you are using these registers from several different interrupt levels (including 'main'), or different threads if you use a RTOS. If so, use appropriate measures to mitigate (disa/ena interrupts, proper locks in RTOS), bit-banding may help if available in your target (you neglected to tell us the STM32 model you are using; GPIO_MODER is IIRC not covered even in M3/M4).

Another option would be to drop EXTI, use a pin which is timer channel as so, change in/out by switching between capture/compare and use the capture interrupt.

Cube/HAL and efficient don't match. Cube/LL may match but by the time you figure out the exact incantation you might be better off using the dreaded registers directly. YMMV.

JW

Posted on February 20, 2018 at 04:04

use a  pin which is timer channel as so, change in/out by switching between capture/compare and use the capture interrupt.

Jan, this sounds very interesting.  Could you point me to example, please? My hardware engineer said that he has assigned the specific pin for a reason, but now he can't recall why  Maybe this is it...

-- pa