Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the UART baud rate after running on STM32 board?

I'm using STM32F746ZG and use five UARTs. All UARTs are working fine. Can someone tell me the procedure to change the baud rate on the USART once it has already been initialized? I'm using USART6 and initialized with 9600 baud rate. After booting, there is no any communication through USART. I want to change the baud rate from 9600 to 57600 or 115200. For this changing, I called HAL_UART_DeInit() and MX_USART6_UART_Init_57600() but it doesn't work. If I didn't change the baud rate, it works fine. But if I change the baud rate, I can't receive the data through USART. If somebody knows the solution, please let me know.

The followings are my code.

int main(void)
{
  HAL_Init();

  SystemClock_Config();


  MX_UART7_Init();
  MX_UART8_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  MX_USART6_UART_Init();

}

void MX_USART6_UART_Init(void)
{
  huart6.Instance = USART6;
  huart6.Init.BaudRate = 9600;
  huart6.Init.WordLength = UART_WORDLENGTH_8B;
  huart6.Init.StopBits = UART_STOPBITS_1;
  huart6.Init.Parity = UART_PARITY_NONE;
  huart6.Init.Mode = UART_MODE_TX_RX;
  huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  huart6.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart6.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart6) != HAL_OK)
  {
    Error_Handler();
  }
}

void MX_USART6_UART_Init_57600(void)
{
  huart6.Instance = USART6;
  huart6.Init.BaudRate = 57600; // change from 9600 to 57600
  huart6.Init.WordLength = UART_WORDLENGTH_8B;
  huart6.Init.StopBits = UART_STOPBITS_1;
  huart6.Init.Parity = UART_PARITY_NONE;
  huart6.Init.Mode = UART_MODE_TX_RX;
  huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  huart6.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart6.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart6) != HAL_OK)
  {
    Error_Handler();
  }
}

int Change_UART(void)
{
  HAL_UART_DeInit(&huart6);
  MX_USART6_UART_Init_57600();

}

I called Change_UART() but it doesn't work.

like image 221
Hans Avatar asked Jul 31 '19 04:07

Hans


3 Answers

Your question should be: how to change the bautrate using the bloatware HAL?

I do not know.

But it can be archived in 3 lines of the simple code.

USART6 -> CR1 &= ~(USART_CR1_UE);
USART6 -> BRR = NEWVALUE;
USART6 -> CR1 |= USART_CR1_UE;
like image 100
0___________ Avatar answered Oct 19 '22 09:10

0___________


For changing baudrate you don't need to reset UART peripheral, just stop any active transfers (polling/IT/DMA). I use a mix of both:

huart.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), new_baudrate);

Where UART_BRR_SAMPLING8() is a macro from stm32f4xx_hal_uart.h and HAL_RCC_GetPCLK2Freq() function comes from _hal_rcc.c.

This way I don't have to calculate BRR values manually, nor execute the whole initialization procedure, which actually toggles GPIO states, thus generating noise on serial line for whatever is sitting on other end of it.

like image 35
stiebrs Avatar answered Oct 19 '22 09:10

stiebrs


You have to abort all running HAL_UART funttions, then de-initialize the uart, change the baudrate init value and initialize it again:

  1. HAL_UART_Abort_IT(&huart1);
    
  2. HAL_UART_DeInit(&huart1);
    
  3. huart1.Init.BaudRate = 57600;
    
  4. if (HAL_UART_Init(&huart1) != HAL_OK) {
        Error_Handler();
    }
    
  5. if (HAL_UART_Receive_IT(&huart1, BUFFER, YOUR_BUFFER_SIZE) != HAL_OK) {
        Error_Handler();
    }
    
like image 1
dbuchber Avatar answered Oct 19 '22 09:10

dbuchber