cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Shutting down debug session

Fede Rico
Associate III
Posted on June 21, 2016 at 11:22

I'm facing with a problem regarding debug session.

When I start debug session, after the configuration of USART2 the debug session is closed.

Keil message:

Cannot access target. Shutting down debug session

The shutdown occur with this instruction:

__HAL_AFIO_REMAP_USART2_ENABLE();

This is my configuration:

  • STM32F103
  • IDE Keil
  • Keil project created with STM32Cube MX
  • Debug Serial Wire with SWCLK, SWDIO, NRST.
  • UART2 only TX and RX

Without debug the board works well, but I need the debug to solve some bug.

Thanks!

7 REPLIES 7
Posted on June 21, 2016 at 14:43

And what does the code in that function actually do? Step into it.

Confirm AFIO and GPIOD clocks are enabled.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Fede Rico
Associate III
Posted on June 21, 2016 at 15:02

Hi clive1,

The code execute this macro

/**

* @brief Enable the remapping of USART2 alternate function CTS, RTS, CK, TX and RX.

* @note ENABLE: Remap (CTS/PD3, RTS/PD4, TX/PD5, RX/PD6, CK/PD7)

* @retval None

*/

#define __HAL_AFIO_REMAP_USART2_ENABLE() SET_BIT(AFIO->MAPR, AFIO_MAPR_USART2_REMAP)

GPIOD clock is enabled by this macro:

__HAL_RCC_GPIOD_CLK_ENABLE();

This is the UART2 Init function:

/* USART2 init function */

static void MX_USART2_UART_Init(void)

{

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_NONE;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

Error_Handler();

}

}  Inside the Init function there is this function's call to:  /* Init the low level hardware */

HAL_UART_MspInit(huart);   

This function initialize the low level of UART2:

else if(huart->Instance==USART2)

{

/* USER CODE BEGIN USART2_MspInit 0 */

/* USER CODE END USART2_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART2_CLK_ENABLE();

/**USART2 GPIO Configuration

PD5 ------> USART2_TX

PD6 ------> USART2_RX

*/

GPIO_InitStruct.Pin = Debug_Serial_TX_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(Debug_Serial_TX_GPIO_Port, &GPIO_InitStruct);

GPIO_InitStruct.Pin = Debug_Serial_RX_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(Debug_Serial_RX_GPIO_Port, &GPIO_InitStruct);

//__HAL_AFIO_REMAP_USART2_ENABLE();

/* Peripheral interrupt init */

HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART2_IRQn);

/* USER CODE BEGIN USART2_MspInit 1 */

/* USER CODE END USART2_MspInit 1 */

}

And here the debug session fails.

Posted on June 21, 2016 at 16:50

Are you sure you don't have an issue externally with PD5? Could you scope the NRST pin and see what that does.

You could perhaps set the pin up as a GPIO and drive it high and low to see if that precipitates the same failure.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
misagh
Associate III
Posted on June 22, 2016 at 09:25

Hi clive1

I have the same problem. It seems that when I try to set a bit in AFIO->MAPR (for example AFIO->MAPR |= 0x30; the debug session fails. Do you have any idea why it fails?

Thanks

Fede Rico
Associate III
Posted on June 22, 2016 at 09:57

Hi clive1,

The GPIO PD5 in directly connected to an FTDI converter. Same thing for GPIO PD6.

There aren't any components along these wires.

This morning I will scope the NRST line to see what happen.

Could this issue related to the remapping of UASART2 on PD5 and PD6?

Thanks for your time.

misagh
Associate III
Posted on June 22, 2016 at 10:19

Hi,

I found a temporary solution for you. You should use

AFIO->MAPR |= AFIO_MAPR_USART2_REMAP | AFIO_MAPR_SWJ_CFG_JTAGDISABLE;

(in case of using SW) instead of using

__HAL_AFIO_REMAP_USART2_ENABLE();

I wonder if anyone can explain why the AFIO->MAPR value after the execution of following code is 0x04000030?!?

AFIO->MAPR = 0x02000000UL | 0x30; // The AFIO->MAPR value is 0x04000030 after execution of this line!!!

Bests, Misagh
Fede Rico
Associate III
Posted on June 24, 2016 at 13:33

Thanks Misagh92!

Your solution works! Now I can debug and read output form UART2.

The only problem is connected to code generation performed by ST CubeMX. When I generate the project's code the 

__HAL_AFIO_REMAP_USART2_ENABLE()

is restored.

But this is not a big problem. Just keep it in mind.