nrf5-calendar-example SOFTDEVICE: INVALID MEMORY ACCESS

I try to integrate https://github.com/NordicPlayground/nrf5-calendar-example into Keil project. But the following error messages shown when accessing NRF_CLOCK.

error messages

nrf5-calendar-example SOFTDEVICE: INVALID MEMORY ACCESS

source code

int main(void) {
    _initVariables();
    

    // Initialize.
    log_init(); // Power_saving
    uart_init(); // Power_saving
    //_showInfo();
    fstorageInit();
    batteryVoltageInit();
    timerInit();
    button_events_init();
    scheduler_init();
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    db_discovery_init();
    services_init();
    advertising_init();
    conn_params_init();
    timerPeriodStart();
    timerStart();
    

    //  SOFTDEVICE: INVALID MEMORY ACCESS occurred when running the following code
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    while(NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
    
     nrf_cal_init();
     nrf_cal_set_callback(calendar_updated, 4);

}

Parents Reply Children
  • Thanks for your reply! I have changed above code to the following code.

    const ret_code_t errCode = sd_clock_hfclk_request();
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("sd_clock_hfclk_request() failed\n");
        }    

        uint32_t hfclkIsRunning = 0;

        while (!hfclkIsRunning) {
            APP_ERROR_CHECK(sd_clock_hfclk_is_running(&hfclkIsRunning) );
        }

    Now it hangs at nrf_cal_init() as the following error messages.

    <debug> app: nrf_cal_init() e
     0>
     0> <error> app: SOFTDEVICE: INVALID MEMORY ACCESS

    void nrf_cal_init(void) {
        NRF_LOG_DEBUG("nrf_cal_init() e\n");
        // Select the 32 kHz crystal and start the 32 kHz clock
        NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos;
        NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
        NRF_CLOCK->TASKS_LFCLKSTART = 1;
        while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
        NRF_LOG_DEBUG("1\n");
        
        // Configure the RTC for 1 minute wakeup (default)
        CAL_RTC->PRESCALER = 0xFFF;
        CAL_RTC->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
        CAL_RTC->INTENSET = RTC_INTENSET_COMPARE0_Msk;
        CAL_RTC->CC[0] = m_rtc_increment * 8;
        CAL_RTC->TASKS_START = 1;

    }

  • Do you have any suggestion about how to change NRF_CLOCK and CAL_RTC related code to Softdevice APIs?

  • You don't need to start the LF clock either as the Softdevice does it when you enable it. So uou can remove this from cal_init():

        NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos;
        NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
        NRF_CLOCK->TASKS_LFCLKSTART = 1;
        while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

  • I have removed NRF_CLOCK related code from nrf_cal_init().Do you have any suggestion about how to change CAL_RTC related code to Softdevice APIs?

    void nrf_cal_init(void) {
        NRF_LOG_DEBUG("nrf_cal_init() e\n");
        
        // Configure the RTC for 1 minute wakeup (default)
        CAL_RTC->PRESCALER = 0xFFF;
        CAL_RTC->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
        CAL_RTC->INTENSET = RTC_INTENSET_COMPARE0_Msk;
        CAL_RTC->CC[0] = m_rtc_increment * 8;
        CAL_RTC->TASKS_START = 1;

    }

  • Yes, RTC0 is reserved to the Softdevice as well, so you need to select another RTC instance.

    In nrf_calendar.h, change:

    // Change the following defines to change the RTC timer used or the interrupt priority
    #define CAL_RTC                 NRF_RTC0
    #define CAL_RTC_IRQn            RTC0_IRQn
    #define CAL_RTC_IRQHandler      RTC0_IRQHandler
    #define CAL_RTC_IRQ_Priority    3

    To:

    // Change the following defines to change the RTC timer used or the interrupt priority
    #define CAL_RTC                 NRF_RTC2
    #define CAL_RTC_IRQn            RTC2_IRQn
    #define CAL_RTC_IRQHandler      RTC2_IRQHandler
    #define CAL_RTC_IRQ_Priority    6

Related