cancel
Showing results for 
Search instead for 
Did you mean: 

What is the sprintf format for a uint64_t? %llu prints as "lu" (Just the characters, no number) Is there something special library I have to turn on? Like with floats?

KiptonM
Lead

Here is the code:

	if (constx8 == 0) // calculate constants one time.
	{
		uint64_t num = (((uint64_t)(((uint32_t)(R21 + R22))) * VREFINT_CAL_VREF) * (*VREFINT_CAL_ADDR)) << 8;
		uint32_t denom = (uint32_t)4095 * R22;
		constx8 = (uint32_t)(num / denom);  // 8 times actual value.
		if (isDebug(DEBUG_encoder_voltage_calc))
		{
			char buf[150];
			sprintf(buf,"Const calc: num: %llu, denom: %lu, constx8: %lu\r\n", num, denom, constx8);
			print_debug_str(buf);
		}
	}

and it prints

"Const calc: num: lu, denom: 2454134784, constx8: 2646"

1 ACCEPTED SOLUTION

Accepted Solutions
KiptonM
Lead

This will work for debugging. Excel can convert hex to an integer.

	if (constx8 == 0) // calculate constants one time.
	{
		uint64_t num = (((uint64_t)(((uint32_t)(R21 + R22))) * VREFINT_CAL_VREF) * (*VREFINT_CAL_ADDR)) << 8;
		uint32_t denom = (uint32_t)4095 * R22;
		constx8 = (uint32_t)(num / denom);  // 8 times actual value.
		if (isDebug(DEBUG_encoder_voltage_calc))
		{
			char buf[150];
			uint32_t nl = num & 0xFFFFFFFF;
			uint32_t nh = (num >> 32) & 0xFFFFFFFF;
			sprintf(buf,"Const calc: num: 0x%08lX%08lX, denom: %lu, constx8: %lu\r\n", nh, nl, denom, constx8);
			print_debug_str(buf);
		}
	}

Const calc: num: 0x00000A5692472000, denom: 13513500, constx8: 841154

View solution in original post

6 REPLIES 6

Would have thought "%ull" was the typical

Will look at how I'm printing 64-bit CRC values, but recall that being "%016llX"

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I tried it

	if (constx8 == 0) // calculate constants one time.
	{
		uint64_t num = (((uint64_t)(((uint32_t)(R21 + R22))) * VREFINT_CAL_VREF) * (*VREFINT_CAL_ADDR)) << 8;
		uint32_t denom = (uint32_t)4095 * R22;
		constx8 = (uint32_t)(num / denom);  // 8 times actual value.
		if (isDebug(DEBUG_encoder_voltage_calc))
		{
			char buf[150];
			sprintf(buf,"Const calc: num: %016llX, denom: %lu, constx8: %lu\r\n", num, denom, constx8);
			print_debug_str(buf);
		}
	}

Const calc: num: 000000000000000lX, denom: 2454134784, constx8: 2646

KiptonM
Lead

It may be the library. It looks like it defaults to reduced C library, With the reduced library and Debug optimization it says I have 8.65 KB Free.

when I switch to standard C library FLASH overflows by 13160 bytes, and when I optimize for size FLASH overflows by 10560 bytes.

printf() and kin does not have place in mcu (and as your case shows, literally). Write your own print routines. Printing out hexadecimals of any length is trivial, decimal is just slightly less trivial.

There are also various open source "lightweight, mcu-optimized" etc. printf() out there. You can google, try, get inspired.

JW

Piranha
Chief II

Newlib-nano printf() functions don't support 64-bit integers.

https://github.com/zephyrproject-rtos/zephyr/issues/29363

https://www.eevblog.com/forum/microcontrollers/printf-uint64_t-with-arm-gcc/

https://stackoverflow.com/questions/33438641/why-do-i-get-lu-when-i-try-to-print-a-u64-variable-with-llu-instead-of-the

Eventually because those functions are using malloc(), are not thread safe/reentrant, doesn't support 64-bit integers, are bloated and have only C standard library features, it is recommended to get rid of them and use nanoprintf or LwPRINTF. These implementations doesn't have any of those limitations and have even some non-standard additional very useful features. Note that for floating point types nanoprintf internally casts double to float and calculations are only 32-bit. That can be a feature or a limitation, depending on the system needs.

KiptonM
Lead

This will work for debugging. Excel can convert hex to an integer.

	if (constx8 == 0) // calculate constants one time.
	{
		uint64_t num = (((uint64_t)(((uint32_t)(R21 + R22))) * VREFINT_CAL_VREF) * (*VREFINT_CAL_ADDR)) << 8;
		uint32_t denom = (uint32_t)4095 * R22;
		constx8 = (uint32_t)(num / denom);  // 8 times actual value.
		if (isDebug(DEBUG_encoder_voltage_calc))
		{
			char buf[150];
			uint32_t nl = num & 0xFFFFFFFF;
			uint32_t nh = (num >> 32) & 0xFFFFFFFF;
			sprintf(buf,"Const calc: num: 0x%08lX%08lX, denom: %lu, constx8: %lu\r\n", nh, nl, denom, constx8);
			print_debug_str(buf);
		}
	}

Const calc: num: 0x00000A5692472000, denom: 13513500, constx8: 841154