2022-07-21 12:33 PM
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"
Solved! Go to Solution.
2022-07-21 02:35 PM
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
2022-07-21 12:41 PM
Would have thought "%ull" was the typical
Will look at how I'm printing 64-bit CRC values, but recall that being "%016llX"
2022-07-21 12:51 PM
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
2022-07-21 01:02 PM
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.
2022-07-21 01:11 PM
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
2022-07-21 01:24 PM
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/
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.
2022-07-21 02:35 PM
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