cancel
Showing results for 
Search instead for 
Did you mean: 

how to use FPU in STM32F407

Chopra.1
Associate II

I am using STM32F407 discovery board which is ARM cortex M-4 based , which comes with FPU.

I need to perform some Floating point number and exponent calculations. for that i need to use to FPU to speed up the process.

I have have written all the peripheral drivers needed for my application myself ( i haven't use the cube HAL layer or cmsis layer code for that )

how can i use the FPU of processor to perform the calculation ? Do i have to write code for that or do i have to add some library ?

i tried to enable the HW based FPU in the setting but when i perform the float / double calculation the code got stuck infinite loop, i think it is happing because the code to perform floating calculations is missing or FPU is not being used?

also can someone tell me if i have to perform software based floating point calculation.

which library i have to add to my code.

software i am using : STM32 Cube IDE

Thank you

kamal chopra

5 REPLIES 5
TDK
Guru

At compile-time, there are flags which are used to tell the compiler the chip has an FPU and to create hardware FPU instructions rather than software emulation. This is a compiler-time choice and not something that can be changed at run-time.

0693W00000DmOeIQAV.png 

Enabling the FPU at run-time is also required, but is a separate step from this. In order to use hardware FPU, it needs to be compiled as such and the FPU needs to be enabled during startup.

If you feel a post has answered your question, please click "Accept as Solution".
Chopra.1
Associate II

Thanks but ,

I have tried doing this but the result is same ,

(i have attacted some img for referance )

0693W00000DmhXQQAZ.png 

0693W00000DmhXzQAJ.png 

this is the first statement in my program here FPU calculation is required ( shown in above photo)

	double accelX_FT_val , accelY_FT_val ,accelZ_FT_val ;
	if ( accel_XTestdata!= 0 )
	{
		//FT[Xg] = 25*131*1.046^(XG_TEST - 1 )
		accelX_FT_val = pow(3425.65 , ( accel_XTestdata - 1)) ;
 
	}else

when this statement runs a hardware error occurs and program goes to infinite loop

0693W00000DmhZvQAJ.png 

Can you help with what i am missing

also .

can you elaborate this Enabling the FPU at run-time is also required, but is a separate step from this. In order to use hardware FPU, it needs to be compiled as such and the FPU needs to be enabled during startup.

thank you

TDK
Guru

You're using the 64-bit "double" type, which is not supported by the FPU on the STM32F4. Only the 32-bit "float" is.

In the infinite loop, look at the value of the VECTACTIVE field in SCB_ICSR to see what interrupt the chip is in.

If you feel a post has answered your question, please click "Accept as Solution".
Chopra.1
Associate II

thanks,

I have changed variable from double to float

then also fault occurs,

As you said,

i have checked , VECTACTIVE field in SCB_ICSR  : VECTACTIVE - 0x3 { so it means hard fault occurs }

also i have checked , these bits :

SCB->CFSR-> FORCED - 0x1 (it is telling me hard fault is taken because of bus fault , usage fault , memory management fault )

SCB->CFSR->NOCP - 0x1 (it tells a attempt to execute coprocessor instruction )

also if have tried some other things

// i tried to run these instruction separately for testing  purpose
 
accelX_FT_val = pow (2,5); // works fine 
 
accelX_FT_val = pow(1 , ( accel_XTestdata - 1)) ;// works fine 
 
accelX_FT_val = pow(1.046 , ( accel_XTestdata - 1)) ; // hard fault ouccrs 
accelX_FT_val = 25*131*accelX_FT_val; // hard fault ouccrs 
accelX_FT_val = pow(3425 , ( accel_XTestdata - 1)) ;  // hard fault ouccrs 
accelX_FT_val = pow(3425.65 , ( accel_XTestdata - 1)) ; // hard fault ouccrs 

as this : accelX_FT_val = pow (2,5); // works fine  so math.h library has been added no problem in that case

accelX_FT_val = pow(1.046 , ( accel_XTestdata - 1)) ; // hard fault occurs  { as it is floating point calculation, could it be due to the reason FPU is disabled ??? }

accelX_FT_val = 25*131*accelX_FT_val; // hard fault ouccrs  ( if i calculated this explicitly result is 8.9245738115672328584839563027024e+45 )

accelX_FT_val = pow(3425 , ( accel_XTestdata - 1)) ; // hard fault ouccrs  ( if i calculate this explicitly result is 8.9245738115672328584839563027024e+45 )

In these two cases hard fault occurs due to over flow of variables ???

The FPU on the CM4F is single precision only, perhaps powf() ?

FPU typically needs to be enabled with code in SystemInit() or before

void SystemInit(void)
{
  /* FPU settings ------------------------------------------------------------*/
  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
    SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
  #endif
... 

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