I can't pass value using variable to log from math.h

20 views Asked by At

I have a problem with using log from math.h library on stm32f411ret6. when i pass a value normally to log eg.

double var = log(2.0); 

than i get 0.69314718056 in var. When i pass a value using variable than stm32 go to HardFault_Handler eq.

double var1 = 2.0;
double var2 = log(var1);

My code.

#include "main.h"

int main(void)
{


    double val = 2.0;
    double cos = log(2.0);
    double result = log((double)val);
    Board_Init();

    for(;;);
}

same problem even for custom function that calculate log.

int main(void)
{


    double val = 2.0;
    double cos = log(2.0);
//  double result = log((double)val);
    double result = Math_Log(2.0, 2.7182);
    Board_Init();

    for(;;);
}

double Math_Log(double number , double base)
{
    double low = 0, high = number;
    double epsilon = 0.000001;
    while (high - low > epsilon) {
        double mid = (low + high) / 2;
        double power = 1;
        for (int i = 0; i < mid; ++i) {
            power *= base;
        }
        if (power < number) {
            low = mid;
        } else {
            high = mid;
        }
    }
    return low;
}

I use stm32cubeIDE and i have marked option "Use C math library"

It would be nice to know what is causing this problem

1

There are 1 answers

0
dawid gurdzinski On

I got it. The problem was with access privileges for coprocessors. fix:

SCB->CPACR |= 0x0F << 20;

Add this in start of main;