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

App_timer and RTC Calendar example confliction

I am using  timer ( created with app_timer and started with period of 1 sec ) and ported RTC calendar example in it . But both features do  not work together ( since RTC is used in both the example ) . I want to have a real time calendar which should print my current time with date -month- year and with that, periodically ,I need some timers( app timers) for sensor data acquisition and other task . So how can we achieve this ? 

exampled used : ble_app_blinky  ( ported RTC calendar and app timer )

SDK version : 15.3.0

Hardware : Nordic NRF 52 DK ( PCA10040)

Parents
  • Hello,

    So what exactly is not working? You mention that the RTC1_IRQ_Handler caused an app_error? What does the error handler say? If it only says "Fatal error." Try adding "DEBUG" to your preprocessor definitions. If you are not sure how to add preprocessor definitions, please let me know what IDE you are using. 

  • So what exactly is not working?

    refer attachment .

  • main code 

    int main(void)
    {
    // Initialize.
    log_init();
    clock_init();
    timers_init();
    system_clock_init();
    #if 0
    i2c_init();
    gpio_init();
    button_init();
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    advertising_start();
    #endif

    // Start execution.
    NRF_LOG_INFO("NRF_LOG_INFO is used for uart\n\n.");
    printf("Printf is used for rtt logs\n");
    NRF_LOG_INFO("Blinky example started.");


    printf("ADV sytarts .........\n");
    =============================================================

    //============================================================= calender
    #if 1
    uint8_t uart_byte;
    uint32_t year, month, day, hour, minute, second;
    //app_timer_pause();
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

    nrf_cal_init();
    nrf_cal_set_callback(calendar_updated, 1);

    // nrf_delay_ms(1000);
    while(1){


    print_current_time();
    //NRF_LOG_INFO("RTC Time +++++++++++++++++++++\r\n");
    NRF_LOG_FLUSH();

    //app_timer_resume();


    nrf_delay_ms(1000);
    }
    #endif
    //===================================================================== calender ends
    // Enter main loop.
    for (;;)
    {
    idle_state_handle();
    }
    }

Reply
  • main code 

    int main(void)
    {
    // Initialize.
    log_init();
    clock_init();
    timers_init();
    system_clock_init();
    #if 0
    i2c_init();
    gpio_init();
    button_init();
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    advertising_start();
    #endif

    // Start execution.
    NRF_LOG_INFO("NRF_LOG_INFO is used for uart\n\n.");
    printf("Printf is used for rtt logs\n");
    NRF_LOG_INFO("Blinky example started.");


    printf("ADV sytarts .........\n");
    =============================================================

    //============================================================= calender
    #if 1
    uint8_t uart_byte;
    uint32_t year, month, day, hour, minute, second;
    //app_timer_pause();
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

    nrf_cal_init();
    nrf_cal_set_callback(calendar_updated, 1);

    // nrf_delay_ms(1000);
    while(1){


    print_current_time();
    //NRF_LOG_INFO("RTC Time +++++++++++++++++++++\r\n");
    NRF_LOG_FLUSH();

    //app_timer_resume();


    nrf_delay_ms(1000);
    }
    #endif
    //===================================================================== calender ends
    // Enter main loop.
    for (;;)
    {
    idle_state_handle();
    }
    }

Children
  • system_clock_init();

    inside this i have 

    bool system_clock_init(void)
    {
    app_timer_create(&m_repeated_timer_id1,
    APP_TIMER_MODE_REPEATED,
    timer_led_event_handler);
    app_timer_start(m_repeated_timer_id1, APP_TIMER_TICKS(1000), NULL);

    return true;
    }

  • This doesn't tell me much unfortunately. Try adding DEBUG to your preprocessor definitions and check the log. If it still doesn't print anything where it points to a specific place in your project, try setting a breakpoint in the app_error_fault_handler() in app_error_weak.c. Try setting a breakpoint in the cases:

    case NRF_FAULT_ID_SDK_ASSERT:

    and 

    case NRF_FAULT_ID_SDK_ERROR:

    Are any of them hit?

  • I have a app timer defined  inside system_clock_init() as ,

    app_timer_create(&m_repeated_timer_id1,
    APP_TIMER_MODE_REPEATED,
    timer_led_event_handler);
    app_timer_start(m_repeated_timer_id1, APP_TIMER_TICKS(1000), NULL);

    And this I am calling here in main.c 

    int main(void)
    {
    // Initialize.
    log_init();
    clock_init();
    timers_init(); <<<<<<<<<<<<<<<<<<<<<<<<< app timer init 
    system_clock_init(); <<<<<<<<<<<<<<<<< app timer create and start

    now just after this , I have added RTC calendar example code as ,

    uint8_t uart_byte;
    uint32_t year, month, day, hour, minute, second;
    //app_timer_pause();
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

    nrf_cal_init();
    nrf_cal_set_callback(calendar_updated, 4);

    while(1){

    print_current_time();
    nrf_delay_ms(1000);
    }

    now this must print the date and time on UART as ,

    <info> app: Uncalibrated time: 01/01/70 - 00:00:00

    <info> app: Calibrated time: 01/01/70 - 00:00:00

    but just after this , it gives ,

    <error> app: Fatal error

    and this fatal error is because , I have started one app timer , once the timer expires after 1 second , 

    its not able to start again , it goes to RTC1_IRQHandler()  from below app_error_check .

    uint32_t err_code = app_sched_event_put(&timer_event, sizeof(timer_event), timeout_handler_scheduled_exec);
    APP_ERROR_CHECK(err_code);

    Which means , app_sched_event_put function is returning error .

    And this function is defined in app_timer.c inside below function .

    static void timeout_handler_exec(timer_node_t * p_timer)

    hoping this will clarify your doubts .

  • FYI , 

    after enabling DEBUG , instead of Fatal error I am getting ,

    <error> app: ERROR 9 [NRF_ERROR_INVALID_LENGTH] at :0
    PC at: 0x00000000
    <error> app: End of error report

  • I have solved the issue .

    In the calendar example , 

    they are starting the LFCLK manually as below ,

    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

    and I have ported this  code in ble_blinky where softdevice starts the LFCLK by it's own .

    So , we need to remove these lines of codes from two places , one from above and the other one from ,

    nrf_cal_init() function and make sure the calendar works on RTC2 , since app_timer uses RTC1 and softdevice  takes RTC0 .

    In this way we can able to run all three RTCs .

Related