cancel
Showing results for 
Search instead for 
Did you mean: 

uart2 not working in stm32 nucleo F411RE

roystm321
Associate III

Hi,

i wrote code that wait for a ping from esp32 and when he got it, it sent back pong to esp32

In the uart1 everthing works fine, but i do not understand why, in uart2 it is not working

I doubled checked the code and everthing looks ok for me, 

 

The code is the same like uart1, i just changed everything from uart1 to uart2

 

 

Code in uart2 - Not Working:

#include "stm32f4xx.h"

#include <string.h>

 

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart2;

uint8_t rxBuffer[5] = {0}; // Buffer for receiving data

const char *ping = "PING";

const char *pong = "PONG";

 

/* Function Prototypes */

void SystemClock_Config(void);

void Error_Handler(void);

void GPIO_Init(void);

void USART2_UART_Init(void);

 

/**

* @brief The application entry point.

* @retval int

*/

int main(void) {

/* Initialize the HAL Library */

HAL_Init();

 

/* Configure the system clock */

SystemClock_Config();

 

/* Initialize GPIO pins and USART2 */

GPIO_Init();

USART2_UART_Init();

 

while (1) {

// Receive 4 bytes (expecting "PING")

HAL_UART_Receive(&huart2, rxBuffer, 4, HAL_MAX_DELAY);

 

// Compare received data to "PING"

if (strncmp((char *)rxBuffer, ping, 4) == 0) {

// If "PING" received, send "PONG" back

HAL_UART_Transmit(&huart2, (uint8_t *)pong, strlen(pong), HAL_MAX_DELAY);

}

}

}

 

/**

* @brief GPIO Initialization Function

* @param None

* @retval None

*/

void GPIO_Init(void) {

GPIO_InitTypeDef GPIO_InitStruct = {0};

 

/* Enable GPIO clock for PA2 and PA3 (USART2 TX/RX pins) */

__HAL_RCC_GPIOA_CLK_ENABLE();

 

/* Configure PA2 (USART2 TX) */

GPIO_InitStruct.Pin = GPIO_PIN_2;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF7_USART2; // AF7 for USART2

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

/* Configure PA3 (USART2 RX) */

GPIO_InitStruct.Pin = GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull

GPIO_InitStruct.Pull = GPIO_PULLUP; // Enable pull-up for RX

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF7_USART2; // AF7 for USART2

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

 

/**

* @brief USART2 Initialization Function

* @param None

* @retval None

*/

void USART2_UART_Init(void) {

/* Enable USART2 clock */

__HAL_RCC_USART2_CLK_ENABLE();

 

/* Initialize USART2 */

huart2.Instance = USART2;

huart2.Init.BaudRate = 9600;

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;

 

if (HAL_UART_Init(&huart2) != HAL_OK) {

Error_Handler(); // Handle initialization error

}

}

 

/**

* @brief System Clock Configuration

* @retval None

*/

void SystemClock_Config(void) {

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 

/* Configure the main internal regulator output voltage */

__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

 

/* Initializes the RCC Oscillators according to the specified parameters */

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 8;

RCC_OscInitStruct.PLL.PLLN = 100;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 4;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {

Error_Handler();

}

 

/* Initializes the CPU, AHB and APB buses clocks */

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |

RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {

Error_Handler();

}

}

 

/**

* @brief This function is executed in case of error occurrence.

*/

void Error_Handler(void) {

while (1) {

// Stay in a loop if there is an error

}

}

 

 

 

Thanks for your help

 

 

 

 

13 REPLIES 13

So instead suggest some solution and help me, you wish me good luck?

The uart1 work great, the problem is with the uart2

In the first place i was tried to do manually the changes and it won׳t work

 

Instead laugh it will be nice to suggest solution, 

 

I past the code that work in uart1