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

crashing from _start without even entering into main() function

Hi,

I am using nrf52832 custom board and sdk17.2.0.

My application is a Freertos application. I see that my application is going to hardfault from _start without entering into the main() function.

Attached sdk_config.h , FreeRTOSConfig.h and the screenshot of the hardfault for reference.

Can someone please help me to resolve the issue?

 8358.FreeRTOSConfig.h6215.sdk_config.h

Parents
  • Seems like it is happening inside the NRF_LOGs. 

    1. If the function call stack can be trusted, then try to disable the logs to see if the issue goes away.
      1. If it does, then the log buffer might not be enough and we can focus more on looking a bit deeper into this NRF_LOG configuration issue. 
      2. If it does not help, then most likely the stack is corrupt and I wont be trusting this function call stack much.
    2. If this is a stack corruption then could happen because your thread might have caused a stack overflow. Try to increase the stack size significantly for this thread and see if that helps.
  • yes after disabling NRF_LOG_ENABLED the issue went away.

  • Hi Swetha, I could try to reply to you within 24 hours of your query, 

    swetha Paladugu said:
    yes after disabling NRF_LOG_ENABLED the issue went away

     It looks like the NRF_LOG is taking too much stack and what you are seeing is a possible stack overflow and hence a memory corruption.

    Normally in our examples we let logger run in its own thread so that we can calibrate the stack size it needs explicitly. For example in our SDK\examples\ble_peripheral\ble_app_hrs_freertos\main.c you can see that we create a logger thread.

    #if NRF_LOG_ENABLED
        // Start execution.
        if (pdPASS != xTaskCreate(logger_thread, "LOGGER", 256, NULL, 1, &m_logger_thread))
        {
            APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
        }
    If you have already done it, then try increasing the stack size for both logger thread and "Thread #2" from the screenshot

  • Yes i'm already doing it. 

    the call stack doesn't clearly say which task it is running. 

    and one more thing i have added the log message in the main function itself not in the task before starting scheduler.

    NRF_LOG_INFO("Current RTC time : %u",get_current_time());

    // nrf_sdh_freertos_init(advertising_start, NULL);

    nrf_sdh_freertos_init(NULL, NULL);

    // Activate deep sleep mode.
    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
    // Start FreeRTOS scheduler.
    vTaskStartScheduler();

  • If increasing the task stack sizes did not help, then I am not sure what is causing this hardfault. if the hardfault_c_handler is not able to give any meaningful address and cause for this hardfault, then it must be a memory corruption that happened that during somewhere in the backend of nrf_log. Try to step through the debugger and try to pin point the exact line where the hardfault is happening. When you understand the context of this hardfault, we might get more hints on how to fix it.

  • but it is not entering into main function itself to step in and debug

  • I see, seeing "Thread #2" in your snapshot made me assume that your device has started into main normally.
    Then something is entirely wrong in your setup. I can see that you have eclipse project. Which startupup files did you add to that project? Is the configuration of the project correct with all the correct preprocessor defines and device files matching nrf52832 device you are using? I do not use eclipse so it is hard for me to understand why your device does not boot properly into main. Seems like a startup issue which mostly point to configuration then.

Reply
  • I see, seeing "Thread #2" in your snapshot made me assume that your device has started into main normally.
    Then something is entirely wrong in your setup. I can see that you have eclipse project. Which startupup files did you add to that project? Is the configuration of the project correct with all the correct preprocessor defines and device files matching nrf52832 device you are using? I do not use eclipse so it is hard for me to understand why your device does not boot properly into main. Seems like a startup issue which mostly point to configuration then.

Children
  • i think i found the issue.

    On calling the below rtc function, that issue is coming.

    The issue is coming from return (mktime(&t1));. If i comment this then i dont see the issue.

    But that is needed. Can you please help?

    uint32_t get_current_time(void)
    {

    rtc_t nowTime;


    get_time(&nowTime);

    struct tm t1 = {.tm_year = RTC_Bcd2ToByte(nowTime.year) + BASE_YEAR,
    .tm_mon = RTC_Bcd2ToByte(nowTime.month) - 1, /* In C, Jan = 0 */
    .tm_mday = RTC_Bcd2ToByte(nowTime.days),
    .tm_wday = RTC_Bcd2ToByte(nowTime.weekDay),
    .tm_hour = RTC_Bcd2ToByte(nowTime.hour),
    .tm_min = RTC_Bcd2ToByte(nowTime.min),
    .tm_sec = RTC_Bcd2ToByte(nowTime.sec)};

    return (mktime(&t1));
    }

  • you are returning the address of t1 which is allocated from stack and the address is invalid outside the get_current_time scope. Either make t1 static by declaring as below (so that it its memory pointer remains valid outside the get_current_time scope 

    static struct tm t1 = {.tm_year = RTC_Bcd2ToByte(nowTime.year) + BASE_YEAR,

    or declare this struct as global variable so it is allocated in the different section of the memory with memory pointer address valid through the runtime of your application.

  • that didn't solve the issue.

    rtc_t nowTime;

    struct tm t1;

    uint32_t get_current_time(void)
    {
    get_time(&nowTime);

    // struct tm t1 = {.tm_year = RTC_Bcd2ToByte(nowTime.year) + BASE_YEAR,
    // .tm_mon = RTC_Bcd2ToByte(nowTime.month) - 1, /* In C, Jan = 0 */
    // .tm_mday = RTC_Bcd2ToByte(nowTime.days),
    // .tm_wday = RTC_Bcd2ToByte(nowTime.weekDay),
    // .tm_hour = RTC_Bcd2ToByte(nowTime.hour),
    // .tm_min = RTC_Bcd2ToByte(nowTime.min),
    // .tm_sec = RTC_Bcd2ToByte(nowTime.sec)};

    t1.tm_year = RTC_Bcd2ToByte(nowTime.year) + BASE_YEAR,
    t1.tm_mon = RTC_Bcd2ToByte(nowTime.month) - 1, /* In C, Jan = 0 */
    t1.tm_mday = RTC_Bcd2ToByte(nowTime.days),
    t1.tm_wday = RTC_Bcd2ToByte(nowTime.weekDay),
    t1.tm_hour = RTC_Bcd2ToByte(nowTime.hour),
    t1.tm_min = RTC_Bcd2ToByte(nowTime.min),
    t1.tm_sec = RTC_Bcd2ToByte(nowTime.sec);

    return (mktime(&t1));

    }

    And same issue is coming even on calling the below set current time function, with ctime and localtime. With this what i observe is there is some issue using the time.h functions. Do i have to enable any macros or add any paths to the project? 

    void set_current_time(time_t current_time)
    {
    ctime(&current_time);

    struct tm ts = *localtime(&current_time);
    rtc_t time_to_rtc = {.sec = RTC_ByteToBcd2(ts.tm_sec),
    .min = RTC_ByteToBcd2(ts.tm_min),
    .hour = RTC_ByteToBcd2(ts.tm_hour),
    .days = RTC_ByteToBcd2(ts.tm_mday),
    .weekDay = RTC_ByteToBcd2(ts.tm_wday),
    .month = RTC_ByteToBcd2(ts.tm_mon + 1),
    .year = RTC_ByteToBcd2(ts.tm_year - BASE_YEAR)};

    set_time(&time_to_rtc);
    }

  • Those API are working quite fine on my end with the nRF5_SDK_17.0.2_d674dde\examples\ble_peripheral\experimental\ble_app_cgms example.

    FreeRTOS should not have any role in this behavior except the stack sizes I mentioned before. So I am not sure what causes the issue you are seeing.

  • The issue got resolved.

    I have added -rtos GDBServer\RTOSPlugin_FreeRTOS this to my debugger settings for debugging crashes between threads, after removing this the issue got resolved. i am not sure how does that plugin affects those time.h api's.

Related