【发布时间】: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);
}
这里的行(使用这个缓冲区)似乎是罪魁祸首:
USBD_CDC_SetRxBuffer(hUsbDevice_0, s_RxBuffers.UserRxBufferFS[s_RxBuffers.Buffer_Number_Receiving] );//Set the buffer to receive incoming data
任何帮助/见解将不胜感激。
【问题讨论】:
-
“崩溃”?那是什么?错误?数据中止?爆炸?有什么具体的吗?
-
我给了一个截图“循环”。它以无限循环的形式出现在 Default_Handler 中。
-
然后通过检查中断状态和故障寄存器找出是哪个异常将您带到那里。
-
您的问题似乎是关于它是在工作的顶部还是底部部分,但假设顶部不工作我会怀疑内存不足,因为您在 F0 上。