cancel
Showing results for 
Search instead for 
Did you mean: 

JTAG Connection Lost After Successfull Programming

sorterus
Associate II

Dear All,

My board has an STM32l072RZ mcu. I am trying to study low power modes. I created a sleep mode example and I flashed it properly. Everything looks great until this point. But, when i tried to connect after first flashing, my jtag(jlink) says that "Connecting to target...ERROR: STM32: Connecting to CPU via connect under reset failed." The two of my boards bricked now.

My simple code(based on CubeMX)

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration----------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
  SystemPower_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  //MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  HAL_GPIO_WritePin(BATT_CE_GPIO_Port, BATT_CE_Pin, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(LED_POWER_RED_GPIO_Port, LED_POWER_RED_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(BT_POWER_GPIO_Port, BT_POWER_Pin, GPIO_PIN_SET);
 
  while (1)
  {
 
  /* USER CODE END WHILE */
	  if(HAL_GPIO_ReadPin(LEFT_BUTTON_GPIO_Port, LEFT_BUTTON_Pin) == 0)
	  {
		/* Enable the power down mode during Sleep mode */
		__HAL_FLASH_SLEEP_POWERDOWN_ENABLE();
 
		/* Suspend Tick increment to prevent wakeup by Systick interrupt.         */
		/* Otherwise the Systick interrupt will wake up the device within 1ms     */
		/* (HAL time base).                                                       */
		HAL_SuspendTick();
 
		/* Enter Sleep Mode , wake up is done once User push-button is pressed */
		HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
	  }
 
	  /* Resume Tick interrupt if disabled prior to sleep mode entry */
	  HAL_ResumeTick();
 
	  if(HAL_GetTick() % 1000 == 0)
	  {
		  HAL_GPIO_TogglePin(LED_POWER_RED_GPIO_Port, LED_POWER_RED_Pin);
	  }
 
	  pin_status = HAL_GPIO_ReadPin(BATT_PGOOD_GPIO_Port, BATT_PGOOD_Pin);
 
  /* USER CODE BEGIN 3 */
 
  }
  /* USER CODE END 3 */
 
}
 
static void SystemPower_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();
 
  /* Enable Ultra low power mode */
  HAL_PWREx_EnableUltraLowPower();
 
  /* Enable the fast wake up from Ultra low power mode */
  HAL_PWREx_EnableFastWakeUp();
 
  /* Enable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
 
  /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  /* Note: Debug using ST-Link is not possible during the execution of this   */
  /*       example because communication between ST-link and the device       */
  /*       under test is done through UART. All GPIO pins are disabled (set   */
  /*       to analog input mode) including  UART I/O pins.           */
  GPIO_InitStructure.Pin = GPIO_PIN_All;
  GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStructure.Pull = GPIO_NOPULL;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
 
  /* Disable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_DISABLE();
  __HAL_RCC_GPIOB_CLK_DISABLE();
  __HAL_RCC_GPIOC_CLK_DISABLE();
  __HAL_RCC_GPIOD_CLK_DISABLE();
  __HAL_RCC_GPIOH_CLK_DISABLE();
  __HAL_RCC_GPIOE_CLK_DISABLE();
}

4 REPLIES 4
Andrew Neil
Evangelist

Are you turning off the pins required for the debug connection ... ?

sorterus
Associate II

Actually, this the main question in my mind. I acquired this example from examples folder of stm32_l0_... package. I supposed that JTAG can debug the CPU although debug pins are disabled. Otherwise, what is the logic of the putting this examples into example folders?

sorterus
Associate II

OK, I solved my problem. The problem is that, I need pre-delay before forever loop. This is my fault, i had not observe all the raws of the example.

PUN CHEE HUEY
Associate II

I have the same problem encountered in my board. The JTAG not working anymore after I flashed the code in first time. May I know how to recover the jtag ?