STM32F0 USB CDC_Init_FS() 和 CDC_Receive_FS() 使用 CubeMX

Posted

技术标签:

【中文标题】STM32F0 USB CDC_Init_FS() 和 CDC_Receive_FS() 使用 CubeMX【英文标题】:STM32F0 USB CDC_Init_FS() and CDC_Receive_FS() using CubeMX 【发布时间】:2016-08-27 12:14:57 【问题描述】:

我正在使用此代码通过 USB 捕获数据。一旦接收到一个字符,上面的代码就会使程序崩溃。底部工作得很好,尽管我无法按我的意愿保存数据。甚至在调用CDC_Receive_FS()...之前,顶部崩溃(无限循环)从未被调用。底部按预期调用CDC_Receive_FS()

在我的一生中,我看不出我如何调用我的结构有什么问题,该结构包含我循环通过的缓冲区数组。

/* Send Data over USB CDC are stored in this buffer       */
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

#define  MAX_COMMANDS_IN_BUFFER 10 //max commands that can be received and saved without overwriting. Each command has a max size of APP_RX_DATA_SIZE

/* Define size for the receive and transmit buffer over CDC */
/* It's up to user to redefine and/or remove those define */
#define APP_RX_DATA_SIZE  256
#define APP_TX_DATA_SIZE  256
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

 static struct
  
  uint32_t Buffer_Number_Receiving, Buffer_Number_Processing;        //Buffer_Number_Receiving is the current position in buffer to receive incoming data. Buffer_Number_Processing is the index of buffer which is being processed.
  uint8_t IsCommandDataReceived; // > 0 , data were received. 0 means no data is available
  uint8_t UserRxBufferFS[MAX_COMMANDS_IN_BUFFER][APP_RX_DATA_SIZE];//it could save <MaxCommandsInBuffer> number of commands
  uint8_t CommandsLens[MAX_COMMANDS_IN_BUFFER]; //save the len of each command
  s_RxBuffers;

static int8_t CDC_Init_FS(void)


  hUsbDevice_0 = &hUsbDeviceFS;
  /* USER CODE BEGIN 3 */
  /* Set Application Buffers */
  USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
  USBD_CDC_SetRxBuffer(hUsbDevice_0, s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving] );//Set the buffer to receive incoming data
  USBD_CDC_ReceivePacket(hUsbDevice_0);
  return (USBD_OK);
  /* USER CODE END 3 */

这不是:

/* Received Data over USB are stored in this buffer       */
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];

/* Send Data over USB CDC are stored in this buffer       */
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

static int8_t CDC_Init_FS(void)

  hUsbDevice_0 = &hUsbDeviceFS;
  /* USER CODE BEGIN 3 */
  /* Set Application Buffers */
  USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
  USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBufferFS);
  USBD_CDC_ReceivePacket(hUsbDevice_0);
  return (USBD_OK);

Loop

这里的行(使用这个缓冲区)似乎是罪魁祸首:

 USBD_CDC_SetRxBuffer(hUsbDevice_0, s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving] );//Set the buffer to receive incoming data

任何帮助/见解将不胜感激。

【问题讨论】:

“崩溃”?那是什么?错误?数据中止?爆炸?有什么具体的吗? 我给了一个截图“循环”。它以无限循环的形式出现在 Default_Handler 中。 然后通过检查中断状态和故障寄存器找出是哪个异常将您带到那里。 您的问题似乎是关于它是在工作的顶部还是底部部分,但假设顶部不工作我会怀疑内存不足,因为您在 F0 上。 【参考方案1】:

我会让 Rx_Buffer 一维并单独处理命令历史记录。

static struct
  
  uint32_t Buffer_Number_Receiving, Buffer_Number_Processing;        //Buffer_Number_Receiving is the current position in buffer to receive incoming data. Buffer_Number_Processing is the index of buffer which is being processed.
  uint8_t IsCommandDataReceived; // > 0 , data were received. 0 means no data is available
  uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  uint8_t CommandsLens[MAX_COMMANDS_IN_BUFFER]; //save the len of each command


 s_RxBuffers;

除此之外,由于您使用的是结构(s_RxBuffers 类型),我认为您没有以正确的方式将缓冲区作为指针传递给您的函数。 我认为你应该这样做:

USBD_CDC_SetRxBuffer(hUsbDevice_0, &s_RxBuffers.UserRxBufferFS[0] );//Set the buffer to receive incoming data

【讨论】:

我也试过(USBD_CDC_SetRxBuffer(hUsbDevice_0, &amp;s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving][0]; );) ,它应该给我地址......它确实存在,但同样的问题存在。我已更改为一个简单的循环缓冲区,邮件缓冲区的尺寸为 on。我不再有问题了......虽然我仍然不明白为什么我不能使用多维缓冲区。

以上是关于STM32F0 USB CDC_Init_FS() 和 CDC_Receive_FS() 使用 CubeMX的主要内容,如果未能解决你的问题,请参考以下文章

stm32f0 uart编程

STM32F0系列——停机模式(低功耗)

STM32F0 SPI 环回 - MISO 上没有数据

STM32F0 DMA“输入溢出”

STM32F0,ST-link v2,OpenOCD 0.9.0:打开失败

Qt Serial Communictaion - 通过 Putty 与 stm32f0 板通信