Connecting WiFi and BLE to MCU? It’s Never Been Easier with ESP32 and RT-Thread

1. Introduction

Recently, a colleague shared a highly valuable technical article titled “Mastering WiFi with SPI: A Deep Dive into the RT-Thread ESP-Hosted Driver | Technical Collection“. Although its technical stack design is refreshing (providing a complete analysis from low-level protocols to high-level drivers), it has received little attention due to the fast-paced content consumption environment. This article aims to extend and supplement that work, and based on the First-Rate MCU platform, perform practical validation to further demonstrate the maturity and compatibility of First-Rate’s driver adaptation within the RT-Thread ecosystem.

Are you still struggling with WiFi connectivity and Bluetooth solutions for your MCU? The RT-Thread community has open-sourced the ESP-Hosted driver and rt-thread_nimble, which can be implemented via a standard SPI interface, and this repository has been organized into an RT-Thread package. For further information, please refer to the article mentioned above. The license is quite friendly, using the MIT license, with links as follows:

https://github.com/Evlers/rt-thread_esp-hosted

https://github.com/Evlers/rt-thread_nimble

The code in this article will also be open-sourced in the following repository:

https://gitee.com/RCSN/hpmicro_rtthread_demo

Note:

1. If your ESP32 module does not work with the firmware provided by the author, you will need to compile the slave part of the ESP-Hosted code using ESP-IDF, which is beyond the scope of this article and requires your own understanding.

2. The SPI slave of the ESP32 can only reach 10M SCLK, while other series can reach 30M SCLK.

Tip:

In wireless IoT applications, First-Rate has also made significant contributions, including WiFi applications based on SDIO drivers.

1. In the HPM SDK, there is an lwIP example based on the AP6256 Wi-Fi module

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

2. In the RT-Thread BSP package, there are examples based on Infineon’s AIROCâ„¢ WiFi + Bluetooth® Combo series, such as CYW43438; as well as SPI WiFi examples for RW007.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

2. Performance of the First-Rate Platform:

WiFi connection performance using the external ESP32:

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-ThreadConnecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

Bluetooth transmission performance using the external ESP32

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-ThreadConnecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-ThreadConnecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

3. Adaptation Process on the First-Rate Platform

This article mainly describes the adaptation and configuration process; other details can be understood independently.

(1) Required Device Drivers

IO device drivers and SPI device drivers are required, with the requirement that the RT-Thread version is above 5.0.0. Below this version, you need to implement host_spi_init yourself. The First-Rate RT-Thread BSP is based on RT-Thread 5.0.2, which has fully implemented the GET_PIN adaptation for GPIO device drivers, as well as related configurations and device registration interfaces for SPI device drivers.

From the author’s adapted spi_wrapper, we can see:

SPI initialization part:

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

SPI data transmission part:

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

(2) Adaptation Process

1. You need to use the SDK manager in First-Rate’s RT-Thread Studio to download and install the BSP package, and create a new template project. For specific operations, refer to the official First-Rate WeChat article titled “RT-Thread BSP v1.9.0 Release | Focus on Optimization

2. Use git clone to place the above two repositories in the libraries directory of the project. This is just for convenience; you can also choose to place them in other directories or even create a new directory.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

3. Add the paths of these two repositories in the kconfig file located in the board folder of the project.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

4. Add the SConscript of these two repositories to the SConstruct file in the main directory of the project to include them in the project compilation.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

5. Finally, use env to open menuconfig to see that these two repositories have been added to the project.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

(3) Configuration Process

1. The lwIP version needs to be v2.1.2; otherwise, related lwIP compilation errors will occur.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

2. Enable SPI DMA for the SPI driver, select GPIO software to operate CS, and choose single-wire mode for IO.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

3. For ESP-Hosted configuration, the IO part needs to be written in PAxx/PBxx format; other operations can refer to the article mentioned above.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

4. Enable Bluetooth, using vhci device for HCI transmission.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-ThreadConnecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

5. If you need to view Bluetooth broadcast messages, you need to enable ulog.

Connecting WiFi and BLE to MCU? It's Never Been Easier with ESP32 and RT-Thread

By following the above steps, you can easily use the First-Rate platform to work with external WiFi and BLE.

4. Conclusion

This article is based on the open-source ESP-Hosted and rt-thread_nimble driver solutions from the RT-Thread community, combined with the First-Rate MCU platform, to fully validate the technical feasibility of implementing Wi-Fi connectivity and Bluetooth (BLE) functionality through a standard SPI interface with the external ESP32 module. Through a simple adaptation process and configuration practice, it demonstrates the driver compatibility and development maturity of First-Rate under RT-Thread, providing developers with a reusable reference case.

Leave a Comment