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: Dimmer Physical Button

Part Number: CC2530
Other Parts Discussed in Thread: Z-STACK, , CC2531

Hi Guys,

I working to build a new version of my dimmer device, at the latest version I have been used only level controller via APP (dimmer UP/Down and ON/OFF) but I would like to implement a physical button to make it without the APP. I have in my mind to implement the physical controller on a single button (one switch). My physical controller needs to be perceptive a single pressure necessary to increase/decrease (up or down 10%) or a strong pressure (holding on) to directly turn on/off.

I think that I need to implement a timer supervision algorithm necessary to check if the events single pressure or a strong pressure (hold on).

I using this Zstack version: Z-Stack Home 1.2.2a.44539 and I have implemented my dimmer over light switch example inside Home Automation folder.

Someone has been implemented it or something with a little bit similarities?

BR,

Alex

  • You can refer to hal_key.c to implement key pressed and send a key event to further detecting long press or single press.

  • Hi Mr. Yikai, usually you come to help me!!! :)

    I have study some algorithm to understanding how to implement a sensed method to detect a strong or short pressure on push button and see in some cases that need to handling timer, port and WTD interrupt, CC2530, for example, I need to start a timer and check the state of the GPIO in an interrupt service routine for that timer. and increment a counter every time you sensed the button was pressed.

    As CC2530 Zstack have a few prepared functions and API, maybe I can use it to implement my code on hal_key.c.

    I saw initially that in hal_key.c I can use HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ) as a starting point, but do you can advise-me auxiliary functions or API to implement it?

    Best Regards,

    Alex

  • If you use pin on port0 as GPI button interrupt, HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ) is the start point and it should call halProcessKeyInterrupt that will call HalKeyPoll to check which pin triggers interrupt in the end and send key event to zclSampleLight_HandleKeys. I suppose you can add your own code in this logic and start a new osal timer event in zclSampleLight_HandleKeys to check if the button is pressed and hold.

  • Mr. Yikai, thanks I´ll work on it!

    Let you know soon!

    BR,

    Alex

  • Hi Mr. Yikai, how are you?

    I´m working to design a sensitive button, as you advise me I try to include code to detect pressure in mydevice_HandleKeys but I had a problem to reach this function when I pressed S1 Button (P0.1) the HAL_KEY_SW_6 never been reached when the breakpoint is activated

    Note: I used at test phase the Smartrf05eb 1.7.1. 

    I have set up a breakpoint in the HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ) function, and at this point working fine, always when I pressed S1 at this point the halProcessKeyInterrupt() it was achieved.   But this does not occur in if ( keys & HAL_KEY_SW_6 )!

    do you can see my configuration below:

    hal_key.c

    /* 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
    
    /* S1 key 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
    
    #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 */
    
    /* edge interrupt */
    #define HAL_KEY_SW_6_EDGEBIT  BV(0)
    #define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE
    
    HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_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_0_IF = 0;
      CLEAR_SLEEP_MODE(); 
    HAL_EXIT_ISR(); }



    onboard.c w/ ISR_ISR_KEYINTERRUPT active on IAR compiler options
        #if defined (ISR_KEYINTERRUPT)
        HalKeyConfig(HAL_KEY_INTERRUPT_ENABLE , OnBoard_KeyCallback);
        #else
        HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback);
        #endif

    zcl_TTa0003.c (my program)

    static void zclTTa0003_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 ) // it is working well when P2 Joystick (UP/DOWN/RIGHT/LEFT/CENTER) is activated
      {
        giLightScreenMode = LIGHT_MAINMODE;
    
        // toggle local light immediately
        zclTTa0003_OnOff = zclTTa0003_OnOff ? LIGHT_OFF : LIGHT_ON;
    #ifdef ZCL_LEVEL_CTRL
    	zclTTa0003_LevelCurrentLevel = zclTTa0003_OnOff ? zclTTa0003_LevelOnLevel : ATTR_LEVEL_MIN_LEVEL;
    #endif
      }
    
      if ( keys & HAL_KEY_SW_6 ) //it is not working.
      {
    	giLightScreenMode = LIGHT_MAINMODE;
    
        // toggle local light immediately
    	char OFF_COMMAND=0x00;
    	HalUARTWrite( HAL_UART_PORT_0, (uint8*)&OFF_COMMAND,1);
    #ifdef ZCL_LEVEL_CTRL
        zclTTa0003_OnOff =	zclTTa0003_OnOff ? LIGHT_OFF : LIGHT_ON;
    #endif
      } 

    Would you help me to fix button 1 / HAL_KEY_SW_6 it?

    BR,

    Alex

  • Do you define ISR_KEYINTERRUPT in your project predefined symbols to enable key interrupt?

  • You can add code for handle long time press/hold and then:

    - Normal press just on or off

    - Press and hold more than 5 seconds the first time will increase level,

    - Press and hold more than 5 seconds second time will decrease level,

  • Hi Mr. Yikai, 

    Yes, I did it before, as you can see below:


    I did not express myself well, when I open posted here, this is happening intermittently. when I pressed S1 always the HAL_ISR_FUNCTION is hit, but the ( keys & HAL_KEY_SW_6 ) does not always occur. I get to press 20 times in a row and it doesn't work, sometimes right away. As you can see in the PIC bellow:


    Interestingly, the joystick (Port P2) always hits ( keys & HAL_KEY_SW_6) and then ( keys & HAL_KEY_SW_1 ) simultaneous. 

    when it should reach only the ( keys & HAL_KEY_SW_1 ), in my opnioun.

    Mr. Yikai, do you help me to fix it?

  • Hi DzungPV, thanks a lot!

    Do you have implemented this code before to increase/decrease the level?

    BR,

    Alex

  • There’s key debounce in hal key behavior. If you press the button for a moment such as more than 500ms, can you see your zclTTa0003_HandleKeys hits every time?

  • Mr. Yikai, 

    I have changed the debounce time at #define HAL_KEY_DEBOUNCE_VALUE  25, to 0, 1, 5, 10 and 50 to test the effects over S1 (P0_1)/keys & HAL_KEY_SW_6 but in all those experience I saw the same behavior, some times working and not working!

    Inclusive as I said all those time that S1 was pressed the HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR ) is reached. Also, I can visually check it through LED4 turn on (in the Smartrf05eb LED1 share electrical connections with S1). 

    It´s sound very stranger to me that P2_0 (joystick) hit zclTTa0003_HandleKeys /HAL_KEY_SW_6 and HAL_KEY_SW_1 always and at the same time!

    But S1(P0_1) still hit intermittently.

    Do you have a new idea?

    Thanks,

    BR

    Alex

  • Does HAL_ISR_FUNCTION hit every time?

  • Mr Yikai,

    Yes, the  HAL_ISR_FUNCTION hit every time!  But zclTTa0003_HandleKeys not!

    BR,

    Alex

  • Mr Yikai,

    stranger still it that  zclTTa0003_HandleKeys (keys & HAL_KEY_SW_6 ) hit every time when pressed any of the buttons on the joystick, even though the Joistick works with P2 port and the respective interrupt (HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR ).

    Note: I commented on all the entries has LED_4 and LED_1 in the code zcl_TTa0003.c, and nothing changed!

    BR,

    Alex

  • Do you test this with TI CC2530DK?

  • Yes, I'm testing this with SmartRF05EB Rev 1.7.1. I´ll try to reproduce it tomorrow with CC2531 USB dongle!

    BR,

    Alex

  • When you try to check if it hits breakpoint on "zclTTa0003_HandleKeys", do you disable breakpoint on "HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )" first?

  • Yes, I have check those functions in the different moments!

    I'll let you know tomorrow about  the test in USB Dongle w/ CC2531!

  • If I remember correctly, P0.1 of CC2530DK is connected as EM_RESET. I would suggest you to use use P0.0 to test SW6.

  • Mr. Yikai,

    I have changed to P0.0 and I saw the same behavior as P0.1 the entry ( keys & HAL_KEY_SW_6 ) was hit intermittently and HAL_ISR_FUNCTION always is hit. 

    Note: S5-EM RESET Key that has a connection with RESET_N PIN at CC2530.

    I have changed those files: hal_key.c as you can see below:

    /* SW_6 is at P0.0 */
    #define HAL_KEY_SW_6_PORT   P0
    #define HAL_KEY_SW_6_BIT    BV(0)
    #define HAL_KEY_SW_6_SEL    P0SEL
    #define HAL_KEY_SW_6_DIR    P0DIR
    
    /* 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(0) /* P0IEN - P0.0 enable/disable bit */
    #define HAL_KEY_SW_6_PXIFG    P0IFG /* Interrupt flag at source */
    
    I have tested in the hal_board_cfg.h to P0.0 and P.0.1 in combination with the code above, but nothing has changed!
    /* S1 */
    #define PUSH1_BV          BV(1)
    #define PUSH1_SBIT        P0_1
    
    or 
    
    /* S1 */
    #define PUSH1_BV          BV(0)
    #define PUSH1_SBIT        P0_0

    I have aborted USB dongle test because of the two-buttons inside the board has a physical connection with P1 port.

    Do you see another code that I need to change?

    BR,

    Alex

  • That's the code that I change too. I also test it on my CC2530DK with SampleSwitch example and it works fine.

  • Hi Mr. Yk firstily thanks a lot for your help, I appreciate your dedication to help me!

    I did as you advise me, I installed a new fold with zstack files and started to work with sampleswitch project example. Before it I comment out in the IAR symbols xLCD_SUPPORTED=DEBUG, unfortunately, it hit ISR Port 0 interruption to be hit all the time. 

    To make the test I used that configuration in IAR: RouterEB node, I didn't change nothing In App files or Hal drivers, I kept P0_1 port for S1 in hal_key.c  

    Note: I have included also ISR interrupt to enable interrupts on IAR Compiler-> Preprocessor-> DefinedSymbols.

    #define HAL_KEY_SW_6_PORT   P0
    #define HAL_KEY_SW_6_BIT    BV(1)

    I only have included those lines below to test the function zclSampleSw_HandleKeys:

     if ( keys & HAL_KEY_SW_6 ) //it is not working
      {
        // toggle local light immediately
    	zclSampleSw_OnOff =	zclSampleSw_OnOff ? LIGHT_OFF : LIGHT_ON;
      } 
    

    After it, I observed the same behavior, when S1 is pressed the ISR (HAL_ISR_FUNCTION) interrupt is hit all the time and keys & HAL_KEY_SW_6 not.

    Coursioulitely, as also occurs in my code, P2 (Joystick buttons) when pressed any key from Joystick always keys & HAL_KEY_SW_6 is hit. 

  • Hi Mr. Yikai, how are you?

    I have good news for you!

    After we checked together all those possible errors in my code and inclusive used an original TI example Sample Switch and we didn´t find any possible error. A has decided to take out all those electrical jumpers connected on P10 connector. After it I starting to connect only necessary jumpers to make my tests: FLASH_CS (15-16), BUEM_RESET(35-36), to my surprise  HAL_KEY_SW_6 is hit all those times when I pressed Button1(P0.1). Not satisfied I starting to connect another jumper on P10. And I saw that when I connecting JOY_MOVE (1-2) the problem returned to occur. 

    Right now I´m working w/ my SmartRF05 to implement the sensitive button, the main objective to initial post.

    I believe that we need to close this post and in the further If I have doubts to design the sensitive button I can open another post for our discussion. Do you agree with me?

    Maybe we need to change the post subject, what do you think?

     

    I would like to say Thanks a lot and sorry for this long discussion!

    BR,

    Alex

  • Yes, I agree to close this thread and create a new post if you have any doubt in the future.

  • Thanks a lot Mr. Yikai.

    BR,

    ALEX