Calling date_time_update_async from main thread leads to stack overflow

Dear Nordic Support Team,

I am using nRF Connect 1.9.1 together with the modem firmware 1.3.2 on a nRF9160 DK.

I noticed that calling the function date_time_update_async in the main thread produces a stack overflow error. This is the case even if a connection to the cellular network was established before calling date_time_update_async .

I have attached three files:

  1. [minimalExample.c] Minimal example to produce the error
  2. [log_dateTimeStackOverflow.txt] Error log for the case when date_time_update_async  is called without prior connection to the cellular network
  3. [log_dateTimeStackOverflow_lte.txt] Error log for the case when date_time_update_async  is called after a connection to the cellular network was established

// Zephyr
#include <zephyr.h>

// Nordic
#include <date_time.h>
#include <modem/lte_lc.h>



static bool isDateTimeUpdateDone = false;

static void callback_dateTimeEvent(const struct date_time_evt *evt)
{
    printk("Event %d\n", evt->type);
    isDateTimeUpdateDone = true;
}



void main(void)
{
    printk("I am running!\n");


    int error = 0;

    // error = lte_lc_init();
    // printk("lte_lc_init: %d\n", error);

    // error = lte_lc_connect();
    // printk("lte_lc_connect: %d\n", error);

    error = date_time_update_async(callback_dateTimeEvent);
    printk("date_time_update_async: %d\n", error);

    while (!isDateTimeUpdateDone)
    {
        printk("Waiting...\n");
        k_sleep(K_MSEC(500));
    }


    printk("I am returning!\n");
}
*** Booting Zephyr OS build v2.7.99-ncs1-1  ***<CR><LF>
I: Starting bootloader<CR><LF>
I: Primary image: magic=good, swap_type=0x4, copy_done=0x1, image_ok=0x1<CR><LF>
I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3<CR><LF>
I: Boot source: none<CR><LF>
I: Swap type: none<CR><LF>
I: Bootloader chainload address offset: 0x10000<CR><LF>
I: Jumping to the first image slot<CR>
*** Booting Zephyr OS build v2.7.99-ncs1-1  ***<CR><LF>
I am running!<CR><LF>
date_time_update_async: 0<CR><LF>
Waiting...<CR><LF>
[00008712] <ESC><err> os: ***** USAGE FAULT *****<ESC><CR><LF>
[00008712] <ESC><err> os:   Stack overflow (context area not valid)<ESC><CR><LF>
[00008713] <ESC><err> os: r0/a1:  0x00038834  r1/a2:  0x41000000  r2/a3:  0x0001001a<ESC><CR><LF>
[00008713] <ESC><err> os: r3/a4:  0xabddef67 r12/ip:  0x00038fd4 r14/lr:  0x41000000<ESC><CR><LF>
[00008714] <ESC><err> os:  xpsr:  0xadffbc00<ESC><CR><LF>
[00008714] <ESC><err> os: Faulting instruction address (r15/pc): 0x00010079<ESC><CR><LF>
[00008715] <ESC><err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0<ESC><CR><LF>
[00008716] <ESC><err> os: Current thread: 0x2000e2b8 (unknown)<ESC><CR><LF>
[00010417] <ESC><err> fatal_error: Resetting system<ESC><CR>
*** Booting Zephyr OS build v2.7.99-ncs1-1  ***<CR><LF>
I: Starting bootloader<CR><LF>
I: Primary image: magic=good, swap_type=0x4, copy_done=0x1, image_ok=0x1<CR><LF>
I: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3<CR><LF>
I: Boot source: none<CR><LF>
I: Swap type: none<CR><LF>
I: Bootloader chainload address offset: 0x10000<CR><LF>
I: Jumping to the first image slot<CR>
*** Booting Zephyr OS build v2.7.99-ncs1-1  ***<CR><LF>
I am running!<CR><LF>
lte_lc_init: 0<CR><LF>
lte_lc_connect: 0<CR><LF>
date_time_update_async: 0<CR><LF>
Waiting...<CR><LF>
[00146596] <ESC><err> os: ***** USAGE FAULT *****<ESC><CR><LF>
[00146597] <ESC><err> os:   Stack overflow (context area not valid)<ESC><CR><LF>
[00146597] <ESC><err> os: r0/a1:  0x00038fc4  r1/a2:  0x41000000  r2/a3:  0xa8044105<ESC><CR><LF>
[00146597] <ESC><err> os: r3/a4:  0xadffbd6b r12/ip:  0x48fc6800 r14/lr:  0x6fedee7e<ESC><CR><LF>
[00146598] <ESC><err> os:  xpsr:  0x00038800<ESC><CR><LF>
[00146598] <ESC><err> os: Faulting instruction address (r15/pc): 0x20010808<ESC><CR><LF>
[00146599] <ESC><err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0<ESC><CR><LF>
[00146600] <ESC><err> os: Current thread: 0x2000e2c0 (unknown)<ESC><CR><LF>
[00148295] <ESC><err> fatal_error: Resetting system<ESC><CR>

In the following are the Kconfig settings I believe to be relevant for this case:

CONFIG_NRF_MODEM_LIB=y
CONFIG_MODEM_KEY_MGMT=y
CONFIG_MODEM_INFO=y

CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_NETWORK_MODE_LTE_M=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n

CONFIG_DATE_TIME=y
CONFIG_DATE_TIME_AUTO_UPDATE=n
CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS=7200

CONFIG_MAIN_STACK_SIZE=32768
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=32768
CONFIG_HEAP_MEM_POOL_SIZE=32768

A little more context:

In my application I have multiple time sources. One of them is Nordic's date time library and another is a RTC. I try to initialize both on system startup when a connection to the cellular network isn't established yet. I am expecting the RTC to do its thing and I expect Nordic's date time library to fail gracefully with the event DATE_TIME_NOT_OBTAINED. However my application crashes with the aforementioned error.

I would like to understand what causes the error and how it can be prevented.

Thank you allot for your help! Please don't hesitate to ask if you have any questions.

Cheers,
Sebastian

Parents
  • Hi Sebastian, sorry for the delay. I have been trying to reproduce the issue, however, some context is missing. Would you be able to upload a project folder with a code snippet that causes the stack overflow? Please consider converting the case to private in case you need to share more than you are comfortable with.

    I was getting some errors when attempting to build without having knowledge about the rest of your prj.conf file as well as any shields etc. you may be using. Therefore I added these three lines to prj.conf:

    CONFIG_NETWORKING=y
    CONFIG_NET_SOCKETS=y
    CONFIG_NEWLIB_LIBC=y

    The code now runs without any stack overflow. The log follows:

    Connected via Serial Port with settings COM6 115200 8n1 rtscts:off
    
    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    Flash regions           Domain          Permissions
    00 01 0x00000 0x10000   Secure          rwxl
    02 31 0x10000 0x100000  Non-Secure      rwxl
    
    Non-secure callable region 0 placed in flash region 1 with size 32.
    
    SRAM region             Domain          Permissions
    00 03 0x00000 0x08000   Secure          rwxl
    04 31 0x08000 0x40000   Non-Secure      rwxl
    
    Peripheral              Domain          Status
    00 NRF_P0               Non-Secure      OK
    01 NRF_CLOCK            Non-Secure      OK
    02 NRF_RTC0             Non-Secure      OK
    03 NRF_RTC1             Non-Secure      OK
    04 NRF_NVMC             Non-Secure      OK
    05 NRF_UARTE1           Non-Secure      OK
    06 NRF_UARTE2           Secure          SKIP
    07 NRF_TWIM2            Non-Secure      OK
    08 NRF_SPIM3            Non-Secure      OK
    09 NRF_TIMER0           Non-Secure      OK
    10 NRF_TIMER1           Non-Secure      OK
    11 NRF_TIMER2           Non-Secure      OK
    12 NRF_SAADC            Non-Secure      OK
    13 NRF_PWM0             Non-Secure      OK
    14 NRF_PWM1             Non-Secure      OK
    15 NRF_PWM2             Non-Secure      OK
    16 NRF_PWM3             Non-Secure      OK
    17 NRF_WDT              Non-Secure      OK
    18 NRF_IPC              Non-Secure      OK
    19 NRF_VMC              Non-Secure      OK
    20 NRF_FPU              Non-Secure      OK
    21 NRF_EGU0             Non-Secure      OK
    22 NRF_EGU1             Non-Secure      OK
    23 NRF_EGU2             Non-Secure      OK
    24 NRF_EGU3             Non-Secure      OK
    25 NRF_EGU4             Non-Secure      OK
    26 NRF_EGU5             Non-Secure      OK
    27 NRF_DPPIC            Non-Secure      OK
    28 NRF_REGULATORS       Non-Secure      OK
    29 NRF_PDM              Non-Secure      OK
    30 NRF_I2S              Non-Secure      OK
    31 NRF_GPIOTE1          Non-Secure      OK
    
    SPM: NS image at 0x10000
    SPM: NS MSP at 0x20017200
    SPM: NS reset vector at 0x12f21
    SPM: prepare to jump to Non-Secure image.
    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    I am running!
    lte_lc_init: 0
    lte_lc_connect: 0
    date_time_update_async: 0
    Waiting...
    Event 1
    I am returning!

    In any case, if you would like to debug when getting stack overflow, you can check the failing instruction with arm-none-eabi-addr2line as explained by Alseth in this post.

    In case arm-none-eabi-addr2line does not work, you might need to add some PATH to your operating system variables. Let us know in case you get stuck on this.

Reply
  • Hi Sebastian, sorry for the delay. I have been trying to reproduce the issue, however, some context is missing. Would you be able to upload a project folder with a code snippet that causes the stack overflow? Please consider converting the case to private in case you need to share more than you are comfortable with.

    I was getting some errors when attempting to build without having knowledge about the rest of your prj.conf file as well as any shields etc. you may be using. Therefore I added these three lines to prj.conf:

    CONFIG_NETWORKING=y
    CONFIG_NET_SOCKETS=y
    CONFIG_NEWLIB_LIBC=y

    The code now runs without any stack overflow. The log follows:

    Connected via Serial Port with settings COM6 115200 8n1 rtscts:off
    
    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    Flash regions           Domain          Permissions
    00 01 0x00000 0x10000   Secure          rwxl
    02 31 0x10000 0x100000  Non-Secure      rwxl
    
    Non-secure callable region 0 placed in flash region 1 with size 32.
    
    SRAM region             Domain          Permissions
    00 03 0x00000 0x08000   Secure          rwxl
    04 31 0x08000 0x40000   Non-Secure      rwxl
    
    Peripheral              Domain          Status
    00 NRF_P0               Non-Secure      OK
    01 NRF_CLOCK            Non-Secure      OK
    02 NRF_RTC0             Non-Secure      OK
    03 NRF_RTC1             Non-Secure      OK
    04 NRF_NVMC             Non-Secure      OK
    05 NRF_UARTE1           Non-Secure      OK
    06 NRF_UARTE2           Secure          SKIP
    07 NRF_TWIM2            Non-Secure      OK
    08 NRF_SPIM3            Non-Secure      OK
    09 NRF_TIMER0           Non-Secure      OK
    10 NRF_TIMER1           Non-Secure      OK
    11 NRF_TIMER2           Non-Secure      OK
    12 NRF_SAADC            Non-Secure      OK
    13 NRF_PWM0             Non-Secure      OK
    14 NRF_PWM1             Non-Secure      OK
    15 NRF_PWM2             Non-Secure      OK
    16 NRF_PWM3             Non-Secure      OK
    17 NRF_WDT              Non-Secure      OK
    18 NRF_IPC              Non-Secure      OK
    19 NRF_VMC              Non-Secure      OK
    20 NRF_FPU              Non-Secure      OK
    21 NRF_EGU0             Non-Secure      OK
    22 NRF_EGU1             Non-Secure      OK
    23 NRF_EGU2             Non-Secure      OK
    24 NRF_EGU3             Non-Secure      OK
    25 NRF_EGU4             Non-Secure      OK
    26 NRF_EGU5             Non-Secure      OK
    27 NRF_DPPIC            Non-Secure      OK
    28 NRF_REGULATORS       Non-Secure      OK
    29 NRF_PDM              Non-Secure      OK
    30 NRF_I2S              Non-Secure      OK
    31 NRF_GPIOTE1          Non-Secure      OK
    
    SPM: NS image at 0x10000
    SPM: NS MSP at 0x20017200
    SPM: NS reset vector at 0x12f21
    SPM: prepare to jump to Non-Secure image.
    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    I am running!
    lte_lc_init: 0
    lte_lc_connect: 0
    date_time_update_async: 0
    Waiting...
    Event 1
    I am returning!

    In any case, if you would like to debug when getting stack overflow, you can check the failing instruction with arm-none-eabi-addr2line as explained by Alseth in this post.

    In case arm-none-eabi-addr2line does not work, you might need to add some PATH to your operating system variables. Let us know in case you get stuck on this.

Children
Related