cancel
Showing results for 
Search instead for 
Did you mean: 

Auto detect budrate serial

ibrahim
Associate II

Posted on June 28, 2016 at 11:48

Hello,

I have a STM32L031K6 and I want some auomatiser my reception function is adding automatic detection of transmission speed (baud rate) how I can do.

thank you very much

11 REPLIES 11
Nesrine M_O
Lead II
Posted on June 28, 2016 at 13:21

Hi enedis.ibrahim,

The USART  peripheral in the STM32L0 embed the Auto baud rate detection by hardware, please refer to the Auto baud rate detection paragraph in the USART section in your related reference manual to have more details about this feature  

-Syrine-

ibrahim
Associate II
Posted on June 28, 2016 at 13:36 Hello Syrine, thank you for your answer, I saw that I can configure it but i not find a code example onlinehow to do it :\ . Thanks again.

huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_AUTOBAUDRATE_INIT;
huart2.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
huart2.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT;

Nesrine M_O
Lead II
Posted on June 28, 2016 at 14:40

Hi enedis.ibrahim ,

In the USART init function add the code below:

/*##Configure the AutoBaudRate method */
UartHandle.AdvancedInit.AdvFeatureInit =UART_ADVFEATURE_AUTOBAUDRATE_INIT;
UartHandle.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
UartHandle.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT; //Mode0
if (HAL_UART_Init(&UartHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/* Wait until Receive enable acknowledge flag is set */
while(__HAL_UART_GET_FLAG(&UartHandle,UART_FLAG_REACK) == RESET)
{}
/* Wait until Transmit enable acknowledge flag is set */ 
while(__HAL_UART_GET_FLAG(&UartHandle,UART_FLAG_TEACK) == RESET)
{}
/* Loop until the end of Autobaudrate phase */
while(__HAL_UART_GET_FLAG(&UartHandle,UART_FLAG_ABRF) == RESET)
{}

“I saw that I can configure it but i not find a code example online�? I recommend you to have a look to this SPL example: STM32F30x_DSP_StdPeriph_Lib_V1.2.3\Projects\STM32F30x_StdPeriph_Examples\USART\USART_AutoBaudRate -Syrine-
ibrahim
Associate II
Posted on June 28, 2016 at 15:16 Thnks syrine, Before isaw this site but the trick if I can not get to know my LIB What is

/* USART enable */
USART_Cmd(EVAL_COM1, ENABLE);
/* Configure the AutoBaudRate method */
USART_AutoBaudRateConfig(EVAL_COM1, USART_AutoBaudRate_StartBit);
/* Enable AutoBaudRate feature */
USART_AutoBaudRateCmd(EVAL_COM1, ENABLE);

Thnaks.
ibrahim
Associate II
Posted on June 28, 2016 at 16:03 Hello syrine; Here's my setup but I get nothing. thnks for all. Usart.c /* Configuration */

UART_HandleTypeDef huart2;
/* USART2 init function */
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_EVEN;
huart2.Init.Mode = UART_MODE_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
huart2.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR; 
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_AUTOBAUDRATE_INIT;
/* Configure the AutoBaudRate method */
huart2.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT; //Mode 0
/* Enable AutoBaudRate feature */
huart2.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
/* Wait until Receive enable acknowledge flag is set */
while(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_REACK) == RESET)
{} 
/* Wait until Transmit enable acknowledge flag is set */ 
while(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TEACK) == RESET)
{} 
/* Loop until the end of Autobaudrate phase */
while(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_ABRF) == RESET)
{}
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
} 
}

Main.c

int main(void)
{
//Initialisation de la carte STM32L031K6
HW_Init(); 
while (1)
{
}

Hardware.c /* Initalisation */

void HW_Init(void)
{ 
/* Remise à zéro de tous les périphériques, Initialise l'interface flash */
/* et System Timer (SysTick) */
HAL_Init();
/* Configuration de l'horloge à 8Mhz pour notre cas */
SystemClock_Config();
/* Initialisation tous les périphériques configurés GPIO (Led verte) */
/* et USART2*/
MX_GPIO_Init();
MX_USART2_UART_Init();
/* If AutoBaudBate error occurred */
if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_ABRE) != RESET)
{}
else
{ 
/* Wait until RXNE flag is set */
while(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE) == RESET)
{}
/* Wait until TXE flag is set */ 
while(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE) == RESET)
{}
/* Receive data */
HAL_UART_Receive_IT(&huart2, Rx_data, 1);
}
}

IRQ_Receive.c /* After interruption receive */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
//Tester L'UART selectionner en l'occurence pour nous c'est l'USART2
if (huart->Instance == USART2)
{
/* Appeler notre fonction receive qui permet de sauvegarder chaque octet */
/* reçu dans un Buffer */
Receive();
 HAL_UART_Receive_IT(&huart2, Rx_data, 1); 

}
}

Posted on June 28, 2016 at 18:28

Here's my setup but I get nothing.

Yes, well changing variables in a structure does not initialize the hardware.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ibrahim
Associate II
Posted on June 29, 2016 at 08:49

Hello my friend,

How can I do ? thank you very much

Posted on June 29, 2016 at 13:27

Look harder at how Syrine initialized the USART and then look at yours.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ibrahim
Associate II
Posted on June 29, 2016 at 16:53

Hello,

I corrected my mistake thnks but I would like to read the estimate obtained by the USART to do if he select the baud rate 9600 to an operation if he found otherwise 1200 will be another operation. I found this book &huart2.instance-> BRR but I can't to compare this value with 834 for 9600 Baude or 6762 for 1200 Baude beaucaude this registe is for uint32_t * and I would also like to do a software reset. Thank you for your kindness.