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.

CC2541 configure switch on P1_7 on customized board

Other Parts Discussed in Thread: CC2541, CC2540

Hii,

We have customized our PCB based on CC2541 and refers to keyfob schematic.  We relocate switch from P0.1 to P1.7, But unable to operate it. It was working well at P0.1 on keyfob but not at P1.7 on customized board.

I have made the following changes in ,

1) hal_key.c ;

Note : current config is encircled in BLUE and earlier configuration in RED.

2) themometer.c

Note : Current config is encircled in BLUE.

I hope I made my point clear, if not please let me know what left.

Thanks in advance.

Regards,

Kumar Karan Jain

  • Don't see you attach or post your code. Please post again.

  • Here are the screenshots from code.

    Sorry for inconvenience caused.

    Thanks for your reply YiKai Chen.

    Regards,

    Kumar Karan Jain

  • jk

    Hii ,


    Thanks for your reply.

    Here I have attached both files again, to ensure this time they are done correctly.  If anything left to mention or clear, please let me know.

    Sorry for inconvenience caused earlier.

    Regards,

    Kumar Karan Jain

  • These registers seem correct. If you set a breakpoint in P1 ISR and press Switch P1.7, does it stop inISR?
  • When set a breakpoint at ISR and run debugger then it got stuck.Don't know why?
  • What do you mean get stuck? Can you show me your code of P1 ISR?
  • Here is the screenshot of the routine.

    I am following the thermometer default program, can be found at default path :-

    C:\Texas Instruments\BLE-CC254x-1.4.0\Projects\ble\Thermometer\CC2541DB.

  • I mean that you should set a breakpoint at p1 ISR function in hal_key.c and check if it stops there.
  • Okey,Sorry I misunderstood it.
    I have placed a breakpoint there and it went inside the routine when switch is pressed.
  • That means your resisters settings are correct.
  • Okey, So what could be missing ?

    Please help me to tackle issue.
  • Can you attach your hal_key.c?
  • Please find below the hal_key.c,

    /**************************************************************************************************
      Filename:       hal_key.c
      Revised:        $Date: 2013-09-20 11:53:10 -0700 (Fri, 20 Sep 2013) $
      Revision:       $Revision: 35401 $

      Description:    This file contains the interface to the HAL KEY Service.


      Copyright 2006-2013 Texas Instruments Incorporated. All rights reserved.

      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.

      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    /*********************************************************************
     NOTE: If polling is used, the hal_driver task schedules the KeyRead()
           to occur every 100ms.  This should be long enough to naturally
           debounce the keys.  The KeyRead() function remembers the key
           state of the previous poll and will only return a non-zero
           value if the key state changes.

     NOTE: If interrupts are used, the KeyRead() function is scheduled
           25ms after the interrupt occurs by the ISR.  This delay is used
           for key debouncing.  The ISR disables any further Key interrupt
           until KeyRead() is executed.  KeyRead() will re-enable Key
           interrupts after executing.  Unlike polling, when interrupts
           are enabled, the previous key state is not remembered.  This
           means that KeyRead() will return the current state of the keys
           (not a change in state of the keys).

     NOTE: If interrupts are used, the KeyRead() fucntion is scheduled by
           the ISR.  Therefore, the joystick movements will only be detected
           during a pushbutton interrupt caused by S1 or the center joystick
           pushbutton.

     NOTE: When a switch like S1 is pushed, the S1 signal goes from a normally
           high state to a low state.  This transition is typically clean.  The
           duration of the low state is around 200ms.  When the signal returns
           to the high state, there is a high likelihood of signal bounce, which
           causes a unwanted interrupts.  Normally, we would set the interrupt
           edge to falling edge to generate an interrupt when S1 is pushed, but
           because of the signal bounce, it is better to set the edge to rising
           edge to generate an interrupt when S1 is released.  The debounce logic
           can then filter out the signal bounce.  The result is that we typically
           get only 1 interrupt per button push.  This mechanism is not totally
           foolproof because occasionally, signal bound occurs during the falling
           edge as well.  A similar mechanism is used to handle the joystick
           pushbutton on the DB.  For the EB, we do not have independent control
           of the interrupt edge for the S1 and center joystick pushbutton.  As
           a result, only one or the other pushbuttons work reasonably well with
           interrupts.  The default is the make the S1 switch on the EB work more
           reliably.

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

    /**************************************************************************************************
     *                                            INCLUDES
     **************************************************************************************************/
    #include "hal_mcu.h"
    #include "hal_defs.h"
    #include "hal_types.h"
    #include "hal_drivers.h"
    #include "hal_adc.h"
    #include "hal_key.h"
    #include "osal.h"

    #if (defined HAL_KEY) && (HAL_KEY == TRUE)

    /**************************************************************************************************
     *                                            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_2_IF P2IF
    #define HAL_KEY_CPU_PORT_1_IF P1IF
    #if defined ( CC2540_MINIDK )
    /* SW_1 is at P0.0 */
    #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

    /* SW_2 is at P1.7 earlier at P0.1 */
    #define HAL_KEY_SW_2_PORT   P1   //P0
    #define HAL_KEY_SW_2_BIT    BV(7)//BV(1)
    #define HAL_KEY_SW_2_SEL    P1SEL//P0SEL
    #define HAL_KEY_SW_2_DIR    P1DIR//P0DIR

    #define HAL_KEY_SW_1_IEN      IEN1  /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_ICTL     P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT  BV(0) /* P0IEN - P0.0 enable/disable bit */
    #define HAL_KEY_SW_1_IENBIT   BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_1_PXIFG    P0IFG /* Interrupt flag at source */

    #define HAL_KEY_SW_2_IEN      IEN2 //IEN1  /* CPU interrupt mask register */
    #define HAL_KEY_SW_2_ICTL     P1IEN//P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_2_ICTLBIT  BV(7)//BV(1) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_2_IENBIT   BV(4)//BV(5) /* Mask bit for all of Port_0 */
    #define HAL_KEY_SW_2_PXIFG    P1IFG//P0IFG /* Interrupt flag at source */

    #define HAL_KEY_SW_1_EDGEBIT  BV(2)//BV(0)

    #else

    /* SW_6 is at P0.1 */
    #define HAL_KEY_SW_6_PORT   P0
    #define HAL_KEY_SW_6_BIT    BV(1)
    #define HAL_KEY_SW_6_SEL    P0SEL
    #define HAL_KEY_SW_6_DIR    P0DIR

    /* 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      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(1) /* P0IEN - P0.1 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P0IFG /* 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

    #endif

    /**************************************************************************************************
     *                                            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 )
    {
      halKeySavedKeys = 0;  // Initialize previous key to 0.

    #if defined ( CC2540_MINIDK )
      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 */
    #else
      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_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */
      HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input */

      P2INP |= PUSH2_BV;  /* Configure GPIO tri-state. */
    #endif

      /* Initialize callback function */
      pHalKeyProcessFunction  = NULL;

      /* Start with key is not configured */
      HalKeyConfigured = FALSE;

    #if defined ( CC2540_MINIDK )
      /* Rising/Falling edge configuratinn */
      PICTL |= HAL_KEY_SW_1_EDGEBIT;   /* Set the edge bit to set falling edge to give int */
      HAL_KEY_SW_1_IEN |= ( HAL_KEY_SW_1_IENBIT | HAL_KEY_SW_2_IENBIT );   /* enable CPU interrupt */
    #endif
    }


    /**************************************************************************************************
     * @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)
      {
    #if defined ( CC2540_MINIDK )
        HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT; /* enable interrupt generation at port */    
        HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT);  /* Clear any pending interrupt */
        HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT; /* enable interrupt generation at port */
        HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT);  /* Clear any pending interrupt */

    #else
        /* 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 */
        HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT);    /* Clear the edge bit */
        /* For falling edge, the bit must be set. */
      #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)
        HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;
      #endif


        /* Interrupt configuration:
         * - Enable interrupt generation at the port
         * - Enable CPU interrupt
         * - Clear any pending interrupt
         */
        HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;
        HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;
        HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);
    #endif // !CC2540_MINIDK

        /* 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 */
      {
    #if defined ( CC2540_MINIDK )
        HAL_KEY_SW_1_ICTL &= ~(HAL_KEY_SW_1_ICTLBIT); /* don't generate interrupt */
        HAL_KEY_SW_2_ICTL &= ~(HAL_KEY_SW_2_ICTLBIT); /* don't generate interrupt */
    #else
        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 */
    #endif  // !CC2540_MINIDK

        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 defined (CC2540_MINIDK)
      if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_1;
      }
      if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_2;
      }
    #else
    #ifdef HAL_BOARD_CC2530EB_REV17
      if ( (HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active high */
    #elif defined (HAL_BOARD_CC2530EB_REV13)
      if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active low */
    #endif
      {
        keys |= HAL_KEY_SW_6;
      }

      if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active low */
      {
        keys |= halGetJoyKeyInput();
      }
    #endif
      return keys;
    }


    /**************************************************************************************************
     * @fn      HalKeyPoll
     *
     * @brief   Called by hal_driver to poll the keys
     *
     * @param   None
     *
     * @return  None
     **************************************************************************************************/
    void HalKeyPoll (void)
    {
      uint8 keys = 0;
      uint8 notify = 0;
    #if defined (CC2540_MINIDK)
      if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_1;
      }
      if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_2;
      }
    #else
      if (!(HAL_KEY_SW_6_PORT & HAL_KEY_SW_6_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_6;
      }

      if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  /* Key is active HIGH */
      {
        keys = halGetJoyKeyInput();
      }
    #endif

      /* 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;
        }
        else
        {
          notify = 1;
        }
      }
      else
      {
        /* Key interrupt handled here */
        if (keys)
        {
          notify = 1;
        }
      }

      /* Store the current keys for comparation next time */
      halKeySavedKeys = keys;

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

      }
    }

    #if !defined ( CC2540_MINIDK )
    /**************************************************************************************************
     * @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 adc;
      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 */

        adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8);

        if ((adc >= 2) && (adc <= 38))
        {
           ksave0 |= HAL_KEY_UP;
        }
        else if ((adc >= 74) && (adc <= 88))
        {
          ksave0 |= HAL_KEY_RIGHT;
        }
        else if ((adc >= 60) && (adc <= 73))
        {
          ksave0 |= HAL_KEY_LEFT;
        }
        else if ((adc >= 39) && (adc <= 59))
        {
          ksave0 |= HAL_KEY_DOWN;
        }
        else if ((adc >= 89) && (adc <= 100))
        {
          ksave0 |= HAL_KEY_CENTER;
        }
      } while (ksave0 != ksave1);

      return ksave0;
    }
    #endif

    /**************************************************************************************************
     * @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 defined ( CC2540_MINIDK )
      if( HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) /* Interrupt Flag has been set by SW1 */
      {
        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 by SW2 */
      {
        HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear Interrupt Flag */
        valid = TRUE;
      }
    #else
      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_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;
      }
    #endif
      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 defined ( CC2540_MINIDK )
      if ((HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) || (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT))
    #else
      if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)
    #endif
      {
        halProcessKeyInterrupt();
      }

      /*
        Clear the CPU interrupt flag for Port_0
        PxIFG has to be cleared before PxIF
      */
    #if defined ( CC2540_MINIDK )
      HAL_KEY_SW_1_PXIFG = 0;
      HAL_KEY_SW_2_PXIFG = 0;
    #else
      HAL_KEY_SW_6_PXIFG = 0;
    #endif
      HAL_KEY_CPU_PORT_0_IF = 0;

      CLEAR_SLEEP_MODE();

      HAL_EXIT_ISR();

      return;
    }

    #if !defined ( CC2540_MINIDK )
    /**************************************************************************************************
     * @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();

      return;
    }
    #endif
    #else

    void HalKeyInit(void){}
    void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){}
    uint8 HalKeyRead(void){ return 0;}
    void HalKeyPoll(void){}

    #endif
    /**************************************************************************************************
    **************************************************************************************************/

  • Since you use P1.7 as input, why don't you implement HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )?
  • Now, changed the above suggested line of code but still not working.
  • Can you show how you modify HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )?
  • Here is the screenshot of HAL_ISR_FUNCTION, changes are encircled.

     

    2)

    Also made a change to hal_key.h. Please verify it, whether I made it correctly or not.

     

     

    Thanks and regards,

    Kumar Karan Jain

  • Try to use the following HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ), halProcessKeyInterrupt, HalKeyPoll and HalKeyRead.

    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )
    {
      HAL_ENTER_ISR();

      if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)
      {
        halProcessKeyInterrupt();
      }

      /*
        Clear the CPU interrupt flag for Port_1
        PxIFG has to be cleared before PxIF
      */
      HAL_KEY_SW_2_PXIFG = 0;
      HAL_KEY_CPU_PORT_1_IF = 0;

      CLEAR_SLEEP_MODE();

      HAL_EXIT_ISR();

      return;
    }

     

    void halProcessKeyInterrupt (void)
    {
      bool valid=FALSE;

      if( HAL_KEY_SW_1_PXIFG & HAL_KEY_SW_1_BIT) /* Interrupt Flag has been set by SW1 */
      {
        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 by SW2 */
      {
        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);
      }
    }

    uint8 HalKeyRead ( void )
    {
      uint8 keys = 0;

      if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_1;
      }
      if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_2;
      }
      return keys;
    }

    void HalKeyPoll (void)
    {
      uint8 keys = 0;
      uint8 notify = 0;
      if (!(HAL_KEY_SW_1_PORT & HAL_KEY_SW_1_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_1;
      }
      if (!(HAL_KEY_SW_2_PORT & HAL_KEY_SW_2_BIT))    /* Key is active low */
      {
        keys |= HAL_KEY_SW_2;
      }

      /* 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;
        }
        else
        {
          notify = 1;
        }
      }
      else
      {
        /* Key interrupt handled here */
        if (keys)
        {
          notify = 1;
        }
      }

      /* Store the current keys for comparation next time */
      halKeySavedKeys = keys;

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

      }
    }

  • Unfortunately, recent suggested solution is also not working.
  • Hello YK CHEN,

    Unfortunately, your latest suggested solution also not worked.
    Please provide us the way to resolve this error.

    Thanks in advance.

    Kumar Karan Jain
  • If you set breakpoint at the line "halProcessKeyInterrupt();" in HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR ). Is the breakpoint hit when you trigger P1.7?
  • Just to recall :-
    There are two switches in original keyfob at
    1) Port0 Pin0
    2) Port1 Pin1
    We have changed switch 2 to Port1 Pin7,already shared configuration screenshots earlier and Switch at Port0pin0 remain as it is.

    Now , As I put a breakpoint at "if (HAL_KEY_SW_2_PXIFG & HAL_KEY_SW_2_BIT)" in "HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )", and press the "Go" command in debug mode. Nothing happens if switch at PORT1 PIN7 is presses but if another switch is pressed at PORT0PIN0, breakpoint has been hit.
    I guess It has not configured to enable interrupt at PORT1.

    Need your reflection to resolve this.

    We have made use of the test header in Keyfob to implement switch at PORT1PIN7.

    Kumar Karan Jain
  • Yes, it seems your register settings are incorrect. Can you post your latest defines about HAL_KEY_SW_2_XXX?
  • Ok.Here are the screenshots of the definations,Please find them.

    SS1:

    SS2:

    SS3:

     .


    Hopefully I have attach all definitions, If anything left, Please let me know.

  • Those defines look correct. Do you init and config SW2 in HalKeyInit() and HalKeyConfig()?
  • Yes, it is configured there.
    Please find the routines below,

    HalKeyConfig():-
    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)
    {
    #if defined ( CC2540_MINIDK )
    HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT; /* enable interrupt generation at port */
    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* Clear any pending interrupt */
    HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT; /* enable interrupt generation at port */
    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear any pending interrupt */

    #else
    /* 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 */
    HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT); /* Clear the edge bit */
    /* For falling edge, the bit must be set. */
    #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)
    HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;
    #endif


    /* Interrupt configuration:
    * - Enable interrupt generation at the port
    * - Enable CPU interrupt
    * - Clear any pending interrupt
    */
    HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;
    HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;
    HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);
    #endif // !CC2540_MINIDK

    /* 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 */
    {
    #if defined ( CC2540_MINIDK )
    HAL_KEY_SW_1_ICTL &= ~(HAL_KEY_SW_1_ICTLBIT); /* don't generate interrupt */
    HAL_KEY_SW_2_ICTL &= ~(HAL_KEY_SW_2_ICTLBIT); /* don't generate interrupt */
    #else
    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 */
    #endif // !CC2540_MINIDK

    osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
    }

    /* Key now is configured */
    HalKeyConfigured = TRUE;
    }




    HalKeyInit():
    void HalKeyInit( void )
    {
    halKeySavedKeys = 0; // Initialize previous key to 0.

    #if defined ( CC2540_MINIDK )
    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 */
    #else
    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_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */
    HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input */

    P2INP |= PUSH2_BV; /* Configure GPIO tri-state. */
    #endif

    /* Initialize callback function */
    pHalKeyProcessFunction = NULL;

    /* Start with key is not configured */
    HalKeyConfigured = FALSE;

    #if defined ( CC2540_MINIDK )
    /* Rising/Falling edge configuratinn */
    PICTL |= HAL_KEY_SW_1_EDGEBIT; /* Set the edge bit to set falling edge to give int */
    HAL_KEY_SW_1_IEN |= ( HAL_KEY_SW_1_IENBIT | HAL_KEY_SW_2_IENBIT ); /* enable CPU interrupt */
    #endif
    }
  • I don't see you set HAL_KEY_SW_2_IEN in HalKeyConfig(). Please add the following red line in your HalKeyConfig.

    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)

    {

    #if defined ( CC2540_MINIDK )

    HAL_KEY_SW_1_ICTL |= HAL_KEY_SW_1_ICTLBIT; /* enable interrupt generation at port */

    HAL_KEY_SW_1_PXIFG = ~(HAL_KEY_SW_1_BIT); /* Clear any pending interrupt */

    HAL_KEY_SW_2_ICTL |= HAL_KEY_SW_2_ICTLBIT; /* enable interrupt generation at port */

    HAL_KEY_SW_2_PXIFG = ~(HAL_KEY_SW_2_BIT); /* Clear any pending interrupt */

    HAL_KEY_SW_2_IEN |= HAL_KEY_SW_2_IENBIT;

  • By adding this line to configuration routine, made switch to work.
    Thanks a lot for your support and cooperation. It wouldn't be easy without your help.

    Thanks once again.

    Regards,
    Kumar Karan Jain
  • You are welcome and it's good to know it works now.
  • Sir,

    Switch is working fine, We have configured a switch to detect long and short press.On long press it suppose to turn OFF advertisement, it is doing so, but problem is after 15sec it automatically turns advertisement ON for 3min.
    Please suggest how could I debug where it is turning ON.

    Regards,
    KKJ
  • Try to check where calls GAPRole_SetParameter( GAPROLE_ADVERT_ENABLE, ....) in your application .
  • Hello Sir,

    Checked for "GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, ....) " in themometer.c but unable to detect the issue.

    Just to recall issue with some modification : When switch at (P1_7) is detected as long press then it has to terminate the connection and did nothing, until short press on same switch is detected to enable advertisement.
    But, after long press detected it terminates connection and also start advertising, Which shouldn't be started.

    Please help us to resolve this issue.

    Thanks in advance.

    Regards.
    KKJ
  • There must be somewhere to initiate advertising. You can search keyword "GAPROLE_ADVERT_ENABLED" in your application and check which one does this.