cancel
Showing results for 
Search instead for 
Did you mean: 

Why does HAL_UART_Receive keep crashing?

Andrew Hazelton
Associate III
Posted on September 26, 2017 at 13:32

Hi. Can someone please help me see where I am going wrong? The program will transmit the UART message but crashes when it gets to the 'HAL_UART_Receive' function. I am using the STM32F746 Discovery Board.

UART_HandleTypeDef MobileGNSSUartHandle;

uint8_t UART_Rx_Buffer[100];

uint8_t UART_Tx_Buffer[] = 'AT\r';

GPIO_InitTypeDef GPIO_InitStruct;

HAL_Init();

/* Peripheral clock enable */

__HAL_RCC_USART6_CLK_ENABLE();

/**USART6 GPIO Configuration

PC7 ------> USART6_RX

PC6 ------> USART6_TX

*/

GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF8_USART6;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

// Enable GPIOs clock

__HAL_RCC_GPIOC_CLK_ENABLE();

// Initialise UART6

MobileGNSSUartHandle.Instance = USART6;

MobileGNSSUartHandle.Init.BaudRate = 115200;

MobileGNSSUartHandle.Init.WordLength = UART_WORDLENGTH_8B;;

MobileGNSSUartHandle.Init.StopBits = UART_STOPBITS_1;

MobileGNSSUartHandle.Init.Parity = UART_PARITY_NONE;

MobileGNSSUartHandle.Init.Mode = UART_MODE_TX_RX;

MobileGNSSUartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;

MobileGNSSUartHandle.Init.OverSampling = UART_OVERSAMPLING_16;

MobileGNSSUartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

MobileGNSSUartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

HAL_UART_Init(&MobileGNSSUartHandle);

HAL_UART_Transmit( &MobileGNSSUartHandle, (uint8_t *)UART_Tx_Buffer, 3, 5000 );

HAL_UART_Receive( &MobileGNSSUartHandle, (uint8_t *)UART_Rx_Buffer, 4, 5000 );
2 REPLIES 2
Ben K
Senior III
Posted on September 26, 2017 at 15:25

The program will transmit the UART message but crashes when it gets to the 'HAL_UART_Receive' function.

Can you be more specific than that? Does the function return with timeout, or a HardFault is raised, or what? Can you provide the contents of the USART's status register to see if a communication error is present?

Andrew Hazelton
Associate III
Posted on October 08, 2017 at 01:42

Hi.

I ended up figuring out this problem. I needed to do the following:

1. GPIOC needs to be enabled before the pins are configured.

2. To make the timeout of HAL_UART_xxx() work I needed to provide a tick to the HAL.

Since the SysTick Handler is also used by embOS, it is defined in RTOSInit_STM32F7x_CMSIS.c, add HAL_IncTick() after OS_TICK_Handle().

/* Peripheral clock enable */

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_USART6_CLK_ENABLE();

/**USART6 GPIO Configuration

PC7 ------> USART6_RX

PC6 ------> USART6_TX

*/

GPIO_InitStruct.Pin = GPIO_PIN_6;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF8_USART6;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF8_USART6;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

// Initialise UART6

MobileGNSSUartHandle.Instance = USART6;

MobileGNSSUartHandle.Init.BaudRate = 115200;

MobileGNSSUartHandle.Init.WordLength = UART_WORDLENGTH_8B;

MobileGNSSUartHandle.Init.StopBits = UART_STOPBITS_1;

MobileGNSSUartHandle.Init.Parity = UART_PARITY_NONE;

MobileGNSSUartHandle.Init.Mode = UART_MODE_TX_RX;

MobileGNSSUartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;

MobileGNSSUartHandle.Init.OverSampling = UART_OVERSAMPLING_16;

MobileGNSSUartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

MobileGNSSUartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if(HAL_UART_DeInit(&MobileGNSSUartHandle) != HAL_OK)

{

SEGGER_RTT_WriteString(0, 'ERROR: Failed to de-initialize UART!\r\n');

}

if(HAL_UART_Init(&MobileGNSSUartHandle) != HAL_OK)

{

SEGGER_RTT_WriteString(0, 'ERROR: Failed to initialize UART!\r\n');

}