Author Topic: Enabling FPU in STM32  (Read 6068 times)

0 Members and 2 Guests are viewing this topic.

Offline uer166Topic starter

  • Frequent Contributor
  • **
  • Posts: 972
  • Country: us
Enabling FPU in STM32
« on: June 26, 2020, 11:40:34 pm »
How do I enable FPU in STM32 without resorting to assembly. Currently I have this from the ARM site which works:
Code: [Select]
void enable_FPU(void)
{
__asm volatile(
// CPACR is located at address 0xE000ED88
"LDR.W   R0, =0xE000ED88 \n\t"
// Read CPACR
"LDR     R1, [R0] \n\t"
// Set bits 20-23 to enable CP10 and CP11 coprocessors
"ORR     R1, R1, #0xF << 20 \n\t"
// Write back the modified value to the CPACR
"STR     R1, [R0] \n\t"
: // No outputs
: // No input operands
: "r0", "r1");
}

But I don't care for assembly, and considering there are proponents here claiming Cortex-M can be programmed entirely in C, someone ought to know how to do it more easily..
 

Offline uer166Topic starter

  • Frequent Contributor
  • **
  • Posts: 972
  • Country: us
Re: Enabling FPU in STM32
« Reply #1 on: June 26, 2020, 11:43:48 pm »
Nevermind, should have googled harder, for other passerbys it's:

Code: [Select]
    // Enable hardware FPU
    SCB->CPACR |= ((3UL << 10*2) | /* set CP10 Full Access */
                   (3UL << 11*2) ); /* set CP11 Full Access */

I'm slowly warming up to the idea of bare-metal development with no HAL or Cube..
 
The following users thanked this post: thm_w, I wanted a rude username

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 7096
  • Country: ca
  • Non-expert
Re: Enabling FPU in STM32
« Reply #2 on: June 27, 2020, 12:06:13 am »
Did you figure out the compiler flags as well?

This is for another micro but looks useful: https://www.silabs.com/community/mcu/32-bit/knowledge-base.entry.html/2014/04/16/how_to_enable_hardwa-vM9u
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 

Offline Sal Ammoniac

  • Super Contributor
  • ***
  • Posts: 1763
  • Country: us
Re: Enabling FPU in STM32
« Reply #3 on: June 27, 2020, 12:26:00 am »
But I don't care for assembly, and considering there are proponents here claiming Cortex-M can be programmed entirely in C, someone ought to know how to do it more easily..

What's wrong with assembly, or perhaps I should be asking why you don't care for it? Knowing assembly can be a big help when debugging when you need to know exactly what the CPU is doing at the lowest level.
"That's not even wrong" -- Wolfgang Pauli
 

Offline Monkeh

  • Super Contributor
  • ***
  • Posts: 8050
  • Country: gb
Re: Enabling FPU in STM32
« Reply #4 on: June 27, 2020, 01:17:39 am »
Dare I ask why you needed to Google a solution to changing a couple bits in a register, when the bits and the register name and address you already knew?
 
The following users thanked this post: mark03

Offline uer166Topic starter

  • Frequent Contributor
  • **
  • Posts: 972
  • Country: us
Re: Enabling FPU in STM32
« Reply #5 on: June 29, 2020, 03:47:02 am »
Dare I ask why you needed to Google a solution to changing a couple bits in a register, when the bits and the register name and address you already knew?

I didn't? I got a warning on a bare-metal STM32Cube project saying basically "please enable FPU, it is not enabled automatically", or something, and it would hardfault on any floating-point instructions, that is all I knew, and it took a while to figure out the right query  :-//

I also assumed you couldn't access the FPU enable bits directly in C easily, just like the R0, R1, etc registers.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15275
  • Country: fr
Re: Enabling FPU in STM32
« Reply #6 on: June 29, 2020, 03:17:51 pm »
Note that if you look at the example projects provided by STM, you'll find out that they (all?) have a system_stm32XXXX.c file, in which the SystemInit() function does enable the FPU:

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

This function is itself called from the startup code (which is in assembly, usually in a file called startup_stm32XXXX.s)

I for one don't use CubeMX and mostly go baremetal with STM32 MCUs, but I still have taken inspiration for my "baremetal" projects on STM's example projects, so the startup code is essentially the same.
 
The following users thanked this post: thm_w, uer166


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf