2023-09-18 02:44 AM - edited 2023-09-18 02:49 AM
Hello. In my project, I would like to printf float type variables such as:
float t_degC = -45 + 175 * t_ticks/65535;
float rh_pRH = -6 + 125 * rh_ticks/65535;
printf("t_degC = %.2f \n",t_degC);
printf("rh_pRH = %.2f \n",rh_pRH);
Initially, I had the following error:
So I have enabled the flag as suggested by the warning:
After adding this flag, I can no longer compile my project as I am getting the following errors:
c:\st\stm32cubeide_1.10.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127\tools\arm-none-eabi\bin\ld.exe: SHT40_test.elf section `.text' will not fit in region `FLASH'
c:\st\stm32cubeide_1.10.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127\tools\arm-none-eabi\bin\ld.exe: region `FLASH' overflowed by 5416 bytes
This is quite strange since my project is pretty much blank. I only have a couple lines of code and it says that it cannot fit in the FLASH section? What is up with the linker flag that I have added to allow printf float variables?
Is it normal that a almost blank project for STM32L031 already uses more than 60% of total flash memory?
Solved! Go to Solution.
2023-09-18 02:48 AM
Printing floats is quite complicated and consumes lots of resources, both FLASH and RAM. Choose an STM32 with larger FLASH or don't use/print floats.
One way to avoid floats is to use fixed decimal point - simply scale all your variables by 100.
JW
2023-09-18 02:48 AM
Printing floats is quite complicated and consumes lots of resources, both FLASH and RAM. Choose an STM32 with larger FLASH or don't use/print floats.
One way to avoid floats is to use fixed decimal point - simply scale all your variables by 100.
JW
2023-09-18 05:30 AM - edited 2023-09-18 12:45 PM
You can also use more aggressive optimization which will reduce code size. In general, STM32CubeIDE isn't created with small executables in mind, but it can work.
I created a nearly-blank project in STM32CubeIDE for that chip and the Debug file size is under 5 kB. You must be adding something else to increase the size.
Edit: nope, apparently using printf with floats just adds like 20kB to flash size. Crazy.
2023-09-18 12:36 PM - edited 2023-09-18 12:38 PM
Use nanolib and not full libc.
Try to use fixed point arithmetic as sayed by Jan.
If you really can't avoid float, you must be sure that const are float and not double. To do that you MUST add after each const float value a 'f' letter.
For example :
float t_degC = -45.0f + 175.0f* (float)t_ticks/65535.0f;
But for sure you can use integer only...
2023-09-18 10:24 PM - edited 2023-09-18 10:25 PM
Thanks for all the responses. I would have never thought that using printf floats are so complex and requires 10's of KB's of FLASH memory.
For now, I just use integer like below:
float t_degC = -45.0f + 175.0f * ((float)(t_ticks/65535.0f));
float rh_pRH = -6.0f + 125.0f * ((float)(rh_ticks/65535.0f));
uint32_t t_degC_to_int = (uint32_t)(t_degC * 100.0f);
uint32_t rh_pRH_to_int = (uint32_t)(rh_pRH * 100.0f);