我通过UART接收数据并存入环形缓冲区。当缓冲区小于256字节时运行正常,但当缓冲区更大时,程序会在代码的第一个for循环处卡住。
以下是我的环形缓冲区实现代码。
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
if(huart->Instance == USART1){
static uint8_t old_pos1 = 0;
uint8_t *ptemp1;
uint8_t i;
if (Size != old_pos1){
if (Size > old_pos1){
ReceivedChars1 = Size - old_pos1;
for (i = 0; i < ReceivedChars1; i++){
pBufferReadyForUser1[i] = RXBufferUser1[old_pos1 + i];
}
}else{
ReceivedChars1 = RX_BUFFER_SIZE - old_pos1;
for (i = 0; i < ReceivedChars1; i++){
pBufferReadyForUser1[i] = RXBufferUser1[old_pos1 + i];
}
if (Size > 0){
for (i = 0; i < Size; i++){
pBufferReadyForUser1[ReceivedChars1 + i] = RXBufferUser1[i];
}
ReceivedChars1 += Size;
}
}
UserDataTreatment(huart, pBufferReadyForUser1, ReceivedChars1);
ptemp1 = pBufferReadyForUser1;
pBufferReadyForUser1 = pBufferReadyForReception1;
pBufferReadyForReception1 = ptemp1;
}
old_pos1 = Size;
}
}
以下是缓冲区的声明
#define RX_BUFFER_SIZE 200
#define USB_BUFFER_SIZE 200
#define BAUDRATE 57600
// Buffer y contadores de USART1
uint8_t RXBufferUser1[RX_BUFFER_SIZE];
uint8_t RXBufferA1[RX_BUFFER_SIZE];
uint8_t RXBufferB1[RX_BUFFER_SIZE];
uint8_t *pBufferReadyForUser1;
uint8_t *pBufferReadyForReception1;
__IO uint32_t ReceivedChars1;
将RX_BUFFER_SIZE和USB_BUFFER_SIZE增加到超过255就让我的代码崩溃
我使用的是STM32F407VET6,STMCubeIDE的构建分析器显示我使用的CCMRAM、RAM和FLASH都不到15%。所以我认为这不是内存不足的问题。
|
|