Skip to main content
Graduate II
January 29, 2025
Solved

Code Debugging using STLINKV2

  • January 29, 2025
  • 3 replies
  • 4148 views

Hello,

I bought STLINKV2 + small board with STM32F103C8T6. I tried to create a simple program - LED blinking - according to example in here:  https://deepbluembedded.com/stm32-gpio-write-pin-digital-output-lab/   and I have STMCubeIDE + MX + others.My STLINKV2 has newest firmware.

Compilation of my example algorithm (in IDE) is ok (see below) , programming is ok, but debugging shows me an error:

**************************************

Target is not responding, retrying...
Target is not responding, retrying...
Shutting down...

*************************************

Can someone help me where is problem ? I would like to try "JTAG/SWD debugging funtion" of my program - i.e. functions "stepOver", "stepInto" etc. Program on processor is running ok and LEDs are blinking ok.

 

*************************************

STMicroelectronics ST-LINK GDB server. Version 7.9.0
Copyright (c) 2024, STMicroelectronics. All rights reserved.

Starting server with the following options:
Persistent Mode : Disabled
Logging Level : 1
Listen Port Number : 61234
Status Refresh Delay : 15s
Verbose Mode : Disabled
SWD Debug : Enabled
InitWhile : Enabled

Waiting for debugger connection...
Debugger connected
Waiting for debugger connection...
Debugger connected
Waiting for debugger connection...
-------------------------------------------------------------------
STM32CubeProgrammer v2.18.0
-------------------------------------------------------------------

 

Log output file: C:\Users\Jerry\AppData\Local\Temp\STM32CubeProgrammer_a04988.log
ST-LINK SN :
ST-LINK FW : V2J45S7
Board : --
Voltage : 3.20V
SWD freq : 4000 KHz
Connect mode: Under Reset
Reset mode : Hardware reset
Device ID : 0x410
Revision ID : Rev X
Device name : STM32F101/F102/F103 Medium-density
Flash size : 64 KBytes
Device type : MCU
Device CPU : Cortex-M3
BL Version : --

Opening and parsing file: ST-LINK_GDB_server_a04988.srec


Memory Programming ...
File : ST-LINK_GDB_server_a04988.srec
Size : 4.65 KB
Address : 0x08000000


Erasing memory corresponding to segment 0:
Erasing internal memory sectors [0 4]
Download in Progress:


File download complete
Time elapsed during download operation: 00:00:00.692

 

Verifying ...

 


Download verified successfully


Target is not responding, retrying...
Target is not responding, retrying...
Shutting down...
Target is not responding, retrying...
Target is not responding, retrying...
Target is not responding, retrying...
Target is not responding, retrying...
Target is not responding, retrying...
Target is not responding, retrying...
Target is not responding, retrying...
Target is not responding, retrying...

*************************************

 

*************************************

 

#include "main.h"

void SystemClock_Config(void);
static void MX_GPIO_Init(void);

int main(void)
{
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();

 /* Infinite loop */
 while (1)
 {
 // LED ON
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
 HAL_Delay(500);

 // LED OFF
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET);
 HAL_Delay(500);
 }
}
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 Error_Handler();
 }
}

/* @brief GPIO Initialization Function
static void MX_GPIO_Init(void)
{
 GPIO_InitTypeDef GPIO_InitStruct = {0};

/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOD_CLK_ENABLE();
 __HAL_RCC_GPIOB_CLK_ENABLE();

 /*Configure GPIO pin Output Level */
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);

 /*Configure GPIO pins : PB14 PB15 */
 GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
 /* User can add his own implementation to report the HAL error return state
 __disable_irq();
 while (1)
 {
 }
 /* USER CODE END Error_Handler_Debug */
}
 
#ifdef USE_FULL_ASSERT
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * @PAram file: pointer to the source file name
 * @PAram line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
 /* USER CODE BEGIN 6 */
 /* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
*************************************

 


Edited to apply proper code formatting; please see How to insert source code for future reference

    This topic has been closed for replies.
    Best answer by SOgal.1

    You have to write your code inside the "USER CODE" comments. Everything written outside will be deleted after a new code generation. Here an example in main.c

     /* Infinite loop */
     /* USER CODE BEGIN WHILE */
     // this code will stay
     while (1)
     {
     /* USER CODE END WHILE */
     // this code will be deleted
     /* USER CODE BEGIN 3 */
     // this code will stay
     }
     /* USER CODE END 3 */
    }

     Hope this helps!

    3 replies

    Visitor II
    January 29, 2025

    Fhi,

    As I understand, you download well your binary, but the connection with the debugger through ST-Link is down. Your configuration uses a 4000Khz SWD frequency, which is very high for a 2 wire 2direction data protocole (like I2C). I suggest to use 400Khz, which is usual in this kind of connection.

    I hope this can help you.

    Regards.

    Butterfly.

    JerryMC22Author
    Graduate II
    January 29, 2025

    Great ! ... and HOW I can change the frequency. In CubeIDE in menu "RUN/DebugConfiguration.../Debugger" there is no possibility to change it. The command line parameters can not be changed similarly to MSVS2022.

    Super User
    January 29, 2025

    this is ment for STM32CubeProgrammer as a "first contact" to tell hardware from software issues.

    Super User
    January 29, 2025

    Try STM32CubeProgrammer for a "first contact" to your board. Your log says "Connect mode: Under Reset" which requires the NRST pin to be connected. Try Normal mode or even Hot plug to attach to the running program. As @butterfly  said, could also be weak or wrong wiring. 

    > I bought STLINKV2

    the real thing or a clone?

    > small board with STM32F103C8T6

    They are often ***.and/or fake.

    You will be better off with a geniue Nucleo board or the STM32C0116-DK - Discovery kit with STM32C011F6 MCU - STMicroelectronics

    hth

    KnarfB

    Super User
    January 29, 2025

    @KnarfB wrote:

    > I bought STLINKV2

    the real thing or a clone?


    @JerryMC22 - see: https://community.st.com/t5/stm32-mcus/how-to-recognize-a-genuine-st-link-v2-versus-a-cloned-one/tac-p/703663/highlight/true#M965

     


    @KnarfB wrote:

    You will be better off with a genuine Nucleo board


    Absolutely!

    JerryMC22Author
    Graduate II
    January 29, 2025

    I know, I have this:

     

    IMG A01.jpg