Skip to main content
Explorer
October 15, 2024
Question

uart2 not working in stm32 nucleo F411RE

  • October 15, 2024
  • 3 replies
  • 2515 views

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

 

 

 

 

    This topic has been closed for replies.

    3 replies

    Graduate II
    October 15, 2024

    Hi,

    Is 9600 baud the correct speed for ESP32?

    Kind regards
    Pedro

    roystm321Author
    Explorer
    October 16, 2024

    Hi,

    Of course, if i was not define 9600 also in esp32 then the uart1 also was not worked.

     

    This is the code i wrote in the esp32:

     

    #include <Arduino.h>
    #include <HardwareSerial.h>


    #define SerialAT Serial2
    void setup() {
      // Start UART1 at 9600 baud
      SerialAT.begin(9600);  // RX -> GPIO16, TX -> GPIO17 (adjust these pins as per your wiring)
      Serial.begin(9600);  // For output to the serial monitor
    }

    void loop() {
      // Send "PING" to STM32
      SerialAT.print("PING");
      Serial.println("Sent: PING");

      // Wait for "PONG" response from STM32
      if (SerialAT.available()) {
        String response = SerialAT.readString();
        Serial.print("Received: ");
        Serial.println(response);

        // If we receive "PONG", print success
        if (response == "PONG") {
          Serial.println("STM32 replied with PONG!");
        }
      }

      delay(1000);  // Delay for 1 second before sending the next "PING"
    }
    ST Employee
    October 16, 2024

    Hi, maybe the cause is because your clock for USART1 and USART2 is different, for USART1 the clock is form APB1 but for USART2 the clock is from APB2, so the BaudRate is different, you can check it.

    roystm321Author
    Explorer
    October 16, 2024

    You can see in the full code i sent of the stm32: (I  paste here just the part of the clocks)

     

     

    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();

    }

    }

     

    I see now that there is also RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2

     

    Do i need to remove it or modifiy it?

    ST Employee
    October 16, 2024

    Hi, I mean you can modify 'RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2' too (same as APB1), then to try again. 

    Graduate II
    October 16, 2024

    Hi,


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

     

    Did you just use find-n-replace "uart1" with "uart2" in main.c ?

    Kind regards
    Pedro

     

    roystm321Author
    Explorer
    October 16, 2024

    actually i asked from chat gpt to change from uart1 to uart2, and then it regenerate the code for uart2

    Graduate II
    October 16, 2024

    hahahaha, that's funny... Good luck!