cancel
Showing results for 
Search instead for 
Did you mean: 

Dual role pin configuration

Posted on April 24, 2018 at 11:35

Hello there,

I am using STM32L4 family MCU. I was wondering either it is possible to configure a pin to act as 2 configurations. In my situation, I am trying to make the pin PA0 to act as UART2 CTS line and External interrupt 0. From what I am reading in the datasheet and by checking the way HAL code for initialization is written it should be possible. The problem I am facing however is that my MCU keeps reseting after this config so maybe I am doing something wrong after all. Here is the code generated by MxCube, it configures the pin as uart2 CTS:

    /**USART2 GPIO Configuration    

    PA0     ------> USART2_CTS

    PA2     ------> USART2_TX

    PA3     ------> USART2_RX

    */

    GPIO_InitStruct.Pin = GSM_UART2_CTS_Pin|GSM_UART2_TX_Pin|GSM_UART2_RX_Pin;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

...

    HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);

    HAL_NVIC_EnableIRQ(USART2_IRQn);

// My added code to also enable EXTI:

    // Add external interrupt config to UART2  CTS line

    GPIO_InitStruct.Pin = GSM_UART2_CTS_Pin;

    GPIO_InitStruct.Mode |= GPIO_MODE_IT_RISING_FALLING;

    HAL_GPIO_Init(GSM_UART2_CTS_GPIO_Port, &GPIO_InitStruct);

    // set IRQ for EXTI0 priority but disable it for now

    HAL_NVIC_SetPriority(EXTI0_IRQn, 5, 0);

    HAL_NVIC_EnableIRQ(EXTI0_IRQn);

Is there any error in my understanding of this? Is this config a possible one, or do I need to turn CTS off before turning EXTI0 on? I would appreciate all help.

#interrput #uart
4 REPLIES 4
Ben K
Senior III
Posted on April 24, 2018 at 11:43

There are certain ways in which a pin might be dual role (e.g. OD output and input at the same time), but alternate function isn't one of them. The second call of HAL_GPIO_Init() discards the previously set alternate function mode for the pin, and only input mode will be functional.

I would recommend simply connecting CTS additionally to a free input pin, which will operate independently as EXTI.

Posted on April 24, 2018 at 11:48

Thanks for answer,

Just to be clear about the MCU being reset: MxCube generates code for UART2 CTS and does not for EXTI, so I had no callback defined, thus by going to default callback the MCU reset. After adding the callback manually it works.

As for your answer, are you 100% sure that a pin cannot be alternate function + external interrupt? Because in the HAL_GPIO_Init function there is no 'if - else' statements for configuring the mode. If I make the pin to be alternate + exti, it will configure both, as they do not overlap each other.

External pin tied to UART2 CTS would of course be convenient, but this is a ready design already.

Posted on April 24, 2018 at 12:23

The different behavior is provided by the hardware, as the MODER bits are written with different values in each call.

Posted on April 24, 2018 at 12:59

Ben,

This time I reversed the initialization order.

  1. I have configured the PA2 as EXTI0.
  2. I have configured UART2 periph with hardware CTS.

The external interrupt 0 works, which would indicate that it was not overlayed with UART2 CTS configuration. Also if I am looking correctly into MODER register, the EXTI config is not configured there.