2

I am upgrading a fully tested C program for the Texas Instruments (TI) MSP430 micro-controller using a different C compiler, changing from the Quadravox, AQ430 Development Tool to the VisualGDB C compiler.

The program compiles with zero errors and zero warnings with VisualGDB. The interrupt service routines, timers, UART control, and more all seem to be working. Certain uses of sprintf, however, are not working. For example:

unsigned int duration;
float msec;

msec = (float)duration;

msec = msec / 125.0;

sprintf(glb_response,"break: %3.1f msec",msec);

This returns: break: %3.1f msec

This is what is expected: break: 12.5 msec

I have learned about the following (from Wikipedia):

The compiler option --printf_support=[full | minimal | nofloat] allows you to use a smaller, feature limited, variant of printf/sprintf, and make that choice at build time.

The valid values are:

full: Supports all format specifiers. This is the default.

nofloat: Excludes support for printing floating point values. Supports all format specifiers except %f, %g, %G, %e, and %E.

minimal: Supports the printing of integer, char, or string values without width or precision flags. Specifically, only the %%, %d, %o, %c, %s, and %x format specifiers are supported

I need full support for printf. I know the MSP430 on my product will support this, as this C program has been in service for years.

My problem is that I can't figure out 1) if VisualGDB has the means to set printf support to full and 2) if so, where and to set it.

Any and all comments and answers will be appreciated.

Clifford
  • 88,407
  • 13
  • 85
  • 165
Mike Jablonski
  • 1,703
  • 6
  • 27
  • 41
  • Doesn't seem the MSP430 toolchain for VisualGDB has that compiler option. Last I heard, floats weren't well supported on the platform, but it's been a while since I used it. – Joachim Isaksson Sep 22 '12 at 16:27
  • 1
    I would not rely on Wikipedia as product documentation! GCC in general is documented at http://gcc.gnu.org/onlinedocs/ and the MSPxxx specific version at http://mspgcc.sourceforge.net/manual/. However the library and the compiler are distinct, support for floating point in stdio is dependent on what C library your toolchain uses. – Clifford Sep 22 '12 at 17:00
  • In addition to the answers here, I'd suggest asking on the VisualGDB vendor's forum: http://forum.sysprogs.com – Michael Burr Sep 22 '12 at 19:36

2 Answers2

11

I would suggest that full support for floating point is both unnecessary and ill-advised. It is a large amount of code to solve a trivial problem; and without floating-point hardware, floating point operations are usually best avoided in any case for performance, code space and memory usage reasons.

So it appears that duration is in units of 1/125000 seconds and that you wish to output a value to a precision of 0.1 milliseconds. So:

unsigned msec_x10 = duration * 10u / 125u ;

sprintf( glb_response, 
         "break: %u.%u msec", 
         msec_x10 / 10,  
         msec_x10 % 10 ) ;

If you want rounding to the nearest tenth (as opposed to rounding down), then:

unsigned msec_x10 = ((duration * 20u) + 125 ) / 250u ;
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • I'm going to accept you answer. I understand your comments and your code. I'm going to eliminate the floating point operations from my program. Thank you! – Mike Jablonski Sep 23 '12 at 01:52
4

I agree with Clifford that if you don't need floats (or only need them for printing), don't use them.

However, if your program is already using floats extensively, and you need a way to print them, consider adapting a public domain printf such as the one from SQLite.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
  • Thank your for your answer. I'm going to take Clifford's advice. That said, I'll also take a look at the public domain printf that you pointed out. – Mike Jablonski Sep 23 '12 at 01:54