This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

clarification needed on CC2650DK HeartRate example

Expert 1340 points

Other Parts Discussed in Thread: CC2650

I'm having trouble tracing through the HeartRate example's stateChangeEvt() code. I try to set a breakpoint at line 845, but the break point keeps moving to line 852

static void HeartRate_stateChangeEvt(gaprole_States_t newState)
{
  // If no change to the GAP Role state has occurred
  if (gapProfileState == newState)
  {
    return; 
  }

  // If connected
  if (newState == GAPROLE_CONNECTED)
  {
    // Get connection handle.
    GAPRole_GetParameter(GAPROLE_CONNHANDLE, &gapConnHandle);
    PINCC26XX_setOutputValue(Board_LED1, 0);
  }
  // If disconnected
  else if (gapProfileState == GAPROLE_CONNECTED && 
           newState != GAPROLE_CONNECTED)
  {
    // Stop periodic measurement of heart rate.
    Util_stopClock(&measPerClock);

    if (newState == GAPROLE_WAITING_AFTER_TIMEOUT)
    {
      // Link loss timeout-- use fast advertising
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL);
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL);
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_FAST_ADV_DURATION);
    }
    else
    {
      // Else use slow advertising
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL);
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL);
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION);
    }

    // Enable advertising.
    HeartRate_toggleAdvertising();
  }
  // If advertising stopped
  else if (gapProfileState == GAPROLE_ADVERTISING && 
            newState == GAPROLE_WAITING)
  {
    // If advertising stopped by user
    if (advCancelled)
    {
      // Disable advertising.
      advCancelled = FALSE;
    }
    // Else if fast advertising switch to slow
    else if (GAP_GetParamValue(TGAP_GEN_DISC_ADV_INT_MIN) == DEFAULT_FAST_ADV_INTERVAL)
    { 
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL);
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL);
      GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION);
      
      // Enable advertising.
      HeartRate_toggleAdvertising();
    }

Furthermore, the else if at line 829 is entered, but then jumps to line 866. Then toggleAdvertising() is called, but even though the CC2650 isn't advertising since the device was just in a connection and is now disconnected, the advState is still TRUE and advertising doesn't start (it is turned off). This doesn't seem correct. Why are all these seemingly weird behaviors occurring? Shouldn't advertising automatically stop when the peripheral is connected to a central?

  • Hi Tosa,

    I just tried setting a breakpoint at the same statement and disconnected from the device. I had no problem hitting the breakpoint when I disconnected the device.(FYI, I am using BLE SDK v2.1, and the code on line 845 is not the same as yours)

    Regarding HeartRate_toggleAdvertising() function, if you step into the function, you will see  the following:  

    // Get the opposite state.

     advState = !advState;

     // Change the GAP advertisement status to opposite of current status.

     GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),

                          &advState);  

    Therefore, the device stops advertising after you disconnect it.

  • Thanks Christin. I modified some code so the line numbers may not match the original and I'm also using v2.1 of the stack. I'm working on another issue now, but will get back to this issue soon. BTW I'm using the xds100v3 debugger built into the DK. Are you using that?
  • Yes, I am using the same debugger.
  • I'm back to looking at this advertising issue and am able to properly step through and hit breakpoints now (I disable optimizations, but not sure if that would be why it's working now). Anyhow I still see some odd behavior in that when the CC2650 is connected, GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED, &advState); returns advState==1. This is proven by:

    static void HeartRate_stateChangeEvt(gaprole_States_t newState)
    {
      // If no change to the GAP Role state has occurred
      if (gapProfileState == newState)
      {
        return; 
      }
    
      // If connected
      if (newState == GAPROLE_CONNECTED)
      {
        // Get connection handle.
        GAPRole_GetParameter(GAPROLE_CONNHANDLE, &gapConnHandle);
        PINCC26XX_setOutputValue(Board_LED1, 0);
        configSensors();
        // FIXME
    	uint8_t advState;
    	GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED, &advState);
    	if (advState)
    		PINCC26XX_setOutputValue(Board_LED2, 1);
    	//END FIXME
      }
      // If disconnected
      else if (gapProfileState == GAPROLE_CONNECTED && 
               newState != GAPROLE_CONNECTED)
      {
        // Stop periodic measurement of heart rate.
    //    Util_stopClock(&measPerClock);
    //	  TIADS1298_stop();
    
    	// FIXME
    	uint8_t advState;
    	GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED, &advState);
    	if (advState)
    		PINCC26XX_setOutputValue(Board_LED3, 1);
    
    	//END FIXME
    
    	if (newState == GAPROLE_WAITING_AFTER_TIMEOUT)
        {
          // Link loss timeout-- use fast advertising
          GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL);
          GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL);
          GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_FAST_ADV_DURATION);
        }
        else
        {
          // Else use slow advertising
          GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL);
          GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL);
          GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION);
        }
    
        // Enable advertising.
        HeartRate_toggleAdvertising();	// FIXME advstate is true (even though it's not advertising; why?) when this is called, so advertising doesn't start
      }
    ...
    }

    In these two if and if-else sections, the LEDs become lit when I connect and disconnect from the device, and the last HeartRate_toggleAdvertising() does not restart advertising since advState==1 when HeartRate_toggleAdvertising() is enetered. Please clarify why this is the case. Thanks!

  • Hi Tosa,

    I believe this is an error from the comment for that line. If you want to continue advertising after disconnected from the master, you can simply define ADVERTISE_WHEN_NOT_CONNECTED=TURE then you will see the HeartRate_toggleAdvertising is called again right after that.