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

HW timer issue

Hi,

  I'm trying to enable a HW timer (TIMER1) in my BLE application.  I'm using SDK 16, and following the pattern shown in examples/peripherals/timer.  However, when I get to nrf_drv_timer_enable(), I get a system reset before exiting this function.  Here is the relevant parts of my code -

const nrf_drv_timer_t SYSTEM_HW_TIMER = NRF_DRV_TIMER_INSTANCE(1);

static void _system_hw_timer_callback(nrf_timer_event_t event_type, void* p_context)
{
    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
            ...
            
            break;

        default:
            break;
    }
}

static void system_timer_tick_init(void)
{
    ...

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    uint32_t err_code = nrf_drv_timer_init(&SYSTEM_HW_TIMER, 
                                           &timer_cfg, 
                                           _system_hw_timer_callback);
    APP_ERROR_CHECK(err_code);

    uint32_t time_ticks = nrf_drv_timer_ms_to_ticks(&SYSTEM_HW_TIMER, 
                                                    SYSTEM_TIMER_TICK_TIME_MS);

    nrf_drv_timer_extended_compare(&SYSTEM_HW_TIMER, 
                                   NRF_TIMER_CC_CHANNEL0, 
                                   time_ticks, 
                                   NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, 
                                   true);

    nrf_drv_timer_enable(&SYSTEM_HW_TIMER);  // This function never completes!!!
}

int main(void)
{
    ...
    // Initialize BLE
    ...
    system_timer_tick_init();
    ...
}
In sdk_config.h, I've enabled the timer, like this -
#ifndef NRFX_TIMER_ENABLED
#define NRFX_TIMER_ENABLED 1
#endif
// <q> NRFX_TIMER0_ENABLED  - Enable TIMER0 instance
 

#ifndef NRFX_TIMER0_ENABLED
#define NRFX_TIMER0_ENABLED 1
#endif

// <q> NRFX_TIMER1_ENABLED  - Enable TIMER1 instance
 

#ifndef NRFX_TIMER1_ENABLED
#define NRFX_TIMER1_ENABLED 1
#endif
  Note that I have removed all of the legacy defines in this file, including TIMER_ENABLED.
  Not sure if it's relevant, but I'm also using FreeRTOS in my project, although the scheduler has not been started when I hit this issue.  
  Any idea why my system crashes when I try to enable the hardware timer?
  Thanks...
Brian
  • Only time for a cursory read, but it seems you would do well by replacing the default assert handler which is probably causing the reset; then it's easier to debug the code by putting a breakpoint on the handler. Add this to main.c:

    // Override the default handler which causes a reset, but track occurrences in case they need investigating
    static uint32_t ErrorHandlerCounter = 0;
    static uint32_t LastError_id = 0;
    static uint32_t LastError_pc = 0;
    void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
    {
       ErrorHandlerCounter++;
       LastError_id = id;
       LastError_pc = pc;
       NRF_LOG_INFO("Error: id %u, pc %u, count %u", LastError_id, LastError_pc, ErrorHandlerCounter);
    }

  • Brian said:
      I can single-step into this function.  When I step from the 1st line to the 2nd line (which I believe starts the timer), the CPU resets and I go back to main. 

     Does this mean that it resets during the NRFX_ASSERT(m_cb[p_instance->instance_id].state == NRFX_DRV_STATE_INITALIZED);?

    If so, then it is the assert. The timer is not initialized.

     

    Brian said:
      I've tested the code I'm using to setup the timer directly in the ble_app_hrs_freertos example code.  This code works fine.  The only other change I had to make was to enable the timer in sdk_config.h (#define TIMER1_ENABLED 1).

     I am sorry. It is hard to say what the issue is only by looking at the sdk_config.h file. Check if 's suggestion points to the assert. If it does, try to initialize the timer first.

    BR,

    Edvin

  • Thanks for the suggestions.  I've resolved the problem.  I'm using Monitor Mode Debugging, and breakpoints within ISR's do not seem to work as expected.  I actually was getting to the timer interrupt handler, but I was not using the FreeRTOS "FromISR" version of the function to release a semaphore.  Once I changed this code in the timer ISR, everything worked fine.

Related