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: CC2530 Ports working with Keys

Part Number: CC2530

Hi Guys, I'am working with my device, running over ZStack 2.4.0, and i used Project Reference as SampleLight.

My Board have connected two key switches, SW1 as P0_4 and SW2 as P0_5. 

The main Idea is to turn on or turn off my light switch according to the keys SW1 or SW2 pressed. 

Initially i changed some code lines to support my New Ports (P0_4 and P0_5) in those files to provide my board to interact with my code:

on hal_borad_cfg.h

/* ------------------------------------------------------------------------------------------------
* Push Button Configuration
* ------------------------------------------------------------------------------------------------
*/

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

/* S1 */
#define PUSH1_BV BV(4) // Written by ASA the original value is 1
#define PUSH1_SBIT P0_4 // Written by ASA the original value is P0_1

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

/* S2 */
#define PUSH2_BV BV(5) // Written by ASA the original value is 0
#define PUSH2_SBIT P0_5 // Written by ASA the original value is P2_0
#define PUSH2_POLARITY ACTIVE_HIGH

on onboard.c:

void InitBoard( uint8 level )
{
if ( level == OB_COLD )
{
// IAR does not zero-out this byte below the XSTACK.
*(uint8 *)0x0 = 0;
// Interrupts off
osal_int_disable( INTS_ALL );
// Check for Brown-Out reset
ChkReset();
}
else // !OB_COLD
{
/* Initialize Key stuff */
//HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback); written by ASA
HalKeyConfig( HAL_KEY_INTERRUPT_ENABLE, OnBoard_KeyCallback);
}

on zcl_sampleLight.c (HandleKeys):

static void zclSampleLight_HandleKeys( byte shift, byte keys )
{
//zAddrType_t dstAddr;
(void)shift; // Intentionally unreferenced parameter
if (shift)
if (keys & HAL_KEY_SW_1 )
{
HalLedSet( HAL_LED_3, HAL_LED_MODE_OFF ); // Written by ASA
P1_4 = 0;
}
if ( keys & HAL_KEY_SW_2 )
{
HalLedSet( HAL_LED_3, HAL_LED_MODE_OFF ); // Written by ASA
P1_4 = 0;
}
if ( keys & HAL_KEY_SW_3 )
{
HalLedSet( HAL_LED_3, HAL_LED_MODE_OFF ); // Written by ASA
}
if ( keys & HAL_KEY_SW_4 )
{
HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );// Written by ASA
}
}

I double checked with fisical port (P0_4) with a basic test, inside a incoming message loop, to verify it my hardware works, and i checked that is was ok. 

if (!P0_4)
{ P1_4 = 0;

But nothing happens, when i using handlekeys in zcl_samplelight.c. Somebody can help me, how to starting to working it?

BR

Alex

 

BR

Alex

  • You should revise hal_key.c instead of hal_borad_cfg.h and onboard.c.
  • Hi Mr Yikai, would you help me, i didn't understanding the relationship between those files posted and hal_key.c code bellow:

    * 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
  • You can replace "#define HAL_KEY_SW_6_BIT BV(1)" with "#define HAL_KEY_SW_6_BIT BV(4)" and "#define HAL_KEY_SW_6_ICTLBIT BV(1)" with "#define HAL_KEY_SW_6_ICTLBIT BV(4)" to make P0.4 as GPI trigger source for SW6.

  • Mr Yikai,
    it's working very well with P0_4 for the SW1, I tried to do the same for SW2 (P0_5) but It didn't working. I´ll explain it bellow:

    /* Joy stick move at P2.0 */
    #define HAL_KEY_JOY_MOVE_PORT P0
    #define HAL_KEY_JOY_MOVE_BIT BV(5)
    #define HAL_KEY_JOY_MOVE_SEL P0SEL
    #define HAL_KEY_JOY_MOVE_DIR P0DIR

    /* edge interrupt */
    #define HAL_KEY_JOY_MOVE_EDGEBIT BV(0)
    #define HAL_KEY_JOY_MOVE_EDGE HAL_KEY_FALLING_EDGE

    /* Joy move interrupts */
    #define HAL_KEY_JOY_MOVE_IEN IEN1 /* CPU interrupt mask register */
    #define HAL_KEY_JOY_MOVE_IENBIT BV(5) /* Mask bit for all of Port_2 */
    #define HAL_KEY_JOY_MOVE_ICTL P0IEN /* Port Interrupt Control register */
    #define HAL_KEY_JOY_MOVE_ICTLBIT BV(5) /* P2IENL - P2.0<->P2.3 enable/disable bit */
    #define HAL_KEY_JOY_MOVE_PXIFG P0IFG /* Interrupt flag at source */

    Would you check, please, if i did something wrong?

    BR
    Alex
  • Joystick uses P2.0 and ADC reading so it's not suitable for changing it. I suggest you to clone SW6 and revise on it for P0.5
  • Hi Mr Yikai. Thanks a lot for your assistance.
    After I made the changes and Key S1, as you advise me, starting to working I observed the following behavior:

    When I press Button S1 to turn on (P0_4 > 1 ON) , it is sometimes ok, sometimes not. I think the problem is maybe in the key debounce.

    I changed the debounce, in the hal_key.c, value from 25 to 35 but i didn't notice differences.

    Can you give me an advice to solve this Problem?

    BR
    Alex
  • Try to set a breakpoint in HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ) and press button to see if it's hit every time in IAR.
  • Hi Mr Yikai,
    after I debugging the halKeyPort0Isr as you advise me, I checked that always the debug breakout and verify that the interrupt is running well. I saw that my actually board using electric components without ceramic capacitor. I´ll building a new board with different electric structure. After it I´ll post it here.

    Definitions:
    #define HAL_KEY_SW_6_PORT P0
    #define HAL_KEY_SW_6_ICTLBIT BV(5) /* P0IEN - P0.5 enable/disable bit */

    About the second key (SW2 -> P0_5), as i posted before, would you can explain me more about the mechanism. You told me to clone SW6 but if I make it I´ll have problem with IAR compiler when those line will interpret wrong because the SW1 coincidences.

    * SW_6 is at P0.5 - Used for SW2 P0.5.
    #define HAL_KEY_SW_6_PORT P0
    #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_PXIFG P0IFG /* Interrupt flag at source */

    I have a few doubts about switches Key works,
    How can I define the labels (or tags) to have a code differences between the two keys, for example, I need to building the SW_7 (define HAL_KEY_SW_7_PORT P0)?

    How can works the relationship between the Keys and the codes (#define HAL_KEY_SW_6_PORT P0 / #define HAL_KEY_SW_6_ICTLBIT BV(5 and others), inside hal_keys.c, and the zclSampleLight_HandleKeys, inside zcl_sampleLight.c? Buy the way how to associate SW6 to SW1?

    BR
    Alex
  • Yes. If interrupt is running well, you should check your HW.
  • Thanks a lot Mr Yikai. I´ll building a new hardware.

    How do I better understand the drive mechanism of the SW2 and related programming code.

    BR
    Alex
  • There's no specific document for this. You can only trace source code and get used to it.

  • Hi Mr Yikai,

    Would you advise me, please, each parameter I need to change to enable interrupt for P0_5?

    I changed those lines in the:

    HAL_KEY_SW_6_BIT to BV(5)

    HAL_KEY_SW_6_PORT to P0

    HAL_KEY_SW_6_ICTLBIT to BV(5)

    But I didn't see any effect during the debug procedure when I spected (breakpoint) in HAL_ISR ....

    If I change the code lines above to P0_4 the interception procedure running very well.

    BR

    Alex

  • You can replace "#define HAL_KEY_SW_6_BIT BV(1)" with "#define HAL_KEY_SW_6_BIT BV(5)" and "#define HAL_KEY_SW_6_ICTLBIT BV(1)" with "#define HAL_KEY_SW_6_ICTLBIT BV(5)" to make P0.5 as GPI trigger source for SW6.
  • Hi Mr Yikai,
    I got success to fix the intermittent problem with my SW1. I built a new hardware and I used a Pull-Up resistor and a Diode to guarantee high logical level, when the switch key (S1) is open and the reverse current.

    BR
    Alex