cancel
Showing results for 
Search instead for 
Did you mean: 

RS485 Communication is not working properly

Vaclav Chrascina
Associate II
Posted on December 08, 2017 at 09:24

Hello,

I got connected measuring device to my STM32F4 - Discovery board (with Open407V-D board). They are communication via RS 485 bus. Measuring device should provide result of measurement after request., but after some time UART remain in BUSY state. I get valid results for cca 4 cycles and then UART freeze. Could you help me to find problem?

Here is my main() code:

int main(void)

{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  MX_USART3_UART_Init();

    

    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);

  while (1)

    {

        while(huart3.gState != HAL_UART_STATE_READY);

        HAL_UART_Transmit_IT(&huart3, (uint8_t*)aTxBuffer, 2);

        while(huart3.RxState != HAL_UART_STATE_READY);

        HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4);

        while(DataRecieved == 0);

        GetMeasurementValue();

        MeasuredDistance = MeasurementValue * 10.0 / 16384.0;

  }

}

After IRQ I am managing RS 485 input/output pin and variable:

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)

{

  /* Set transmission flag: transfer complete */

    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);

    DataRecieved = 0;

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)

{

  /* Set transmission flag: transfer complete */

    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);

    DataRecieved = 1;

}

And my UART settings:

static void MX_USART3_UART_Init(void)

{

  huart3.Instance = USART3;

  huart3.Init.BaudRate = 38400;

  huart3.Init.WordLength = UART_WORDLENGTH_9B;

  huart3.Init.StopBits = UART_STOPBITS_1;

  huart3.Init.Parity = UART_PARITY_EVEN;

  huart3.Init.Mode = UART_MODE_TX_RX;

  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart3.Init.OverSampling = UART_OVERSAMPLING_16;

  if (HAL_UART_Init(&huart3) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

Any suggestions where is problem? Thank you very much.

1 REPLY 1
Jack Peacock
Associate II
Posted on December 09, 2017 at 03:56

Is the RS-485 full or half duplex?  If half, how do you handle line turnaround?

Is HAL_UART_TxCpltCallback() called after the USART TXNE or the TC interrupt?  For half duplex you must wait for the TC after the last byte of transmission, before turning around the line, otherwise the last byte will be corrupted.  TXNE will tell you when to send the next byte of a frame, but the frame does not end until TC is asserted.

When the USART 'freezes' what do the USART status registers show, and is it sending or receiving?  Where do you clear receive errors?  If an error occurs (overrun, framing, parity) it may  produce the symptoms you describe.  Some STM32 parts have an errata entry regarding this.

I don't use the HAL so no idea how much (or how little) HAL calls actually do.  I use RS-485 in half duplex mode and do see an occasional RX error so do not assume communications are miraculously error free.

  Jack Peacock