简体   繁体   English

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

[英]STM32F0 USB CDC_Init_FS() and CDC_Receive_FS() using CubeMX

I am using this code to capture data over the USB. 我正在使用此代码通过USB捕获数据。 The top code crashes the program once a character is received. 收到字符后,顶层代码会使程序崩溃。 The bottom works just fine, although I cannot save the data as I want. 底部工作得很好,尽管我无法按需保存数据。 The top crashes (endless loop) even before calling CDC_Receive_FS() ...which is never called. 甚至在调用CDC_Receive_FS()之前,它的顶部崩溃(无休止的循环CDC_Receive_FS() ……这从未被调用过。 The bottom calls CDC_Receive_FS() as expected. 底部按预期方式调用CDC_Receive_FS()

For the life of me, I cannot see what is wrong with how I am calling my struct that holds an array of buffers that I loop through. 在我的一生中,我看不到如何调用保存循环循环的缓冲区的结构有什么问题。

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

This does not: 这不是:

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

The line here (the use of this buffer) seems to be the culprit: 这行(使用此缓冲区)似乎是罪魁祸首:

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

Any help/insight would be greatly appreciated. 任何帮助/见解将不胜感激。

I would make the Rx_Buffer monodimensional and handle the command history separatly. 我将使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;

Apart from this, since you are usinc a struct (s_RxBuffers type), I think you are not passing the buffer as a pointer in the right way to your function. 除此之外,由于您使用的是结构体(s_RxBuffers类型),我认为您没有以正确的方式将缓冲区作为指针传递给函数。 I think you should do something like this: 我认为您应该执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM