Category Archives: Repair Tricks

Be Careful, There are Bugs !

Pitfalls in STM32 Standard Peripheral Library & Possible Solutions

#1 : USART DATA TRANSFER FUNCTIONS, NO CHECK ON TXE & RXNE FLAGS :P

Be Careful, There are Bugs !

 

 

 

 

 

 

 

 

 

Where ? in  STM32f4xx_usart.c

void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
  /* Check the parameters */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
  assert_param(IS_USART_DATA(Data));
  /* Transmit Data */
  USARTx->DR = (Data & (uint16_t)0x01FF);
}

In the above function USART_SendData(…. , ….) There is no check for Transmit complete flag which goes High when the content of TDR register has been transferred into the shift register. Since this check was unavailable in the code provided by ST, Whenever you try to transmit too much of data¬†continuously over USART some of the data goes unnoticed and could not be sent as we are not waiting till the last byte got transmitted. This issues can be corrected by adding a simple line of code in the function as given below.

void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
  /* Check the parameters */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
  assert_param(IS_USART_DATA(Data));
  /* Transmit Data */
  USARTx->DR = (Data & (uint16_t)0x01FF);
  while((USARTx->SR & (1<<7))==0); // Wait till last byte got transmitted   

}

Similar kind of Correction is required in USART_ReceiveData

 

 

If you also find some improvement ot Bug in this library or related information that can be useful for others, Do share with us. Thanks