Embedded RTOS Memory and Low-Power Configuration Practices

    This article explains why a clean build does not guarantee a working embedded system when an RTOS kernel and file system are added to a 32-bit microcontroller. It shows how to size memory pools and thread stacks from default values to calculated estimates, how to plan on-chip static memory as a file system disk with proper partitioning, and how to enable the low-power macro in the build script. A final checklist covers return-value checks, runtime stack and pool monitoring, partition cross-checking, macro version control, and real power measurements.

    1. Background: Why “It Compiles” Does Not Mean “It Runs”

    Adding an RTOS kernel and a companion file system to a 32-bit high-performance microcontroller is a common choice in embedded software architecture today. The workflow usually relies on a graphical configuration tool to generate the initial framework for pins, clocks, peripherals, and middleware, after which the build system compiles an executable image. The biggest risk in this chain is that the configuration UI is only responsible for generating code; it does not verify whether resources are sufficient. As a result, a project may build cleanly and then crash on the first message send or the first file write after flashing.

    The problems seen in hands-on practice fall into three areas: the middleware’s default stack and memory pool sizes are too small, the partition capacity is insufficient when on-chip static memory is used as the disk medium, and the build script lacks the low-power enable macro. Each is broken down below with reusable criteria.

    2. Memory Pools and Thread Stacks: From “Default Values” to “Calculated Values”

    2.1 Why the Defaults Are Not Enough

    RTOS memory management depends on two kinds of resources: pooled memory for kernel objects (thread control blocks, message queues, mutexes, event flag groups, and so on), and the dedicated stack space each thread needs to run. The defaults from a graphical tool target a “minimal runnable sample.” Once components such as a file system, logging, and protocol parsing are layered on, both the object count and the call depth increase significantly.

    Deciding whether capacity is enough should not rely on trial and error. Estimate it in this order:

    1. Count the kernel objects, multiply by the aligned size of each control block and its metadata, and you get the lower bound of the memory pool.
    2. Add the file system’s own cache, directory entries, and read/write buffer requirements.
    3. Estimate the stack depth of each thread by its worst-case call chain, including interrupt nesting and library overhead.
    4. Reserve roughly 20% to 30% headroom for later feature iterations.

    2.2 A Practical Starting Point

    When no precise measurement is available, start with a relatively small memory pool (on the order of a few thousand bytes) and a stack of about a thousand bytes per thread, then observe the actual high-water marks with runtime monitoring. Note that the exact numbers depend heavily on the chip model, compiler optimization level, library implementation, and business logic; the figures above are only engineering starting points, not mandated parameters for any platform.

    1. /* Example: stack and priority configuration when the app creates a thread */
    2. #define APP_STACK_SIZE (1024) /* Unit is defined by the specific kernel API; recheck against the actual call chain */
    3. #define APP_PRIORITY (5)
    4. UINT app_thread_create(CHAR *name)
    5. {
    6. return tx_thread_create(&app_thread, name, app_entry,
    7. APP_STACK_SIZE, NULL, 0,
    8. APP_PRIORITY, APP_PRIORITY,
    9. TX_NO_TIME_SLICE, TX_AUTO_START);
    10. }

    2.3 Diagnostic Methods

    A stack overflow typically does not raise a direct error. Instead, adjacent variables are silently overwritten, or the system resets randomly or hits a HardFault. The recommended approach is to enable the kernel’s built-in stack filling and runtime information collection, then check after long stress runs whether the marks at the stack bottom have been corrupted. Memory pool exhaustion usually shows up as object creation returning a failure code, so check return values line by line during initialization instead of ignoring them.

    3. Using On-Chip Static Memory as a Disk: Capacity Planning and Partition Design

    A file system needs a medium. When there is no external storage chip, carving out a region of on-chip static memory as the disk is the most direct option, but its total capacity is often only tens of kilobytes and it must coexist with the application’s data segment and stacks.

    Key planning points:

    • First confirm the region size reserved for this purpose in the linker script; it must meet the file system’s minimum usable capacity (in practice usually more than 30 KB, depending on cluster size, the number of directory entries, and the file system version).
    • Round the partition start address and length to erase/alignment boundaries to avoid out-of-bounds access into neighboring data.
    • Be explicit about the medium’s write-endurance limit; frequent small-file writes should be paired with write coalescing and fewer metadata updates.
    • Have a plan for power-loss consistency; critical data can use dual copies or checksums.

    If the linker reports a region overflow, static memory has already been squeezed by the data segment. In that case, shrink global buffers and move large arrays to external storage rather than blindly enlarging the disk partition.

    4. The Low-Power Enable Macro: A Key Part of the Build Script

    A build project generated by a graphical configuration tool does not enable the kernel’s low-power support by default. The result is that calls to the “enter low power” interface have no real effect, or the related code is trimmed at compile time by conditional compilation, showing up as sleep current that stays high.

    The fix belongs in the symbol definition set of the build script, where the low-power enable macro must be added to the compile-time macro list:

    1. # Example: append the macro to the compile definition set in the build script
    2. set(PROJECT_DEFINITIONS
    3. ${PROJECT_DEFINITIONS}
    4. TX_LOW_POWER
    5. )

    After adding it, reconfigure and rebuild completely, and confirm the macro actually reaches the compile command (verify by inspecting the compile arguments or the preprocessed output). Also note that low-power support requires the kernel’s idle hook and clock wake sources to work together; enabling only the macro without providing a wake condition may leave the system asleep with no way to resume.

    5. Rollout Checklist

    1. Verify each object creation return value during initialization and print the error code on failure.
    2. Periodically sample the stack high-water mark and remaining memory pool via the runtime information interfaces.
    3. Cross-check the disk partition size against the linker script region to avoid overlap.
    4. Keep build script macro definitions centrally managed, with the low-power macro under version control.
    5. Validate sleep and wake current on a real power measurement bench, not just by reading the code path.

    6. Conclusion

    The hard part of porting an RTOS is rarely “getting it to run”; it is resource boundaries and configuration consistency. Treating the memory pool, thread stacks, and disk partition as engineering parameters that must be calculated, and treating the low-power macro as part of the build configuration subject to review, surfaces many hidden failures early in development. If you need large-scale pre-production builds and static checks in the cloud, a container platform and log service can host a CI pipeline that automatically regresses configuration changes.