gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:01:31 25 4
gpt4 key购买 nike

我正在使用此代码通过 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

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

最佳答案

我会将 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

关于c - 使用 CubeMX 的 STM32F0 USB CDC_Init_FS() 和 CDC_Receive_FS(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36987390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com