cancel
Showing results for 
Search instead for 
Did you mean: 

stm32F4 UART Hardware Flow RTS/CTS + interrupts

fritz
Associate II
Posted on August 31, 2016 at 11:52

Hello,

I am currently working on a project to communicate with a Bluetooth module (BT121), to do that I use the  STM32F407 eval board. Here is my configuration :

  // Initialize the UART3, huart3

  huart3.Instance = USART_SERIAL;

  huart3.Init.BaudRate = USART_SERIAL_BAUDRATE;

  huart3.Init.WordLength = UART_WORDLENGTH_8B;

  huart3.Init.StopBits = UART_STOPBITS_1;

  huart3.Init.Parity = UART_PARITY_NONE;

  huart3.Init.Mode = UART_MODE_TX_RX;

  huart3.Init.HwFlowCtl = UART_HWCONTROL_RTS_CTS;

  huart3.Init.OverSampling = UART_OVERSAMPLING_16;

  HAL_UART_Init(&huart3);

After that I added in void HAL_UART_MspInit(UART_HandleTypeDef* huart) the following code :

__HAL_RCC_GPIOB_CLK_ENABLE()

__USART3_CLK_ENABLE();

 

    /**USART3 GPIO Configuration    

    PB11     ------> USART3_RX

    PB10     ------> USART3_TX

    PB14     ------> USART3_RTS

    PB13     ------> USART3_CTS

    */

    //Rx and Tx pins

    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL; //GPIO_PULLUP;  //

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    //RTS and CTS pins

    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /*♯♯-3- Configure the NVIC for UART  */  

    /* NVIC for USARTx */

    HAL_NVIC_SetPriority(USART3_IRQn, 0, 1);

    HAL_NVIC_EnableIRQ(USART3_IRQn);

   

And the interrupt handler :

void USART3_IRQHandler(void)

{

   uint32_t isrflags   = READ_REG(huart3.Instance->SR);

   uint32_t cr1its     = READ_REG(huart3.Instance->CR1);

   uint32_t cr3its     = READ_REG(huart3.Instance->CR3);

   uint32_t errorflags = 0x00U;

   uint32_t dmarequest = 0x00U;

   /* If no error occurs */

  errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));

   if(errorflags == RESET)

  {

    /* UART in mode Receiver -------------------------------------------------*/

    if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))

    {

       __HAL_UART_CLEAR_FLAG(&huart3, UART_IT_RXNE);

   

      // Save the data in the buffer

      StringLoop[rx_index++]  = (uint8_t)(huart3.Instance->DR & (uint16_t)0x01FF);

      

      if(--huart3.RxXferCount == 0)

    {

      __HAL_UART_DISABLE_IT(&huart3, UART_IT_RXNE);

      /* Check if a transmit process is ongoing or not */

      if(huart3.State == HAL_UART_STATE_BUSY_TX_RX)

      {

        huart3.State = HAL_UART_STATE_BUSY_TX;

      }

      else

      {

        /* Disable the UART Parity Error Interrupt */

        __HAL_UART_DISABLE_IT(&huart3, UART_IT_PE);

        /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */

        __HAL_UART_DISABLE_IT(&huart3, UART_IT_ERR);

        huart3.State = HAL_UART_STATE_READY;

      }

      

      while (HAL_UART_GetState(&huart3) != HAL_UART_STATE_READY)

      {

      }

    }

    }

  }

  /* UART in mode Transmitter ------------------------------------------------*/

  if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))

  {

    UART_Transmit_IT(&huart3);

    //return;

  }

 

  /* UART in mode Transmitter end --------------------------------------------*/

  if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))

  {

    UART_EndTransmit_IT(&huart3);

    //return;

  }

This code is working because I can communicate but at some point RTS pin from STM32F4 stays high, I checked and a parity error flag is reported and when I check my uart registers I have:

SR   ====>0x08012331

DR  ====>0x08012335

BRR====>0x08012339

CR1 ====>0x0801233D

CR2 ====>0x00000000

CR3 ====>0x00000000

GTPR ====>0x00000000

If you have an idea on this issue please help me.

PS : I add an image of the logic analyser.

Thank you.

#stm32f4 #usart
4 REPLIES 4
fritz
Associate II
Posted on September 01, 2016 at 09:32

Hi Hannibal,

Thanks for the reply, I checked the thread and so far I think I do not have the same problem also I do not have an ''overrun error''. In my case it looks like when I have my issue the uart is in a weird mode when you see the register state it doesn't make any sense.

Fritz

Walid FTITI_O
Senior II
Posted on September 01, 2016 at 18:59

Hi 

After checking the I/O configuration and pins on datasheet, you would chech the exter,nal module circuit and connection with the Eval board. Keep the manual/datasheet in your hand.

one other point, are you using CAN ? because it might be sharing the same pin with USART. If it is the case you would make the remapping like described in some threads around.

-Hannibal-
fritz
Associate II
Posted on September 06, 2016 at 12:15

Hi,

I checked everything and I still have my issue. Also Hannibal the CAN is not used in my application. If someone has some ideas feel free to share.

Thanks in advance,