Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

nRF5_SDK_16 : "double"/"float" mismatch BUG in "NRF_LOG_INTERNAL_MODULE()" and "nrf_fprintf_fmt()"

The functions “nrf_log_frontend_std_...()” called by “NRF_LOG_INTERNAL_MODULE(...)” use only 4-byte (uint32_t) arguments, which is not suitable for arguments of type “double”.
(See: \components\libraries\log\src\nrf_log_frontend.c)

void nrf_log_frontend_std_1(uint32_t            severity_mid,
                            char const * const p_str,
                            uint32_t           val0)
{
    uint32_t args[] = {val0};
    std_n(severity_mid, p_str, args,  ARRAY_SIZE(args));
}

And further, when calling “nrf_fprintf_fprintf_fmt()”, the "float"-argument is misinterpreted as “double”.
(\external\fprintf\nrf_fprintf_format.c)
...
#if NRF_MODULE_ENABLED(NRF_FPRINTF_DOUBLE)
    case 'f':
    {
        double dbl = va_arg(*p_args, double);  // “float”-argument may be here!
        float_print(p_ctx,
                    dbl,
                    NumDigits,
                    FieldWidth,
                    FormatFlags,
                    false);
        break;
...
#endif
...

Possible workaround:
...
#if NRF_MODULE_ENABLED(NRF_FPRINTF_DOUBLE)
    case 'f':
    {
         if (p_ctx->p_user_ctx != NULL)      // by: nrf_fprintf_fmt (p_cli->p_fprintf_ctx, ...) : (NRF_CLI_DEF)
         {
           double dbl = va_arg(*p_args, double);
           float_print(p_ctx,
                       dbl,
                       NumDigits,
                       FieldWidth,
                       FormatFlags,
                       false);
         }
         else //: p_ctx->p_user_ctx == NULL  // by: nrf_log_backend_serial_put() : (.p_user_ctx = NULL)
         {
            int32_t val_int32 = va_arg(*p_args, int32_t);
            float dbl = *(float *) &val_int32;
            float_print(p_ctx,
                        dbl,
                        NumDigits,
                        FieldWidth,
                        FormatFlags,
                        false);
         }
        break;
...
#endif
...

And now you should use uint32_t type conversion to log float values, for example:
   ...
   float val_float = -1234567.0;
   NRF_LOG_INTERNAL_MODULE (0, NRF_LOG_SEVERITY_INFO_RAW, "%f", *(uint32_t *) &val_float);
   ...





Related