FPU Enable - STM32f401CCU6

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
madhavan
Posts: 29
Joined: Fri May 21, 2021 1:22 am

FPU Enable - STM32f401CCU6

Post by madhavan »

Hi, I am trying to enable the FPU on stm32f401CCU6 .

I keept the following setting as Default in STM Official Core - Duino.

Tools----->Optimize------>Smallest(-Os default).
Tools-----> Debug Symbols ------> None
Tools ------> C Runtime Library ----->Newlib nano (default)

Really what is the exact use of use of above setting ? i do not know and it was working as default . It will be useful if there is brief information of above setting .

i thought of use FPU and i was read forum discussion and attempt to use fpu .
i have tested the following setting condition giving different result for the below attached main code (without FPU enable code ).

Tools----->Optimize------>Smallest(-Os default) = Float sum (ms): 0
Tools----->Optimize------>Smallest(-Os ) with LTO= Float sum (ms): 0
Tools----->Optimize------>Fast(-O1 ) = Float sum (ms): 17
Tools----->Optimize------>Fast(-O1 ) with LTO= Float sum (ms): 17
Tools----->Optimize------>Faster(-O2 ) = Float sum (ms): 0
Tools----->Optimize------>Faster(-O2 ) with LTO= Float sum (ms): 0
Tools----->Optimize------>Fastest-O3 ) = Float sum (ms): 0
Tools----->Optimize------>Fastest(-O3 ) with LTO= Float sum (ms): 0
Tools----->Optimize------>Debug(-Og)= Float sum (ms): 35 .

Code: Select all

int sensorValue = 0; 


void setup() {

  
  Serial.begin(9600);
  float fl_2, fl_3;
  unsigned long tloop,tf,to ;
uint32_t itop = 1000000;
fl_2    = 1.00;
fl_3    = 0.00;
to = micros();
for (uint32_t i = 0; i < itop; i++)
{
  fl_2    = fl_2 + fl_3;
  fl_3    = fl_2 + fl_3;
}
tf  = micros() - to;
Serial.print("Float sum (ms): ");
Serial.print((tf - tloop)/2000);
Serial.println();
Serial.println();
 
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(ATEMP);
  Serial.println(sensorValue);
  delay(1000);
}
the below attached main code is working with followed Setting included with FPU enabling code .

Tools----->Optimize------>Fast(-O1 ) = Float sum (ms): 17
Tools----->Optimize------>Fast(-O1 ) with LTO= Float sum (ms): 17
Tools----->Optimize------>Debug(-Og)= Float sum (ms): 35 .

the below attached main code is "" Not working “ - Getting Freeze with followed setting included with FPU enabling code .

Tools----->Optimize------>Smallest(-Os default)
Tools----->Optimize------>Faster(-Os )
Tools----->Optimize------>Faster(-O2 ) with LTO
Tools----->Optimize------>Fastest(-O3 )
Tools----->Optimize------>Fastest(-O3 ) with LTO

Code: Select all

int sensorValue = 0; 


void setup() {

  enablefpu();
  
  Serial.begin(9600);
  float fl_2, fl_3;
  unsigned long tloop,tf,to ;
uint32_t itop = 1000000;
fl_2    = 1.00;
fl_3    = 0.00;
to = micros();
for (uint32_t i = 0; i < itop; i++)
{
  fl_2    = fl_2 + fl_3;
  fl_3    = fl_2 + fl_3;
}
tf  = micros() - to;
Serial.print("Float sum (ms): ");
Serial.print((tf - tloop)/2000);
Serial.println();
Serial.println();
  
 
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(ATEMP);
  Serial.println(sensorValue);
  delay(1000);
}

void enablefpu() {
    __asm volatile
   (
     "  ldr.w r0, =0xE000ED88    \n"  /* The FPU enable bits are in the CPACR. */
     "  ldr r1, [r0]             \n"  /* read CAPCR */
     "  orr r1, r1, #( 0xf << 20 )\n" /* Set bits 20-23 to enable CP10 and CP11 coprocessors */
      "  str r1, [r0]              \n" /* Write back the modified value to the CPACR */
      "  dsb                       \n" /* wait for store to complete */
    "  isb"                          /* reset pipeline now the FPU is enabled */
   );
}
Please Identify where i am doing mistake and suggest me FPU Enabling and setting usage .
ag123
Posts: 1657
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: FPU Enable - STM32f401CCU6

Post by ag123 »

the FPU is normally already enabled, in both official STM and libmaple based cores.
Accordingly, it helps to put that enable_fpu code in its own .cpp file, then include the definition and include it from a #include
madhavan
Posts: 29
Joined: Fri May 21, 2021 1:22 am

Re: FPU Enable - STM32f401CCU6

Post by madhavan »

hi, Kept default setting .

Tools----->Optimize------>Smallest(-Os default).
Tools-----> Debug Symbols ------> None
Tools ------> C Runtime Library ----->Newlib nano (default)

Code: Select all


#include"enable_fpu.h"

int sensorValue = 0; 


void setup() {

  
  
  Serial.begin(9600);
  float fl_2, fl_3;
  unsigned long tloop,tf,to ;
uint32_t itop = 1000000;
fl_2    = 1.00;
fl_3    = 0.00;
to = micros();

for (uint32_t i = 0; i < itop; i++)
{
  fl_2    = fl_2 + fl_3;
  fl_3    = fl_2 + fl_3;
}
tf  = micros() - to;

Serial.print("Float sum (ms): ");
Serial.print((tf );
Serial.println();
Serial.println();
  
 
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(ATEMP);
  Serial.println(sensorValue);
  delay(1000);
}
cpp

Code: Select all

#include"enable_fpu.h"


void enablefpu() {
    __asm volatile
   (
     "  ldr.w r0, =0xE000ED88    \n"  /* The FPU enable bits are in the CPACR. */
     "  ldr r1, [r0]             \n"  /* read CAPCR */
     "  orr r1, r1, #( 0xf << 20 )\n" /* Set bits 20-23 to enable CP10 and CP11 coprocessors */
      "  str r1, [r0]              \n" /* Write back the modified value to the CPACR */
      "  dsb                       \n" /* wait for store to complete */
    "  isb"                          /* reset pipeline now the FPU is enabled */
   );
}
header

Code: Select all

#ifndef ENABLE_FBU_H_
#define ENABLE_FBU_H_

void enablefpu();

#endif
output ------> Float sum-Micsec: 71478

without FpU output ---------> Float sum-Micsec :71478
Looks and
as you mentioned FpU is default enabled
Post Reply

Return to “General discussion”