cancel
Showing results for 
Search instead for 
Did you mean: 

UART4 on STM32F407 not receiving data

GScot
Associate II
static void MX_UART4_Init(void)
{
 
  huart4.Instance = UART4;
  huart4.Init.BaudRate = 9600;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
}
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration----------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_UART4_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  int x = 0;
  unsigned char mydata[24];
  HAL_UART_Receive_IT(&huart4, mydata, 2);
  while (1)
  {
 
  /* USER CODE END WHILE */
 
  /* USER CODE BEGIN 3 */
  if (x % 10000 == 0) {
      HAL_UART_Transmit_IT(&huart4, (unsigned char*)"/1Q\r", 4);
      //HAL_UART_Receive_IT(&huart4, mydata, 2);
  }
  ++x;
 
  }
  /* USER CODE END 3 */
 
}
void UART4_IRQHandler(void)
{
  /* USER CODE BEGIN UART4_IRQn 0 */
 
  /* USER CODE END UART4_IRQn 0 */
  HAL_UART_IRQHandler(&huart4);
  /* USER CODE BEGIN UART4_IRQn 1 */
 
  /* USER CODE END UART4_IRQn 1 */
}

Hello,

I'm trying to receive data on UART4 but cannot get it using either a call to HAL_UART_Receive_IT(&huart4, mydata, 2); or HAL_UART_Receive(&huart4, mydata, 2, 0xffff);

This code was generated using the STM32CubeMX program with only UART4 being enabled (and the system clock). Transmitting data works fine and I can see incoming data on the receive line. I can see the interrupt is enabled in Keil toolchain and all the bits seem to be set correctly in the UART registers. Also, I can get USART2 to work correctly.

Wonder if anyone might have an idea?

Many thanks,

Gary

P.S. I haven't included all the code because most of it is boilerplate from the Cube utility.

5 REPLIES 5
T J
Lead

I had a lot of trouble with U4 on the L073

but in the end, I was on the wrong PIN... PC12 instead of PC10... rookie mistake...

I cannot see your pin declarations.. but please check that first.

Do you get an interrupt when you transmit ?

That will indicate the interrupts are set correctly.

GScot
Associate II

Hi TJ. Thanks for your response.

Yes, transmit works fine and I can see both the Rx and Tx parts of the UART are set in the status bit. I'm going to recheck that I have the right pins. I know, that's an easy mistake to make, but I've check again and again and it looks right.

Thanks!!

-Gary

Try not observing the UART registers in the debugger if you did so.

JW

GScot
Associate II

This is embarrassing but wanted to follow up. It turns out that the voltage signal coming into the UART port was not going through the full voltage swing from 3.3 to GND. This was due to incorrect wiring on our board. Thank you for the responses but there was nothing in firmware that could compensate for the bad hardware.

Thanks for coming back with the solution. Please mark it as "best"

> incorrect wiring on our board

There's nothing to be ashamed of.

JW