Skip to content

SWD gone #52

Description

@quantrpeter

Hi All
Before i burn the program using STM32CubeIDE, st-info can dump out correct info, but not after.

/Users/peter>st-info --probe
Found 1 stlink programmers
  version:    V2J46S7
  serial:     E1007200D0D2139393740544
  flash:      524288 (pagesize: 16384)
  sram:       131072
  chipid:     0x431
  dev-type:   STM32F411xC_xE

After i burn the program

/Users/peter>st-info --probe
Failed to enter SWD mode
Found 1 stlink programmers
  version:    V2J46S7
  serial:     E1007200D0D2139393740544
  flash:      0 (pagesize: 0)
  sram:       0
  chipid:     0x000
  dev-type:   unknown

And st-flash erase not working too, the SWD completely gone !!!

/Users/peter>st-flash erase
st-flash 1.8.0
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type

This is my program

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2025 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>
#include "usbd_cdc_if.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

  HAL_Delay(1000); // Allow debugger to attach before peripherals start

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
DMA_HandleTypeDef hdma_memtomem_dma2_stream0;
/* USER CODE BEGIN PV */
#define USB_BUF_LEN 128

uint8_t usbTxBuf[USB_BUF_LEN];
uint16_t usbTxBufLen;
volatile uint8_t usbTxPending = 0; // send from main loop when ready

uint8_t usbRxBuf[USB_BUF_LEN];
uint16_t usbRxBufLen;
uint8_t usbRxFlag = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
/* USER CODE BEGIN PFP */
void Process_USB_Command(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// Buffer for accumulating received bytes until 0x0D
void XferCpltCallback(DMA_HandleTypeDef *hdma);
uint8_t Buffer_Src[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
#define BUFFER_DEST_SIZE (100 * 1024)
int pointer = 0;
uint8_t Buffer_Dest[4];
uint8_t Buffer[BUFFER_DEST_SIZE];
volatile uint32_t gpio_value;  // To store the 32-bit GPIO register value
volatile uint8_t transfer_complete = 0;  // Flag to indicate transfer completion

char commandBuf[USB_BUF_LEN];
uint16_t commandLen = 0;

void USB_RXCallback(uint8_t *Buf, uint32_t *Len) {
//	for (uint32_t i = 0; i < *Len; i++) {
//		uint8_t b = Buf[i];
//		// Treat both CR and LF as terminators; ignore empty commands (handles CRLF double-terminator)
//		if (b == '\r' || b == '\n') {
//			if (commandLen > 0) {
//				commandBuf[commandLen] = '\0';  // Null-terminate
//				if (strcmp(commandBuf, "ping") == 0) {
//					strcpy((char*) usbTxBuf, "\r\npong\r\n");
//					usbTxBufLen = strlen((char*) usbTxBuf);
//					usbTxPending = 1;
//				} else if (strcmp(commandBuf, "start") == 0) {
//					// Transfer 4 bytes (32-bit GPIO register) from GPIOA IDR to Buffer_Dest
//					HAL_DMA_Start_IT(&hdma_memtomem_dma2_stream0,
//							(uint32_t) &GPIOA->IDR, (uint32_t) Buffer_Dest, 4);
//					strcpy((char*) usbTxBuf, "\r\nstarted\r\n");
//					usbTxBufLen = strlen((char*) usbTxBuf);
//					usbTxPending = 1;
//				} else {
//					strcpy((char*) usbTxBuf, "\r\ncommand not found\r\n");
//					usbTxBufLen = strlen((char*) usbTxBuf);
//					usbTxPending = 1;
//				}
//				// Reset buffer for next command
//				commandLen = 0;
//			}
//		} else {
//			// Accumulate command characters (no per-byte echo to avoid BUSY drops)
//			if (commandLen < USB_BUF_LEN - 1) {
//				commandBuf[commandLen++] = b;
//			}
//		}
//	}
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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 */
	HAL_Delay(1000); // Longer window to let SWD attach after reset
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config(); // Configure system clock with safe HSI-only settings

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
//  MX_DMA_Init();
//  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
//	hdma_memtomem_dma2_stream0.XferCpltCallback = &XferCpltCallback;
//
//	// Configure some GPIOA pins as inputs with pulldown to see different values
//	GPIO_InitTypeDef GPIO_InitStruct = { 0 };
//	GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
//	GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 |
//	GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 |
//	GPIO_PIN_15;  // All pins except PA11/PA12 (USB) and PA13/PA14 (SWD)
//	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
//	GPIO_InitStruct.Pull = GPIO_PULLDOWN;  // Pull up to see 1s in the register
//	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	while (1) {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		// If there's a pending USB transmit, try to send it now (retry until not busy)
//		if (usbTxPending) {
//			if (CDC_Transmit_FS(usbTxBuf, usbTxBufLen) == USBD_OK) {
//				usbTxPending = 0;
//			}
//		}
//
//		// Check if DMA transfer completed
//		if (transfer_complete) {
//			// Store the latest sample in the buffer
//			Buffer[pointer + 0] = Buffer_Dest[0];
//			Buffer[pointer + 1] = Buffer_Dest[1];
//			Buffer[pointer + 2] = Buffer_Dest[2];
//			Buffer[pointer + 3] = Buffer_Dest[3];
//			pointer += 4;
//
//			if (pointer >= BUFFER_DEST_SIZE) {
//				// Buffer is full - send all collected data
//
//				// Send "started" message first
//				strcpy((char*) usbTxBuf, "started\r\n");
//				while (CDC_Transmit_FS(usbTxBuf, strlen((char*) usbTxBuf))
//						!= USBD_OK) {
//					HAL_Delay(1);
//				}
//
//				// Send data in large batches to optimize USB transmission
//				char batch_buffer[10240]; // Much larger batch buffer for better performance
//				int batch_pos = 0;
//
//				for (int x = 0; x < BUFFER_DEST_SIZE; x += 4) {
//					int line_len = sprintf(batch_buffer + batch_pos,
//							"%d > 0x%02X 0x%02X 0x%02X 0x%02X\r\n", x,
//							Buffer[x], Buffer[x + 1], Buffer[x + 2],
//							Buffer[x + 3]);
//
//					batch_pos += line_len;
//
//					// Send batch when buffer is getting full (leave room for next line)
//					if (batch_pos > 10150 || x >= BUFFER_DEST_SIZE - 4) {
//						while (CDC_Transmit_FS((uint8_t*) batch_buffer,
//								batch_pos) != USBD_OK) {
//							HAL_Delay(1);  // Wait if USB is busy
//						}
//						batch_pos = 0;  // Reset batch buffer
//					}
//				}
//
//				// Send final "end" message
//				char hex_string[100];
//				sprintf(hex_string, "end\r\n");
//				while (CDC_Transmit_FS((uint8_t*) hex_string,
//						strlen(hex_string)) != USBD_OK) {
//					HAL_Delay(1);
//				}
//
//				// Reset for next acquisition
//				pointer = 0;  // Reset pointer to start of buffer
//				transfer_complete = 0;
//
//				// Stop DMA - acquisition is complete
//				// If you want continuous acquisition, comment out the next line
//				// HAL_DMA_Abort_IT(&hdma_memtomem_dma2_stream0);
//
//			} else {
//				// Continue collecting samples
//				transfer_complete = 0;  // Reset flag
//				HAL_DMA_Start_IT(&hdma_memtomem_dma2_stream0,
//						(uint32_t) &GPIOA->IDR, (uint32_t) Buffer_Dest, 4);
//			}
//		}
	}
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  * Use HSI as a safe default and drive USB 48MHz from PLLQ.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  // HSI=16MHz -> VCOin=1MHz (M=16), VCO=192MHz (N=192), SYSCLK via HSI, USB 48MHz (Q=4)
  RCC_OscInitStruct.PLL.PLLM = 16;
  RCC_OscInitStruct.PLL.PLLN = 192;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    // Fallback: disable PLL if configuration fails, stay on HSI (already handled)
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_OFF; // Ensure PLL is off
    (void)HAL_RCC_OscConfig(&RCC_OscInitStruct); // Reconfigure to HSI-only
  }

  /** 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_HSI; // Use HSI as SYSCLK source
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  (void)HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
}

/**
  * Enable DMA controller clock
  * Configure DMA for memory to memory transfers
  *   hdma_memtomem_dma2_stream0
  */
//static void MX_DMA_Init(void)
//{
//
//  /* DMA controller clock enable */
//  __HAL_RCC_DMA2_CLK_ENABLE();
//
//  /* Configure DMA request hdma_memtomem_dma2_stream0 on DMA2_Stream0 */
//  hdma_memtomem_dma2_stream0.Instance = DMA2_Stream0;
//  hdma_memtomem_dma2_stream0.Init.Channel = DMA_CHANNEL_0;
//  hdma_memtomem_dma2_stream0.Init.Direction = DMA_MEMORY_TO_MEMORY;
//  hdma_memtomem_dma2_stream0.Init.PeriphInc = DMA_PINC_ENABLE;
//  hdma_memtomem_dma2_stream0.Init.MemInc = DMA_MINC_ENABLE;
//  hdma_memtomem_dma2_stream0.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
//  hdma_memtomem_dma2_stream0.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
//  hdma_memtomem_dma2_stream0.Init.Mode = DMA_NORMAL;
//  hdma_memtomem_dma2_stream0.Init.Priority = DMA_PRIORITY_VERY_HIGH;
//  hdma_memtomem_dma2_stream0.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
//  hdma_memtomem_dma2_stream0.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
//  hdma_memtomem_dma2_stream0.Init.MemBurst = DMA_MBURST_SINGLE;
//  hdma_memtomem_dma2_stream0.Init.PeriphBurst = DMA_PBURST_SINGLE;
//  if (HAL_DMA_Init(&hdma_memtomem_dma2_stream0) != HAL_OK)
//  {
//    Error_Handler( );
//  }
//
//  /* DMA interrupt init */
//  /* DMA2_Stream0_IRQn interrupt configuration */
//  HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
//  HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
//
//}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
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_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : LED_Pin */
  GPIO_InitStruct.Pin = LED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);

  /* USER CODE BEGIN MX_GPIO_Init_2 */

  /* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
void XferCpltCallback(DMA_HandleTypeDef *hdma) {
	// Transfer completed - combine the 4 bytes to a 32-bit value for easier
	// interpretation
	gpio_value = (Buffer_Dest[3] << 24) | (Buffer_Dest[2] << 16)
			| (Buffer_Dest[1] << 8) | Buffer_Dest[0];
	transfer_complete = 1;
	__NOP(); // Line reached only if transfer was successful. Toggle a breakpoint
			 // here
}
/* USER CODE END 4 */

/**
  * @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 */

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions