Skip to main content
wb0gaz
Associate III
January 18, 2026
Solved

STM32CubeMX (6.16.1) for STM32C011J6 (SO8) - set PA13/PA14 BACK to STLINK mode

  • January 18, 2026
  • 2 replies
  • 244 views

I need to use STM32CubeMX to generate initialization routine for two pins (7,8) on SO8 package of STM32CubeC011 MCU that switches the pin's roles BACK to STLINK mode. I created a new project on STM32CubeMX whose only task is to initialize pins 7 and 8 to PA13/PA14 SWCLK/SWDIO. I wanted to copy the GPIO initialization function MX generated into an existing application which had previously (that is, after start-up) changed the two pins to another mode; after that work is done, the two pins no longer need to be in GPIO mode and can be switched back to STLINK mode to facilitate firmware upgrade/debug/etc.

Two screen captures from STLINK32MX are below (pin assignments and project specifications.)

The resulting (very short) C language program generates the following code for initialization of the two pins:

 

static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

In the code segment above, MX turns on the Port A clock, but does not generate instructions that would modify the alternate function registers for the two pins (7 and 8 on SO8 package) to the values needed to restore the pin's roles to SWDIO and SWCLK. It appears STM32CubeMX assumes the target is already in that mode at power-up/reset, so there is no need to generate the instructions I need.

I've not included any of the code from the original project (which is otherwise working properly) - it  also uses initialization code routines generated by STM32CubeMX then copied into the application under development - that approach has been fully successful to this point. I would like to keep the code solely based on STM32CubeMX generated initialization routines.

Grateful for any advice, follow-up questions, suggestions, or requests for clarification of problem.

 

 

Best answer by TDK

The following code will initialize PA13/PA14 back to their SWD functions:

GPIO_InitTypeDef GPIO_InitStruct = {0};

GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SWJ;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SWJ;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

You could have set these pins to a different AF mode to get CubeMX to generate the code for it. Then change the AF to AF0.

2 replies

TDK
TDKBest answer
Super User
January 18, 2026

The following code will initialize PA13/PA14 back to their SWD functions:

GPIO_InitTypeDef GPIO_InitStruct = {0};

GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SWJ;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SWJ;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

You could have set these pins to a different AF mode to get CubeMX to generate the code for it. Then change the AF to AF0.

"If you feel a post has answered your question, please click ""Accept as Solution""."
wb0gaz
wb0gazAuthor
Associate III
January 18, 2026

Thank you!

That solves the immediate need and provides valuable guidance on using STM32CubeMX going forward!

Dave