Memory Planning and Low-Power Configuration for Embedded Real-Time Systems

    When on-chip SRAM is used as a RAM disk, system memory, file-system workspace, and disk space compete for the same limited resource. This article explains how to size and separate those regions, verify runtime usage, and enable and test low-power support that a project generator may not turn on by itself.

    1. Background

    In a resource-constrained embedded system, a real-time operating system typically reserves memory for thread control blocks, thread stacks, message queues, timers, memory pools, and internal management structures. If a file system is also enabled and on-chip static RAM is partitioned as a RAM disk or virtual storage volume, the same memory must hold file data, directory information, caches, and media management data.

    This design is fast and simple because no external memory device is required. The drawback is equally clear: on-chip memory is limited, so runtime memory, file-system workspace, and disk space compete directly. If the default partition does not match system needs, the most common failures are initialization errors, file-creation failures, thread-start faults, or memory exhaustion after the system has been running for a while.

    When porting a real-time system, a successful build is not enough. Memory layout, feature trimming, and build configuration must be checked together.

    2. Capacity Requirements When On-Chip Memory Is Used as a RAM Disk

    2.1 Disk size is not usable file space

    After a region of static RAM is assigned as a disk, the space available for file data is usually smaller than the partition size. The file system also stores:

    • Volume or disk management information
    • The root directory and subdirectory structures
    • File control blocks and file attributes
    • A file allocation table or equivalent space-management data
    • Read/write caches and alignment buffers
    • Temporary workspace used during file-system initialization and runtime

    Even if the application only stores a few configuration files, the partition should not be sized from the total file size alone. Reserve enough management space for the file system, plus margin for later features.

    In practice, when on-chip static RAM is the default RAM disk, about 32 KB of contiguous space is a reasonable starting allocation. That figure is not a fixed standard for every project; it is a conservative lower bound. The actual size still depends on the file-system type, cluster size, cache policy, number of files, and application data volume.

    2.2 Separate system memory from disk memory

    When planning memory, divide storage into clear regions:

    1. Code region: program instructions and read-only data
    2. Runtime region: the real-time operating system, thread stacks, message objects, and dynamic memory
    3. RAM-disk region: configuration, logs, or temporary data stored by the file system
    4. Peripheral buffers: communication, image, audio, or high-speed acquisition
    5. Reserved region: margin for upgrades, debugging, and later features

    If the disk region overlaps the system heap, or if the start address and length in the linker script do not match the memory configuration, memory corruption can be hard to see. A typical pattern is a normal boot followed by faults when writing files, creating threads, or handling heavy communication load.

    Mark the start address and size of each region in the linker script or memory configuration file, and check for overlap at build time.

    2.3 Confirm the plan with runtime statistics

    Compiler reports of static usage cannot fully show whether the system is safe at runtime. Also check:

    • Free heap space and the historical minimum free heap
    • Peak stack use of each thread
    • Free disk space after file-system initialization
    • Open-file count and cache occupancy
    • Temporary allocations under high concurrency
    • Memory fragmentation after long runs

    If the system supports memory watermarks, record them after startup, after file operations, and while high-load tasks run. For thread stacks, do not start with an unnecessarily large fixed allocation, and do not size stacks from average use during normal runs. Use peaks and exceptional paths.

    3. Common Reasons Low-Power Features Do Not Take Effect

    3.1 A generated project is not always in sync with kernel capabilities

    When a graphical configuration tool generates a real-time system project, it usually adds source files, include paths, and basic settings. Some features are optional compile-time options. Whether they are enabled depends not only on the configuration UI, but also on whether the build system defines the corresponding preprocessor macros.

    Low-power support is a typical case. Even if the related source files are already in the project, the compiler may still build the kernel for normal operation if the enabling macro is missing. The idle thread then never enters the expected low-power path.

    The project builds and runs, but measured power does not match expectations. A successful compile does not prove that low-power support is enabled.

    3.2 Check macro definitions in the build configuration

    For scripted builds, inspect the build configuration in the generated directory, especially the section that stores macros added by the generator.

    Add the enable macro required by the real-time system’s low-power feature. Use the name defined by the current kernel version and project template. Do not rely on memory, because names can differ across versions.

    After the change, do a full rebuild rather than compiling only the files that appear to have changed. Preprocessor macros affect many kernel sources, and an incremental build can keep old object files and give a false result.

    Use this sequence:

    1. Open the project’s build configuration file.
    2. Find the automatically generated macro set.
    3. Add the compile macro for the low-power feature.
    4. Save the configuration and regenerate the project.
    5. Clean old intermediate and object files.
    6. Run a full build.
    7. Check the compile command or preprocessor output to confirm the macro reaches the relevant sources.

    4. How to Verify Low-Power Behavior

    4.1 Confirm the compiled code path

    Low-power support is usually selected by conditional compilation. Inspect preprocessor output to see whether the low-power code is included. You can also place a debug marker near the low-power entry, such as toggling a debug pin, incrementing a counter, or writing a short log.

    The low-power entry is often reached from the idle thread. If the system always has a high-frequency polling task, a software timer with a very short period, or a continuously running background thread, the idle thread may not get a long enough idle window. Even with the correct macro, power may not drop noticeably.

    4.2 Test with the hardware conditions in place

    Software low-power support is only part of the power-control path. Clocks and hardware must cooperate. During testing, confirm that:

    • Unnecessary peripheral clocks are off while idle
    • Peripherals are not left active
    • Interrupt sources do not wake the processor too often
    • The debug interface does not block the low-power state
    • Wake sources are configured correctly
    • Power management and clock switching follow the chip manual

    With a debugger attached, some debug settings can prevent the processor from fully entering the target low-power state. Final measurements should be taken on hardware close to the deployed setup, and should separate running power, idle power, and recovery time after wake-up.

    5. Engineering Practices for Memory and Low-Power Configuration

    Put memory partitions and build macros under version control so they do not depend on a developer’s local environment. Every developer and the continuous-integration environment should use the same partition definitions and build options.

    Add build-time checks for overlapping memory regions, whether the disk region meets the minimum size, whether the low-power macro is present, and whether the object files contain the low-power code path. Configuration regressions can then be caught when code is submitted.

    Reduce unnecessary file-system use. For a system that stores only a few parameters, limit the number of files, shrink caches, choose a suitable cluster size, and avoid writing large logs to the RAM disk for long periods. If data keeps growing, use larger external storage instead of continually shrinking runtime memory.

    Low-power design cannot depend on a single compile macro. Also adjust scheduling, cut pointless polling, and replace high-frequency spinning with events, signals, or blocking waits. Test the full path: enter low power, generate a wake-up, and resume operation.

    6. Summary

    On an embedded system with limited on-chip static RAM, treat about 32 KB as an initial reference size when that memory is partitioned as a RAM disk, and check the combined use of the system heap, thread stacks, file-system caches, and peripheral buffers. A partition that is too small can fail file-system initialization or leave the system short of memory at runtime. Overlapping partitions can corrupt data in ways that are hard to locate.

    A project generator does not always add the compile macro required for low-power support. Check the macro set in the build configuration, add the enable option, perform a full rebuild, and verify the source branch, idle-thread behavior, measured power, and wake-up path.

    A stable, maintainable real-time embedded system with real low-power behavior needs memory layout, build options, task scheduling, and hardware testing to be considered together.