Advanced Optimizations for ARM Microcontrollers
Overview
This tutorial will describe advanced optimization techniques for developing applications for ARM Microcontrollers. These optimization techniques will not result in speed or size optimizations in all cases and in some cases may actually degrade performance. However, once an application has been developed these optimizations may be attempted in an effort to squeeze increased performance or reduce the code footprint of a given application. These methods should only be attempted after all potential coding optimizations have been performed.
Table of Contents
Modifications to C code generation options
There are several options that can lead to improved code size or performance through changes to the LabVIEW C code generation options. In the project explorer, under Build Specifications right-click the Application and choose Properties.

This will bring up the Build Specifications Properties dialog window. From the Categories column select Application Information. This option provides the ability to modify the Code Generation Options.

This option allows several checkboxes that can be used in combination to potentially optimize our ARM Microcontroller applications. Additionally, disabling debugging will also reduce the size of the application.
Use stack variables will indicate that variables should be used on the stack and not the heap. This can lead to C compiler optimizations that will lead tofaster code execution, however this option cannot be used with USB debugging options.
Disable parallel execution this will result in a smaller code size by reducing the overhead associated with scheduling multiple loops. In applications with multiple loops this will cause one loop to finish before the other loop executes. This can result in a performance hit in applications or in extreme cases cause the application to halt execution.
Generated guard code generates extra code to prevent common coding mistakes that can cause an embedded application to crash or function incorrectly. For example, guard code can prevent dividing by zero or indexing out of range in an array. Checking this option will result in code that executes slower and has a larger memory footprint.
Generate integer only generates C code without any floating-point declarations or operations. This option creates a smaller embedded application because it does not link any floating point-specific libraries. This option should only be selected for applications that have been designed to operate without any floating point operations.
Generate C style function calls generates the calling interface to all VIs as C-style function calls without any default data initializations, which can reduce the code size by as much as 50% for a small VI. An error occurs if any input or output to any VI is unwired when you build the VI into an embedded application
Enable expression folding generates better-performing and more efficient code by collapsing groups of nodes into single expressions that C compilers recognize easily. You cannot debug an embedded application while using expression folding because expression folding eliminates some of the wires in the generated C code. If you overrun the bounds of an array or divide by zero, the application might crash.
Enable cross-module optimization (on by default) instructs µVision to build the application twice causing a smaller final application size. This option increases the time spent building the application.
Compiler Options
LabVIEW provides several options to pass arguments to the Keil µVision compiler that can optimize our application’s performance. From the Build Specifications Properties window select Advanced Compiling Options from the Category panel.

Under the Optimization pull-down window select one of the following options.
Level 0 (-O0) uses minimum optimization. The compiler performs simple optimizations that do not impair debugging.
Level 1 (-O1) uses restricted optimization. The optimizations allow most C code debugging activities.
Level 2 (-O2)—(Default) uses high optimization. The optimizations prevent some C code debugging activities.
Level 3 (-O3) uses the maximum optimization for space unless you place a checkmark in the Optimize for time checkbox. This level of optimization makes debugging C code difficult.
Additionally, Optimize for time will potentially result in faster performing applications at a cost of increased code size.
Keil µVision Optimizations
Finally, there are some options that can be accessed through the Keil µVision IDE that can improve performance – modifying the heap size and changing target options.
Modifying heap size
Applications that use a lot of memory space can be prone crashing because the memory usage during run-time exceeds the available heap size. In this case, the heap size needs to be increased to make the application more stable. In other instances, if the applications size takes up a lot of memory, the application cannot be built because it is impossible to allocate the application into the target hardware's memory. In this case, reducing the heap size is an option to reduce the code footprint. The following steps outline how to modify the heap size.
1. Build the application. Right-click the application from the Project Explorer window and select Build.

2. Once the application has been built, right click the target and select Show Keil µVision.

3. Once the Keil µVision IDE is open, open the file LPC2300.s. This is the file name for the MCB2300 Keil Evaluation board and may vary depending on your target.

4. Within this file, search for the variable name Heap_Size.
5. This variable can be increased or decreased depending on your application requirements. Once the variable size has been set, save the file, close Keil µVision, and rebuild the application in LabVIEW.
Changing target options
This is an advanced option that is entirely dependent on the hardware target that is being used. Refer to the hardware vendor’s suggestions for potential optimizations. This document will only describe how to access the target options window from Keil µVision.
1. As in the previous section, build the application from LabVIEW and then open Keil µVision.
2. From the µVision, click on the Options For Target button.
3. This will bring up the Options for Target window. This will allow you to make advanced configuration options for your target, such as taking advantage of additional memory or adjusting assembly options.

Conclusion
There is no single method for optimizing applications for ARM Microcontrollers. Each application is unique and the methods to optimize it will differ depending on the architecture of the applications and the hardware available. A trial and error method may be required when trying to maximize performance out of your ARM hardware.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).


