cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303k8 UART receive data read loop issue

a1rWAXTA
Associate II

Hello,

When I try to read USART status register (RXNE bit), the execution stucks upon while loop which means no data have been readen: 

 

char uart2_read(void)
{
	while(!(USART2->ISR & USART_ISR_RXNE)){}

	return USART2->RDR;
}

 

I think I initialized everying neccessary to receive (and transmit) data:

 

void uart2_rxtx_init(void)
{
	RCC->AHBENR |= RCC_GPIOA_ENABLE_FLAG;
	RCC->APB1ENR = RCC_USART2_ENABLE_FLAG;

	//PA2 ALTERNATIVE FUNCTION SET
	GPIOA->MODER &= ~(1U << 4);
	GPIOA->MODER |= (1U << 5);

	//AF7
	GPIOA->AFR[0] |= (1U << 8);
	GPIOA->AFR[0] |= (1U << 9);
	GPIOA->AFR[0] |= (1U << 10);
	GPIOA->AFR[0] &= ~(1U << 11);

	//PA3 ALTERNATIVE FUNCTION SET
	GPIOA->MODER &= ~(1U << 6);
	GPIOA->MODER |= (1U << 7);

	//AF7
	GPIOA->AFR[0] |= (1U << 12);
	GPIOA->AFR[0] |= (1U << 13);
	GPIOA->AFR[0] |= (1U << 14);
	GPIOA->AFR[0] &= ~(1U << 15);

	USART2->BRR = (uint16_t)(HSI_FREQ_HZ / UART_BAUDRATE);
	USART2->CR1 = (CR1_TE | CR1_RE);
	USART2->CR1 |= (1U << 0);
}

 

I use realterm terminal for the communication. Can someone explain why it doen't work? 

1 REPLY 1
Saket_Om
ST Employee

Hello @a1rWAXTA ,

 

Please make sure to include the following settings in your Init function:

  • StopBits
  • WordLength
  • Parity
  • CLKPolarity
  • CLKPhase
  • CLKLastBit
If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar