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

APP_TIMER is not functioning.

Hello

Created in SDK v17 and simple_timer example.
I made a simple code to control the LED using a timer. Every 500 milliseconds, the LED turns on sequentially. LED ON and OFF works well when running one line at a time with debug, but the timer doesn't seem to be running.
In sdk_config.h, APP_TIMER_ENABLE is set to 1.

#include "app_timer.h"
#include <stdio.h>
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"

#define TIMEOUT_VALUE                    50000                          /**< 50 mseconds timer time-out value. */
#define TOGGLE_LED_COUNTER               (500 / (TIMEOUT_VALUE / 1000)) /**< Interval for toggling a LED. Yields to 500 mseconds. */
#define STATE_TRANSIT_COUNTER_INIT_VALUE (4 * TOGGLE_LED_COUNTER)       /**< Initial value for the state transition counter.  */
#define GENERIC_DELAY_TIME               1000                           /**< Generic delay time used by application. */


APP_TIMER_DEF(m_timer_id);

void gpio_init(void)
{
  nrf_gpio_cfg_output(BSP_LED_0); //board_led1
  nrf_gpio_pin_set(BSP_LED_0);

  nrf_gpio_cfg_output(BSP_LED_1); //board_led2
  nrf_gpio_pin_set(BSP_LED_1);

  nrf_gpio_cfg_output(BSP_LED_2); //board_led3
  nrf_gpio_pin_set(BSP_LED_2);

  nrf_gpio_cfg_output(BSP_LED_3); //board_led4
  nrf_gpio_pin_set(BSP_LED_3);
}


char Move_LED[] = {BSP_LED_0, BSP_LED_1, BSP_LED_2, BSP_LED_3};


static void timer_handler(void * p_context)
{
    nrf_gpio_pin_toggle(BSP_LED_3);  //0.5 sec toggle
}


static void timers_init(void)
{
    ret_code_t err_code;

    // Initialize timer module.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    err_code = app_timer_create(&m_timer_id,
                                APP_TIMER_MODE_SINGLE_SHOT, //SINGLE_SHOT or REPEATED //single shot timer
                                timer_handler);   
    APP_ERROR_CHECK(err_code); 
}


static void timer_start()
{
    ret_code_t err_code;

    // Start application timers.
    err_code = app_timer_start(m_timer_id, APP_TIMER_TICKS(500), NULL); //0.5sec delay
    APP_ERROR_CHECK(err_code);
}


static void move()
{
      for(int i=0; i<5; i++)
      {
        nrf_gpio_pin_set(BSP_LED_0); //led off
        nrf_gpio_pin_set(BSP_LED_1);
        nrf_gpio_pin_set(BSP_LED_2);
        
        timer_start();
        nrf_gpio_pin_clear(BSP_LED_0);
        timer_start();
        nrf_gpio_pin_clear(BSP_LED_1);
        timer_start();
        nrf_gpio_pin_clear(BSP_LED_2);
        timer_start();
      }
            
    /*for(int i=0; i<5; i++) //not output
    {
      nrf_gpio_pin_set(BSP_LED_0); //led off
      nrf_gpio_pin_set(BSP_LED_1);
      nrf_gpio_pin_set(BSP_LED_2);

      for(int j=0; j<3; j++)
      {
        nrf_gpio_pin_set(Move_LED[i]);
        timer_start();
      }
    }*/
}


/**@brief Function for the Power Management.
 */
static void power_manage(void)
{
    // Use directly __WFE and __SEV macros since the SoftDevice is not available.

    // Wait for event.
    __WFE();

    // Clear Event Register.
    __SEV();
    __WFE();
}


int main(void)
{
    timers_init();
    gpio_init(); 

    nrf_gpio_pin_set(BSP_LED_0); //led off
    nrf_gpio_pin_set(BSP_LED_1);
    nrf_gpio_pin_set(BSP_LED_2);

    //move();

    for (;;)
    {
        power_manage();
        move();
    }
}

Can I know the problem?


Thank you.

  • Try activating LFCLK, the app timer uses the RTC peripheral, which runs on the LFCLK clock. You can use nrfx_clock to activate it. Or simply write NRF_CLOCK->TASKS_LFCLKSTART = 1.

  • Hello!
    Thank you for helping me.

    As you said, I've added parts about nrf_clock.  And I added nrf_drv_clock.c to nRF_Drivers, but the timer still doesn't work.

    This is the file list and path.

                                 

    ../../../config
    ../../../../../../components
    ../../../../../../components/boards
    ../../../../../../components/drivers_nrf/nrf_soc_nosd
    ../../../../../../components/libraries/atomic
    ../../../../../../components/libraries/atomic_fifo
    ../../../../../../components/libraries/balloc
    ../../../../../../components/libraries/bsp
    ../../../../../../components/libraries/button
    ../../../../../../components/libraries/delay
    ../../../../../../components/libraries/experimental_section_vars
    ../../../../../../components/libraries/log
    ../../../../../../components/libraries/log/src
    ../../../../../../components/libraries/memobj
    ../../../../../../components/libraries/ringbuf
    ../../../../../../components/libraries/scheduler
    ../../../../../../components/libraries/simple_timer
    ../../../../../../components/libraries/sortlist
    ../../../../../../components/libraries/strerror
    ../../../../../../components/libraries/timer
    ../../../../../../components/libraries/util
    ../../../../../../components/toolchain/cmsis/include
    ../../..
    ../../../../../../external/fprintf
    ../../../../../../integration/nrfx
    ../../../../../../integration/nrfx/legacy
    ../../../../../../modules/nrfx
    ../../../../../../modules/nrfx/drivers/include
    ../../../../../../modules/nrfx/hal
    ../../../../../../modules/nrfx/mdk
    ../config

    Code for newly added A in sdk_config.h.

    // <e> NRF_CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver - legacy layer
    //==========================================================
    #ifndef NRF_CLOCK_ENABLED
    #define NRF_CLOCK_ENABLED 1
    #endif
    // <o> CLOCK_CONFIG_LF_SRC  - LF Clock Source
     
    // <0=> RC 
    // <1=> XTAL 
    // <2=> Synth 
    // <131073=> External Low Swing 
    // <196609=> External Full Swing 
    
    #ifndef CLOCK_CONFIG_LF_SRC
    #define CLOCK_CONFIG_LF_SRC 1
    #endif
    
    // <q> CLOCK_CONFIG_LF_CAL_ENABLED  - Calibration enable for LF Clock Source
     
    
    #ifndef CLOCK_CONFIG_LF_CAL_ENABLED
    #define CLOCK_CONFIG_LF_CAL_ENABLED 0
    #endif
    
    // <o> CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    
    // <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef CLOCK_CONFIG_IRQ_PRIORITY
    #define CLOCK_CONFIG_IRQ_PRIORITY 6
    #endif
    
    
    
    // </e>
    // <e> NRFX_CLOCK_ENABLED - nrfx_clock - CLOCK peripheral driver
    //==========================================================
    #ifndef NRFX_CLOCK_ENABLED
    #define NRFX_CLOCK_ENABLED 1
    #endif
    // <o> NRFX_CLOCK_CONFIG_LF_SRC  - LF Clock Source
     
    // <0=> RC 
    // <1=> XTAL 
    // <2=> Synth 
    // <131073=> External Low Swing 
    // <196609=> External Full Swing 
    
    #ifndef NRFX_CLOCK_CONFIG_LF_SRC
    #define NRFX_CLOCK_CONFIG_LF_SRC 1
    #endif
    
    // <o> NRFX_CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef NRFX_CLOCK_CONFIG_IRQ_PRIORITY
    #define NRFX_CLOCK_CONFIG_IRQ_PRIORITY 6
    #endif
    
    
    // <e> NRFX_CLOCK_CONFIG_LOG_ENABLED - Enables logging in the module.
    //==========================================================
    #ifndef NRFX_CLOCK_CONFIG_LOG_ENABLED
    #define NRFX_CLOCK_CONFIG_LOG_ENABLED 0
    #endif
    // <o> NRFX_CLOCK_CONFIG_LOG_LEVEL  - Default Severity level
     
    // <0=> Off 
    // <1=> Error 
    // <2=> Warning 
    // <3=> Info 
    // <4=> Debug 
    
    #ifndef NRFX_CLOCK_CONFIG_LOG_LEVEL
    #define NRFX_CLOCK_CONFIG_LOG_LEVEL 3
    #endif
    
    // <o> NRFX_CLOCK_CONFIG_INFO_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_CLOCK_CONFIG_INFO_COLOR
    #define NRFX_CLOCK_CONFIG_INFO_COLOR 0
    #endif
    
    // <o> NRFX_CLOCK_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NRFX_CLOCK_CONFIG_DEBUG_COLOR
    #define NRFX_CLOCK_CONFIG_DEBUG_COLOR 0
    #endif
    

    I also attached the sdk_config.h.
    Is there anything else I need to add or change to use the app_timer?

    Thank you.config.zip

  • Are you calling init and start on the nrf_drv_clock? There are a lot of examples that use it, examples/peripheral/wdt is one.

  • Thank you, Jimmie!
    By adding 'NRF_CLOCK-> TASKS_LFCLKSTART = 1;' to the main(), the timer is now active.

    Can I ask you about other problems?

    The timer works fine, but I think there's a problem with the move() function that I created.
    I want to turn on LED 1,2,3 sequentially every 0.5 seconds and repeat it. So I used the timer like delay, but I think the method is wrong.

    static void move()
    {
          for(int i=0; i<5; i++)
          {
            nrf_gpio_pin_set(BSP_LED_0); //led off
            nrf_gpio_pin_set(BSP_LED_1);
            nrf_gpio_pin_set(BSP_LED_2);
            //nrf_delay_ms(500);
            timer_start();
            
            //LED1->LED2->LED3->LED OFF (loop)
            nrf_gpio_pin_clear(BSP_LED_0);
            timer_start();
            nrf_gpio_pin_clear(BSP_LED_1);
            timer_start();
            nrf_gpio_pin_clear(BSP_LED_2);
            timer_start();
            //nrf_delay_ms(500);
          }
          .
          .
    int main()
    {
    .
    .
        for(;;)
        {
            move();
        }
    }

    If I do this, LED1,2,3 will remain on. It doesn't move as set. The timer is running every half a second as set.

    What do you think about this?

    Thank you again.

Related