1

I'm displaying a 32bit timer value on my putty-console. The timer includes the time in microseconds since startup of my stm32wb55. With following code, it works exaclty like I want:

uint32_t time_micro32

sprintf((char*)buf,
          "Time: %lu \r\n",
         (time_micro32));

But now, I want to display the time in 64bit resolution with uint64_t time_micro64. I tried many, but nothing works. Can anybody help me, please? I´m programming in STM32CubeIDE

AnBa
  • 41
  • 7
  • Why would you use microsecond resolution and then print it with some PC programming stdio.h barf that at best has millisecond resolution? – Lundin Feb 11 '21 at 11:33

1 Answers1

2

I´m programming in STM32CubeIDE

So you are using most probably using newlib in it's "nano" version - a smaller version of newlib, which does not handle printing 64-bit variables.

Link with full newlib version (or use other C standard library) and the code will work fine. Search your gui - there should be an option like "use -nano version of the standard library" or "pass -specs=nano.specs to the compiler". Uncheck that option. Make sure that -lnano nor -specs=nano.specs or similar options are not passed to the compiler. After that, you can use %llu or "%"PRIu64.

Prefer to use snprintf instead of sprintf.

Are there any other options?

Roll your own implementation that would convert 64-bit variable into a string - writing such function is very trivial. Then use this function and print the resulting string. I have such function.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Switching the Runtime library from `specs=nano.specs` to `standard C library` was the key! Thank you! – AnBa Feb 11 '21 at 12:57
  • "After that you can use %llu or "%"PRIu64." --> sounds good today. Later on when, with `long long` as 128-bit, using `%llu` with `uint64_t` will fail. Best to print with `"%"PRIu64`. – chux - Reinstate Monica Feb 11 '21 at 13:28
  • Linked function looks wrong. Rather than `div_t d = div(v, 10);`, I'd expect `lldiv_t d = lldiv(v, 10);`. But that remains insufficient when `v > LLONG_MAX`. Hopefully only `c = v % 10;` path is used. Note: `llabs(LLONG_MIN)` is UB. – chux - Reinstate Monica Feb 11 '21 at 13:36