2023-11-08 01:26 PM
Hello, I'm building a communication between two devices an Arduino Mega 2560 and STM3210E-EVAL. For the communication I need to use RS232. In between the two devices I use the MAX232. I got it working one way where the STM32 sends 1 byte to the Arduino Mega. Next I wanted to test communication to the STM32 by sending the character 'X', but the STM32 doesn't receive the data. I use the simple polling method to receive the UART data.
This is how my main function looks like. For the printf() I use UART1.
uint8_t rx_data[10];
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_ADC1_Init();
MX_FSMC_Init();
MX_USART2_UART_Init();
MX_USB_PCD_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (HAL_UART_Receive(&huart2, rx_data, 10, 1000) == HAL_OK) {
printf("Data");
} else {
printf("No Data");
}
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
This is how UART2 is initialized:
/* USER CODE END USART2_Init 1 */
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;
I read somewhere that the problem could be in the hal_msp.c file so here is the init of UART in that file.
/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example
* @PAram huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = USART1_TX_Pin|USART1_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
else if(huart->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART2 GPIO Configuration
PA1 ------> USART2_RTS
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_InitStruct.Pin = USART2_RTS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(USART2_RTS_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = USART2_TX_Pin|USART2_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART2_MspInit 1 */
/* USER CODE END USART2_MspInit 1 */
}
}
/**
* @brief UART MSP De-Initialization
* This function freeze the hardware resources used in this example
* @PAram huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOA, USART1_TX_Pin|USART1_RX_Pin);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
else if(huart->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspDeInit 0 */
/* USER CODE END USART2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART2_CLK_DISABLE();
/**USART2 GPIO Configuration
PA1 ------> USART2_RTS
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
HAL_GPIO_DeInit(GPIOA, USART2_RTS_Pin|USART2_TX_Pin|USART2_RX_Pin);
/* USER CODE BEGIN USART2_MspDeInit 1 */
/* USER CODE END USART2_MspDeInit 1 */
}
}
In the attached images you can see the configuration.
If someone knows what is wrong let me know, because I'm stuck and I don't have that much experience to debug this problem. If you need more info just let me know.
Solved! Go to Solution.
2023-11-08 01:54 PM
If you want one character, try doing that, and not 10
Perhaps try a loop-back confirm if its a reception issue (ie RS232 DB9 Pin 2 to Pin 3), or a signal issue. Send and receive back.
Show wiring
Put a scope on the signal, check USART2->SR for any sticky reception errors
Been an age but doesn't the STM32F1 USART_RX pin need to be in INPUT Mode, not PP AF ?
2023-11-08 01:54 PM
If you want one character, try doing that, and not 10
Perhaps try a loop-back confirm if its a reception issue (ie RS232 DB9 Pin 2 to Pin 3), or a signal issue. Send and receive back.
Show wiring
Put a scope on the signal, check USART2->SR for any sticky reception errors
Been an age but doesn't the STM32F1 USART_RX pin need to be in INPUT Mode, not PP AF ?
2023-11-09 01:21 AM
I got it working by changing the USART2_RX pin to INPUT Mode. First I could not change the mode. I had to disable FSMC somehow the pins are connected to each other. Maybe you know why this is?