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.

is it possible to use CC2530 AS SWITCH (REMOTE BUTTON) WITH 3RD PARTY DEVIICES (EFR32MG12)?

Other Parts Discussed in Thread: CC2530, Z-STACK, CC2531, PACKET-SNIFFER, CC2531EMK, CC-DEBUGGER

Hi Team,

Today i ordered cc2530 device to use in my project as switch remote which will join my zigbee network also with 3rd party devices (EFR32MG12 Silicon labs-coordinator).

Can i have any pre defined code(example codes) here for cc2530 device for switch(button) operation.

I want know whether is there any code are available in IDE for the Switch button operation using ZIGBEE 3.0 for CC2530 Device? 

please let me know, thanking you.

  • Hi,

    Please download the Z-Stack 3.0.2 software and refer to the Z-Stack 3.0.2\Projects\zstack\HomeAutomation foldare for example projects to be imported into IAR EW8051 10.20.1

    Regards,
    Ryan

  • Dear Ryan,

    thanks for the instant reply and information.  

    As i mentioned above i am using 3rd party device as Coordinator  EFR32MG12. 

    I am adding  CC2530 OR CC2531 into the ZIGBEE Network which is created by the Coordinator(EFR32MG12).

    How i can i received ZCL commands from coordinator or SEND ON/OFF commands to coordinator or router which are using or working the another IDE.

    How can i add this CC2530 (SWITCH) into the zigbee network with coordinator (EFR32MG12) using ZCL Commands?

    Please let me know, thanking you.

    Best Regards

    sri

  • Hi sri,

    Please refer to the Z-Stack 3.0 Developer's Guide and Sample Application User's Guide in the Documents folder.  I suggest you start with a CC2530 ZC to understand how the default examples operate.  You would also benefit from having a sniffer device and viewing over-the-air packets with PACKET-SNIFFER 2 or Ubiqua software.  There are several similar E2E posts that you can reference.

    Regards,
    Ryan

  • You can use SampleSwitch example in Z-Stack 3.0.2 to send ZCL ON/OFF command. To receive ON/OFF command, you can use SampleLight.

  • Dear, 

    Thanks for the information. as you mentioned i downloaded Z-Stack 3.0.2 and i imported Sample Switch and Sample Light into the IAR 10.20.1 Version EW.

    Later by using CC Debugger i download and debug the firmware into my CC2530 (light-coordinator) and CC2530 (switch-end device) devices respectively. and i didn't have display to my both CC2530 switch and light devices. 

    And i downloaded the smart RF7  flash programmer to check the OTA  and Packet Sniffer between Coordinator and End Device when i press button in switch device. 

    But i didn't understand first how to create network between devices using ZCL commands.

    please let me know how to form mesh network between this Coordinator and End Devices using ZCL Commands FOR ON/OFF BUTTON AND TOGGLE LED (create, bind and send). 

    Thanking You for Assistance.

  • If you do not have the push button and LCD UI from the SMARTRF05EBK then you will need to invoke bdb_StartCommissioning manually through modifications to the zcl_samplesw.c file.  These are Base Device Behavior (BDB), not Zigbee Cluster Library (ZCL), operations.

    Regards,
    Ryan

  • CC2530 is not recommended for new Zigbee 3.0 products. I would suggest you to buy LAUNCHXL-CC2652R1 and use it for new Zigbee 3.0 design.

  • 1. LAUNCHXL-CC2652R1 use SIMPLELINK-CC13X2-26X2-SDK and Z-Stack example in it. It's similar but different from Z-Stack 3.0.2.

    2. You can use LAUNCHXL-CC2652R1 as coordinator but there is no existing example would bridge signal to Amazon cloud. You have to do this by yourself.

  • Dear Yk, 

    thanks for the information.

    Is there any example how to make invoke bdb_StartCommissioning manually through modifications to the zcl_samplesw.c file? for Button and Led operation for cc2530 or cc2531 without using  SMARTRF05EBK. 

     i had seen following  E2E post:

    https://e2e.ti.com/support/wireless-connectivity/zigbee-and-thread/f/zigbee-thread-forum/610901/cc2530-cc2530-ports-working-with-keys

    https://e2e.ti.com/support/wireless-connectivity/zigbee-and-thread/f/zigbee-thread-forum/742632/cc2530-how-to-change-sample-projects-in-zstack-as-i-need#pifragment-322104=1    and some other  related E2E posts also.

    but i didn't understand how to do it manually.  Please help and let me know, Thanking You.

  • I suppose you can call bdb_StartCommissioning at the end of zclSampleSw_Init. If you have button on your EVB, you can call bdb_StartCommissioning in zclSampleSw_HandleKeys which would be trigger when button is pressed.

  • Dear,

    In CC2531 device i have 2 button S1 & S2 in the devices.

    Can i use any one button in that  S1 & S2, i dint whether its possible to use them or not for my operation. can I use them for this test now. or do i need to use any external Button?

    Please let me know,  Thanking you

  • I see no problem to use them if you configure GPI of those buttons correctly.

  • Dear,

    IN LIGHT COORDINATOR DEVICE I CALLED:

      "bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_FORMATION);  at the end of in zclSampleLight_Init();

    IN SWITCH END DEVICE I CALLED:

    bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING); in  zclSampleSw_Init();

    and in both Switch&light i called below function for handlekeys.

    static void zclSample_HandleKeys( byte shift, byte keys )
    {
    if (keys & HAL_KEY_SW_2)
    {

    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_BLINK); //blink hardware LED

    }
    if (keys & HAL_KEY_SW_1)
    {

    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_OFF); //blink hardware LED

    }
    }

    i didn't chances any hardware pins for button and i just used the default things and for led also i dint add any led i just called LED2_BV. 

    but its not working and i cant see any response in both devices.

    please let me know what went wrong. thanking you

  • Dear, 

     here is can see my button in hal_board_cfg.h.

    without any Button press in my switch device, i can see the led Bink on/off Automatically after the debugging in Light Device. please help to sort out this issue. thanking you.

  • (I pressed your previous message as "TI Thinks Resolved" on accident)

    If you have a CC-DEBUGGER and CC2531EMK then please use the IAR debugger to set breakpoints and pause your program operation to view the call stack.    If the device is a ZC and the blinking only lasts for three minutes then this could be the network-joining process.  Otherwise your code may be stuck in a HAL assert.

    Regards,
    Ryan

  • You should revise hal_key.c to use S1 and S2.

  • Dear, 

    Thanks for the information.

    YK as you mentioned i made changes in hal_key.c

    According to the CC2531 Schematic :

    S1 BUTTON IS P1_2

    S2 BUTTON IS P1_3

    As i mentioned above in my buttons in hal_board_cfg.h,

    #define ACTIVE_LOW !
    #define ACTIVE_HIGH !! /* double negation forces result to be '1' */

    /* S1 */
    #define PUSH1_BV BV(2)
    #define PUSH1_SBIT P1_2

    #if defined (HAL_BOARD_CC2530EB_REV17)
    #define PUSH1_POLARITY ACTIVE_HIGH
    #elif defined (HAL_BOARD_CC2530EB_REV13)
    #define PUSH1_POLARITY ACTIVE_LOW
    #else
    #error Unknown Board Indentifier
    #endif

    /* S2 */
    #define PUSH2_BV BV(3)
    #define PUSH2_SBIT P1_3
    #define PUSH2_POLARITY ACTIVE_HIGH

    Later,

    Now i made changes as you mentioned  in hal_key.c  file as below.

    /* SW_6 is at P1.2 */


    #define HAL_KEY_SW_6_PORT P1
    #define HAL_KEY_SW_6_BIT BV(2)
    #define HAL_KEY_SW_6_SEL P1SEL
    #define HAL_KEY_SW_6_DIR P1DIR

    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT BV(2)
    #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE


    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT BV(2) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG P1IFG /* Interrupt flag at source */

    and i didnt used /* Joy stick move at P1.3 */ for my Button s2  P1.3. because i read your answer in Some other E2E post as Joystick uses P2.0 and ADC reading so it's not suitable for changing it.

    so I clone SW6 P1.2 and revised  it for P1.3 -s2 button.

    /* SW_6 is at P1.3 */


    #define HAL_KEY_SW_6_PORT P1
    #define HAL_KEY_SW_6_BIT BV(3)
    #define HAL_KEY_SW_6_SEL P1SEL
    #define HAL_KEY_SW_6_DIR P1DIR

    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT BV(3)
    #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE


    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT BV(3) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG P1IFG /* Interrupt flag at source */

    but it still not working, please help me, thanking you.

  • For using P1.2 as GPI to triggered button interrupt, you don't configure register correctly. You should revise the following registers for P1.2. You can refer to CC253x user guide to know correct register settings.

    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT BV(1)
    #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE
    
    
    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT BV(2) /* P1IEN - P1.2 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG P1IFG /* Interrupt flag at source */

  • Dear YK,

    Thanks a lot for your help.

    As i did for the  Button S1 - P1.2,  i did same for the Button S2 -P1.3. 

    but for the button P1.3 i am getting warnings as incompatible redefinition of macro.

    should i need to consider that warning? or can i ignore it.

    please let me know. thanking you. 

  • It's obvious warning. You have defined HAL_KEY_SW_6_BIT and HAL_KEY_SW_6_ICTLBIT twice. I don't mean to lecture but this is very fundamental in C programming.

  • Dear,

    As i mentioned, i added bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING);  in  zclSampleSw_Init();

    and i used S1 and S2 AS SWITCH P1.2 AND P1.3.

    According to the below macro i used them  in zclSampleSw.c

     #define HAL_KEY_SW_6 0x20   // Button S1 if available
    #define HAL_KEY_SW_7 0x40    // Button S2 if available

    i used them in my function in zclSampleSw.c file.

    static void zclSampleSw_HandleKeys( byte shift, byte keys )
    {
    if (keys & HAL_KEY_SW_7)
    {

    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_BLINK); //blink hardware LED

    }
    if (keys & HAL_KEY_SW_6)
    {

    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_OFF); //blink hardware LED

    }
    }

    later i made changes in the hal_key.c as below code as you mentioned. i even try to find the problem by set breakpoints also but i didn't d understand issue. 

     I made changes in hal_key.c as mentioned. but i dint understand why its not working.

    please help me, thanking you.

    #include "hal_mcu.h"
    #include "hal_defs.h"
    #include "hal_types.h"
    #include "hal_board.h"
    #include "hal_drivers.h"
    #include "hal_adc.h"
    #include "hal_key.h"
    #include "osal.h"
    
    #if (defined HAL_KEY) && (HAL_KEY == TRUE)
    
    /**************************************************************************************************
     *                                              MACROS
     **************************************************************************************************/
    
    /**************************************************************************************************
     *                                            CONSTANTS
     **************************************************************************************************/
    #define HAL_KEY_RISING_EDGE   0
    #define HAL_KEY_FALLING_EDGE  1
    
    #define HAL_KEY_DEBOUNCE_VALUE  25
    
    /* CPU port interrupt */
    #define HAL_KEY_CPU_PORT_0_IF P0IF
    #define HAL_KEY_CPU_PORT_1_IF P0IF
    #define HAL_KEY_CPU_PORT_2_IF P2IF
    
    /* SW_6 is at P0.1 */
    #define HAL_KEY_SW_6_PORT   P1
    #define HAL_KEY_SW_6_BIT    BV(2)
    #define HAL_KEY_SW_6_SEL    P1SEL
    #define HAL_KEY_SW_6_DIR    P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT  BV(1)
    #define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE
    
    
    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT   BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT  BV(2) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P1IFG /* Interrupt flag at source */
    
    /* SW_2 is at P1.3 */
    #define HAL_KEY_SW_7_PORT   P1
    #define HAL_KEY_SW_7_BIT    BV(3)
    #define HAL_KEY_SW_7_SEL    P1SEL
    #define HAL_KEY_SW_7_DIR    P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_7_EDGEBIT  BV(1)
    #define HAL_KEY_SW_7_EDGE     HAL_KEY_RISING_EDGE
    
    
    /* SW_6 interrupts */
    #define HAL_KEY_SW_7_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_7_IENBIT   BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_7_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_7_ICTLBIT  BV(3) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_7_PXIFG    P1IFG /* Interrupt flag at source */
    
    /* Joy stick move at P2.0 */
    #define HAL_KEY_JOY_MOVE_PORT   P2
    #define HAL_KEY_JOY_MOVE_BIT    BV(0)
    #define HAL_KEY_JOY_MOVE_SEL    P2SEL
    #define HAL_KEY_JOY_MOVE_DIR    P2DIR
    
    /* edge interrupt */
    #define HAL_KEY_JOY_MOVE_EDGEBIT  BV(3)
    #define HAL_KEY_JOY_MOVE_EDGE     HAL_KEY_FALLING_EDGE
    
    /* Joy move interrupts */
    #define HAL_KEY_JOY_MOVE_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_JOY_MOVE_IENBIT   BV(1) /* Mask bit for all of Port_2 */
    #define HAL_KEY_JOY_MOVE_ICTL     P2IEN /* Port Interrupt Control register */
    #define HAL_KEY_JOY_MOVE_ICTLBIT  BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */
    #define HAL_KEY_JOY_MOVE_PXIFG    P2IFG /* Interrupt flag at source */
    
    #define HAL_KEY_JOY_CHN   HAL_ADC_CHANNEL_6
    
    
    /**************************************************************************************************
     *                                            TYPEDEFS
     **************************************************************************************************/
    
    
    /**************************************************************************************************
     *                                        GLOBAL VARIABLES
     **************************************************************************************************/
    static uint8 halKeySavedKeys;     /* used to store previous key state in polling mode */
    static halKeyCBack_t pHalKeyProcessFunction;
    static uint8 HalKeyConfigured;
    bool Hal_KeyIntEnable;            /* interrupt enable/disable flag */
    
    /**************************************************************************************************
     *                                        FUNCTIONS - Local
     **************************************************************************************************/
    void halProcessKeyInterrupt(void);
    uint8 halGetJoyKeyInput(void);
    
    
    
    /**************************************************************************************************
     *                                        FUNCTIONS - API
     **************************************************************************************************/
    
    
    /**************************************************************************************************
     * @fn      HalKeyInit
     *
     * @brief   Initilize Key Service
     *
     * @param   none
     *
     * @return  None
     **************************************************************************************************/
    void HalKeyInit( void )
    {
      /* Initialize previous key to 0 */
      halKeySavedKeys = 0;
    
      HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT);    /* Set pin function to GPIO */
      HAL_KEY_SW_7_SEL &= ~(HAL_KEY_SW_7_BIT); 
    #if ! defined ENABLE_LED4_DISABLE_S1
      HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */
    #endif
       #if ! defined ENABLE_LED4_DISABLE_S1
      HAL_KEY_SW_7_DIR &= ~(HAL_KEY_SW_7_BIT);    /* Set pin direction to Input */
    #endif
    
       HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT ); /* Set pin function to GPIO */
      HAL_KEY_SW_7_SEL &= ~(HAL_KEY_SW_7_BIT ); /* Set pin direction to Input */
    
      /* Initialize callback function */
      pHalKeyProcessFunction  = NULL;
    
      /* Start with key is not configured */
      HalKeyConfigured = FALSE;
    }
    
    
    /**************************************************************************************************
     * @fn      HalKeyConfig
     *
     * @brief   Configure the Key serivce
     *
     * @param   interruptEnable - TRUE/FALSE, enable/disable interrupt
     *          cback - pointer to the CallBack function
     *
     * @return  None
     **************************************************************************************************/
    void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
    {
      /* Enable/Disable Interrupt or */
      Hal_KeyIntEnable = interruptEnable;
    
      /* Register the callback fucntion */
      pHalKeyProcessFunction = cback;
    
      /* Determine if interrupt is enable or not */
      if (Hal_KeyIntEnable)
      {
        /* Rising/Falling edge configuratinn */
    
        PICTL &= ~(HAL_KEY_SW_6_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)
        PICTL |= HAL_KEY_SW_6_EDGEBIT;
      #endif
    
    
        /* Interrupt configuration:
         * - Enable interrupt generation at the port
         * - Enable CPU interrupt
         * - Clear any pending interrupt
         */
        HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;
        HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;
        HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);
    
    
    
        /* Rising/Falling edge configuratinn */
    
          PICTL &= ~(HAL_KEY_SW_7_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_SW_7_EDGE == HAL_KEY_RISING_EDGE)
        PICTL |= HAL_KEY_SW_7_EDGEBIT;
      #endif
    
    
        /* Interrupt configuration:
         * - Enable interrupt generation at the port
         * - Enable CPU interrupt
         * - Clear any pending interrupt
         */
       
        HAL_KEY_SW_7_ICTL |= HAL_KEY_SW_7_ICTLBIT;
        HAL_KEY_SW_7_IEN |= HAL_KEY_SW_7_IENBIT;
        HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT);;
    
    
        /* Do this only after the hal_key is configured - to work with sleep stuff */
        if (HalKeyConfigured == TRUE)
        {
          osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */
        }
      }
      else    /* Interrupts NOT enabled */
      {
        HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */
        HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT);   /* Clear interrupt enable bit */
    
         HAL_KEY_SW_7_ICTL &= ~(HAL_KEY_SW_7_ICTLBIT); /* don't generate interrupt */
         HAL_KEY_SW_7_IEN &= ~(HAL_KEY_SW_7_IENBIT);   /* Clear interrupt enable bit */
    
        osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
      }
    
      /* Key now is configured */
      HalKeyConfigured = TRUE;
    }
    
    
    /**************************************************************************************************
     * @fn      HalKeyRead
     *
     * @brief   Read the current value of a key
     *
     * @param   None
     *
     * @return  keys - current keys status
     **************************************************************************************************/
    uint8 HalKeyRead ( void )
    {
      uint8 keys = 0;
    
      if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
      }
      if (HAL_PUSH_BUTTON2())
      {
        keys |= HAL_KEY_SW_7;
      }
      if ((HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))  /* Key is active low */
      {
        keys |= halGetJoyKeyInput();
      }
      if ((HAL_KEY_SW_7_PORT & HAL_KEY_SW_7_BIT))  /* Key is active low */
      {
        keys |= halGetJoyKeyInput();
      }
    
      return keys;
    }
    
    
    /**************************************************************************************************
     * @fn      HalKeyPoll
     *
     * @brief   Called by hal_driver to poll the keys
     *
     * @param   None
     *
     * @return  None
     **************************************************************************************************/
    void HalKeyPoll (void)
    {
      uint8 keys = 0;
    
     if ((HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))  /* Key is active Low */
      {
        keys = halGetJoyKeyInput();
      }
     if ((HAL_KEY_SW_7_PORT & HAL_KEY_SW_7_BIT))  /* Key is active High */
      {
        keys = halGetJoyKeyInput();
      }
    
      /* If interrupts are not enabled, previous key status and current key status
       * are compared to find out if a key has changed status.
       */
      if (!Hal_KeyIntEnable)
      {
        if (keys == halKeySavedKeys)
        {
          /* Exit - since no keys have changed */
          return;
        }
        /* Store the current keys for comparation next time */
        halKeySavedKeys = keys;
      }
      else
      {
        /* Key interrupt handled here */
      }
    
      if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
      }
      if (HAL_PUSH_BUTTON2())
      {
        keys |= HAL_KEY_SW_7;
      }
      /* Invoke Callback if new keys were depressed */
      if (pHalKeyProcessFunction
    #ifdef HAL_LEGACY_KEYS
        && keys //in legacy modes, only report key presses and do not report when a key is released
    #endif
        )
      {
        (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
      }
    }
    
    /**************************************************************************************************
     * @fn      halGetJoyKeyInput
     *
     * @brief   Map the ADC value to its corresponding key.
     *
     * @param   None
     *
     * @return  keys - current joy key status
     **************************************************************************************************/
    uint8 halGetJoyKeyInput(void)
    {
      /* The joystick control is encoded as an analog voltage.
       * Read the JOY_LEVEL analog value and map it to joy movement.
       */
     
      uint8 ksave0 = 0;
      uint8 ksave1;
    
      /* Keep on reading the ADC until two consecutive key decisions are the same. */
      do
      {
        ksave1 = ksave0;    /* save previouse key reading */
    
        if (HAL_PUSH_BUTTON1())
        {
           ksave0 |= HAL_KEY_SW_6;
        }
        else if(HAL_PUSH_BUTTON2())
        {
          ksave0 |= HAL_KEY_SW_7;
        }
      } while (ksave0 != ksave1);
    
      return ksave0;
    }
    
    
    
    
    
    /**************************************************************************************************
     * @fn      halProcessKeyInterrupt
     *
     * @brief   Checks to see if it's a valid key interrupt, saves interrupt driven key states for
     *          processing by HalKeyRead(), and debounces keys by scheduling HalKeyRead() 25ms later.
     *
     * @param
     *
     * @return
     **************************************************************************************************/
    void halProcessKeyInterrupt (void)
    {
      bool valid=FALSE;
    
      if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */
        valid = TRUE;
      }
       if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT); /* Clear Interrupt Flag */
        valid = TRUE;
      }
      if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */
        valid = TRUE;
      }
    
      if (valid)
      {
        osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);
      }
    }
    
    /**************************************************************************************************
     * @fn      HalKeyEnterSleep
     *
     * @brief  - Get called to enter sleep mode
     *
     * @param
     *
     * @return
     **************************************************************************************************/
    void HalKeyEnterSleep ( void )
    {
    }
    
    /**************************************************************************************************
     * @fn      HalKeyExitSleep
     *
     * @brief   - Get called when sleep is over
     *
     * @param
     *
     * @return  - return saved keys
     **************************************************************************************************/
    uint8 HalKeyExitSleep ( void )
    {
      /* Wake up and read keys */
      return ( HalKeyRead () );
    }
    
    /***************************************************************************************************
     *                                    INTERRUPT SERVICE ROUTINE
     ***************************************************************************************************/
    
    /**************************************************************************************************
     * @fn      halKeyPort0Isr
     *
     * @brief   Port0 ISR
     *
     * @param
     *
     * @return
     **************************************************************************************************/
    
    HAL_ISR_FUNCTION( halKeyPort1Isr, P0INT_VECTOR )
    {
      HAL_ENTER_ISR();
    
      if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)
      {
        halProcessKeyInterrupt();
      }
      if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT)
      {
        halProcessKeyInterrupt();
      
      }
      /*
        Clear the CPU interrupt flag for Port_0
        PxIFG has to be cleared before PxIF
      */
      HAL_KEY_SW_6_PXIFG = 0;
      HAL_KEY_SW_7_PXIFG = 0;
      HAL_KEY_CPU_PORT_1_IF = 0;
      
      CLEAR_SLEEP_MODE();
      HAL_EXIT_ISR();
    }
    /**************************************************************************************************
     * @fn      halKeyPort2Isr
     *
     * @brief   Port2 ISR
     *
     * @param
     *
     * @return
     **************************************************************************************************/
    HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )
    {
      HAL_ENTER_ISR();
      
      if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)
      {
        halProcessKeyInterrupt();
      }
    
      /*
        Clear the CPU interrupt flag for Port_2
        PxIFG has to be cleared before PxIF
        Notes: P2_1 and P2_2 are debug lines.
      */
      HAL_KEY_JOY_MOVE_PXIFG = 0;
      HAL_KEY_CPU_PORT_2_IF = 0;
    
      CLEAR_SLEEP_MODE();
      HAL_EXIT_ISR();
    }
    
    #else
    
    
    void HalKeyInit(void){}
    void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){}
    uint8 HalKeyRead(void){ return 0;}
    void HalKeyPoll(void){}
    
    #endif /* HAL_KEY */

  • What have you discovered through the debug efforts?  Does the code go through zclSampleSw_Init and HalKeyInit/HalKeyConfig?  And are you using a CC530 or CC2531 and is the correct variant selected inside IAR?

    Regards,
    Ryan

  • Do you try to set breakpoint in “HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )” and check if it hits when you press button?

  • Hi Guys, I have similar problem to add 2 buttons on my custom board, I have observed that my main problem is under HalKeyPoll() function.

    I have using HAL_PUSH_BUTTON1 (HAL_KEY_SW_6 - 0x20) and HAL_PUSH_BUTTON2 (HAL_KEY_SW_7 -0x40). When I set up breakpoints I have observed that the two buttons are reached at same time and after it the key value is 0x60 . When it is occurs my device turn on or turn off simultaneous the two led and pint associated OnOff circuits. My code can´t see differences between HAL_PUSH_BUTTON1 or HAL_PUSH_ BUTTON2.

    note: the firsts steps of interrupts are running very well, I can see it using breakpoints on HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT or HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT. When S1 or S2 are pressed!

  • Are you sure your  “HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )” is triggered when S1 or S2 is pressed?

  • Dear,

    Thanks for the information. 

    yes, i checked as you mentioned by  setting  breakpoint in “HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )”. 

    I cant able to reach the breakpoint when i press the button or when i run press the next statement. 

    I seen my code is struct at the osal_run_system(); 

     please let me how can i solve this issue. thanking you.

  • Do you define ISR_KEYINTERRUPT in "Defined symbols" of your project?

  • Dear,

    I dint understand "Defined symbols" in your project means?

     In zcsampleSw.c i made 

    static void zclSampleSw_HandleKeys( byte shift, byte keys )
    {
    if (keys & HAL_KEY_SW_7)
    {

    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_BLINK); //blink hardware LED

    }
    if (keys & HAL_KEY_SW_6)
    {

    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_OFF); //blink hardware LED

    }
    }

    and i add all the related include file in zcsamplesw.c. 

    Define symbols means?  i define in zcsampleSW. C OR ZMAIN.C?

    please let me know.

  • You can refer to my screenshot.

  • Dear, 

    Thanks for the screenshot, but i dint have the option ISR_KEYINTERRUPT  in  my Defined symbols: (one line per line).

    in that i can all this:

    SECURE=1

    TC_LINKKEY_JOIN
    NV_INIT
    NV_RESTORE
    xPOWER_SAVING
    NWK_AUTO_POLL
    xZTOOL_P1
    xMT_TASK
    xMT_APP_FUNC
    xMT_SYS_FUNC
    xMT_ZDO_FUNC
    xMT_ZDO_MGMT
    xMT_APP_CNF_FUNC
    LCD_SUPPORTED=DEBUG
    MULTICAST_ENABLED=FALSE
    ZCL_READ
    ZCL_DISCOVER
    ZCL_WRITE
    ZCL_BASIC
    ZCL_IDENTIFY
    ZCL_ON_OFF
    ZCL_GROUPS.

    bUT I DINT HAVE IAR_KEYINTERRUPT in my sample switch project. please let me know, thanking you.

  • You have to add ISR_KEYINTERRUPT (not IAR_KEYINTERRUPT) into "Defined symbols" of your project to make key interrupt work.

  • Dear YK,

    Before i thought i need to add  IAR_KEYINTERRUPT from the available dialog in the Defined symbols. but now i added it manually.

    Now after putting the breakpoint at HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) i can see breakpoint hits when i press the RUN(F5)  After adding the  IAR_KEYINTERRUPT

    But its not hitting the HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) when i press the button S1 OR S2 in Device. 

    Please let me know, thanking you.

  • It means that you don't setup registers correctly for S1 and S2 button in your hal_key.c.

  • Hi Mr YK, I´ll open a new forum post. To prevent confusion between me and Sri Naga doubts!

  • Hi Mr Sri, as Mr Y.K told you I mean that you not finished the key configuration, including key init and Key config. Do you can see my key configuration on key.c file below. I used P0.1 and P0.4 in my case.

    Note: In my case the breakpoint hit, for each the respectively correspondents lines HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT and HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT, in the  HAL_ISR_FUNCTION( halKeyPort1Isr, P0INT_VECTOR ). 

    /* SW_6 settings at P0.4 */
    //- Definitions
    
    #define HAL_KEY_SW_6_ENABLE
    #define HAL_KEY_SW_6_PORT   P0
    #define HAL_KEY_SW_6_BIT    BV(4)
    #define HAL_KEY_SW_6_SEL    P0SEL
    #define HAL_KEY_SW_6_DIR    P0DIR
    
    #define HAL_KEY_SW_6_EDGEBIT  BV(0)
    #define HAL_KEY_SW_6_EDGE     HAL_KEY_RISING_EDGE
    
    
    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN      IEN1  /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT   BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL     P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT  BV(4) /* P0IEN - P0.4 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P0IFG /* Interrupt flag at source */
    
    
    /* SW_7 settings at P0.1 */
    // - Definitions
    
    #define HAL_KEY_SW_7_ENABLE
    #define HAL_KEY_SW_7_PORT   P0
    #define HAL_KEY_SW_7_BIT    BV(1)
    #define HAL_KEY_SW_7_SEL    P0SEL
    #define HAL_KEY_SW_7_DIR    P0DIR
    
    
    #define HAL_KEY_SW_7_EDGEBIT  BV(0)
    #define HAL_KEY_SW_7_EDGE     HAL_KEY_RISING_EDGE
    
    
    /* SW_7 interrupts */
    
    #define HAL_KEY_SW_7_IEN      IEN1  /* CPU interrupt mask register */
    #define HAL_KEY_SW_7_IENBIT   BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_7_ICTL     P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_7_ICTLBIT  BV(1) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_7_PXIFG    P0IFG /* Interrupt flag at source */
    
    
    /**************************************************************************************************
     * @fn      HalKeyInit
     *
     * @brief   Initilize Key Service
     *
     * @param   none
     *
     * @return  None
     **************************************************************************************************/
    void HalKeyInit( void )
    {
      /* Initialize previous key to 0 */
      halKeySavedKeys = 0;
    
      HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT);    /* Set pin function to GPIO */
      HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */
      
      HAL_KEY_SW_7_SEL &= ~(HAL_KEY_SW_7_BIT);    /* Set pin function to GPIO */
      HAL_KEY_SW_7_DIR &= ~(HAL_KEY_SW_7_BIT);    /* Set pin direction to Input */
    
      /* Initialize callback function */
      pHalKeyProcessFunction  = NULL;
    
      /* Start with key is not configured */
      HalKeyConfigured = FALSE;
    }
    
    
    /**************************************************************************************************
     * @fn      HalKeyConfig
     *
     * @brief   Configure the Key serivce
     *
     * @param   interruptEnable - TRUE/FALSE, enable/disable interrupt
     *          cback - pointer to the CallBack function
     *
     * @return  None
     **************************************************************************************************/
    void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
    {
      /* Enable/Disable Interrupt or */
      Hal_KeyIntEnable = interruptEnable;
    
      /* Register the callback fucntion */
      pHalKeyProcessFunction = cback;
    
      /* Determine if interrupt is enable or not */
      if (Hal_KeyIntEnable)
      {
        /* Rising/Falling edge configuratinn */
        PICTL &= ~(HAL_KEY_SW_6_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)
        PICTL |= HAL_KEY_SW_6_EDGEBIT;
      #endif
      
        /* Interrupt configuration:
         * - Enable interrupt generation at the port
         * - Enable CPU interrupt
         * - Clear any pending interrupt
         */
        HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;
        HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;
        HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);
    
    
       /* Rising/Falling edge configuratinn */
        PICTL &= ~(HAL_KEY_SW_7_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_SW_7_EDGE == HAL_KEY_FALLING_EDGE)
        PICTL |= HAL_KEY_SW_7_EDGEBIT;
      #endif
    
    	HAL_KEY_SW_7_ICTL |= HAL_KEY_SW_7_ICTLBIT;
        HAL_KEY_SW_7_IEN |= HAL_KEY_SW_7_IENBIT;
        HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT);
    
        /* Rising/Falling edge configuratinn */
    
        /* Interrupt configuration:
         * - Enable interrupt generation at the port
         * - Enable CPU interrupt
         * - Clear any pending interrupt
         */
    
        /* Do this only after the hal_key is configured - to work with sleep stuff */
        if (HalKeyConfigured == TRUE)
        {
          osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */
        }
      }
      else    /* Interrupts NOT enabled */
      {
        HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */
        HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT);   /* Clear interrupt enable bit */
    	
    	HAL_KEY_SW_7_ICTL &= ~(HAL_KEY_SW_7_ICTLBIT); /* don't generate interrupt */
        HAL_KEY_SW_7_IEN &= ~(HAL_KEY_SW_7_IENBIT);   /* Clear interrupt enable bit */
    
        osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
      }
    
      /* Key now is configured */
      HalKeyConfigured = TRUE;
    }
    
    

  • Dear Alexander Argollo, 

    Thank you so much for your kind help.

  • Dear,

    I Think i made switch 6 p1.2 with correct register setup according to you above reference.

    * SW_6 is at P1.2 */
    #define HAL_KEY_SW_6_PORT P1
    #define HAL_KEY_SW_6_BIT BV(2)
    #define HAL_KEY_SW_6_SEL P1SEL
    #define HAL_KEY_SW_6_DIR P1DIR

    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT BV(2)
    #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE

    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT BV(4) /* Mask bit for all of Port_1 */
    #define HAL_KEY_SW_6_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT BV(2) /* P0IEN - P1.2 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG P1IFG /* Interrupt flag at source */

    /* SW_7 is at P1.3 */
    #define HAL_KEY_SW_7_PORT P1
    #define HAL_KEY_SW_7_BIT BV(3)
    #define HAL_KEY_SW_7_SEL P1SEL
    #define HAL_KEY_SW_7_DIR P1DIR

    /* edge interrupt */
    #define HAL_KEY_SW_7_EDGEBIT BV(2)
    #define HAL_KEY_SW_7_EDGE HAL_KEY_RISING_EDGE


    /* SW_7 interrupts */
    #define HAL_KEY_SW_7_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_7_IENBIT BV(4) /* Mask bit for all of Port_1 */
    #define HAL_KEY_SW_7_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_7_ICTLBIT BV(3) /* P0IEN - P1.3 enable/disable bit */
    #define HAL_KEY_SW_7_PXIFG P1IFG /* Interrupt flag at source */

    but s1 and s2 are not responding. is this problem in s1 and s2 register configuration or somewhere else.

    please let me know, thanking you.

  • Dear, 

    Below you can see my “HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR );

    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )
    {
    HAL_ENTER_ISR();

    if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)
    {
    halProcessKeyInterrupt();
    }
    if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT)
    {
    halProcessKeyInterrupt();

    }
    /*
    Clear the CPU interrupt flag for Port_0
    PxIFG has to be cleared before PxIF
    */
    HAL_KEY_SW_6_PXIFG;
    HAL_KEY_SW_7_PXIFG;
    HAL_KEY_CPU_PORT_1_IF;

    CLEAR_SLEEP_MODE();
    HAL_EXIT_ISR();
    }

    void halProcessKeyInterrupt(void)
    {
    bool valid =FALSE;

    if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT) /* Interrupt Flag has been set */
    {
    HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */
    valid = TRUE;
    }

    if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT) /* Interrupt Flag has been set */
    {
    HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT); /* Clear Interrupt Flag */
    valid = TRUE;
    }

    if (valid)
    {
    osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);
    }
    }

    pleasee let me know were i went wrong, thanking you.

  • Mr Sri, could you see as a reference my hal_board_cfg.h configuration (according P0 Ports). Please check your configuration according P1.2 and P1.3 that you using!

    /* S1 */
    #define PUSH1_BV          BV(4)
    #define PUSH1_SBIT        P0_4
    #define PUSH1_POLARITY    ACTIVE_HIGH
    
    /* S2 */
    #define PUSH2_BV          BV(1)
    #define PUSH2_SBIT        P0_1
    #define PUSH2_POLARITY    ACTIVE_HIGH

  • Dear Alexander Argollo,

    Thanks for the kind  reply and help.

    yes, but i also did in the same way.

    * S1 */ p1.2
    #define PUSH1_BV BV(2)
    #define PUSH1_SBIT P1_2

    #if defined (HAL_BOARD_CC2530EB_REV17)
    #define PUSH1_POLARITY ACTIVE_HIGH
    #elif defined (HAL_BOARD_CC2530EB_REV13)
    #define PUSH1_POLARITY ACTIVE_LOW
    #else
    #error Unknown Board Indentifier
    #endif

    /* S2 */  P1.3
    #define PUSH2_BV BV(3)
    #define PUSH2_SBIT P1_3
    #define PUSH2_POLARITY ACTIVE_HIGH

  • Mr Sri,

    Would you can observe that you using S1 / PUSH1_POLARITY between predefined symbol HAL_BOARD_CC2530EB_REV17. If you don't using it in your compiler options symbols please, take out (#define PUSH1_POLARITY ACTIVE_HIGH) and do as you defined in S2 or if you using including all those lines inside. 

    * S1 */ p1.2
    #define PUSH1_BV BV(2)
    #define PUSH1_SBIT P1_2
    #define PUSH1_POLARITY ACTIVE_HIGH
    
    /* S2 */ P1.3
    
    #define PUSH2_BV BV(3)
    #define PUSH2_SBIT P1_3
    #define PUSH2_POLARITY ACTIVE_HIGH
    

  • Dear Alexander Argollo,

    Yes, i remove that macro even though my s1 or s2 are not hitting the  BREAKPOINT HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )  when i press the button S1  and S2.

    always thankful to you for kind help and information.

  • I use the following codes in hal_key.c for SW6 (P1.2)

    #define HAL_KEY_CPU_PORT_1_IF P1IF
    
    /* SW_6 is at P1.2 */
    #define HAL_KEY_SW_6_PORT   P1
    #define HAL_KEY_SW_6_BIT    BV(2)
    #define HAL_KEY_SW_6_SEL    P1SEL
    #define HAL_KEY_SW_6_DIR    P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT  BV(1)
    #define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE
    
    
    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT   BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT  BV(2) /* P1IEN - P1.2 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P1IFG /* Interrupt flag at source */
    
    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )
    {
      HAL_ENTER_ISR();
    
      if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)
      {
        halProcessKeyInterrupt();
      }
    
      /*
        Clear the CPU interrupt flag for Port_0
        PxIFG has to be cleared before PxIF
      */
      HAL_KEY_SW_6_PXIFG = 0;
      HAL_KEY_CPU_PORT_1_IF = 0;
      
      CLEAR_SLEEP_MODE();
      HAL_EXIT_ISR();
    }
    

    and I also revise the following code in hal_board_cfg.h for P1.2 as HAL_PUSH_BUTTON1

    #define PUSH1_BV          BV(2)
    #define PUSH1_SBIT        P1_2

    I have no problem to trigger zclSampleSw_HandleKeys by pressing S1 in CC2531 USB dongle.

  • Sri, 

    As Mr Y.K said, you need to change, in your code, the SW6/P1.2 settings in the follow lines bellow:

    from:

    #define HAL_KEY_SW_6_EDGEBIT BV(2)

    #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */

    to:

    #define HAL_KEY_SW_6_EDGEBIT  BV(1)

    #define HAL_KEY_SW_6_IEN      IEN2  /* CPU interrupt mask register */

    I believe that after it you will trigger well the follow line (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT).

    Please let us know about your progress!

  • Dear YK,

    Thanks for the information and kind  help.

    yes i did the register setup and HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) in the same way as you did.

    and i changed the configuration in  hal_board_cfg.h for both p1.2 and p1.3 with the referenced information by you and Alexander here.

    But still i cant able to trigger the zclsampleSw_Handlekeys when i press the  S1 or S2.

    below you can see my static void zclSampleSw_HandleKeys Function. 

     

    static void zclSampleSw_HandleKeys( byte shift, byte keys )
    {

    if (keys & HAL_KEY_SW_7)
    {

    HalLedSet(LED2_BV, HAL_LED_MODE_TOGGLE); //blink hardware LED

    }

    if (keys & HAL_KEY_SW_6)
    {

    HalLedSet(LED2_BV, HAL_LED_MODE_OFF); //blink hardware LED

    }

    }

    at the end of void zclSampleSw_Init( byte task_id ) i called bdb_StartCommissioning(); as below:

     bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING);

    Please let me know were I went wrong, Thanking you.

  •  Dear Alexander,

    Sure,

    I think i have  problem in my static void zclSampleSw_HandleKeys( byte shift, byte keys ). 

    But for now i am still trying to find out were i went wrong.  I will let you know.

    thanking you.

  • Dear Sri, 

    I would like that you check it before: Do you got success to trigger (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT) and (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT) in the hal_key.c?

    if you got, please, check in the HalKeyPoll() if you got success to trigger HAL_PUSH_BUTTON1 and HAL_PUSH_BUTTON2, when you pressed S1 or S2, and the respective Keys values. You need to get SW6 Key = 0x20, when pressed S1, and SW7 Key = 0x40 when pressed S2. According you have defined in hal_key.h

     if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
      }
      if (HAL_PUSH_BUTTON2())
      {
        keys |= HAL_KEY_SW_7;
      }

  • Dear Alexander,

    yes i used 

    #define HAL_KEY_SW_6 0x20 // Button S1 if available
    #define HAL_KEY_SW_7 0x40 // Button S2 if available.

    no i dint get successful trigger even in the hal_key.c as you mentioned.

    i dint know why its not triggered when i press s1 or s2. i am still trying to solve this issue.

    thanking you for your help and assistance.

  • Do you try my steps for using SW6 (P1.2)? I had verified it on my CC2530 USB dongle.

  • Dear,

    In hal_key.c when i put  break point at HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) i can able to reach when i reach WHEN I PRESS run (F5).

    But i cant able to reach when i press button S1 or S2 IN DEVICE CC2531.

    #define HAL_KEY_SW_6 0x20 // Button S1 if available
    #define HAL_KEY_SW_7 0x40 // Button S2 if available

    CC2531:

    Button S1   -   P1.2

    Button S2   -   P1.3

    I setup registers correctly for S1 by following your code.

    Please let me know where i went wrong. thanking you.

    In zclsamplesw.c
    
    void zclSampleSw_Init( byte task_id )
    {
    bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING);
    }
    
    
    static void zclSampleSw_HandleKeys( byte shift, byte keys )
    {
    
    if (keys & HAL_KEY_SW_6)
    {
    
    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_OFF); //blink hardware LED
    
    }
    if (keys & HAL_KEY_SW_7)
    {
    
    bdb_StartCommissioning(BDB_COMMISSIONING_MODES);
    HalLedSet(LED2_BV, HAL_LED_MODE_ON); //blink hardware LED
    
    }
    
    
    
    
    
    
    
    In hal_board_cfg.c
    
    #define ACTIVE_LOW        !
    #define ACTIVE_HIGH       !!    /* double negation forces result to be '1' */
    
    
    /* S1 */
    #define PUSH1_BV          BV(1)
    #define PUSH1_SBIT        P1_2
    #define PUSH1_POLARITY    ACTIVE_HIGH
    
    /* S2 */
    #define PUSH2_BV          BV(3)
    #define PUSH2_SBIT        P1_3
    #define PUSH2_POLARITY    ACTIVE_HIGH
    
    
    
    
    
    
    
    In hal_key.c
    
    #define HAL_KEY_CPU_PORT_1_IF P1IF
    
    /* SW_6 is at P1.2 */
    #define HAL_KEY_SW_6_PORT   P1
    #define HAL_KEY_SW_6_BIT    BV(2)
    #define HAL_KEY_SW_6_SEL    P1SEL
    #define HAL_KEY_SW_6_DIR    P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT  BV(1)
    #define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE
    
    
    /* SW_6 interrupts */
    #define HAL_KEY_SW_6_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT   BV(4) /* Mask bit for all of Port_1 */
    #define HAL_KEY_SW_6_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT  BV(2) /* P0IEN - P1.2 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P1IFG /* Interrupt flag at source */
    
    
    /* SW_7 is at P1.3 */
    #define HAL_KEY_SW_7_PORT   P1
    #define HAL_KEY_SW_7_BIT    BV(3)
    #define HAL_KEY_SW_7_SEL    P1SEL
    #define HAL_KEY_SW_7_DIR    P1DIR
    
    /*S7-edge interrupt */
    #define HAL_KEY_SW_7_EDGEBIT  BV(1)
    #define HAL_KEY_SW_7_EDGE     HAL_KEY_FALLING_EDGE
    
    
    /* SW_7interrupts */
    #define HAL_KEY_SW_7_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_7_IENBIT   BV(4) /* Mask bit for all of Port_1 */
    #define HAL_KEY_SW_7_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_7_ICTLBIT  BV(3) /* P0IEN - P1.3 enable/disable bit */
    #define HAL_KEY_SW_7_PXIFG    P1IFG /* Interrupt flag at source */
    
    
    static uint8 halKeySavedKeys;     /* used to store previous key state in polling mode */
    static halKeyCBack_t pHalKeyProcessFunction;
    static uint8 HalKeyConfigured;
    bool Hal_KeyIntEnable;    
    
    void halProcessKeyInterrupt(void);
    uint8 halGetJoyKeyInput(void);
    uint8 halGetSWKeyInput(void);
    
    
       
       
    void HalKeyInit( void )
    {
      /* Initialize previous key to 0 */
      halKeySavedKeys = 0;
    
      HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT);    /* Set pin function to GPIO */
      HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */
    
        HAL_KEY_SW_7_SEL &= ~(HAL_KEY_SW_7_BIT);    /* Set pin function to GPIO */
       HAL_KEY_SW_7_DIR &= ~(HAL_KEY_SW_7_BIT);    /* Set pin direction to Input */
      
      /* Initialize callback function */
      pHalKeyProcessFunction  = NULL;
    
      /* Start with key is not configured */
      HalKeyConfigured = FALSE;
    }  
    
    
    void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
    {
      /* Enable/Disable Interrupt or */
      Hal_KeyIntEnable = interruptEnable;
    
      /* Register the callback fucntion */
      pHalKeyProcessFunction = cback;
    
      /* Determine if interrupt is enable or not */
      if (Hal_KeyIntEnable)
      {
        /* Rising/Falling edge configuratinn */
         PICTL &= ~(HAL_KEY_SW_6_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)
        PICTL |= HAL_KEY_SW_6_EDGEBIT;
      #endif
        
         /* Interrupt configuration:
         * - Enable interrupt generation at the port
         * - Enable CPU interrupt
         * - Clear any pending interrupt
         */
        HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;
        HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;
        HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);
    
    
       /* Rising/Falling edge configuratinn */
        PICTL &= ~(HAL_KEY_SW_7_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_SW_7_EDGE == HAL_KEY_FALLING_EDGE)
        PICTL |= HAL_KEY_SW_7_EDGEBIT;
      #endif
    
    	HAL_KEY_SW_7_ICTL |= HAL_KEY_SW_7_ICTLBIT;
        HAL_KEY_SW_7_IEN |= HAL_KEY_SW_7_IENBIT;
        HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT);
    
        
    
        /* Do this only after the hal_key is configured - to work with sleep stuff */
        if (HalKeyConfigured == TRUE)
        {
          osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */
        }
      }
      else    /* Interrupts NOT enabled */
      {
       // HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */
        HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT);   /* Clear interrupt enable bit */
        osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
       
         //HAL_KEY_SW_7_ICTL &= ~(HAL_KEY_SW_7_ICTLBIT); /* don't generate interrupt */
        HAL_KEY_SW_7_IEN &= ~(HAL_KEY_SW_7_IENBIT);   /* Clear interrupt enable bit */
        osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
      }
    
      /* Key now is configured */
      HalKeyConfigured = TRUE;
    }
    
    
    uint8 HalKeyRead ( void )
    {
      uint8 keys = 0;
    
      if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
      }
    
      if (HAL_PUSH_BUTTON2())  /* Key is active low */
      {
        keys |= HAL_KEY_SW_7;
      }
    
      return keys;
    }
    
    
    void HalKeyPoll (void)
    {
      uint8 keys = 0;
    
     
    
      /* If interrupts are not enabled, previous key status and current key status
       * are compared to find out if a key has changed status.
       */
      if (!Hal_KeyIntEnable)
      {
        if (keys == halKeySavedKeys)
        {
          /* Exit - since no keys have changed */
          return;
        }
        /* Store the current keys for comparation next time */
        halKeySavedKeys = keys;
      }
      else
      {
        /* Key interrupt handled here */
      }
    
      if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
      }
     if (HAL_PUSH_BUTTON2())
      {
        keys |= HAL_KEY_SW_7;
      }
      
      /* Invoke Callback if new keys were depressed */
      if (keys  && pHalKeyProcessFunction)
     {
        (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
      }
    }
    
    
    uint8 halGetSWKeyInput(void)
    {
      /* The joystick control is encoded as an analog voltage.
       * Read the JOY_LEVEL analog value and map it to joy movement.
       */
      
      uint8 ksave0 = 0;
      uint8 ksave1;
    
      /* Keep on reading the ADC until two consecutive key decisions are the same. */
      do
      {
        ksave1 = ksave0;    /* save previouse key reading */
    
         if (HAL_PUSH_BUTTON1())
        {
           ksave0 |= HAL_KEY_SW_6;
        }
        else if(HAL_PUSH_BUTTON2())
        {
          ksave0 |= HAL_KEY_SW_7;
        }
      } while (ksave0 != ksave1);
    
      return ksave0;
    }
    
    void halProcessKeyInterrupt (void)
    {
      bool valid=FALSE;
    
      if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */
        valid = TRUE;
      }
    
      if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_SW_7_PXIFG = ~(HAL_KEY_SW_7_BIT); /* Clear Interrupt Flag */
        valid = TRUE;
    
      }
    
      if (valid)
      {
        osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);
      }
    }
    void HalKeyEnterSleep ( void )
    {
    }
    
    uint8 HalKeyExitSleep ( void )
    {
      /* Wake up and read keys */
      return ( HalKeyRead () );
    }
    
    
    
    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )
    {
      HAL_ENTER_ISR();
    
      if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)
      {
        halProcessKeyInterrupt();
      
       /*
        Clear the CPU interrupt flag for Port_0
        PxIFG has to be cleared before PxIF
      */
      HAL_KEY_SW_6_PXIFG = 0;
      HAL_KEY_CPU_PORT_1_IF = 0;
      
    }
    
    
    if (HAL_KEY_SW_7_PXIFG & HAL_KEY_SW_7_BIT)
      {
        halProcessKeyInterrupt();
      
      /*
        Clear the CPU interrupt flag for Port_0
        PxIFG has to be cleared before PxIF
      */
     
      HAL_KEY_SW_7_PXIFG = 0;
      HAL_KEY_CPU_PORT_1_IF = 0;
      }
        
      CLEAR_SLEEP_MODE();
      HAL_EXIT_ISR();
    }
    
    
    #else
    
    
    void HalKeyInit(void){}
    void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){}
    uint8 HalKeyRead(void){ return 0;}
    void HalKeyPoll(void){}
    
    #endif /* HAL_KEY */