ST意法半导体
直播中

乔丽娜

7年用户 1017经验值
私信 关注
[问答]

stm32F4虚拟端口不工作的原因?如何解决?

我正在尝试使用 STM32f407vgt6 的虚拟端口与我的 win 10 电脑进行通信。我想发送 16 个字节的数据。
我在 cubeMX 中创建了一个基础项目并插入了 cdc_transmit。可以通过虚拟端口将 16 字节的数据发送到我的电脑。我可以在 hterm 中看到传入的数据。
我的问题是,当我添加一些代码行(例如设置一个 ADC)时,无法通过 hterm 或 labview 访问 COM 端口。如果发送缓冲区大于 19 个字节,我会遇到同样的问题。
以下程序不工作。
  • /* USER CODE BEGIN Header */
  • /**
  •   ******************************************************************************
  •   * @file           : main.c
  •   * @brief          : Main program body
  •   ******************************************************************************
  •   * @attention
  •   *
  •   *

    © Copyright (c) 2020 STMicroelectronics.
  •   * All rights reserved.
  •   *
  •   * 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        {
  •                 txbuff = 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
  •           {
  •                   show = (float) adc1Data*(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 %drn", file, line) */
  •   /* USER CODE END 6 */
  • }
  • #endif /* USE_FULL_ASSERT */

我附上了一个工作示例。所以我不知道错误在哪里。








回帖(1)

张曼曼

2023-2-6 11:53:14
请仅通过设置 USB CDC 功能进行测试。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分