STM32L496RGT, HAL and UART
Hello all,
I'm having some serious problems communicating with a device attached to USART2 on a custom STM32L496RGT, board, configured in CubeMX.
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
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_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{I reset the device using a GPIO signal at which point it sends the string "%REBOOT%" to the USART.
If I set up the USART to generate an RXNE interrupt which I handle in my own code and store in a ring buffer, the string is received without difficulty:
const int buf_sz = 32;
char buf[buf_sz];
HAL_GPIO_WritePin(BL_RST_GPIO_Port, BL_RST_Pin, GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(BL_RST_GPIO_Port, BL_RST_Pin, GPIO_PIN_SET);
read_from_ring(buf_sz, buf);
Which I'm taking to demonstrate the the USART baud rate etc has been correctly configured and that the board wiring is OK.
However, if I don't enable the interrupt in CubeMX and just use the HAL call:
const int buf_sz = 32;
char buf[buf_sz];
HAL_GPIO_WritePin(BL_RST_GPIO_Port, BL_RST_Pin, GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(BL_RST_GPIO_Port, BL_RST_Pin, GPIO_PIN_SET);
HAL_StatusTypeDef hal = HAL_OK;
for(int i = 0; hal == HAL_OK && i < cnt; ++i) {
hal = HAL_UART_Receive(&huart2, &buf[i], 1, 1000);
}I either receive one character or nothing at all, all terminated with a timeout I also tried a single read
const int buf_sz = 32;
char buf[buf_sz];
HAL_GPIO_WritePin(BL_RST_GPIO_Port, BL_RST_Pin, GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(BL_RST_GPIO_Port, BL_RST_Pin, GPIO_PIN_SET);
HAL_StatusTypeDef hal = HAL_UART_Receive(&huart2, buf, buf_sz, 1000);with the same result: either nothing or just one character gets read and then there's a timeout
Using the interrupt handler to read would actually fine for me, but the problem is that sending strings from the MCU also seems to fail, and I'm guessing that it's for the same reason that the simple read fails.
Is there some trick to getting STM32L496RGT, HAL and UART to work together?