2016-06-28 02:48 AM
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
2016-06-28 04:21 AM
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-2016-06-28 04:36 AM
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_AUTOBAUDRATE_INIT;
huart2.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
huart2.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT;
2016-06-28 05:40 AM
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-
2016-06-28 06:16 AM
/* 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.
2016-06-28 07:03 AM
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);
}
}
2016-06-28 09:28 AM
Here's my setup but I get nothing.
Yes, well changing variables in a structure does not initialize the hardware.2016-06-28 11:49 PM
Hello my friend,
How can I do ? thank you very much2016-06-29 04:27 AM
Look harder at how Syrine initialized the USART and then look at yours.
2016-06-29 07:53 AM