cancel
Showing results for 
Search instead for 
Did you mean: 

UART framing error on STM32F0 device

gaetanbusson
Associate II
Posted on March 19, 2015 at 12:18

Hello!

I am currently using STM32F030R8T8 for communication purpose (RS485).

The functions I wrote to communicate seems to be working fine but most of the time the communication stops and refuses to restart.

I implemented the ''HAL_UART_ErrorCallback'' callback function to understand the bug. When the debugger stops in this callback, uart status is the following : ''HAL_UART_ERROR_FE''.

The only way I found to make the communication work again is to power off and on the microcontroller but this is not acceptable for the system I am currently developping.

My question is the following : How can I reinitialize UART softwarly from the ''HAL_UART_ErrorCallback'' callback function ?

I have already tried the following solutions but any of them are working :

-

         - 

Setting the error code to ''HAL_UART_ERROR_NONE''

-

         

- Setting the uart state to ''HAL_UART_STATE_READY''

-

         

- Calling ''HAL_UART_DeInit'' and then ''HAL_UART_Init'' functions

About framing error :

My microcontrroller is communicating with another STM32F030R8T8 that has exactly the same program (and so, the same clocking configuration). I use internal oscillator with no PLL and the frequency is low. Can I resolve my framing error by activation the PLL to give my microcontroller more accurency (my uart baudrate is 9600) ?

#uart-framing-error-on-stm32f0-de
1 REPLY 1
gregkam
Associate II
Posted on November 25, 2015 at 16:43

Hello, On My project I found a similar situation and after +3 days debugging to find the cause and its solution, essentially I solved with:

 

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)

{

  //I implement this .. but eventually you can decide with different approach

  //Many macros/proc on this code .. is just to be a hint (mem/rec about this possibility)

  __HAL_UART_CLEAR_OREFLAG(huart);

  __HAL_UART_CLEAR_NEFLAG(huart);

  __HAL_UART_CLEAR_FEFLAG(huart);

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

  __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);

 

 //The most important thing when UART framing error occur/any error is restart the RX process 

 if(huart->Instance == USART1)

  {

  

    //Restarting the RX, .. 1 byte. .. u8DATUartShortRxBuffer is My own rx buffer 

    HAL_UART_Receive_IT(huart, u8DATUartShortRxBuffer1, 1);

  }

  if(huart->Instance == USART2)

  {

  

    //Restarting the RX, .. 1 byte. 

    HAL_UART_Receive_IT(huart, u8DATUartShortRxBuffer2, 1);

 

  }

}

void MX_USART1_UART_Init(void)

{

  huart1.Instance = USART1;

  huart1.Init.BaudRate = 115200;

  huart1.Init.WordLength = UART_WORDLENGTH_8B;

  huart1.Init.StopBits = UART_STOPBITS_1;

  huart1.Init.Parity = UART_PARITY_NONE;

  huart1.Init.Mode = UART_MODE_TX_RX;

  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart1.Init.OverSampling = UART_OVERSAMPLING_16;

  huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;

  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  HAL_UART_Init(&huart1);

}

/* USART2 init function */

void MX_USART2_UART_Init(void)

{

  huart2.Instance = USART2;

  huart2.Init.BaudRate = 4800;

  huart2.Init.WordLength = UART_WORDLENGTH_8B;

  huart2.Init.StopBits = UART_STOPBITS_1;

  huart2.Init.Parity = UART_PARITY_NONE;

  huart2.Init.Mode = UART_MODE_TX_RX;

  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart2.Init.OverSampling = UART_OVERSAMPLING_16;

  huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;

  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

 

  //When parity is on use, from ST forum => 8-bit + parity is configured as 9-bit in STM32 implementation.

 

#if 0

//In some case, own ckts, is necessary a pin swap .. on My case it's disabled

huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT | UART_ADVFEATURE_SWAP_INIT;

huart2.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;

 huart2.AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE;

#endif

 

  HAL_UART_Init(&huart2);

}

===================================================================================================

On My case, after a power-on the reception stops on UART2, but the transition still happens.

I observed:

1.) that on generated HAL codes was not including a basic code for the function

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart), I put on My own module this above function;

2.) On debug mode by Keil, on TX/RX  no any problem was observed when the firmware was transferred directly (F8) without a new power-on;

3.) I think that when an error occur/HAL_UART_ErrorCallback, the reception must be restarted as on HAL_UART_ErrorCallback, independentlly of the STM32 families (STM32F0x, STM32F1x, STM32F2x, STM32F3x, STM32F4, ..)

4.) On real world Common errors that can happens and eventually stops the reception/communication w/ HAL_UART_ERROR_FE, HAL_UART_ERROR_NE, errors .. be aware and implement the re-start reception as similar above (HAL_UART_ErrorCallback).

#define HAL_UART_ERROR_FE        ((uint32_t)0x00000004)

#define HAL_UART_ERROR_NE        ((uint32_t)0x00000002)

Minor things that I observed:

With STM32CubeMX was configured all pin outs and generated the HAL device drivers.

After that all pin out was rightly configured I put attention to the clock sources (to be confident that clock jitter is not  problem source)

(for UART1 clock Mux, I'm using SYSCLK=48MHz, PLLCLK from  with one crystal of 8MHz that the PLL Source Mux->HSE was adjusted to achieve the desired clock/48MHz)

PLL Source Mux -> HSE, PREDiv=/1, VCOInput=8, PLLMul=x6 ..

I'm using Keil MDK-ARM 5.16a IDE, as compiler tools..

Tool Version Numbers:

Toolchain:        MDK-ARM Professional  Version: 5.16a

Toolchain Path:    C:\Keil_v5\ARM\ARMCC\Bin

C Compiler:         Armcc.exe        V5.06 (build 20)

Assembler:          Armasm.exe        V5.06 (build 20)

Linker/Locator:     ArmLink.exe        V5.06 (build 20)

Library Manager:    ArmAr.exe        V5.06 (build 20)

Hex Converter:      FromElf.exe        V5.06 (build 20)

CPU DLL:               SARMCM3.DLL          V5.16a

Dialog DLL:         DCM.DLL              V1.13.2.0

Target DLL:             STLink\ST-LINKIII-KEIL_SWO.dll      V2.0.17.0

Dialog DLL:         TCM.DLL              V1.14.5.0

STM32CubeMX 4.11.0,  Firmware package for Family STM32F0 1.4.0

STM32F042C6Tx uController with LQFP48 package.

UART1Tx = PIN30, UART2Rx=PIN31, UART2Tx=12 and UART2Rx=13

My microcontrroller is communicating with another embedded coomunication module (RF/module) by USART2.

Eventually this info can help you or someone else/other,

Greg