DEV Community

Hedy
Hedy

Posted on

How to migrate from STM32 to ESP32?

Migrating from STM32 (typically ARM Cortex-M) to ESP32 (Xtensa or RISC-V) involves transitioning between two powerful yet different microcontroller ecosystems. Here’s a tailored, step-by-step guide:

Image description

STM32 to ESP32 Migration Guide
1. Define Your Project Requirements
Ask yourself:

  • Are you using Wi-Fi/Bluetooth? (ESP32 has built-in)
  • What peripherals are critical? (ADC, PWM, UART, etc.)
  • What are your real-time constraints?
  • Will you use RTOS (e.g., FreeRTOS)?

2. Compare Hardware Features

Image description

3. Development Tools

Image description

4. Porting Code
GPIO Example
STM32:

c

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
Enter fullscreen mode Exit fullscreen mode

ESP32 (ESP-IDF):

c

gpio_set_level(GPIO_NUM_5, 1);
Enter fullscreen mode Exit fullscreen mode

UART Example
STM32:

c

HAL_UART_Transmit(&huart1, buffer, len, HAL_MAX_DELAY);
Enter fullscreen mode Exit fullscreen mode

ESP32:

c

uart_write_bytes(UART_NUM_1, buffer, len);
Enter fullscreen mode Exit fullscreen mode

Timers / Delay

  • STM32: HAL_Delay(1000);
  • ESP32: vTaskDelay(pdMS_TO_TICKS(1000)); (FreeRTOS)

5. Hardware Adjustments

  • Power: ESP32 operates at 3.3V; ensure level shifting if needed.
  • Pinout: Map new GPIO numbers accordingly.
  • Clock source: ESP32 has built-in oscillator; external crystal often optional.
  • Flash boot mode: Remember ESP32 bootstrapping pins (e.g., GPIO0).

6. Wi-Fi/Bluetooth (if needed)
ESP32 brings wireless capabilities. Use:

c

#include "esp_wifi.h"
#include "esp_bt.h"
Enter fullscreen mode Exit fullscreen mode

Set up via esp_wifi_init() and similar APIs.

7. Test and Debug

  • Use UART for logging: esp_log_level_set(TAG, ESP_LOG_INFO);
  • Use built-in debugger (JTAG via ESP-Prog) or serial console for debugging.

Summary Table

Image description

Suggested Next Steps

  1. Install ESP-IDF or try Arduino IDE for easier transition.
  2. Start with simple GPIO/UART examples.
  3. Gradually port and test each module (timers, communication, etc.).
  4. Use idf.py menuconfig to configure hardware like Wi-Fi, UARTs, and flash.

Top comments (0)