This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Switching issue for config mode in Beacon.

Hello,

          I am new to BLE. I am currently working on a beacon application. I have used the beacon example code from SDK and modified it. I am using SDK-17.1.0 controller is nrf52832 with softdevice 132. My Beacon mode is working fine. Now I am using button interrupt to put my device into config mode. But as soon as I enter into the advertising init function My device gets restarted. When debugged found that it was showing error code 4. I have gone through one of the post but could not able to solve the error. In my beacon mode, I am directly transmitting my raw data over BLE.


/**@brief Function for initializing the Advertising functionality.
 */
void advertising_init(void)
{

 ret_code_t     err_code;
    uint8_t        adv_flags;
    ble_advertising_init_t init;

    memset(&init, 0, sizeof(init));

    adv_flags                             = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    init.advdata.name_type                = BLE_ADVDATA_FULL_NAME;
    init.advdata.include_appearance       = true;
    init.advdata.flags                    = adv_flags;
    init.advdata.uuids_complete.uuid_cnt  = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    init.advdata.uuids_complete.p_uuids   = m_adv_uuids;

    init.config.ble_adv_whitelist_enabled = false;
    init.config.ble_adv_fast_enabled      = true;
    init.config.ble_adv_fast_interval     = APP_ADV_FAST_INTERVAL;
    init.config.ble_adv_fast_timeout      = APP_ADV_DURATION;
    init.config.ble_adv_slow_enabled      = false;

    init.evt_handler   = on_adv_evt;
    init.error_handler = ble_advertising_error_handler;

    err_code = ble_advertising_init(&m_advertising, &init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);


}


void BC_advertising_init(void)
{
   uint32_t      err_code;
   // ble_advdata_t advdata;
   // uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
   ble_gap_addr_t device_addr;
 
    memset(&m_adv_params, 0, sizeof(m_adv_params));

    m_adv_params.properties.type =  BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED; //BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
    m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
    m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval        = NON_CONNECTABLE_ADV_INTERVAL;
    m_adv_params.duration        = 0;       // Never time out.

    sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE);      // DCDC enabled

    sd_ble_gap_addr_get(&device_addr);

    BC_advertData[MAC_1]=device_addr.addr[5];			 //MAC ADDRESS
    BC_advertData[MAC_1+1]=device_addr.addr[4];
    BC_advertData[MAC_1+2]=device_addr.addr[3];
    BC_advertData[MAC_1+3]=device_addr.addr[2];
    BC_advertData[MAC_1+4]=device_addr.addr[1];
    BC_advertData[MAC_1+5]=device_addr.addr[0];
    err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
    APP_ERROR_CHECK(err_code);

    sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV,NULL,0);

    APP_ERROR_CHECK(err_code);

    if(err_code == NRF_SUCCESS)
    {
      NRF_LOG_INFO("sd_ble_gap_adv_set_configure init successful !");
    }else
    {
      NRF_LOG_INFO("sd_ble_gap_adv_set_configure init failed !");
    }
}


void change_to_app_ble_advertising_init(void)
{
	bool erase_bonds;
	
    gap_params_init();
    gatt_init();
    advertising_init();
    services_init();
    conn_params_init();
    advertising_start(erase_bonds);
}


static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{

#if DEBUG_INTERRUPT
write_uart_data((unsigned char *)("\n in button_event_handler  "),0);
write_uart_integer(button_action);
write_uart_integer(pin_no);
#endif

    switch (pin_no)
    {
        case SWITCH_INTERRUPT:
                    if(button_action==1)
                    {
                        #if DEBUG_INTERRUPT
            		write_uart_data((unsigned char *)("\n button_action 1:"),0);
            		write_uart_integer(button_action);
                        #endif
                        Config_key_timer_start();
                    }
            	else if(button_action==0)
            	{
                        #if DEBUG_INTERRUPT
        		write_uart_data((unsigned char *)("\n button_action 0:"),0);
        		write_uart_integer(button_action);
                        #endif

                         //CONFIG KEY CHECK============================================================
        		Config_key_timer_stop();			//stop configuration timer
        		if((gb_Config_key_ctr.counts==THREE_SEC_PRESS || gb_Config_key_ctr.counts>=THREE_SEC_PRESS+1) && gb_Config_key_ctr.counts<FIVE_SEC_PRESS)
        		{
                              write_uart_data((unsigned char *)("\n ************In 1**************"),0);

        			if(gb_Button_Tap.Device_state==DEVICE_OFF)	//only applicable if device is turned off first
        			{

                                    write_uart_data((unsigned char *)("\n ************In 2**************"),0);
                                      //Mode of Operation
                                      if(gb_Tag_Mode.Data_Mode)
                                      {
                                          write_uart_data((unsigned char *)("\n ************MOVE THE DEVICE IN CONFIG MODE**************"),0);

                                          gb_Tag_Mode.Data_Mode=0;		//Clear Data mode flag
                                          gb_Tag_Mode.Config_Mode=1;		//Move the device in configuration mode
                                          
                                          Broadcast_Timer_Stop();		//stop Broadcasting the Data Timer
                                          change_to_app_ble_advertising_init();
                                          nrf_delay_ms(200);
                                          
                                          advertising_start(0);

                                          General_Timer_Start();		//for configuration LED blinks
                                          LED_Timer_Start(Configuration_Mode);	//Start LED timer

                                      }
                                      else if(gb_Tag_Mode.Config_Mode)
                                      {
                                          write_uart_data((unsigned char *)("\n ************MOVE THE DEVICE IN DATA MODE**************"),0);
                                          gb_Tag_Mode.Config_Mode=0;			//Clear Config mode flag
                                          gb_Tag_Mode.Data_Mode=1;			//Move the device in Data mode

                                          BC_advertising_init();			//Init stack for Non connectable broadcasting
                                          General_Timer_Start();			//start timer for Measurement and Datalogging
                                          Broadcasting_Timer_Start();			//Start Broadcasting timer
                                          Meas_start_sequence();

                                          LED_Timer_Start(Normal_Mode);					//Start LED timer for normal mode
                                      }
        			}//device off
        		}//three secs key pressed
        		else if (gb_Config_key_ctr.counts==FIVE_SEC_PRESS || gb_Config_key_ctr.counts>=FIVE_SEC_PRESS+1)
        		{
        			write_uart_data((unsigned char *)("\n FIVE_SEC_PRESS"),0);
        		}
        		gb_Config_key_ctr.counts=0;			//Clear counting for configuration key press
//===============================================================================
        		if(gb_Button_Tap.First_Tap==0)
        		{
                              gb_Button_Tap.First_Tap=1;
                              key_timer_start();
                              write_uart_data((unsigned char *)("\n First tap:"),0);
        		}
        		else if(gb_Button_Tap.First_Tap ==1 && gb_Button_Tap.Second_Tap==0)
        		{
                              gb_Button_Tap.Second_Tap=1;
                              write_uart_data((unsigned char *)("\n Second tap:"),0);
        		}

        		if(gb_Button_Tap.First_Tap ==1 && gb_Button_Tap.Second_Tap ==1)
        		{
                          gb_Button_Tap.First_Tap=0;
                          gb_Button_Tap.Second_Tap=0;
                          write_uart_data((unsigned char *)("\n DOUBLE TAP RECEIVED>>>>>>>>>>>>>>>>>>>>>>>"),0);

                          if(gb_Button_Tap.Device_state==DEVICE_ON)
                          {
                            write_uart_data((unsigned char *)("\n Device is ON.. Turn it OFF"),0);
                            Multi_Blink_LED(IOID_RED_LED,100,0,1);
                            gb_Button_Tap.Device_state=DEVICE_OFF;			//change device state
                            General_Timer_Stop();					//stop timer for Measurement and Datalogging
                            Broadcast_Timer_Stop();					//stop Broadcasting the Data
                            Data_Counter=0;						//clear data counter
                          }
                          else// DEVICE_OFF
                          {
                            write_uart_data((unsigned char *)("\n Device is OFF.. Turn it ON"),0);

                            gb_Tag_Mode.Config_Mode=0;			    //Clear Config mode flag
                            gb_Tag_Mode.Data_Mode=1;			    //Move the device in Data mode
                            gb_Button_Tap.Device_state=DEVICE_ON;           //change device state
                            LED_Timer_Start(Normal_Mode);                   //Start LED timer
                            Multi_Blink_LED(IOID_BLUE_LED,100,0,1);         
                            General_Timer_Start();			    //start timer for Measurement and Datalogging
                            Broadcasting_Timer_Start();			    //Start Broadcasting timer

                            DELAY_MS(1000);
                            twi_master_init();
                            INIT_SPI_pins();
                            test_sensors();                                 //test all sensor
                            Set_pin_to_defalut();                 // uninitialized I2C & SPI pins
                            //Meas_start_sequence();
                          }
        		}
        	}
            break;

        default:
            #if DEBUG_INTERRUPT
            write_uart_data((unsigned char *)("Send button default"),0);
            #endif
            APP_ERROR_HANDLER(pin_no);
            break;
    }
}// end of function  button_event_handler(uint8_t pin_no, uint8_t button_action)




int main(void)
{
    bool erase_bonds;
    ret_code_t err_code;
    //Initialize.
    
    nrf_delay_ms(1000);       //Power ON delay
     #if 1
    //log_init();
        #endif
    timers_init();          
    #ifndef DISABLE_MY_UART
    uart1_init();								//Initialize UART for the first time
    write_uart_data((unsigned char *)("UART init"),0);
    #endif
    
    ble_stack_init();
    gap_params_init();
    gatt_init();
    BC_advertising_init();
    //advertising_init();
    services_init();
    conn_params_init();
    peer_manager_init();

    // Start execution.
    NRF_LOG_INFO("Bond Management example started.");
    Init_GPIO();                  //      GPIO initialization 
    Init_all_peripherals();       // Initialize peripherals for measurement
    power_management_init();
   					 				//Enter in Measurement mode on startup
   
    Read_Battery_Init();          // configure ADC for Battery measurement
    Process_Measurement();                                 //check timer and process measurement
    gb_FCONFIG_DATA.FSensor_Advertising_time=10;
    gb_FCONFIG_DATA.FSensor_Advertising_count=1;
    gb_FCONFIG_DATA.FSensor_Advertising_Delay=4000;
    gb_FCONFIG_DATA.FDatalogging_status=0x01;

    gb_FCONFIG_DATA.FBroadcasting_status=0x01;
    
    gb_Button_Tap.Device_state  = DEVICE_ON;
    gb_FCONFIG_DATA.FSesnor_ScanTime= 10;
    gb_Tag_Mode.Data_Mode=1;
    gb_Tag_Mode.Config_Mode=0;
    gb_Timer_Flags.Start_Meas = TRUE;
   // Init_Saved_Configurations();					// Load the
    Broadcasting_Timer_Start();   //Start Advertising timer (30 seconds)
    /*************************************************************************************/
    //Key Initializations
    Button_init();              // Configure Button
    Init_key_timer();		//Double tap timer
    Init_Config_key_timer();	//config timer
    /*************************************************************************************/
    Meas_start_sequence();  	
    General_Timer_Start();
    /* Configuration flag for HTU21d to read temp & humidity*/      
    gb_FCONFIG_DATA.FSensor_Temp=0x01;     
    gb_FCONFIG_DATA.FSensor_Hum=0x01;   
    write_uart_data((unsigned char *)("Bond Management example started."),0);
    
    // Enter main loop.
    for (;;)
    {
      
          if(gb_Tag_Mode.Data_Mode)                                   //check if data mode activated
            {
                  Process_Measurement();                                 //check timer and process measurement
                  if( gb_GENERAL_FLAGS.BATTERY_LOW==1)                    //INDICATE BATTERY LOW 20%
                  {
                    if(gb_Timer_Flags.LED_time == TRUE)
                    {
                      LED_Timer_Start(Battery_Low_Mode);							 //Start LED timer
                      Multi_Blink_LED(LED_RED,100,0,1);
                    }
                  }
                  else
                  {
                    if(gb_Timer_Flags.LED_time == TRUE)                //ELSE NORMAL INDICATION
                    {
                      LED_Timer_Start(Normal_Mode);							 //Start LED timer
                      Multi_Blink_LED(LED_RED,100,0,1);
                    }
                  }
            }//data mode
            else if(gb_Tag_Mode.Config_Mode)                            //check if it is in config mode
            {
    //-------------------------------------------------------------------------------------------------
              if(gb_Timer_Flags.LED_time == TRUE)
              {
                LED_Timer_Start(Configuration_Mode);							 //Start LED timer
                Multi_Blink_LED(LED_BLUE,100,0,1);
              }
  //          Display_print0(dispHandle, 0, 0, "Inside Config Mode");
  //          Meas_Timer_Stop();                            // stop Measurement timer and measurement
              //Data_Counter=0;                               //start datalogging again
              memset(Raw_Param_Data,0xff,Raw_Param_len);               //clear the buffer for datalog
    //----------------------------------------------------------------------------------------------------
            }
             idle_state_handle();
      }
    
}// End of main()

 kindly suggest what to do for switching into config mode.... I haven't added any services yet but in future will be added.

Thank You

Parents
  • Hello Vidar,

                      If I start debugging in debug configuration, the project does not get started... It gives errors but the code is working in release configuration. I don't know-how.

  • Hello,

    Please post a screenshot of the errors you get.

  • Below snap is before starting the debug.

    Below image is after calling bc_advertising_init()

    Befor ble_advertising_init() function

    after  ble_advertising_init() calling

    Should I make m_adv_handle  zero.

    m_adv_handle=0; like this? 

  • Thanks. So after a closer look at you code I notice you are not using ble_advertising_init() in BC_advertising_init(). This means the advertising module is not initialized until you call advertising_init(), and this will cause your adv. handle to be set back to 0xFF here:

    A hack to get around this is to set the .initialized flag to 'true' before calling ble_advertising_init() in advertising_init():

    static void advertising_init(void)
    {
        ...
        m_advertising.adv_handle = m_adv_handle;
        m_advertising.initialized = true;
    
        err_code = ble_advertising_init(&m_advertising, &init);
        APP_ERROR_CHECK(err_code);
        ...

  • Hello Vidar,

                      Your hack worked! Thank You... but now I am getting issues where connectable advertising is getting started.

    Yes, I did not use the ble_advertising_init() function for beacon mode. Because I followed the beacon example from SDK where this function was not called.

    Now I am currently debugging the advertising start issue.

    Below snap is before entering the sd_ble_gap_adv_start() function

    after 

    Is this because of the advertising handle? Any suggestions?

  • When I enabled the log I found that the below error. I am only replicating BMS example now still error coming. Forget about the beacon mode switching for now. 

     nrf_delay_ms(1000);       //Power ON delay
         #if 1
        log_init();
            #endif
        timers_init();          
        #ifndef DISABLE_MY_UART
        //uart1_init();								//Initialize UART for the first time
        write_uart_data((unsigned char *)("UART init"),0);
        #endif
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        //BC_advertising_init();
        advertising_init();
        services_init();
        conn_params_init();
        peer_manager_init();
    
        // Start execution.
        NRF_LOG_INFO("Bond Management example started.");
        Init_GPIO();                  //      GPIO initialization 
       // Init_all_peripherals();       // Initialize peripherals for measurement
       
       					 				//Enter in Measurement mode on startup
       
        Read_Battery_Init();          // configure ADC for Battery measurement
        Process_Measurement();                                 //check timer and process measurement
        gb_FCONFIG_DATA.FSensor_Advertising_time=10;
        gb_FCONFIG_DATA.FSensor_Advertising_count=1;
        gb_FCONFIG_DATA.FSensor_Advertising_Delay=4000;
        gb_FCONFIG_DATA.FDatalogging_status=0x01;
    
        gb_FCONFIG_DATA.FBroadcasting_status=0x01;
        
        gb_Button_Tap.Device_state  = DEVICE_ON;
        gb_FCONFIG_DATA.FSesnor_ScanTime= 10;
        gb_Tag_Mode.Data_Mode=1;
        gb_Tag_Mode.Config_Mode=0;
        gb_Timer_Flags.Start_Meas = TRUE;
       // Init_Saved_Configurations();					// Load the
       // Broadcasting_Timer_Start();   //Start Advertising timer (30 seconds)
        /*************************************************************************************/
        //Key Initializations
        //Button_init();              // Configure Button
        //Init_key_timer();		//Double tap timer
        //Init_Config_key_timer();	//config timer
        /*************************************************************************************/
        //Meas_start_sequence();  	
        //General_Timer_Start();
        /* Configuration flag for HTU21d to read temp & humidity*/      
        gb_FCONFIG_DATA.FSensor_Temp=0x01;     
        gb_FCONFIG_DATA.FSensor_Hum=0x01;   
        write_uart_data((unsigned char *)("Bond Management example started."),0);
         nrf_delay_ms(1000);       //Power ON delay
        advertising_start(erase_bonds);
    
        // Enter main loop.

    <info> app_timer: RTC: initialized.
    <info> ble_bms: Init complete.
    <info> app: Bond Management example started.
    <info> app: Fast adverstising.
    <error> app: ERROR 214503 [Unknown error code] at D:\Projects\nRF5_SDK_17.1.0_ddde560\examples\ble_app_bms\main.c:691
    PC at: 0x00032573
    <error> app: End of error report

  • Please upload the whole main file so I can see what code you have at line 691.

Reply Children
Related