SAM4E MCU Tour - 23. Using FPU in AS6 (GCC)

Publisher:码字奇才Latest update time:2017-01-07 Source: eefocusKeywords:SAM4E  MCU  AS6  GCC  FPU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Floating Point Unit (FPU) is a unit used to process floating point operations.

In order to use the FPU, in addition to enabling the FPU, you also need to set the compiler to generate special instructions for floating-point operations. Although these two tasks are completed by default in the project template used by the development board in Atmel Studio 6, this time we still introduce the setting method and simply test the efficiency of the FPU.

 

1. Compiler Settings

In AS6.1 SP2, the compiler used is arm-none-eabi-gcc.exe, version 4.7.3, where "none" means no operating system is specified, and "eabi" means the binary file interface used is eabi.

  1. In ARM GCC, you can set the floating point ABI using the -mfloat-abi option:

    The compiler used by AS6.1 uses the soft option by default. To use the FPU, the softfp option will be used here.

  • soft: Call the soft floating-point library to support floating-point operations. Common instructions are used in GCC to simulate floating-point operations.

  • softfp: Use FPU for floating point operations. However, when calling functions, general registers are still used to pass floating point parameters. This requires additional type conversion overhead.

  • hard: Use the FPU for floating-point operations. When calling a function, use the FPU's registers to pass floating-point parameters.

  • Use the -mfpu option to set the type of FPU hardware.

    SAM4E is equipped with the Cortex-M4F FPU, which implements the FPv4-SP version (SP stands for single precision) of the floating point extension. In addition, it also has 32 32-bit single-precision registers, which can also be used as 16 64-bit double-precision registers for load, store and move operations.

    So you need to assign -mfpu to fpv4-sp-d16, where d16 means there are 16 64-bit registers.

  • Settings in AS6.

    In the Solution Explorer, right-click the project and go to the Properties page. Then select the Toolchain tab and select ARM/GUN C Compiler.image

    Note that modifications also need to be made in the Release version configuration.

  •  

    2. Enable FPU

    When the board is reset, the FPU is disabled. However, the startup file used in AS6 will enable the FPU according to the compiler settings.

    1. How to enable FPU

      Writing 0b11 to the CP10 and CP11 fields of the FPU's CPACR register will open up full access to the FPU. In addition, this register can only be read and written in privileged mode.

      In CMSIS, the address of this register is defined as a reserved address. However, the corresponding API is provided in fpu.h:

      1
      2
      #include // Will conflict with sam.h macro definitions, use board.h
      fpu_enable();

       

      The implementation of fpu_enable() is as follows:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      /** CPACR register */
      #define ADDR_CPACR 0xE000ED88
      #define REG_CPACR (*((volatile uint32_t *)ADDR_CPACR))
       
      /** Save the current interrupt status of the CPU and mask it*/
      irqflags_t flags;
      flags = cpu_irq_save();
       
      /** Modify CPACR register */
      REG_CPACR |= (0xFu << 20);
       
      __DSB(); /** Wait for register modification to complete*/
      __ISB(); /** Clear the processor pipeline */
       
      /** Decide whether to re-enable interrupts based on the settings */
      cpu_irq_restore(flags);

       

    2. What has been done in AS6

      In the AS6 project template used by the development board, the program entry function is Reset_Handler().

      Therefore, you only need to set the compiler parameters to automatically enable the FPU.

      PS: __GNUC__ is a macro predefined in the GCC compiler, __VFP_FP__ is predefined when GCC enables floating-point operations, and __SOFTFP__ is predefined when soft-emulated floating-point operations are used. GCC can print out predefined macros using the "-dM –E" parameter.

    • Before calling the main() function, the function executes the following code:

      1
      2
      3
      #if __FPU_USED
          fpu_enable();
      #endif
    • __FPU_USED is defined in the following code:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      //...
      /* Determine whether the compiler used is GCC */
      #elif defined ( __GNUC__ )
        /* Determine whether floating-point operations are enabled and the operations are not implemented by software*/
        #if defined (__VFP_FP__) && !defined(__SOFTFP__)
            /* Determine whether the target platform has FPU */
          #if (__FPU_PRESENT == 1)
            #define __FPU_USED 1
      #else
      //...
    • __FPU_PRESENT is defined in sam4e16e.h:

      1
      2
      /**< SAM4E16E does provide a FPU */
      #define __FPU_PRESENT 1

     

    3. Testing

    In the first example tutorial, we used an empty loop to delay and complete the LED blinking. Here, we modify the loop body of this empty loop to operate on a floating point number. Then observe the difference in the LED blinking frequency when using or not using the hardware FPU.

    Modify the delay function as follows:

    1
    2
    3
    4
    5
    6
    void Delay(int num)
    {
        volatile float f = 1.0f;
        for (volatile int i = 0; i < 1024 * 64 * num; ++i )
            f*= 1.1f; 
    }

     

    Then compile and execute the program using the “-mfloat-abi=softfp” and “-mfloat-abi=soft” options respectively, and observe the frequency of the LED flashing.


    Keywords:SAM4E  MCU  AS6  GCC  FPU Reference address:SAM4E MCU Tour - 23. Using FPU in AS6 (GCC)

    Previous article:SAM4E MCU Tour - 22. Introduction and initialization of GMAC and PHY
    Next article:SAM4E MCU Tour - 24. Using DSP Library to Find Vector Dot Product

    Latest Microcontroller Articles
    Change More Related Popular Components

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    About Us Customer Service Contact Information Datasheet Sitemap LatestNews


    Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

    Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号