This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Simple BLE example crash when using floats

Hi Nordic Devzone,

I am using the simple BLE blink peripheral example that simply advertises and blinks an LED. I am using the ble_app_blinky_pca10040_s132 example found in the SDK

I have added the following lines of code in the infinite loop in main

float a = 1.0f;
float b = 2.0f;

float c = a / b;
NRF_LOG_INFO("c = %f", c); 

When I run the code, I get c = ----blank----- printing out over and over very fast.

Why does this issue occur?

Any help would be appreciated.

  • Literally the code line following my qouted sentence. It may be less visible in US/EN settings, but the example uses unicode lower and upper quotation signs instead of ASCII ("). The string looks „fancy” instead of  "normal".

  • I see it, hmm that should not have happened. It seems to have been used a lot in the release notes, but never in the rest of the SDK, except for that sentence. I'll ask our Tech Writers to fix it :) 


  • According to FPU instruction set table, a float division takes 14 cycles.

    From Table of processor instructions a signed/unsigned division takes 2-12 cycles. 
    "Division operations terminate when the divide calculation completes, with the number of cycles required dependent on the values of the input operands. Division operations are interruptible, meaning that an operation can be abandoned when an interrupt occurs, with worst-case latency of one cycle, and restarted when the interrupt completes".

    A modulo operation uses a divide operation to determine the remainder of a division. If you use the modulo operation with integers as the base of your timing you will have a very very bad day. 
    In general, if you're using calculations where the operands are known at compile-time the compiler can, depending on its optimization settings and abilities, run the calculation at compile time and store the output as constants. There are other ways to optimize too. 

    Again, if you rely on SW execution to time HW operations you will have a very very bad day. 

    There are 4 TIMER's and 2 RTC's available to the application, I suggest you use one of them to time your HW operations. The examples we use often includes the Timer library. You can create another app_timer to control your serial communications. 

Related