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.

CC2530: Configure button 1.7 and 1.6

Part Number: CC2530


Hello,

How can I configure HAL_KEY_SW_1 to be P1.7 and HAL_KEY_SW_2 as P1.6 in SampleLight? I know it's in hal_key.c, but I've tried setting up HAL_KEY_SW_1 as P1.7, but I can't get it to work. I would really appreciate any help.

/**************************************************************************************************
 *                                            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_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   P2
#define HAL_KEY_SW_6_BIT    BV(0)
#define HAL_KEY_SW_6_SEL    P2SEL
#define HAL_KEY_SW_6_DIR    P2DIR

/* edge interrupt */
#define HAL_KEY_SW_6_EDGEBIT  BV(0)
#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(1) /* Mask bit for all of Port_0 */
#define HAL_KEY_SW_6_ICTL     P2IEN /* Port Interrupt Control register */
#define HAL_KEY_SW_6_ICTLBIT  BV(0) /* P0IEN - P0.1 enable/disable bit */
#define HAL_KEY_SW_6_PXIFG    P2IFG /* Interrupt flag at source */
///////////////////////////////////////////////////////////////////////////
//////SW1

#define HAL_KEY_SW_1_PORT   P1
#define HAL_KEY_SW_1_BIT    BV(7)
#define HAL_KEY_SW_1_SEL    P1SEL
#define HAL_KEY_SW_1_DIR    P1DIR

/* edge interrupt */
#define HAL_KEY_SW_1_EDGEBIT  BV(0)
#define HAL_KEY_SW_1_EDGE     HAL_KEY_FALLING_EDGE


/* SW_1 interrupts */
#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     P1IEN /* Port Interrupt Control register */
#define HAL_KEY_SW_1_ICTLBIT  BV(7) /* P0IEN - P0.1 enable/disable bit */
#define HAL_KEY_SW_1_PXIFG    P1IFG /* Interrupt flag at source */




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




/**************************************************************************************************
 *                                        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_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */
  
  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 */



  /* 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
    
    PICTL &= ~(HAL_KEY_SW_1_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


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



    /* 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_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 */

    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;
    keys |= HAL_KEY_SW_1;
  }


  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 */
  }

  if (HAL_PUSH_BUTTON1())
  {
    keys |= HAL_KEY_SW_6;
    keys |= HAL_KEY_SW_1;
  }

  /* Invoke Callback if new keys were depressed */
  if (keys && (pHalKeyProcessFunction))
  {
    (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
 **************************************************************************************************/






/**************************************************************************************************
 * @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_1_PXIFG & HAL_KEY_SW_1_BIT)  /* Interrupt Flag has been set */
  {
    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_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, P1INT_VECTOR )
{
  HAL_ENTER_ISR();
  
  if (HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT)
  {
    halProcessKeyInterrupt();
  }

  /*
    Clear the CPU interrupt flag for Port_0
    PxIFG has to be cleared before PxIF
  */
  HAL_KEY_SW_1_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_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_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 */

  • At a first glance, I see you need the revise the following lines

    /* SW_1 interrupts */
    #define HAL_KEY_SW_1_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT BV(5) /* Mask bit for

    To

    /* SW_1 interrupts */
    #define HAL_KEY_SW_1_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT BV(4) /* Mask bit for

    You can refer to CC2530 user guide at www.ti.com/.../swru191f.pdf to setup register correctly.
  • I tried that, but P1.7 still doesn't work. Why is this so?
  • Do you enable keyboard interrupt in function InitBoard() in file OnBoard.c?

    /* Initialize Key stuff */
    OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;
    HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
  • I did and it still doesn't work.
  • The edge bit of P1.7 setting is wrong. You should also revise the following line

    /* edge interrupt */
    #define HAL_KEY_SW_1_EDGEBIT BV(0)

    To

    /* edge interrupt */
    #define HAL_KEY_SW_1_EDGEBIT BV(2)
  • I did that, but it still doesn't work. What do you suggest?

  • You change the register to SW6. You should do it to SW1, right?
  • I am not sure what you mean, but below is the port registers for P1.7 for SW1. Is it wrong? Why doesn't it work?

    #define HAL_KEY_SW_1_PORT P1
    #define HAL_KEY_SW_1_BIT BV(7)
    #define HAL_KEY_SW_1_SEL P1SEL
    #define HAL_KEY_SW_1_DIR P1DIR

    /* edge interrupt */
    #define HAL_KEY_SW_1_EDGEBIT BV(2)
    #define HAL_KEY_SW_1_EDGE HAL_KEY_FALLING_EDGE


    /* SW_1 interrupts */
    #define HAL_KEY_SW_1_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_1_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT BV(7) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_1_PXIFG P1IFG /* Interrupt flag at source */
  • The configurations look fine now. Try to set a breakpoint in HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) and try to trigger P1.7 to see if it hits.
  • The breakpoint isn't hit unfortunately. Why is this so and how can it be fixed?

  • Can you attach your latest hal_key.c?
  • Here's the file.

    /**************************************************************************************************
     *                                            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_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   P2
    #define HAL_KEY_SW_6_BIT    BV(0)
    #define HAL_KEY_SW_6_SEL    P2SEL
    #define HAL_KEY_SW_6_DIR    P2DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT  BV(0)
    #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(1) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL     P2IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT  BV(0) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P2IFG /* Interrupt flag at source */
    ///////////////////////////////////////////////////////////////////////////
    //////SW1
    
    #define HAL_KEY_SW_1_PORT   P1
    #define HAL_KEY_SW_1_BIT    BV(7)
    #define HAL_KEY_SW_1_SEL    P1SEL
    #define HAL_KEY_SW_1_DIR    P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_1_EDGEBIT  BV(2)
    #define HAL_KEY_SW_1_EDGE     HAL_KEY_FALLING_EDGE
    
    
    /* SW_1 interrupts */
    #define HAL_KEY_SW_1_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT   BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_1_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT  BV(7) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_1_PXIFG    P1IFG /* Interrupt flag at source */
    
    
    
    
    /**************************************************************************************************
     *                                            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);
    
    
    
    
    /**************************************************************************************************
     *                                        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_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */
      
      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 */
    
    
    
      /* 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
        
        PICTL &= ~(HAL_KEY_SW_1_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
    
    
        /* 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);
        
        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);
    
    
    
        /* 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_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 */
    
        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;
        keys |= HAL_KEY_SW_1;
      }
    
    
      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 */
      }
    
      if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
        keys |= HAL_KEY_SW_1;
      }
    
      /* Invoke Callback if new keys were depressed */
      if (keys && (pHalKeyProcessFunction))
      {
        (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
     **************************************************************************************************/
    
    
    
    
    
    
    /**************************************************************************************************
     * @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_1_PXIFG & HAL_KEY_SW_1_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_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, P1INT_VECTOR )
    {
      HAL_ENTER_ISR();
      
      if (HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT)
      {
        halProcessKeyInterrupt();
      }
    
      /*
        Clear the CPU interrupt flag for Port_0
        PxIFG has to be cleared before PxIF
      */
      HAL_KEY_SW_1_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_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_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 */
    
    
    
    
    
    /**************************************************************************************************
    **************************************************************************************************/
    
    
    
    

  • Set a breakpoint inside "if (Hal_KeyIntEnable){...}" in function HalKeyConfig() and see if it goes into the function and configure P1.7 interrupt.
  • I set a breakpoint in there and it was hit.

  • Do you disable HAL_LCD in your project compile options.
  • I believe so. Below is my compiler options.

    ISR_KEYINTERRUPT
    SECURE=1
    TC_LINKKEY_JOIN
    NV_INIT
    NV_RESTORE
    POWER_SAVING
    NWK_AUTO_POLL
    HOLD_AUTO_START
    ZTOOL_P1
    MT_TASK
    MT_APP_FUNC
    MT_SYS_FUNC
    MT_ZDO_FUNC
    xLCD_SUPPORTED=DEBUG
    MULTICAST_ENABLED=FALSE
    ZCL_READ
    ZCL_WRITE
    ZCL_REPORT
    ZCL_EZMODE
    ZCL_BASIC
    ZCL_IDENTIFY
    ZCL_ON_OFF
    ZCL_SCENES
    ZCL_GROUPS
    xZCL_LEVEL_CTRL
    ZCL_DIAGNOSTIC
    FEATURE_SYSTEM_STATS
  • Check if your application uses P1.7 to other purpose. You can check P1SEL, P1DIR, IEN2, PICTL register values to see if it is correct.
  • 1) How do I check if P1.7 is used for other purposes? I don't believe it should be.
    2) I suppose it must be the registers because if I set HAL_KEY_SW_1 or 2 to P2.0, then it definitely works because I have the correct registers for P2.0. I'm not sure how to figure out the registers on the site, www.ti.com/.../swru191f.pdf. Do you mind telling me how to look for the registers for P1.7 or any port in general from that site?
  • You can use IAR to debug the Program and pause it to check those registers.
  • Well is there a way to configure P1.7 as an input in zcl_samplelight.c? I would just use P1.7 and not HAL_KEY_SW_1. So the code would look like the below.

    if ( keys & P1_7) //P2_0 works, but P1_7 doesn't

    So how can I configure P1.7 as an input port? I know how to configure it as an output, but not input.
  • if ( keys & P1_7) ??? It's incorrect way to check p1.7.
  • It works if I say if ( keys & P2_0). How could I set P1.7 as input in zcl_samplelight.c?
  • I think it works accidentally. You should not do it in that way.
  • I tried the orgiginal SampleLight program and used HAL_KEY_SW_1 to test the button. However when my button was connected to P1.0, the breakpoint was not hit. Why is this so. Why would the breakpoint on HAL_KEY_SW_1 hit? My button wiring is correct.

  • How do you setup P1.0 as interrupt and where do you set breakpoint?

  • I actually left HAL_KEY_SW_1 the way it was. What port is it actually connected to on the CC2530? I set a breakpoint at zclSampleLight_OnOff = zclSampleLight_OnOff ? LIGHT_OFF : LIGHT_ON;

  • Try to set breakpoint in
    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) and trigger SW1 to see if it hits first.
  • I set a breakpoint and it is always constantly being hit without me having to press the P1.7 button. This is strange that the breakpoint is hit, but I P1.7 isn't working. Why is this so and how can it be fixed?

  • Do you check P1IFG value to know which pin triggers ISR?

  • I am not sure how to do that. How do I check the P1IFG value to figure out which pin triggers ISR? I don't have anything, but the P1.7 connected to the CC2530. Also do you know if there may be any other problems that could relate to this issue?
  • When it hits ISR breakpoint, you can add P1IFG in debug watch window to check its value.
  • I get the value 'v' (0x76) the first time the breakpoint is hit. Then it immediately hits the breakpoint every single time and it outputs a value of'\0' (0x00).
  • 0x76 means P1. 1, P1.2, P1.4,P1.5,P1.6 trigger ISR. How do you deal with those pins?
  • I only use pin P1.2 to control the direction of a motor, but I don't use anything else. I didn't setup any of the other pins up. The only pins I need to use for the CC2530 are P0.5, P1.2, P1.3, P1.6, P1.7, P2.0. How can I disable everything else?
  • When ISR is triggered, please check what value of P1IEN is?
  • I get the value of '€' (0x80) everytime the breakpoint is hit.
  • I find another bug in your code. You should revise "#define HAL_KEY_CPU_PORT_1_IF P0IF" to "#define HAL_KEY_CPU_PORT_1_IF P1IF"
  • Now the ISR is hit when I press P1.7. However in the zcl_samplelight.c, the breakpoint isn't hit. Why is this so and how can it be fixed?


    THE BREAKPOINT IN THIS CODE ISN'T BEING HIT IN zcl_samplelight.c:

    static void zclSampleLight_HandleKeys( byte shift, byte keys )
    {
    if ( keys & HAL_KEY_SW_1 )
    {
    KeyPressCnt=0;
    osal_start_timerEx( zclSampleLight_TaskID, KEYHOLD_EVT, 100);

    giLightScreenMode = LIGHT_MAINMODE;

    // toggle local light immediately
    zclSampleLight_OnOff = zclSampleLight_OnOff ? LIGHT_OFF : LIGHT_ON;
    #ifdef ZCL_LEVEL_CTRL
    zclSampleLight_LevelCurrentLevel = zclSampleLight_OnOff ? zclSampleLight_LevelOnLevel : ATTR_LEVEL_MIN_LEVEL;
    #endif

    for(int i=0; i<10; i++){
    Onboard_wait(10000);
    }
    }
  • You should replace the following code in HalKeyPoll

    if (HAL_PUSH_BUTTON1())
    {
    keys |= HAL_KEY_SW_6;
    keys |= HAL_KEY_SW_1;
    }

    with

    if (HAL_PUSH_BUTTON1())
    {
    keys |= HAL_KEY_SW_6;
    }
    if (P1_7==0)
    {
    keys |= HAL_KEY_SW_1;
    }
  • Thank you so much. It works now! I just have one last question, when I try to set HAL_KEY_SW_1 (will use HAL_KEY_SW_2 later) to P1.6 in the code below, the button doesn't work and ISR isn't hit. I'm pretty sure one of the values is wrong. Can you point out which value may be incorrect for P1.6?

    #define HAL_KEY_SW_1_PORT P1
    #define HAL_KEY_SW_1_BIT BV(6)
    #define HAL_KEY_SW_1_SEL P1SEL
    #define HAL_KEY_SW_1_DIR P1DIR

    /* edge interrupt */
    #define HAL_KEY_SW_1_EDGEBIT BV(2)
    #define HAL_KEY_SW_1_EDGE HAL_KEY_FALLING_EDGE


    /* SW_1 interrupts */
    #define HAL_KEY_SW_1_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_1_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT BV(6) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_1_PXIFG P1IFG /* Interrupt flag at source */
  • If you set breakpoint in ISR and trigger P1.6, does it hit?
  • Do you mean it doesn't hit HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) when you trigger P1.6?
  • It doesn't hit HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ) when I trigger P1.6.
  • Do you define HAL_KEY_CPU_PORT_1_IF as P1IF?
  • Can you attach your latest hal_key.c using P1.6?
  • I changed it so HAL_KEY_SW_2 uses P1.6 now and HAL_KEY_SW_1 still uses P1.7. Below is the code.

    /**************************************************************************************************
     *                                            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_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 P1IF
    #define HAL_KEY_CPU_PORT_2_IF P2IF
    
    /* SW_6 is at P0.1 */
    #define HAL_KEY_SW_6_PORT   P2
    #define HAL_KEY_SW_6_BIT    BV(0)
    #define HAL_KEY_SW_6_SEL    P2SEL
    #define HAL_KEY_SW_6_DIR    P2DIR
    
    /* 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      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT   BV(1) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL     P2IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT  BV(0) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P2IFG /* Interrupt flag at source */
    ///////////////////////////////////////////////////////////////////////////
    
    #define HAL_KEY_SW_1_PORT P1
    #define HAL_KEY_SW_1_BIT BV(7)
    #define HAL_KEY_SW_1_SEL P1SEL
    #define HAL_KEY_SW_1_DIR P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_1_EDGEBIT BV(2)
    #define HAL_KEY_SW_1_EDGE HAL_KEY_FALLING_EDGE
    
    
    /* SW_1 interrupts */
    #define HAL_KEY_SW_1_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_1_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT BV(7) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_1_PXIFG P1IFG /* Interrupt flag at source */
    
    ///////////////////////////////////////////////////////////////////////////
    
    #define HAL_KEY_SW_2_PORT P1
    #define HAL_KEY_SW_2_BIT BV(6)
    #define HAL_KEY_SW_2_SEL P1SEL
    #define HAL_KEY_SW_2_DIR P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_2_EDGEBIT BV(2)
    #define HAL_KEY_SW_2_EDGE HAL_KEY_FALLING_EDGE
    
    
    /* SW_1 interrupts */
    #define HAL_KEY_SW_2_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_2_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_2_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_2_ICTLBIT BV(6) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_2_PXIFG P1IFG /* Interrupt flag at source */
    
    
    
    #define port P1_7
    #define port2 P1_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);
    
    
    
    
    /**************************************************************************************************
     *                                        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_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */
      
      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 */
    
    
      /* 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
        
        PICTL &= ~(HAL_KEY_SW_1_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
    
    
    
        /* 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);
        
        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);
    
    
    
        /* 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_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 */
    
    
        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 (port==0)
      {
        keys |= HAL_KEY_SW_1;
      }
      
      if (port2==0)
      {
        keys |= HAL_KEY_SW_2;
      }
    
    
      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 */
      }
    
      if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
      }
      
      if (port==0)
      {
        keys |= HAL_KEY_SW_1;
      }
      
      if (port2==0)
      {
        keys |= HAL_KEY_SW_2;
      }
    
      /* Invoke Callback if new keys were depressed */
      if (keys && (pHalKeyProcessFunction))
      {
        (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
     **************************************************************************************************/
    
    
    
    
    
    
    /**************************************************************************************************
     * @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_1_PXIFG & HAL_KEY_SW_1_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_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, P1INT_VECTOR )
    {
      HAL_ENTER_ISR();
    
      if (HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT)
      {
        halProcessKeyInterrupt();
      }
      
      if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)
      {
        halProcessKeyInterrupt();
      }
    
      /*
        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_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_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_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 */
    
    
    
    
    
    /**************************************************************************************************
    **************************************************************************************************/

  • You don't add code for SW2 in HalKeyInit HalKeyConfig
  • Ok. Now I've added the code for SW2. However ISR isn't hit when I press P1.6. Why is this so?

    /**************************************************************************************************
     *                                            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_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 P1IF
    #define HAL_KEY_CPU_PORT_2_IF P2IF
    
    /* SW_6 is at P0.1 */
    #define HAL_KEY_SW_6_PORT   P2
    #define HAL_KEY_SW_6_BIT    BV(0)
    #define HAL_KEY_SW_6_SEL    P2SEL
    #define HAL_KEY_SW_6_DIR    P2DIR
    
    /* 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      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_6_IENBIT   BV(1) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_6_ICTL     P2IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_6_ICTLBIT  BV(0) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P2IFG /* Interrupt flag at source */
    ///////////////////////////////////////////////////////////////////////////
    
    #define HAL_KEY_SW_1_PORT P1
    #define HAL_KEY_SW_1_BIT BV(7)
    #define HAL_KEY_SW_1_SEL P1SEL
    #define HAL_KEY_SW_1_DIR P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_1_EDGEBIT BV(2)
    #define HAL_KEY_SW_1_EDGE HAL_KEY_FALLING_EDGE
    
    
    /* SW_1 interrupts */
    #define HAL_KEY_SW_1_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_1_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT BV(7) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_1_PXIFG P1IFG /* Interrupt flag at source */
    
    ///////////////////////////////////////////////////////////////////////////
    
    #define HAL_KEY_SW_2_PORT P1
    #define HAL_KEY_SW_2_BIT BV(6)
    #define HAL_KEY_SW_2_SEL P1SEL
    #define HAL_KEY_SW_2_DIR P1DIR
    
    /* edge interrupt */
    #define HAL_KEY_SW_2_EDGEBIT BV(2)
    #define HAL_KEY_SW_2_EDGE HAL_KEY_FALLING_EDGE
    
    
    /* SW_1 interrupts */
    #define HAL_KEY_SW_2_IEN IEN2 /* CPU interrupt mask register */
    #define HAL_KEY_SW_2_IENBIT BV(4) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_2_ICTL P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_2_ICTLBIT BV(6) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_2_PXIFG P1IFG /* Interrupt flag at source */
    
    
    
    #define port P1_7
    #define port2 P1_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);
    
    
    
    
    /**************************************************************************************************
     *                                        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_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */
      
      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 */
    
    
      /* 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
        
        PICTL &= ~(HAL_KEY_SW_1_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
        
        PICTL &= ~(HAL_KEY_SW_2_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_SW_2_EDGE == HAL_KEY_FALLING_EDGE)
        PICTL |= HAL_KEY_SW_2_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);
        
        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);
    
    
    
        /* 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_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 */
    
    
        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 (port==0)
      {
        keys |= HAL_KEY_SW_1;
      }
      
      if (port2==0)
      {
        keys |= HAL_KEY_SW_2;
      }
    
    
      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 */
      }
    
      if (HAL_PUSH_BUTTON1())
      {
        keys |= HAL_KEY_SW_6;
      }
      
      if (port==0)
      {
        keys |= HAL_KEY_SW_1;
      }
      
      if (port2==0)
      {
        keys |= HAL_KEY_SW_2;
      }
    
      /* Invoke Callback if new keys were depressed */
      if (keys && (pHalKeyProcessFunction))
      {
        (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
     **************************************************************************************************/
    
    
    
    
    
    
    /**************************************************************************************************
     * @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_1_PXIFG & HAL_KEY_SW_1_BIT)  /* Interrupt Flag has been set */
      {
        HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* 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 = ~(HAL_KEY_SW_2_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, P1INT_VECTOR )
    {
      HAL_ENTER_ISR();
    
      if (HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT)
      {
        halProcessKeyInterrupt();
      }
      
      if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)
      {
        halProcessKeyInterrupt();
      }
    
      /*
        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_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_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_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 */
    
    
    
    
    
    /**************************************************************************************************
    **************************************************************************************************/