This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

snprintf with float leads to hardfault

Hello,

I am modifying mqtt_simple example to send float over mqtt.

If I change %f to %d, it works without crash.

I have tried enable/disable FPU or increasing HEAP_MEM_POOL_SIZE in prj.conf but not working.

My workaround is to print %d.%d instead of %f. But any advice is appreciated.

2021-10-18T03:42:02.353Z DEBUG modem << [00:02:25.606,597] [1;31m<err> os: ***** USAGE FAULT *****[0m
2021-10-18T03:42:02.364Z DEBUG modem << [00:02:25.606,597] [1;31m<err> os:   Stack overflow (context area not valid)[0m
2021-10-18T03:42:02.407Z DEBUG modem << [00:02:25.606,628] [1;31m<err> os: r0/a1:  0x200149f8  r1/a2:  0x000204fb  r2/a3:  0x00000020[0m
2021-10-18T03:42:02.409Z DEBUG modem << [00:02:25.606,628] [1;31m<err> os: r3/a4:  0x20015b78 r12/ip:  0x2001cc98 r14/lr:  0x0002731b[0m
2021-10-18T03:42:02.413Z DEBUG modem << [00:02:25.606,628] [1;31m<err> os:  xpsr:  0xfffffe00[0m
2021-10-18T03:42:02.417Z DEBUG modem << [00:02:25.606,628] [1;31m<err> os: s[ 0]:  0x00000000  s[ 1]:  0x00000000  s[ 2]:  0x00000000  s[ 3]:  0x00000000[0m
2021-10-18T03:42:02.466Z DEBUG modem << [00:02:25.606,658] [1;31m<err> os: s[ 4]:  0x00000000  s[ 5]:  0x00000000  s[ 6]:  0x00000000  s[ 7]:  0x00000000[0m
2021-10-18T03:42:02.469Z DEBUG modem << [00:02:25.606,658] [1;31m<err> os: s[ 8]:  0x00000000  s[ 9]:  0x00000000  s[10]:  0x00000003  s[11]:  0x00000000[0m
2021-10-18T03:42:02.472Z DEBUG modem << [00:02:25.606,658] [1;31m<err> os: s[12]:  0x00000000  s[13]:  0x00000000  s[14]:  0x00000000  s[15]:  0x00000000[0m
2021-10-18T03:42:02.474Z DEBUG modem << [00:02:25.606,658] [1;31m<err> os: fpscr:  0x00000066[0m
2021-10-18T03:42:02.476Z DEBUG modem << [00:02:25.606,689] [1;31m<err> os: Faulting instruction address (r15/pc): 0x00000020[0m
2021-10-18T03:42:02.480Z DEBUG modem << [00:02:25.606,689] [1;31m<err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0[0m
2021-10-18T03:42:02.483Z DEBUG modem << [00:02:25.606,689] [1;31m<err> os: Current thread: 0x20015b78 (unknown)[0m
2021-10-18T03:42:02.484Z DEBUG modem << [00:02:25.711,181] [1;31m<err> os: Halting system[0m
Call stack:
F00EFF7F    bl 0x000216B4 <sniprintf>
F000FB51    bl 0x00021DA4 <_svfprintf_r>
F7FFF827    bl 0x00020FB4 <_printf_float>
F006FA31    bl 0x00027530 <__cvt>
F7EAF972    bl 0x00011868 <_dtoa_r>
B099        sub sp, sp, #0x64

SECTION_SUBSEC_FUNC(TEXT,__fault,z_arm_exc_spurious)

	mrs r0, MSP
	mrs r1, PSP
	push {r0, lr}

int err;
char buf[150];
int len;

len = snprintf(buf, sizeof(buf),
    "{"
        "\"pressure\":%f"
    "}", pressure);
if (len < 0) {
    LOG_ERR("Failed to create ALT cloud message");
    return false;
}
err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
		buf, len);

  • Hi 

    Could you try to enable CONFIG_NEWLIB_LIBC_FLOAT_PRINTF in your project, by adding this to your prj.conf file:

    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

    Using this configuration I was able to use snprintf with float parameters on the nRF9160. 

    Best regards
    Torbjørn

  • Hi,

    Thank you for the prompt reply.

    It works.

    But there is room for improvement in debug message. It is hard to guess "Stack overflow" equal missing CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

  • Hi 

    Good to hear it solved the issue Slight smile

    Stack overflow is a quite typical error when something is wrong in Zephyr unfortunately. Running the debugger and trying to figure out the chain of events leading to it is usually the best way to try and figure out what is going on. 

    Best regards
    Torbjørn

Related