Issue with STM32 and FPU

Sysprogs forums Forums VisualGDB Issue with STM32 and FPU

  • This topic has 2 replies, 2 voices, and was last updated 1 year ago by Anonymous.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #35112
    Dschadu
    Participant

    Hi,

    I stumbeld uppon a big issue:
    Within a minimal-testing program, the return value of sqrtf() is always 1.
    Setup: GCC 12.3.1, cmake 3.20.3.2, STM32F401CCU6.
    Project created with VisualGDB and STM32CubeMX.

    This is the test-program (cut in length, due to the size):

    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    #include 
    /* USER CODE END Includes */
    [Cut]
    int main(void)
    {
    // [Cut]
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    /* USER CODE BEGIN 2 */
    float x = 125;
    float y = 125;
    float xx = powf(x, 2); // Is correct: 15625
    float yy = powf(y, 2); // Is correct: 15625
    float xxyy = xx + yy; // Is korrect: 31250
    float r0 = sqrtf(xxyy); // Is wrong: 1
    float r1 = r0 - 36;
    /* USER CODE END 2 */
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
    }

    The Init function for the FPU is called:

    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
    // [Cut]
    }

    If I change the following, within the file BSP.cmake:
    set(_core_commonflags -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard)
    to
    set(_core_commonflags -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=soft)

    the result of sqrtf() from the equation above is 1127270102, which is obviously wrong.

    If I use the STM32CubeIDE instead, its working as intended with the same code (with hardware FPU). There has to be something wrong with the cmake scripts provided. But I’m not able to figure it out.
    I would appreciate if you could solve this issue. Yes, I know, I’m not within the support period anymore – but if there is no fix to this issue, this would be wasted money. If you are able to fix this, or if this is fixed with the latest version, I will buy the latest version. Otherwise this is a big roadblock and I’m forced to use the CubeIDE – which I would prefer not to.

    Thanks for your help in advance.

    #35115
    Dschadu
    Participant

    Well…. that’s embarrassing and I’m sorry.

    Visual Studio interprets “r0” as ASM Register “r0”, and not the float variable “r0”. I renamed it, and it shows the correct value.

    Barked at the wrong tree here! Again, I’m sorry 🙂

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.