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

nrf5_calendar example callback function?

Hello,

I am using nrf52840 and SDK 16.0.0

I am doing some work with the calendar example and got some questions.

Q1.

Inside the main function in main.c file there is nrf_cal_set_callback(calendar_updated, 4); function. How it can be called multiple times even though it is defined before the while(true) loop? How does it work?

Q2.

I am integrating ble_app_template example with this calendar example. My beacon is broadcasting advertisement packets. If I wanna update manufacturer specific data(lets say a time stamp) every 1 second, is it possible to locate advertisement update function inside this call back function(nrf_cal_set_callback())?

Thank you.

  • To be more specific about Q2,

    Advertisement update function looks like below. I got the function from this thread.

    static void advertising_update_mfg_data(void)
    {
        ret_code_t             err_code;
    
        static ble_advdata_t new_data; // SDK 16.x.x implementation is similar to advertising_init
    
        // Variables used for manufacturer specific data
        ble_advdata_manuf_data_t p_manuf_specific_data;
        uint8_array_t            adv_manuf_data_array;
        uint8_t                  adv_manuf_data_data[1];
        
        // Configuration of manufacturer specific data
        adv_manuf_data_data[0] = nrf_cal_get_time()->tm_sec;   // Update second
        
        
        adv_manuf_data_array.p_data      = adv_manuf_data_data;
        adv_manuf_data_array.size        = sizeof(adv_manuf_data_data);
    
        p_manuf_specific_data.company_identifier = 0x0059;
        p_manuf_specific_data.data       = adv_manuf_data_array; 
          
        new_data.p_manuf_specific_data   = &p_manuf_specific_data;
    
        new_data.name_type               = BLE_ADVDATA_SHORT_NAME;
        new_data.short_name_len          = 5;
        new_data.include_appearance      = false;
        new_data.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
        new_data.uuids_complete.uuid_cnt = 0;
        new_data.uuids_complete.p_uuids  = m_adv_uuids;
    
        //SDK16.x.x implementation will handle all buffering and encoding inside the update function
        err_code = ble_advertising_advdata_update(&m_advertising, &new_data, NULL);  
        APP_ERROR_CHECK(err_code);
    
    }

    Callback function and update function.

    void nrf_cal_set_callback(void (*callback)(void), uint32_t interval)
    {
        // Set the calendar callback, and set the callback interval in seconds
        cal_event_callback = callback;
        m_rtc_increment = interval;
        m_time += CAL_RTC->COUNTER / 8;
        CAL_RTC->TASKS_CLEAR = 1;
        CAL_RTC->CC[0] = interval * 8;  
        
        advertising_update_mfg_data(); // update function
    }

    Is there any other way to update this manufacturer data every 1 sec?

  • Hi,

    Q1. When you call nrf_cal_set_callback(), this tells the nrf_calendar library which function to call with events. The callback will be called whenever the nrf_calendar library has an event for the application. The calendar library uses the RTC peripheral to keep track of time, which will generate interrupts on given conditions. From the interrupt handler, the application callback will be called if there are relevant events for the application.

    Q2. This looks ok to me.

    Best regards,
    Jørgen

  • Hello,

    Could you let me know if I understand everything correctly?

    So, inside the nrf_calendar.c file, this function below is the interrupt handler right?

    void CAL_RTC_IRQHandler(void)
    {
        if(CAL_RTC->EVENTS_COMPARE[0])
        {
            CAL_RTC->EVENTS_COMPARE[0] = 0;
            
            CAL_RTC->TASKS_CLEAR = 1;
            
            m_time += m_rtc_increment;
            if(cal_event_callback) cal_event_callback();
        }
    }

    Now, I have questions, what does this line mean?

    CAL_RTC->EVENTS_COMPARE[0]

  • Yes, that is correct. That line is a reference to a register in the RTC peripheral itself. CAL_RTC is defined in the header, allowing you to easily change it if the default RTC instance used is occupied by other libraries (RTC0 is by default used by the softdevice when running BLE application).

  • Thanks for your reply. However, whenever I try to use RTC1 by changing #define in the header it returns this compile error. So I had to use RTC2 instead because it does not return any error. Could you kindly explain this situation please? Thank you !!

Related