Search code examples
cstm32nucleofmodf

Does fmodf() cause a hardfault in stm32?


I am trying to create a modulated waveform out of 2 sine waves. To do this I need the modulo(fmodf) to know what amplitude a sine with a specific frequency(lo_frequency) has at that time(t). But I get a hardfault when the following line is executed:

j = fmodf(2 * PI * lo_frequency * t, 2 * PI);

Do you have an idea why this gives me a hardfault ?

Edit 1:

I exchanged fmodf with my_fmodf:

float my_fmodf(float x, float y){
    if(y == 0){
        return 0;
    }
    float n = x / y;
    return x - n * y;
}

But still the hardfault occurs, and when I debug it it doesn't even jump into this function(my_fmodf).

Heres the whole function in which this error occurs:

int* create_wave(int* message){
    /* Mixes the message signal at 10kHz and the carrier at 40kHz.
     * When a bit of the message is 0 the amplitude is lowered to 10%.
     * When a bit of the message is 1 the amplitude is 100%.
     * The output of the STM32 can't be negative, thats why the wave swings between
     * 0 and 256 (8bit precision for faster DAC)
     */
    static int rf_frequency = 10000;
    static int lo_frequency = 40000;
    static int sample_rate = 100000;
    int output[sample_rate];
    int index, mix;
    float j, t;
    for(int i = 0; i <= sample_rate; i++){
        t = i * 0.00000001f; // i * 10^-8
        j = my_fmodf(2 * PI * lo_frequency * t, 2 * PI);

        if (j < 0){
            j += (float) 2 * PI;
        }
        index = floor((16.0f / (lo_frequency/rf_frequency * 0.0001f)) * t);
        if (index < 16) {
            if (!message[index]) {
                mix = 115 + sin1(j) * 0.1f;
            } else {
                mix = sin1(j);
            }
        } else {
            break;
        }
        output[i] = mix;
    }
    return output;
}

Edit 2:

I fixed the warning: function returns address of local variable [-Wreturn-local-addr] the way "chux - Reinstate Monica" suggested.

int* create_wave(int* message){
    static uint16_t rf_frequency = 10000;
    static uint32_t lo_frequency = 40000;
    static uint32_t sample_rate = 100000;
    int *output = malloc(sizeof *output * sample_rate);
    uint8_t index, mix;
    float j, n, t;
    for(int i = 0; i < sample_rate; i++){
        t = i * 0.00000001f; // i * 10^-8
        j = fmodf(2 * PI * lo_frequency * t, 2 * PI);
        if (j < 0){
            j += 2 * PI;
        }
        index = floor((16.0f / (lo_frequency/rf_frequency * 0.0001f)) * t);
        if (index < 16) {
            if (!message[index]) {
                mix = (uint8_t) floor(115 + sin1(j) * 0.1f);
            } else {
                mix = sin1(j);
            }
        } else {
            break;
        }
        output[i] = mix;
    }
    return output;
}

But now I get the hardfault on this line:

output[i] = mix;

EDIT 3:

Because the previous code contained a very large buffer array that did not fit into the 16KB SRAM of the STM32F303K8 I needed to change it.

Now I use a "ping-pong" buffer where I use the callback of the DMA for "first-half-transmitted" and "completly-transmitted":

void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef * hdac){
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
    for(uint16_t i = 0; i < 128; i++){
        new_value = sin_table[(i * 8) % 256];
        if (message[message_index] == 0x0){
            dac_buf[i] = new_value * 0.1f + 115;
        } else {
            dac_buf[i] = new_value;
        }
    }
}

void HAL_DAC_ConvCpltCallbackCh1 (DAC_HandleTypeDef * hdac){
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
    for(uint16_t i = 128; i < 256; i++){
        new_value = sin_table[(i * 8) % 256];
        if (message[message_index] == 0x0){
            dac_buf[i] = new_value * 0.1f + 115;
        } else {
            dac_buf[i] = new_value;
        }
    }
    message_index++;
    if (message_index >= 16) {
        message_index = 0;
        // HAL_DAC_Stop_DMA (&hdac1, DAC_CHANNEL_1);
    }
}

And it works the way I wanted: output

But the frequency of the created sine is too low. I cap at around 20kHz but I'd need 40kHz. I allready increased the clock by a factor of 8 so that one is maxed out: maxed. I can still decrease the counter period (it is 50 at the moment), but when I do so the interrupt callback seems to take longer than the period to the next one. At least it seems so as the output becomes very distorted when I do that.

I also tried to decrease the precision by taking only every 8th sine value but I cant do this any more because then the output does not look like a sine wave anymore.

Any ideas how I could optimize the callback so that it takes less time ? Any other ideas ?


Solution

  • Does fmodf() cause a hardfault in stm32?

    It is other code problems causing the hard fault here.

    Failing to compile with ample warnings

    Best code tip: enable all warnings. @KamilCuk
    Faster feedback than Stackoverflow.

    I'd expect something like below on a well enabled compiler.

    return output;
    warning: function returns address of local variable [-Wreturn-local-addr]
    

    Returning a local Object

    Cannot return a local array. Allocate instead.

    // int output[sample_rate];
    int *output = malloc(sizeof *output * sample_rate);
    return output;
    

    Calling code will need to free() the pointer.

    Out of range array access

    static int sample_rate = 100000;
    int output[sample_rate];
    
    // for(int i = 0; i <= sample_rate; i++){
    for(int i = 0; i < sample_rate; i++){
        ...
        output[i] = mix;
    }
    

    Stack overflow?

    static int sample_rate = 100000; int output[sample_rate]; is a large local variable. Maybe allocate or try something smaller?

    Advanced: loss of precision

    A good fmodf() does not lose precision. For a more precise answer consider double math for the intermediate results. An even better approach is more involved.

    float my_fmodf(float x, float y){
        if(y == 0){
            return 0;
        }
        double n = 1.0 * x / y;
        return (float) (x - n * y);
    }
    

    Can I not use any function within another ?

    Yes. Code has other issues.