You are here

Managing Application Size and Speed with PSoC Creator | Cypress Semiconductor

Managing Application Size and Speed with PSoC Creator

In embedded systems it is often true to say that "every byte counts". When your application runs solely from on-chip memory you have two very hard limits (flash ROM and SRAM) on the amount of memory you can use in your application. Modern compilers do an excellent job of optimizing code and data space, often getting very close to hand-crafted assembler in terms of memory-efficiency.

Watch out though because, by default, PSoC Creator does not optimize your code!

PSoC Creator Build Configurations Pull-Down

This is because of an inherent side-effect of compiler optimizations - they make the application harder to debug. The obvious example of this is when the code you are stepping through in the debugger has been modified by the tail merging optimization, which replaces multiple sequences of identical instructions with a single sequence. When you step the code it can jump about to really strange places in your source code, because the code you think you are debugging isn't where you think it is! But, as I said before, optimizations are really important and so, to avoid total madness, we need a way to turn optimizations on when we need them and off when we're designing and debugging.

PSoC Creator makes it really easy to do this by offering build configurations. There are two of them - Debug and Release - and they are complete sets of build options for a project. By default, a new project uses the Debug configuration which enables debugging and turns off all but the "safest" optimizations. With this configuration you can use the debugger to perfect your code before swapping to the Release configuration and simply re-building the source code.

The Release configuration turns on optimizations and disables debugging. Try it now and see your flash and SRAM usage drop! By default this configuration optimizes for space but you can quickly switch to speed optimization by simply editing the Build settings (look for Optimization in the Compiler section).

Switching configurations is really simple. There is a pull-down in PSoC Creator's main button panel and all you need to do is make your choice and build. It will only re-compile the source code - the hardware design does not change after all - and so experimenting with the options can be very fast indeed. If you are on the edge of your memory budget, or have a very tight window of time to complete a task, I strongly recommend using build configurations to help you make a careful, informed evaluation of compiler optimizations for your application.

Comments

Bob Marlowe's picture

Recently I had a project with a PSoC4 that did not fit with "Debug" setting, but with "Release". Since I still had to debug some of the code (No, I still did not learn how to write bug-free code) I was a bit stuck.
My solution was to create a library for the already degugged (and now error-free) functions which was compiled with the "Release" setting. The untested part of the project was then compiled with "Debug" setting and that allowed me to get these parts error-free as well.

yfs's picture

Good thinking, Bob. You can set individual build options on files, but I always forget which ones I have changed and feel like I have lost a little control. Your method of putting the "finished" files in a library is a nice solution.

Another option that I have not really tested thoroughly enough to comment on, is the -Og option on GCC. This, if I may quote the manual, "enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience". It sounds great - but I have not personally tried it so I do not know how good the optimization is or whether the debugging is really unaffected. Worth looking into if either of us every get a free moment!

-- Mark.

akagan's picture

Beware of this feature! I built in the test procedure using some external hardware controlled by a shift register. Because the project was at the limit of the resources of the PSoC, I opted to bit bang the shift register output and for the timing delays I simply read a memory location. I got the system to work under the Debug condition, but when I used the Release the test procedure stopped working- the timing loops were completely optimised out (because the memory location was never used elsewhere) and the 24 clock pulses to the shift register became one long pulse. I acknowledge that I should taek a more rigorous approach to my programming or used a different approach for the delay (like asm("nop");) but it came as a complete shock that something worked in debug but not in release. And in circuit debugging is not possible in release (at least I don't think so) so of course you are left to guess what is happening.

I am just saying you should be aware that the different optimization setting can lead to unexpected problems, especially if you aren't following strict C programming practices.

yfs's picture

Yes, the compiler realized that your were reading a variable that cannot change and so took out the loop. A simple solution is to declare the variable to be "volatile". This explicitly tells the compiler that the variable may change and prevents it from optimizing the code.
That said, delay loops are notoriously sensitive to compiler optimization. You might want to look into using the SYSTICK interrupt to do periodic bit banging instead.
Last thought - you can enable debugging in optimized code but it is not always great fun! And the more you optimize, the less fun it is. The compiler does some aggressive things with code sharing and elimination, for example, that make stepping look like it is broken. And variables do not update the way you would expect. It is OK to do it, but be warned that it can be frustrating.

Rosetta's picture

I think that all information of any PSoC project is there in .hex file generated after compilation.
I tried to compile different projects in both debug and release mode and get same hex file for both mode. why?

jb_1593366's picture

"Switching configurations is really simple. There is a pull-down in PSoC Creator's main button panel and all you need to do is make your choice and build."

When I do this, it doesn't change.  I change to Release, press Ok, and re-open the window and it says "Debug (Active)" again.

jb_1593366's picture

Aha!  You can't adjust this from Build Settings, you have to adjust it from the main window.  http://www.cypress.com/comment/212356#comment-212356

jordanss123 jordanss123's picture

replica yeezy boost 350 Thanks for your submission. I also think that laptop computers are becoming more and more popular these days, and now will often be the only sort of computer used in a household.

 

Muneer Ahmed's picture

Great post yet I was thinking about whether you could compose a litte more on this subject? I"d be exceptionally grateful in the event that you could expound a tad bit further. Value it! preschool in Owensboro KY

Muneer Ahmed's picture

I'm constantly searching on the internet for posts that will help me. Too much is clearly to learn about this. I believe you created good quality items in Functions also. Keep working, congrats! Church of Christ preschool

Muneer Ahmed's picture

You have written a great article here. I have heard from a lot of people on this site that you are the greatest writer on this site and after reading this article, I agree with them.  what is a call option and how does it work

Muneer Ahmed's picture

Much obliged to you for peopling get the data they require. Extraordinary stuff of course. Keep up the colossal work!!!  when is the best time to trade binary options

Muneer Ahmed's picture

Adding fruit to juices varies the flavor, but it comes with a price -- extra calories. If you are doing a juice fast and drinking green juices to sum up the review

Muneer Ahmed's picture

Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. online

Muneer Ahmed's picture

wow this good but ,I like your post and good pics may be any peoples not like because defrent mind all poeple , enutrition

ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.