chprintf for 64 bit values Topic is solved

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
meatball
Posts: 32
Joined: Thu May 19, 2016 4:39 pm
Has thanked: 9 times
Been thanked: 2 times

chprintf for 64 bit values

Post by meatball »

I have a GPS unit with that leaves me with ten digits to print out, so I've needed to use int64_t to store these coordinates.

My maths in the code are working with these values well, since debugging shows the variables turning out how they should be, but I'm having trouble printing the values to the serial terminal.

I always hate the ambiguity of "integer" and "long" as the chprintf description uses:

* The following parameter types (p) are supported:
* - <b>x</b> hexadecimal integer.
* - <b>X</b> hexadecimal long.
* - <b>o</b> octal integer.
* - <b>O</b> octal long.
* - <b>d</b> decimal signed integer.
* - <b>D</b> decimal signed long.
* - <b>u</b> decimal unsigned integer.
* - <b>U</b> decimal unsigned long.
* - <b>c</b> character.
* - <b>s</b> string.

Since chprintf() seems to be unable to printf 64 bit numbers, does that mean longs are 32 bit? This is not what I expected.

I assumed a long was 64 bits, so I've been calling

Code: Select all

volatile int64_t test1 = 4000000000;
volatile int64_t test2 = 4000000000;

chprintf((BaseSequentialStream *)&SD2, "%D,%D\r\n",test1, test2);

Notice how both variables are the same value, but the terminal output is:

Code: Select all

-294967296,0


Interesting, yes? I'm having trouble interpreting this output since neither of the variables are getting optimized out as far as I can tell. They are volatile. Is there a way I can print 64 bit ints?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf for 64 bit values

Post by Giovanni »

Hi,

64 bits values are "long long" and currently not supported by chprintf, you could convert it in string and then print with %s.

Note that the support is not in chprintf because it would bring 64 bits math libraries and code size would increase, it is meant as a small utility not a full printf replacement.

Giovanni
meatball
Posts: 32
Joined: Thu May 19, 2016 4:39 pm
Has thanked: 9 times
Been thanked: 2 times

Re: chprintf for 64 bit values

Post by meatball »

Ah OK. Good to know then. I'll work on an itoa() or sprintf() implementation to handle the printing then.

I'll keep using chprintf() for the lightweight matter then.

Thanks!
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: chprintf for 64 bit values

Post by rew »

There are some long "strings" in GPS that only have numbers in them. You could interpret them as integers. But maybe a string is a better type?
Anyway, if you do go the "big integer" route... When printing, I think I would just chprintf ("%d%09d", num/1000000000LL, num % 1000000000LL); Be carful not to use the top 2-3 bits with this trick.....
Post Reply