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.

Initial Key Interrupt error

Other Parts Discussed in Thread: CC2530

Dear YK Chen,

i am working with Z-stack1.2.2a , i changed SampleSw and i want to have 2 LED and 2 Switch .

i want to use keys in Interrupt mode , and i changed key.c , hal_board_CFG , ...

when i press key 1 , correct working and led1 turn ON/OFF , but when i press key 2  both LEDs change,

please help me!

  • Key1 --> P0.7
    Key2 --> P0.6

    LED1 --> P0.1
    LED2 -->P0.4
  • What HW do use?

  • CC2530DK or EVB from other vendors?
  • CC2530DK , I design a Modul whith CC2530 IC and i us it
  • Do Interrupt related on External OSC 32.768 KHz ?
  • Can you show me your hal_key.c?
  • /**************************************************************************************************
    * INCLUDES
    **************************************************************************************************/
    #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_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    #include "osal.h"

    #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 P1IF



    /* SW_1 is at P0.1 */
    #define HAL_KEY_SW_1_PORT P0
    #define HAL_KEY_SW_1_BIT BV(0)
    #define HAL_KEY_SW_1_SEL P0SEL
    #define HAL_KEY_SW_1_DIR P0DIR
    #define HAL_KEY_SW_1_EDGEBIT BV(0)
    #define HAL_KEY_SW_1_EDGE HAL_KEY_FALLING_EDGE
    #define HAL_KEY_SW_1_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_1_ICTL P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT BV(0) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_1_PXIFG P0IFG /* Interrupt flag at source */


    /* SW_2 is at P0.5 */
    #define HAL_KEY_SW_2_PORT P0
    #define HAL_KEY_SW_2_BIT BV(5)
    #define HAL_KEY_SW_2_SEL P0SEL
    #define HAL_KEY_SW_2_DIR P0DIR
    #define HAL_KEY_SW_2_EDGEBIT BV(0)
    #define HAL_KEY_SW_2_EDGE HAL_KEY_FALLING_EDGE
    #define HAL_KEY_SW_2_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_SW_2_IENBIT BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_2_ICTL P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_2_ICTLBIT BV(5) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_2_PXIFG P0IFG /* Interrupt flag at source */


    /* SW_3 is at P0.7 */
    #define HAL_KEY_SW_3_PORT P0
    #define HAL_KEY_SW_3_BIT BV(7)
    #define HAL_KEY_SW_3_SEL P0SEL
    #define HAL_KEY_SW_3_DIR P0DIR
    #define HAL_KEY_SW_3_EDGEBIT BV(0)
    #define HAL_KEY_SW_3_EDGE HAL_KEY_FALLING_EDGE
    #define HAL_KEY_SW_3_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_SW_3_IENBIT BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_3_ICTL P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_3_ICTLBIT BV(7) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_3_PXIFG P0IFG /* Interrupt flag at source */

    /* SW_4 is at P1.6 */
    #define HAL_KEY_SW_4_PORT P1
    #define HAL_KEY_SW_4_BIT BV(6)
    #define HAL_KEY_SW_4_SEL P1SEL
    #define HAL_KEY_SW_4_DIR P1DIR
    #define HAL_KEY_SW_4_EDGEBIT BV(2)
    #define HAL_KEY_SW_4_EDGE HAL_KEY_RISING_EDGE
    #define HAL_KEY_SW_4_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_4_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_4_ICTL P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_4_ICTLBIT BV(6) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_4_PXIFG P0IFG /* Interrupt flag at source */

    /* SW_5 is at P1.7 */
    #define HAL_KEY_SW_5_PORT P1
    #define HAL_KEY_SW_5_BIT BV(7)
    #define HAL_KEY_SW_5_SEL P1SEL
    #define HAL_KEY_SW_5_DIR P1DIR
    #define HAL_KEY_SW_5_EDGEBIT BV(2)
    #define HAL_KEY_SW_5_EDGE HAL_KEY_RISING_EDGE
    #define HAL_KEY_SW_5_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_5_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_5_ICTL P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_5_ICTLBIT BV(7) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_5_PXIFG P0IFG /* Interrupt flag at source */

    /**************************************************************************************************
    * 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);

    /**************************************************************************************************
    * @fn HalKeyInit
    *
    * @brief Initilize Key Service
    *
    * @param none
    *
    * @return None
    **************************************************************************************************/
    void HalKeyInit( void )
    {
    /* Initialize previous key to 0 */
    halKeySavedKeys = 0;

    HAL_KEY_SW_1_SEL &= ~(HAL_KEY_SW_1_BIT); /* Set pin function to GPIO */
    HAL_KEY_SW_1_DIR &= ~(HAL_KEY_SW_1_BIT); /* Set pin direction to Input */

    HAL_KEY_SW_2_SEL &= ~(HAL_KEY_SW_2_BIT); /* Set pin function to GPIO */
    HAL_KEY_SW_2_DIR &= ~(HAL_KEY_SW_2_BIT); /* Set pin direction to Input */

    HAL_KEY_SW_3_SEL &= ~(HAL_KEY_SW_3_BIT); /* Set pin function to GPIO */
    HAL_KEY_SW_3_DIR &= ~(HAL_KEY_SW_3_BIT); /* Set pin direction to Input */

    HAL_KEY_SW_4_SEL &= ~(HAL_KEY_SW_4_BIT); /* Set pin function to GPIO */
    HAL_KEY_SW_4_DIR &= ~(HAL_KEY_SW_4_BIT); /* Set pin direction to Input */

    HAL_KEY_SW_5_SEL &= ~(HAL_KEY_SW_5_BIT); /* Set pin function to GPIO */
    HAL_KEY_SW_5_DIR &= ~(HAL_KEY_SW_5_BIT); /* Set pin direction to Input */


    // 3-State Poert1  and Pulldown Port 0
    P0INP &= ~0xA1;
    P1INP |= 0xC0;
    P2INP |= 0x20;



    /* 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_1_EDGEBIT); /* Clear the edge bit */
    PICTL &= ~(HAL_KEY_SW_2_EDGEBIT); /* Clear the edge bit */
    PICTL &= ~(HAL_KEY_SW_3_EDGEBIT); /* Clear the edge bit */
    PICTL &= ~(HAL_KEY_SW_4_EDGEBIT); /* Clear the edge bit */
    PICTL &= ~(HAL_KEY_SW_5_EDGEBIT); /* Clear the edge bit */

    /* For falling edge, the bit must be set. */
    #if (HAL_KEY_SW_1_EDGE == HAL_KEY_FALLING_EDGE)
    PICTL |= HAL_KEY_SW_1_EDGEBIT;
    #endif
    #if (HAL_KEY_SW_2_EDGE == HAL_KEY_FALLING_EDGE)
    PICTL |= HAL_KEY_SW_2_EDGEBIT;
    #endif
    #if (HAL_KEY_SW_3_EDGE == HAL_KEY_FALLING_EDGE)
    PICTL |= HAL_KEY_SW_3_EDGEBIT;
    #endif
    #if (HAL_KEY_SW_4_EDGE == HAL_KEY_FALLING_EDGE)
    PICTL |= HAL_KEY_SW_4_EDGEBIT;
    #endif
    #if (HAL_KEY_SW_5_EDGE == HAL_KEY_FALLING_EDGE)
    PICTL |= HAL_KEY_SW_5_EDGEBIT;
    #endif


    /* Interrupt configuration:
    * - Enable interrupt generation at the port
    * - Enable CPU interrupt
    * - Clear any pending interrupt
    */
    HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT;
    HAL_KEY_SW_1_IEN |= HAL_KEY_SW_1_IENBIT;
    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT);

    HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT;
    HAL_KEY_SW_2_IEN |= HAL_KEY_SW_2_IENBIT;
    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT);

    HAL_KEY_SW_3_ICTL |= HAL_KEY_SW_3_ICTLBIT;
    HAL_KEY_SW_3_IEN |= HAL_KEY_SW_3_IENBIT;
    HAL_KEY_SW_3_PXIFG = ~(HAL_KEY_SW_3_BIT);

    HAL_KEY_SW_4_ICTL |= HAL_KEY_SW_4_ICTLBIT;
    HAL_KEY_SW_4_IEN |= HAL_KEY_SW_4_IENBIT;
    HAL_KEY_SW_4_PXIFG = ~(HAL_KEY_SW_4_BIT);

    HAL_KEY_SW_5_ICTL |= HAL_KEY_SW_5_ICTLBIT;
    HAL_KEY_SW_5_IEN |= HAL_KEY_SW_5_IENBIT;
    HAL_KEY_SW_5_PXIFG = ~(HAL_KEY_SW_5_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_1_ICTL &= ~(HAL_KEY_SW_1_ICTLBIT); /* don't generate interrupt */
    HAL_KEY_SW_1_IEN &= ~(HAL_KEY_SW_1_IENBIT); /* Clear interrupt enable bit */

    HAL_KEY_SW_2_ICTL &= ~(HAL_KEY_SW_2_ICTLBIT); /* don't generate interrupt */
    HAL_KEY_SW_2_IEN &= ~(HAL_KEY_SW_2_IENBIT); /* Clear interrupt enable bit */

    HAL_KEY_SW_3_ICTL &= ~(HAL_KEY_SW_3_ICTLBIT); /* don't generate interrupt */
    HAL_KEY_SW_3_IEN &= ~(HAL_KEY_SW_3_IENBIT); /* Clear interrupt enable bit */

    HAL_KEY_SW_4_ICTL &= ~(HAL_KEY_SW_4_ICTLBIT); /* don't generate interrupt */
    HAL_KEY_SW_4_IEN &= ~(HAL_KEY_SW_4_IENBIT); /* Clear interrupt enable bit */

    HAL_KEY_SW_5_ICTL &= ~(HAL_KEY_SW_5_ICTLBIT); /* don't generate interrupt */
    HAL_KEY_SW_5_IEN &= ~(HAL_KEY_SW_5_IENBIT); /* Clear interrupt enable bit */


    osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
    }

    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_1;
    }

    if (HAL_PUSH_BUTTON2())
    {
    // keys |= HAL_KEY_SW_2;
    }

    if (HAL_PUSH_BUTTON3())
    {
    // keys |= HAL_KEY_SW_3;
    }

    if (HAL_PUSH_BUTTON4())
    {
    // keys |= HAL_KEY_SW_4;
    }

    if (HAL_PUSH_BUTTON5())
    {
    // keys |= HAL_KEY_SW_5;
    }

    return keys;
    }


    /**************************************************************************************************
    * @fn HalKeyPoll
    *
    * @brief Called by hal_driver to poll the keys
    *
    * @param None
    *
    * @return None
    **************************************************************************************************/
    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 */


    }


    /* Key interrupt handled here */

    if (HAL_PUSH_BUTTON1())
    {
    keys |= HAL_KEY_SW_1;
    }
    if (HAL_PUSH_BUTTON2())
    {
    //keys |= HAL_KEY_SW_2;
    HalLedSet ( HAL_LED_2, HAL_LED_MODE_TOGGLE );
    }
    if (HAL_PUSH_BUTTON3())
    {
    // keys |= HAL_KEY_SW_3;
    }
    if (HAL_PUSH_BUTTON4())
    {
    // keys |= HAL_KEY_SW_4;
    HalLedSet ( HAL_LED_3, HAL_LED_MODE_TOGGLE );
    }
    if (HAL_PUSH_BUTTON5())
    {
    // keys |= HAL_KEY_SW_5;
    }



    /* Invoke Callback if new keys were depressed */
    if (keys && (pHalKeyProcessFunction))
    {
    (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
    }

    }

    /**************************************************************************************************
    * @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_1_PXIFG & HAL_KEY_SW_1_BIT) /* Interrupt Flag has been set */
    {
    HAL_KEY_SW_1_PXIFG =0; /* Clear Interrupt Flag */
    valid = TRUE;
    }

    if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT) /* Interrupt Flag has been set */
    {
    HAL_KEY_SW_2_PXIFG = 0; /* Clear Interrupt Flag */
    valid = TRUE;
    }

    if (HAL_KEY_SW_3_PXIFG & HAL_KEY_SW_3_BIT) /* Interrupt Flag has been set */
    {
    HAL_KEY_SW_3_PXIFG = 0; /* Clear Interrupt Flag */
    valid = TRUE;
    }


    if (HAL_KEY_SW_4_PXIFG & HAL_KEY_SW_4_BIT) /* Interrupt Flag has been set */
    {
    HAL_KEY_SW_4_PXIFG = 0; /* Clear Interrupt Flag */
    valid = TRUE;
    }


    if (HAL_KEY_SW_5_PXIFG & HAL_KEY_SW_5_BIT) /* Interrupt Flag has been set */
    {
    HAL_KEY_SW_5_PXIFG = 0; /* 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( halKeyPort0Isr, P0INT_VECTOR )
    {


    HAL_ENTER_ISR();
    if (HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT)
    {
    halProcessKeyInterrupt();
    HAL_KEY_SW_1_PXIFG = 0;
    HAL_KEY_SW_2_PXIFG = 0;
    HAL_KEY_SW_3_PXIFG = 0;

    HAL_KEY_CPU_PORT_0_IF = 0;

    }


    if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)
    {
    halProcessKeyInterrupt();
    HAL_KEY_SW_1_PXIFG = 0;
    HAL_KEY_SW_2_PXIFG = 0;
    HAL_KEY_SW_3_PXIFG = 0;

    HAL_KEY_CPU_PORT_0_IF = 0;

    }


    if (HAL_KEY_SW_3_PXIFG & HAL_KEY_SW_3_BIT)
    {
    halProcessKeyInterrupt();
    HAL_KEY_SW_1_PXIFG = 0;
    HAL_KEY_SW_2_PXIFG = 0;
    HAL_KEY_SW_3_PXIFG = 0;

    HAL_KEY_CPU_PORT_0_IF = 0;

    }




    /*
    Clear the CPU interrupt flag for Port_0
    PxIFG has to be cleared before PxIF
    */
    HAL_KEY_SW_1_PXIFG = 0;
    HAL_KEY_SW_2_PXIFG = 0;
    HAL_KEY_SW_3_PXIFG = 0;

    HAL_KEY_CPU_PORT_0_IF = 0;

    CLEAR_SLEEP_MODE();
    HAL_EXIT_ISR();
    }
    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )
    {


    HAL_ENTER_ISR();


    if (HAL_KEY_SW_4_PXIFG & HAL_KEY_SW_4_BIT)
    {
    halProcessKeyInterrupt();
    HAL_KEY_SW_4_PXIFG = 0;
    HAL_KEY_SW_5_PXIFG = 0;
    HAL_KEY_CPU_PORT_1_IF = 0;

    }

    if (HAL_KEY_SW_5_PXIFG & HAL_KEY_SW_5_BIT)
    {
    halProcessKeyInterrupt();
    HAL_KEY_SW_4_PXIFG = 0;
    HAL_KEY_SW_5_PXIFG = 0;
    HAL_KEY_CPU_PORT_1_IF = 0;

    }


    /*
    Clear the CPU interrupt flag for Port_0
    PxIFG has to be cleared before PxIF
    */
    HAL_KEY_SW_4_PXIFG = 0;
    HAL_KEY_SW_5_PXIFG = 0;
    HAL_KEY_CPU_PORT_1_IF = 0;

    CLEAR_SLEEP_MODE();
    HAL_EXIT_ISR();
    }






    /**************************************************************************************************
    **************************************************************************************************/

  • I don't see any key mapped to P0.6 in your hal_key.c. You should check this first.

  • Sorry ,
    Switch 1 --> P0.0
    Switch2 --> P0.5
    Switch3 -->P0.7
    Switch4 --> P1.6
    Switch5 --> P1.7


    key1 connect to micro switch
    key2 connect to Switch2 & Switch4 (Because i want to have Rising and falling Edge)
    key3 connect to Switch3 & Switch5 (Because i want to have Rising and falling Edge)
  • I am confused by your descriptions. Can you elaborate what your problem is?
  • Interrupt Not working correctly :(
  • Which one doesn't work? HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ) or HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )?