RAM Optimization Techniques for Renesas Synergy™ Applications

A horrible moment that every developer has encountered at one time or another is when they compile their application only to discover that it doesn’t fit on their microcontroller. The application may have reached its RAM limits or maybe even its code size. One thing is certain, the developer is in for an optimization session. The question is where to begin? For the Renesas Synergy™ Platform, there are several different techniques that a developer can use to optimize their application.

The first technique that developers can use is to optimize the BSP. The BSP contains several different SRAM sources that are preconfigured to fit multiple application needs. One memory need is to provide a heap to the BSP that is used for library functions such as printf. The default value is set to 4kB (0x1000) which is oversized for any resource constrained application. When library functions are used, a developer may need to experiment to get a size that works correctly with their application. If the library functions are not used, the heap size can be set to zero.

Another memory area that could be adjusted is the process stack. The process stack is used for ThreadX threads while the main stack is used for the application that is located at the reset vector. If a developer is creating a bare-metal application, they could set the process stack size to zero in order to squeeze more memory from the application.

The final BSP area that can be optimized is the main stack. The main stack is used during the boot-up process to initialize the application and ThreadX. The main stack is only used during start-up and afterwards each thread will have its own stack from which it will allocate memory. The default main stack size is set to 4 kB (0x1000) which again is most likely oversized for a resource constrained device. A developer can adjust the main stack size to one that fits their application. This can be done experimentally by filling in the main stack with a known pattern and then monitoring how much stack space is used to start-up the application.

 

A second technique that developers can employ to optimize their memory usage is to perform a worst-case stack analysis on each thread. The default thread stack size is 1024 bytes which is overkill if the thread is only blinking an LED. Developers can adjust the stack size in the Synergy Configurator by clicking on the thread and then viewing its properties. How can a developer figure out what to set the stack size to? The RTOS resources view can be used while running the target in debug mode to view the maximum stack usage per thread during run-time. Once a developer has started the debugging session, they can go to Renesas Views > Partner > OS > RTOS Resources. In the pull-down, a developer would then select ThreadX and run the application through its paces and worst case conditions. When a developer is happy with how long the system has executed, they can then suspend the application and select the Stack tab to view the MaxStackUsage as shown below:

 

Once a developer has these values, they can pad them a bit by 10 – 15% or whatever makes them comfortable and update their thread stack values.

 

Developers who are using USB also have a third technique available to them which is to decrease the USBX buffer pool size. The buffer pool is allocated at run-time and by default will use 4kB of SRAM. The SSP 1.2.0 user manual specifies that this can be reduced to 3 kB as shown below:

 

 

void tx_application_define_user (void *first_unused_memory)

{

    UINT status;

    /* Initialize UsbX Memory */

    ux_system_initialize((CHAR *) first_unused_memory, (1024 * 3), UX_NULL, 0);

/* … */

    _ux_dcd_rx_initialize(0);

}

 

Finally, if a developer has used all these techniques and still finds they need more memory, they can try using the compiler to further optimize their code. By default, most applications are compiled with the optimizer set at –O2 but it can also be adjusted for size, -Os, and memory footprint.

With these techniques, the reader is now armed with the knowledge necessary to make sure that their applications can successfully fit in their memory footprint efficiently and without issues.

Until next time,

Live long and profit!

Professor_IoT

 

Hot Tip of the Week

Make sure you regularly visit the Renesas Synergy tools and kits page to check out new application projects. https://www.renesas.com/en-us/products/synergy/tools-kits.html#sampleCodes

For example, just a few of the recently added Module Guides with useful application projects are for the:

Check out the complete set!