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

I have a question about the application of the timer.

Hello

I'm working on SDK v17.
Receives certain data from the app, and LED timers work according to the data received.

The code that receives data from the app and parts for the timer.

#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "nordic_common.h"
#include "nrf.h"
#include "ble_hci.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_conn_params.h"
#include "nrf_sdh.h"
#include "nrf_sdh_soc.h"
#include "nrf_sdh_ble.h"
#include "nrf_ble_gatt.h"
#include "nrf_ble_lesc.h" 
#include "nrf_ble_qwr.h"
#include "app_timer.h"
#include "ble_nus.h"
#include "app_uart.h"
#include "app_util_platform.h"
#include "bsp_btn_ble.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_drv_twi.h"

#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "app_button.h" 
#include "nrf_delay.h" 
#include "peer_manager.h" 
#include "peer_manager_handler.h" 
#include "fds.h" 

#include "app_error.h"
#include "bsp.h" 
#include "app_pwm.h" 

#include "nrf_drv_saadc.h" //ADC
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"

#include "app_mpu.h" //mpu6050

APP_TIMER_DEF(m_LeftLED_timer_id); //Left LED Timer, repeate timer
APP_TIMER_DEF(m_RightLED_timer_id); //Right LED Timer, repeate timer
APP_TIMER_DEF(m_CenterLED_timerid); //Center LED Timer, repeate timer
APP_TIMER_DEF(m_PairLED_timerid); //Pairing LED Timer, repeate timer
APP_TIMER_DEF(m_CrushLED_timerid); //repeate timer


static void LeftLED_timers_start();
static void RightLED_timers_start();
static void CenterLED_timers_start();
static void CrushLED_timers_start();

static void LeftLED_timers_stop();
static void RightLED_timers_stop();
static void CenterLED_timers_stop();
static void CrushLED_timers_stop();

uint32_t right_count;
uint32_t left_count;

static void LeftLED_timer_handler(void * p_context)
{
  static uint32_t left_i;
  uint32_t left_led_on = ((left_i++) % 4); //0,1,2,3 loop
  uint32_t left_led_off = left_led_on; //0,1,2,3

  //static uint32_t left_count;
  left_count++;

  if(left_count == 20) //End after 5times
  {
    /*bsp_board_led_on(0); //Off
    bsp_board_led_on(1);
    bsp_board_led_on(2);*/
    //nrf_delay_ms(100); //Not Applicable

    LeftLED_timers_stop();

    CenterLED_timers_start();

    left_count = 0; //Initialization required for recall
  }

  if(left_led_off == 3)
  {
    //printf("LED OFF TEST \n");
    bsp_board_led_on(0); //Off
    bsp_board_led_on(1);
    bsp_board_led_on(2);
  }

  printf("LED Count : %d \n", left_count);

  if(left_count != 0)
  {
    bsp_board_led_off(left_led_on); //ON<->OFF(Maybe a hardware problem?)
  }
}


static void RightLED_timer_handler(void * p_context)
{
  static uint32_t right_i;
  uint32_t right_led_on = ((right_i++) % 4); //0,1,2,3 loop
  uint32_t right_led_off = right_led_on; //0,1,2,3

  //static uint32_t right_count;
  right_count++;
  printf("LED Count : %d \n", right_count);

  if(right_count == 20) //end after 5times
  {
    /*bsp_board_led_on(0); //Off
    bsp_board_led_on(1);
    bsp_board_led_on(2);*/

    RightLED_timers_stop();

    CenterLED_timers_start();

    right_count = 0; //Initialization required for recall
  }

  if(right_led_off == 3)
  {
    //printf("LED OFF TEST \n");
    bsp_board_led_on(0); //Off
    bsp_board_led_on(1);
    bsp_board_led_on(2);
  }

  if(right_count != 0)
  {
    bsp_board_led_off2(right_led_on); //ON<->OFF(Maybe a hardware problem?), Custom BSP
  }
}


static void CenterLED_timer_handler(void * p_context)
{
  nrf_gpio_pin_toggle(BAT_LED_1);
  nrf_gpio_pin_toggle(BAT_LED_2);
  nrf_gpio_pin_toggle(BAT_LED_3);
}


static void PairLED_timer_handler(void * p_context)
{
  nrf_gpio_pin_toggle(APP_LED_1); //toggle led
  //printf("Pairing LED....\n");
}


static void CrushLED_timer_handler(void * p_context)
{
  nrf_gpio_pin_toggle(BAT_LED_1);
  nrf_gpio_pin_toggle(BAT_LED_2);
  nrf_gpio_pin_toggle(BAT_LED_3);
  nrf_gpio_pin_toggle(APP_LED_2);

  pwm_change_frequency(1000); 
  do_play_buzzer();
}


static void timers_init(void)
{
    ret_code_t err_code;

    // Initialize timer module.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
/*
    // Create Battery timers.
    err_code = app_timer_create(&m_battery_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                battery_level_meas_timeout_handler);
    APP_ERROR_CHECK(err_code);
*/

    // Create Left LED timers.
    err_code = app_timer_create(&m_LeftLED_timer_id,
                                APP_TIMER_MODE_REPEATED, 
                                LeftLED_timer_handler);   
    APP_ERROR_CHECK(err_code); 


    // Create Right LED timers.
    err_code = app_timer_create(&m_RightLED_timer_id,
                                APP_TIMER_MODE_REPEATED, 
                                RightLED_timer_handler);   
    APP_ERROR_CHECK(err_code); 

    //Create Center LED timers
    err_code = app_timer_create(&m_CenterLED_timerid,
                                APP_TIMER_MODE_REPEATED,
                                CenterLED_timer_handler);
    APP_ERROR_CHECK(err_code);


   //Create Pairing LED timers/
    err_code = app_timer_create(&m_PairLED_timerid,
                                APP_TIMER_MODE_REPEATED,
                                PairLED_timer_handler);
    APP_ERROR_CHECK(err_code);

   //Create Crush LED timers/
    err_code = app_timer_create(&m_CrushLED_timerid,
                                APP_TIMER_MODE_REPEATED,
                                CrushLED_timer_handler);
    APP_ERROR_CHECK(err_code);
}


static void LeftLED_timers_start()
{
    ret_code_t err_code;

    pwm_change_frequency(3000);
    do_play_buzzer();
    CenterLED_timers_stop();

    nrf_gpio_pin_clear(BAT_LED_1); //LED off
    nrf_gpio_pin_clear(BAT_LED_2);
    nrf_gpio_pin_clear(BAT_LED_3);

    // Start application timers.
    err_code = app_timer_start(m_LeftLED_timer_id, APP_TIMER_TICKS(500), NULL); //0.5sec delay
    APP_ERROR_CHECK(err_code);
    printf("Left_Led Start\n");

    App_Left = 1; //running
}


static void RightLED_timers_start()
{
    ret_code_t err_code;

    pwm_change_frequency(5500);
    do_play_buzzer();
    CenterLED_timers_stop();

    nrf_gpio_pin_clear(BAT_LED_1); //LED off
    nrf_gpio_pin_clear(BAT_LED_2);
    nrf_gpio_pin_clear(BAT_LED_3);

    // Start application timers.
    err_code = app_timer_start(m_RightLED_timer_id, APP_TIMER_TICKS(500), NULL); //0.5sec delay
    APP_ERROR_CHECK(err_code);
    printf("Right_Led Start\n");

    App_Right = 1; //running
}


static void CenterLED_timers_start()
{
  ret_code_t err_code;

  err_code = app_timer_start(m_CenterLED_timerid, APP_TIMER_TICKS(1000), NULL); //Toggle 1sec
  APP_ERROR_CHECK(err_code);
  printf("Center Led Start\n");
}


static void PairLED_timers_start()
{
  ret_code_t err_code;

  err_code = app_timer_start(m_PairLED_timerid, APP_TIMER_TICKS(1000), NULL); //Toggle 1sec
  APP_ERROR_CHECK(err_code);
  printf("Scanning Start\n");
}


static void CrushLED_timers_start()
{
  CenterLED_timers_stop();

  ret_code_t err_code;

  err_code = app_timer_start(m_CrushLED_timerid, APP_TIMER_TICKS(1000), NULL); //Toggle 1sec
  APP_ERROR_CHECK(err_code);
  printf("Crush!!\n");  
  //crush_state = 1;
 
  App_Crush = 1;
}



static void LeftLED_timers_stop() //Stop Timer
{
    ret_code_t err_code;
    err_code = app_timer_stop(m_LeftLED_timer_id); 
    APP_ERROR_CHECK(err_code);
    printf("Left_Led Stop\n");

    nrf_gpio_pin_clear(BAT_LED_1); //LED off
    nrf_gpio_pin_clear(BAT_LED_2);
    nrf_gpio_pin_clear(BAT_LED_3);

    App_Left = 0; //No running
}


static void RightLED_timers_stop() //Stop Timer
{
    ret_code_t err_code;
    err_code = app_timer_stop(m_RightLED_timer_id); 
    APP_ERROR_CHECK(err_code);
    printf("Right_Led Stop\n");

    nrf_gpio_pin_clear(BAT_LED_1); //LED off
    nrf_gpio_pin_clear(BAT_LED_2);
    nrf_gpio_pin_clear(BAT_LED_3);

    App_Right = 0; //No running
}


static void CenterLED_timers_stop() //Stop Timer
{
    ret_code_t err_code;
    err_code = app_timer_stop(m_CenterLED_timerid); 
    APP_ERROR_CHECK(err_code);
    printf("Center Led Stop\n");
}

static void PairLED_timers_stop() //Stop Timer
{
    ret_code_t err_code;

    err_code = app_timer_stop(m_PairLED_timerid); 
    APP_ERROR_CHECK(err_code);
}


static void CrushLED_timers_stop()
{
    ret_code_t err_code;

    err_code = app_timer_stop(m_CrushLED_timerid); 
    APP_ERROR_CHECK(err_code);

    printf("Crush Led Stop\n");

    nrf_delay_ms(700); //start after 0.7sec
    App_Crush = 0; //No running

    CenterLED_timers_start();
}



static void nus_data_handler(ble_nus_evt_t * p_evt) //Receive from App
{
    uint8_t string_buffer[BLE_NUS_MAX_DATA_LEN+1];

    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
        memcpy(string_buffer, p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
        string_buffer[p_evt->params.rx_data.length] = 0;


        if(strcmp("1", string_buffer) == 0)  //Left
        {
          //LeftLED_timers_start();

          if(App_Right == 1) //if right led is running
          {
            RightLED_timers_stop();
            right_count = 0;
            LeftLED_timers_start();

            printf("R stop L start!\n");
          }

          if(App_Crush == 1) //if crush led is running
          {
            printf("Turn it off in the app! \n");
          }

          else 
          {
            LeftLED_timers_start();
          }
        }

        else if(strcmp("2", string_buffer) == 0) //Right
        {
          //RightLED_timers_start();

          if(App_Left == 1) //if left led is running
          {
            LeftLED_timers_stop();
            left_count = 0;
            RightLED_timers_start();

            printf("L stop R start!\n");
          }

          if(App_Crush == 1) //if crush led is running
          {
            printf("Turn it off in the app! \n");
          }

          else 
          {
            RightLED_timers_start();
          }
        }

        else if(strcmp("3", string_buffer) == 0) //Crush
        {
          CrushLED_timers_start();
        }
      
        else if(strcmp("4", string_buffer) == 0) //Cancel crush led
        {
          CrushLED_timers_stop();
        }
  }
}

When receive the following data from the app, each LED timer is output.

1 : Left LED Timer Start

2 : Right LED Timer Start

3 : Crush LED Timer Start

4 : Crush LED Timer Stop

Left and Right timers are stopped after running for a certain period of time.  The Crush timer continues to run. (can stop by sending data from the app)

If the app sends '1' to run the Left timer and sends '2' to run the Right timer before the Left timer completes, the Left timer stops and the Right timer runs.

This is almost going well. However, when you stop a timer that is in operation and run another timer, the other timer is initially run twice.

Can I know the reason for this?
Thank you!

Related