I am using STM32CubeIDE with STM32G030F6Px Controller which have 64kB of Flash memory. Currently I am using 36.5kB of flash but when in add a few line of code in a function (Note CurrTemp is a global variable of uint8_t) i.e.
int16_t sensorValue, temperatureC, temperatureK;
int32_t voltageOut;
sensorValue = 0; voltageOut = 0; temperatureC = 0; temperatureK = 0;
sensorValue = (int16_t)ADCRead();
sensorValue = (int16_t)(4095.0 - sensorValue);
voltageOut = (int32_t)((sensorValue * 3250.0) / 4095.0);
temperatureK = (int16_t)(voltageOut / 10.0);
temperatureC = (int16_t)(temperatureK - 273);
CurrTemp = (uint8_t)temperatureC;
I get Errors
c:\st\stm32cubeide_1.7.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: fan_retest.elf section `.text' will not fit in region `FLASH'
c:\st\stm32cubeide_1.7.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 2372 bytes
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:64: fan_retest.elf] Error 1
"make -j4 all" terminated with exit code 2. Build might be incomplete.
What's the issue I don't understand.
CodePudding user response:
Almost certainly the problem is caused by using floating point calculations which pull in the math library and the all the associated exception handling code from the standard library.
To find out if this is the case you need to look in the map file output by the linker.
It is possible to stub out the error handling code and just get the floating point libraries, and maybe that will fit in a 32kB or 64kB microcontroller, but really this is not the normal way to do something like this. The normal way to do something like this on a small microcontroller is to use fixed point.
CodePudding user response:
As Tom V already pointed out, the reason is probably the floating point libraries that get included in the build.
But there is a way that may reduce the memory footprint: Use float
instead of double
. For example, by default 3250.0
is a double
. But if you write 3250.0f
instead, you make it float
, which makes calculations faster and take less flash space. Remember to append f suffix to all floating point literals you use.
Still, it's better to avoid floating point calculations on targets that lack hardware floating point support.