Why does UART4 RDR register not read anything? [SOLVED]
Hello
I've been working for a few days with a BL475e IoT node that has a SMT32L475VGT6 microprocessor as standard.
I am trying to communicate via UART with an Arduino UNO without satisfactory results. Below I share the code:
I call from main() the following function:
# MX_UART4_UART_Init #
void MX_UART4_UART_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;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
HAL_UART_MspInit(&huart4);
if (HAL_UART_Init(&huart4) != HAL_OK) {
Error_Handler();
}
}The HAL_UART_MspInit function is inside the same .c file as the MX_UART4_UART_Init function. Plus I also wanted to configure from the MX Programmer UART1 and UART3 (although I don't use them).
# HAL_UART_MspInit #
void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) {
GPIO_InitTypeDef GPIO_InitStruct = { 0 };
RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
if (uartHandle->Instance == UART4) {
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_UART4;
PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}
/* UART4 clock enable */
__HAL_RCC_UART4_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**UART4 GPIO Configuration
PA0 ------> UART4_TX
PA1 ------> UART4_RX
*/
GPIO_InitStruct.Pin = ARD_D1_Pin | ARD_D0_Pin;
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_UART4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else if (uartHandle->Instance == USART1) {
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**USART1 GPIO Configuration
PB6 ------> USART1_TX
PB7 ------> USART1_RX
*/
GPIO_InitStruct.Pin = ST_LINK_UART1_TX_Pin | ST_LINK_UART1_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
else if (uartHandle->Instance == USART3) {
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3;
PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}
/* USART3 clock enable */
__HAL_RCC_USART3_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/**USART3 GPIO Configuration
PD8 ------> USART3_TX
PD9 ------> USART3_RX
*/
GPIO_InitStruct.Pin = INTERNAL_UART3_TX_Pin | INTERNAL_UART3_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
}From another function, which is also called from main(), I try to receive the data. I use getters & setters to move the handles from one function to another.
# SerialUART4Init #
/* Serial UART Init function */
void SerialUART4Init(void) {
UART_HandleTypeDef gUart;
gUart = GetUART4Handle();
uint8_t myRxData[10] = { 0 };
while (1) {
HAL_UART_Receive(&gUart, myRxData, 10, 10);
}
}The huart4 handle is declared as a global variable, inside the .c file in which the MX_UART4_UART_Init function is contained.
# GetUART4Handle #
UART_HandleTypeDef GetUART4Handle(void) {
return huart4;
}When I try to receive the data, in polling mode, I see that I need to have the UART_FLAG_RXNE flag reset, so I get back "HAL_TIMEOUT". How can I reset this flag?
However, debugging the program, I see that the huart->Instance->RDR register is always set to 0.
Regardless of whether or not I am able to save the value of this register, if I am actually reading data, should RDR be at a value other than 0?
To rule out options, I do a sanity check and connect to +3V3 the PA1 pin (UART4_RX). In this way, I understand that the transmission would be a constant high logic level. Well, RDR is still at 0.
What is the problem? Thank you very much in advance.

