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.

CC2650DK HeartRate example issue when advertising on button press

Expert 1340 points

I'm using the CC2650DK and HeartRate example. I noticed that pressing the LEFT button doesn't always toggle advertising (in non-connected mode of course). I made a two line modification to the firmware to toggle LED1 with the same button press, and it confirms that the button press is not always detected (either that or it's detected multiple times due to bouncing since I see the LED toggle on then off on one press). I noticed the eval board has no debounce capacitor and example firmware has no debounce. Is lack of debounce the cause of this problem, or is it the RTOS itself? If it's debounce, how can I implement a debounce in firmware to resolve this? Thanks!

  • Hi Tosa,

    Actually debouncing is being done in board_key.c. Can you try with some larger value for the debouncing timer? The debouncing timer duration is defined as KEY_DEBOUNCE_TIMEOUT in board_key.h.

    - Cetri
  • It doesn't look like debouncing is the issue per the scope. But I don't quite understand how the software debounce works. It looks like if a button is pressed, then the Board_keyCallback() interrupt handler is called. This then starts a clock which calls Board_keyChangeHandler() upon expiration. This calls HeartRate_keyPressHandler() which sends a message to the queue. But if there's no hardware debounce, then couldn't the Board_keyCallback() interrupt handler be triggered multiple times? And what is the cause of the advertising to not always toggle on the button press?

  • Hi Tosa,

    The debouncing mechanism used in board_key.c is to prevent another key event from happening within KEY_DEBOUNCE_TIMEOUT from the last event. For example, assuming KEY_DEBOUNCE_TIMEOUT is 200 ms, if a key interrupt happens, a 200 ms timer will start. Then, if another key event happens by contact bounce within that 200 ms, the timer will restart with the new 200 ms period, an so on. Eventually, the keyChangeHandler() will be called just once 200 ms after the last interrupt.

    By the way, is the LED's behavior(on/off) aligned with the advertising status(enable/disable)?

    - Cetri
  • Hi Cetri,

    Yes, I made the LED1 toggle when when the LEFT switch toggles advertising. I think I understand how the debounce works now. However, it doesn't seem to work so well with short values of KEY_DEBOUNCE_TIMEOUT. If KEY_DEBOUNCE_TIMEOUT is 200ms or 500ms, holding the button longer than KEY_DEBOUNCE_TIMEOUT, can cause the action to occur again once the button is released. But if I make it 1000ms, then this issue does not seem to occur. But the user must hold the button for at least a second for the state to toggle...any thoughts?

    Thanks!
  • Hi Tosa,

    Reducing the value of KEY_DEBOUNCE_TIMEOUT, 100 ms for example, and checking the pin value again in the key handler might help. The key handler will look like as follows in that case:

    static void HeartRate_handleKeys(uint8_t shift, uint8_t keys)
    {
    
      ...
      
      // Left key.
      if (keys & KEY_LEFT)
      {
        if (PIN_getInputValue(Board_KEY_LEFT) == 0)  // Check if the key is still pressed
        {
          // If not in a connection, toggle advertising on and off.
          if(gapProfileState != GAPROLE_CONNECTED)
          {  
            // Set fast advertising interval for user-initiated connections.
            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);
    
            // Toggle GAP advertisement status.      
            // Set flag if advertising was cancelled.
            if (HeartRate_toggleAdvertising() == FALSE)
            {
              advCancelled = TRUE;
            }
          }
        }
      }
    }

    - Cetri

  • That seems to help Cetri. Thanks!