Difference between revisions of "BoostC tiny Wiki"

From OpenCircuits
Jump to navigation Jump to search
Line 68: Line 68:
  
  
Use parenthsis not operation conventions:
+
Use parenthesis not operation conventions:
  
 
   c    = a + ( b * c )  
 
   c    = a + ( b * c )  

Revision as of 02:26, 21 January 2009

Introduction

This is the very beginning of a Wiki for BoostC. Its organization will probably change a lot in the near future ( if we can get the free labor required ). It may move to the SourceBoost site if they want to host it. Since BoostC is proprietary it may be inappropriate for it to grow too big here unless we can find some explicit support for it. For now here it is. Much of this material applies to compilers other than BoostC and to environments other than PICs embedded systems. Major sections will probably be split into seperate pages. SourceBoost Homepage

Tips/Tricks/Gotchas

Watch out for set-bit!

The function set-bit() is almost right, but it should be set_bit(). This and similar errors are subtraction, and the result is error messages ( how about a sample ) that are not very helpful.

Rebuild It

Sometimes I have been able to get rid of odd errors by forcing a rebuild by erasing all but the project file and the .c and .h files. The manual suggests that Ctrl+F7 or Ctrl+build command do pretty much the same thing.

Subroutine Signatures

Often we have several versions of a subroutine with different signatures ( set and type of call arguments ) Try to check that you are calling the version you want. Casting may help.

Flakey Stack

If a program is flakey, particularly after working in earlier versions you may have run out of stack space for the call return stack.

To help you tell, look at all the linker messages. Also open the code window and look at the call tree ( View -> Code Bar -> Call Tree ), see anything in red?

The fix:

  • Move the function inline in the code ( if only called once or if small )
  • Declare as an inline function.
  • Use the linker options to add a software stack.

Bad Options

Check you options, target should be right. Recently I had a problem with the Compiler location option. It seems to be a option of the project ( which makes sense if you want different projects to behave differently ) not the ide installation, so especially if you got the project from someone else check it. For me a bad compiler location made the build take forever and do nothing; the compiler gave a useful error message.

Standard C Issues

Use a Lint Program

Has anyone configured one for BoostC?

Read This

C Traps and Pitfalls From Wikipedia, the free encyclopedia Note that there is a free download or a longer ( for purchase ) book.

Good Pratices

Opinions may differ!

Put a good header in the program

I keep refining my, see one of my projects like PIC Stepper Motor Demonstration and Test Project or an page with a version of the header A C Program Header Example.

Avoid Shortcuts

For example I always use braces with an if statement, there is less chance of error if someone adds a line:

if ( isTrue ) {
   ix ++;
}

Use full declarations:

 unsigned char ix;

    //not

 char ix;

The reader, and perhaps even the programmer may not remember the default or remember it correctly


Use parenthesis not operation conventions:

 c    = a + ( b * c ) 

    //not

 c    = a + b * c

Read This

Recommended C Style and Coding Standards from Bell Labs

Optimization

As a general rule it has been observed that many programmers spend too much time on optimization and that often the compiler can do a better job than the programmer. Often readability of the code suffers for "optimizations" that do not really optimize anything. That said, it is worthwhile to optimize the algorithm. C does not know the purpose of the code, the programmer should, the compiler can only optimize the code in doing what you said, not what you want.


Questions

Questions:

  • Is shifting better than multiplying/dividing by poweres of 2?
  • does if( intcon & (1<<T0IF) ) work better than test_bit( intcon, T0IF )?
  • Is there a time penality to using local variables.

Optimizations that Seem to Work

Declare variables as the simplest type that will work. Keep it small, keep it unsigned unless you need it otherwise.

Optimizations that Seem Not to Work

Some of these may not truely attempts at optimization, just different ways of writing code, in andy case we compare them to other presumabably clearer ways of writing the code.

Generally these test were done by writing the code and then examining the casm file.

Shift, vs Test

In interrupts we often see a bit test as

    if( intcon & (1<<T0IF) )

by which it is meant:

    if( test_bit(  intcon, T0IF) )

Looking at the generated code: it is identical, so why not use the clearer formulation?

Code Snips that may Be Helpful

You are counting down and want to know when an unsigned number goes negative ( never ). You could declare it signed, slowing everything down. Instead check against FF. This assumes you do not use FF on the positive side.

  if ( ix == 0xFF ) ..


Example Programs and Projects

Go to PIC Links and search ( page search not google ) on "BoostC".

Further reading

  • Wikibooks: C programming is about C programming in general (alas, focuses on programs running on desktop computers, rather than small microcontrollers).