Diving Deep into OpenThread Architecture for STM32WB

Introduction

After setting up OpenThread in the previous episode, it’s now time to understand what’s happening under the hood. This episode will dive into the OpenThread stack running on the STM32WB platform. It will break down how the system initializes the Thread protocol. It also manages the Thread protocol using the provided APIs and middleware components.

OpenThread Architecture Overview

OpenThread, when ported to STM32WB, is built on top of a layered architecture. It abstracts hardware-specific drivers and exposes high-level APIs that can be used in applications. Key components include:

  • OpenThread Core: The protocol logic handling routing, addressing, and mesh behavior.
  • Platform Abstraction Layer (PAL): Adapts OpenThread to STM32WB hardware using HAL/LL drivers.
  • Application Layer: User logic where you configure, initialize, and run Thread networking functions.

Thread Stack Initialization on STM32WB

When the firmware runs, the stack is initialized in several stages inside app_thread.c. This includes:

  1. otSysInit() — Initializes system-level components, timers, and platform drivers.
  2. otInstanceInitSingle() — Creates a single OpenThread instance (used in most embedded applications).
  3. otIp6SetEnabled() — Enables IPv6 stack required for communication between nodes.
  4. otThreadSetEnabled() — Enables the Thread networking stack and triggers role assignment (e.g., REED or Router).

Role and State Management

Once Thread is enabled, the stack dynamically chooses a role based on network conditions and configuration. To retrieve the current role, the following function is used:

otDeviceRole role = otThreadGetDeviceRole(instance);

This allows your application to react depending on whether the device is a Leader, Router, or End Device.

Thread Stack Events and Callbacks

The OpenThread stack notifies the application about internal state changes using callback mechanisms. For instance, you can register handlers for:

  • State change (e.g., role transition, IP address changes)
  • Network data updates
  • UDP/CoAP message reception
otSetStateChangedCallback(instance, &MyCallback, context);

Key Data Structures

Some important structures used by OpenThread that developers should be familiar with:

  • otInstance: Central structure representing a device’s Thread state and configuration.
  • otIp6Address: Structure representing an IPv6 address used for addressing nodes.
  • otMessage: Used for UDP/CoAP message transmission and reception.

Power and Resource Considerations

STM32WB is a dual-core MCU. OpenThread runs on the Cortex-M0+ core while the user application runs on Cortex-M4. The inter-core communication is handled via shared memory and command queues. Developers must avoid race conditions by relying on ST’s provided APIs and synchronization mechanisms.

Conclusion

When you understand the architectural breakdown of OpenThread on STM32WB, you gain better control. This includes the behavior, performance, and debugging of your mesh network. In the next episode, we’ll explore how devices join a Thread network. We will also see how CoAP can be used for communication between nodes.

Leave a comment