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

Convert int64_t to string

I'm using NCS 1.8.0 and nRF9160 DK, and I need to convert unix_time from int64_t to string, my problem is exactly what is reported in these cases:

devzone.nordicsemi.com/.../snprintf-int64_t-not-working
https://devzone.nordicsemi.com/f/nordic-q-a/83967/long-long-int-value-sprintf

My conversion attempts can be seen below, but in all of them I don't get the number in value_buf_time, but I get lu.

snprintf(value_buf_time, sizeof(value_buf_time), "%"PRIu64, unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%ld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%lld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
The output of all printk is:
 
Publicando o time: lu
 
In other cases the solution was to insert CONFIG_NEWLIB_LIBC_NANO=n. But I can't insert this because when I have LIBC_NANO=n I end up getting a HARD_FAULT in the lte_lc_init_and_connect() function call. Is there another solution to make this conversion work?
Thank you.
Parents
  • Hello Gabriel,

    But I can't insert this because when I have LIBC_NANO=n I end up getting a HARD_FAULT in the lte_lc_init_and_connect() function call.

    What kind of hard fault do you get?

    Is there another solution to make this conversion work?

    I have modified the hello_world sample as shown below and everything appears to work as expected so far:

    main.c

    #include <zephyr.h>
    #include <sys/printk.h>
    #include <stdio.h>
    #include <inttypes.h>
    #include <logging/log.h>
    
    #include <modem/lte_lc.h>
    
    LOG_MODULE_REGISTER(main);
    
    int main()
    {
        int64_t t = 1634617662563;
    	char buf[64];
    	int ret;
    
        printk("Hello World! %s\n", CONFIG_BOARD);
    	printk("ts:%"PRIi64"\n", t);
    
    	snprintf(buf, sizeof(buf),
        "{"
            "\"ts\":%" PRIi64
    		"}\n",
            t);    
    	printk("%s", buf);
    
    	ret = lte_lc_init_and_connect();
    	if(ret){
    		printk("Modem initialisation and network connection failed: %d\n",ret);
    	}
    
        return 0;
    }

    prj.conf

    # Logging
    CONFIG_LOG=y
    CONFIG_LTE_LINK_CONTROL_LOG_LEVEL_DBG=y
    
    # LTE link control
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
    CONFIG_LTE_NETWORK_MODE_LTE_M_GPS=y
    
    # Other configurations
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_NANO=n
    CONFIG_MAIN_STACK_SIZE=2056
    CONFIG_RESET_ON_FATAL_ERROR=n

    Terminal output

    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    Hello World! nrf9160dk_nrf9160
    ts:1634617662563
    {"ts":1634617662563}
    [00:00:00.619,171] <dbg> lte_lc.init_and_config: System mode (4) and preference (0) are already configured
    [00:00:01.404,083] <dbg> lte_lc.at_handler_cereg: +CEREG notification: +CEREG: 2,"76C1","014ACE00",7
    ~
    [00:00:01.404,235] <dbg> lte_lc_helpers.parse_cereg: Network registration status: 2
    [00:00:01.404,266] <dbg> lte_lc_helpers.parse_cereg: LTE mode: 7
    [00:00:01.444,641] <dbg> lte_lc.at_handler_cscon: +CSCON notification
    [00:00:02.536,224] <dbg> lte_lc.at_handler_cereg: +CEREG notification: +CEREG: 1,"76C1","014ACE00",7,,~
    [00:00:02.536,499] <dbg> lte_lc_helpers.parse_cereg: Network registration status: 1
    [00:00:02.536,499] <dbg> lte_lc_helpers.parse_cereg: LTE mode: 7

    Regards,

    Markus

Reply
  • Hello Gabriel,

    But I can't insert this because when I have LIBC_NANO=n I end up getting a HARD_FAULT in the lte_lc_init_and_connect() function call.

    What kind of hard fault do you get?

    Is there another solution to make this conversion work?

    I have modified the hello_world sample as shown below and everything appears to work as expected so far:

    main.c

    #include <zephyr.h>
    #include <sys/printk.h>
    #include <stdio.h>
    #include <inttypes.h>
    #include <logging/log.h>
    
    #include <modem/lte_lc.h>
    
    LOG_MODULE_REGISTER(main);
    
    int main()
    {
        int64_t t = 1634617662563;
    	char buf[64];
    	int ret;
    
        printk("Hello World! %s\n", CONFIG_BOARD);
    	printk("ts:%"PRIi64"\n", t);
    
    	snprintf(buf, sizeof(buf),
        "{"
            "\"ts\":%" PRIi64
    		"}\n",
            t);    
    	printk("%s", buf);
    
    	ret = lte_lc_init_and_connect();
    	if(ret){
    		printk("Modem initialisation and network connection failed: %d\n",ret);
    	}
    
        return 0;
    }

    prj.conf

    # Logging
    CONFIG_LOG=y
    CONFIG_LTE_LINK_CONTROL_LOG_LEVEL_DBG=y
    
    # LTE link control
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
    CONFIG_LTE_NETWORK_MODE_LTE_M_GPS=y
    
    # Other configurations
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_NANO=n
    CONFIG_MAIN_STACK_SIZE=2056
    CONFIG_RESET_ON_FATAL_ERROR=n

    Terminal output

    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    Hello World! nrf9160dk_nrf9160
    ts:1634617662563
    {"ts":1634617662563}
    [00:00:00.619,171] <dbg> lte_lc.init_and_config: System mode (4) and preference (0) are already configured
    [00:00:01.404,083] <dbg> lte_lc.at_handler_cereg: +CEREG notification: +CEREG: 2,"76C1","014ACE00",7
    ~
    [00:00:01.404,235] <dbg> lte_lc_helpers.parse_cereg: Network registration status: 2
    [00:00:01.404,266] <dbg> lte_lc_helpers.parse_cereg: LTE mode: 7
    [00:00:01.444,641] <dbg> lte_lc.at_handler_cscon: +CSCON notification
    [00:00:02.536,224] <dbg> lte_lc.at_handler_cereg: +CEREG notification: +CEREG: 1,"76C1","014ACE00",7,,~
    [00:00:02.536,499] <dbg> lte_lc_helpers.parse_cereg: Network registration status: 1
    [00:00:02.536,499] <dbg> lte_lc_helpers.parse_cereg: LTE mode: 7

    Regards,

    Markus

Children
  • Hello Markus,

    Sorry, I made a mistake and maybe the error is not related to lte_lc_init_and_connect(). I got confused because right after the connection I get the error, but in fact this is caused by the LTE_LINK_CONTROL=y, which allows the execution of the code only after the connection.

    To make it easier for you to reproduce the error, I've replicated this behavior in the mqtt_simple sample. To get unix_time I'm using the date_time library, and I need to add in prj.conf:

    CONFIG_DATE_TIME=y

    CONFIG_DATE_TIME_MODEM=y

    Adding the above library and settings doesn't cause a problem in the sample, but when adding:

    CONFIG_NEWLIB_LIBC_NANO=n

    I get the same error in the mqtt_simple sample that I get in my application:

    [00:00:00.220,550] <inf> mqtt_simple: LTE Link Connecting...
    [00:00:03,974,517] <err> os: ***** USAGE FAULT *****
    [00:00:03,974,548] <err> os: Stack overflow (context area not valid)
    [00:00:03,974,548] <err> os: r0/a1: 0x20018530 r1/a2: 0x2001fb48 r2/a3: 0x0002ded4
    [00:00:03,974,548] <err> os: r3/a4: 0x2001fc24 r12/ip: 0x00000060 r14/lr: 0x00025a45
    [00:00:03,974,578] <err> os: xpsr: 0x61000200
    [00:00:03,974,578] <err> os: Faulting instruction address (r15/pc): 0x00022884
    [00:00:03,974,578] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    [00:00:03,974,578] <err> os: Current thread: 0x20019690 (unknown)
    [00:00:04,032,897] <err> fatal_error: Resetting system

    If you just add CONFIG_NEWLIB_LIBC_NANO=n in the mqtt_simple sample everything works. But to get the unix_time it is also necessary to add CONFIG_DATE_TIME=y and CONFIG_DATE_TIME_MODEM=y. So adding these 3 settings in the mqtt_simple example is possible to reproduce the error, I believe there is some incompatibility between these 3 settings!

Related