0
votes

I am trying to set up a communication between my STM32F4 - Discovery with Open 407V-D development board and a peripheral using UART3 as a RS-485 bus.

I have problem with my communication becouse Rx state of UART remain busy.

Could somebody please explain me what am I doing wrong?

Should I somehow edit HAL_UART_IRQHandler or what setting am I missing?

Here is my code:

#include "main.h"
#include "stm32f4xx_hal.h"


UART_HandleTypeDef huart3; 
uint8_t Ocular_1_RxBuffer[4]; 
uint8_t Ocular_1_TxBuffer[2] = {0x01,0x86};

__IO ITStatus UartReady;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);

int main(void)
{


 HAL_Init();
 SystemClock_Config();

 MX_GPIO_Init();
 MX_USART3_UART_Init();


HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);   //set RS 485 into transmit mode

while (1)
{
    int Timeout = 1000000;

while(huart3.gState != HAL_UART_STATE_READY)        //wait for UART
{
    Timeout--;
    if(Timeout == 0) 
    Error_Handler();
}
Timeout = 1000000;
if(HAL_UART_Transmit_IT(&huart3, (uint8_t*)Ocular_1_TxBuffer, 2) != HAL_OK)     //Send request
{
    Error_Handler();
}
while(huart3.RxState != HAL_UART_STATE_READY)                           //wait for UART     
{
    Timeout--;
    if(Timeout == 0) 
    Error_Handler();
}
Timeout = 1000000;
if(HAL_UART_Receive_IT(&huart3, (uint8_t*)Ocular_1_RxBuffer, 4) != HAL_OK)                  //Response
{
    Error_Handler();
}
while(UartReady == RESET)                                               //Wait for response
{
    Timeout--;
    if(Timeout == 0) 
    Error_Handler();
}
}


}

I have successfully received response from my peripheral device, but my code generate Error_Handler() after HAL_UART_RxCpltCallback() function.

Could somebody please explain this behavior to me?

My callback functions:

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
  /* Set transmission flag: transfer complete */
 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);
 UartReady = RESET;

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
 /* Set transmission flag: transfer complete */
 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);
 UartReady = SET;
}
1

1 Answers

0
votes

Please mention the number of bytes you are receiving in current scenario.?

Some debugging tips are -

  1. Try increasing the size of your buffer and check if you are receiving any data.

  2. Make sure you are re initialising your buffer after 4 bytes are read. If not the buffer can overflow and may lead to error handler.

  3. Make sure you transmitter always sends 4 bytes.

  4. Confirm if your buad rate matches on both devices. Also settings like parity and all are same in receiver and transmitter.

  5. After every 4 bytes you need to call the HAL_UART_Receive_IT() again to configure and wait for next interrupt.

  6. Add Error callback too, and confirm if execution moves to this callback. If then add prints in driver to find out whats the error cause, whether its like overrun error / Noise error / Parity Error etc.