cancel
Showing results for 
Search instead for 
Did you mean: 

stm 32F4 Virtual comport won't work

FD.1
Associate II

Hello,

i am trying to use the virtual comport of the stm32f407vgt6 to communicate with my win 10 pc. I want to send 16 bytes of data.

I created a base project in cubeMX and insert the cdc_transmit. There it is possible to send 16 bytes of data via the virtual comport to my pc. I can see the incoming datas in hterm.

My Problem is, that it's impossible to access the COM port via hterm or labview, when i add some lines of code, e.g. set up an ADC. If the send buffer is bigger then 19 bytes i got the same problem.

The following program isn't working.

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "i2c.h"
#include "i2s.h"
#include "spi.h"
#include "tim.h"
#include "usb_device.h"
#include "gpio.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TX_BUF_SIZE 	16
#define adcSequenz		8
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
uint16_t DeviceAdress= 0x20 << 1;
 
volatile uint8_t Tx_Flag;
uint32_t ms_cnt = 0;
uint8_t txbuff[TX_BUF_SIZE];
 
uint32_t cycCnt = 0;
uint16_t adc1Data[adcSequenz];
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
extern uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t len);
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/**
  * @brief  Period elapsed callback in non blocking mode
  * @param  htim: TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	uint8_t i;
 
	for(i=0; i<TX_BUF_SIZE; i++)
	{
		txbuff[i] = cycCnt;  // AD Wandler simuliert
	}
 
	// Start ADC DMA
    if (htim->Instance == htim14.Instance)
    {
    	HAL_ADC_Start(&hadc1);
    	HAL_ADC_Start_DMA(&hadc1, (uint32_t*) adc1Data, adcSequenz);
        /* Toggle LED */
    	//HAL_GPIO_TogglePin(GPIOD, LD4_Pin);
    	ms_cnt++;
    	if(ms_cnt >= 500)
    	{
    		Tx_Flag = 1;
    		ms_cnt = 0;
    	}
    }
}
/* 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_StatusTypeDef stat = 0;
  uint8_t adcTxBuffer[16];
  uint8_t adcRxBuffer[16];
 
  uint8_t datatx[2];
  uint8_t datarx[1];
  datarx[0]=0x00;
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_I2C1_Init();
  MX_I2S3_Init();
  MX_SPI1_Init();
  MX_USB_DEVICE_Init();
  MX_ADC1_Init();
  MX_TIM14_Init();
  /* USER CODE BEGIN 2 */
  float show[adcSequenz];
  int i = 0;
  //int j = 0;
 
  adcTxBuffer[0] = 0x00;
  adcTxBuffer[1] = 0xF8;
  stat = HAL_I2C_Mem_Write(&hi2c1, DeviceAdress, 0x02, 2, adcTxBuffer, 2, 100);
  stat = HAL_I2C_Mem_Read(&hi2c1, DeviceAdress, 0x02, 2, adcRxBuffer, 2, 100);
  HAL_Delay(1);
  HAL_GPIO_TogglePin(GPIOA, 1);
  HAL_Delay(1);
  HAL_GPIO_TogglePin(GPIOA, 1);
 
  //stat = HAL_I2C_Mem_Read(&hi2c1, DeviceAdress, 0x00, 2, adcRxBuffer, 2, 100);
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  for(i=0; i<adcSequenz; i++)
	  {
		  show[i] = (float) adc1Data[i]*(float) 3.3 / (float)0xFFF;
	  }
 
 
	  if(Tx_Flag == 1)
	  {
 
//		for(j = 0; j < 8; j++)
//		{
//			txbuff[2*j+1] = adc1Data[j] & 0x00FF;
//			txbuff[2*j] = (adc1Data[j] & 0xFF00) >> 8;
//		}
		  cycCnt++;
		  Tx_Flag = 0;
		  CDC_Transmit_FS(txbuff, sizeof(txbuff));
		  HAL_GPIO_TogglePin(GPIOD, LD4_Pin);
	  }
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
  PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/* USER CODE BEGIN 4 */
 
/* 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 */
 
  /* 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,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
 

I attached a working example. So i have no idea where the mistake can be.

1 ACCEPTED SOLUTION

Accepted Solutions
YS.K
Associate III

Refer to the video information below

Please test it by only setting up the USB CDC function.

(only cdc, no timer adc i2c etc)

https://www.youtube.com/watch?v=AYICE0gU-Sg

https://www.youtube.com/watch?v=h9T0RTu9Muc

https://m.blog.naver.com/eziya76/220945926732 <- CubeMX CDC auto code error fix

View solution in original post

2 REPLIES 2
YS.K
Associate III

Refer to the video information below

Please test it by only setting up the USB CDC function.

(only cdc, no timer adc i2c etc)

https://www.youtube.com/watch?v=AYICE0gU-Sg

https://www.youtube.com/watch?v=h9T0RTu9Muc

https://m.blog.naver.com/eziya76/220945926732 <- CubeMX CDC auto code error fix

FD.1
Associate II

Hello,

thanks for your reply. The STM USB training video saved the day.

I have tested the communication with 115 Bytes now. That works in the basic programm.

I will include the other parts like adc and dma and i2c later step by step and check if it is still working.