cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 freezes when UART ringbuffer user bigger buffer

SGonz.2
Associate II

Hello!

I'm receiving data by UART in a ring buffer. When my buffers are lower than 256 it works, but when higher, it get stuck in the first for loop of my code.

This is my ring buffer.

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;
  }
}

And here the declarations of buffers.

#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;

What I'm doing wrong? Increasing RX_BUFFER_SIZE and USB_BUFFER_SIZE over 255 is enough to break my code :(

I'm using a STM32F407VET6, STMCubeIDE shows in the builder analyzer that I'm using less than 15% of the CCMRAM, RAM and FLASH. So I think it's not a memory shortage.

1 ACCEPTED SOLUTION

Accepted Solutions

Use wider variables, 32-bit counters are arguably more efficient any way

uint8_t i;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

Use wider variables, 32-bit counters are arguably more efficient any way

uint8_t i;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

What a silly error! I was focusing on buffers and forgot about that little counter.

Thank you very much! Now it works!