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 Interrupt on Key press and want to calculate the time of key press

Other Parts Discussed in Thread: CC2530, Z-STACK

Hi All,

Presently Whenever i press the key and release the key, im calculating the how much time the key was pressed. 

I am calculating that using the function zllSampleLight_HandleKeys(); 

static void zllSampleLight_HandleKeys( byte shift, byte keys )
{


static uint32 keyPressTime_panic = 0;


if ( keys ){
if(key == HAL_PANIC_SWITCH ) 
keyPressTime_panic = osal_getClock();
}

}

else {
if(key == HAL_PANIC_SWITCH ) {

if (keyPressTime_panic) {
keyPressTime_panic = ( osal_getClock() - keyPressTime_panic );
if ( (keyPressTime_panic <= KEY_HOLD_SHORT_INTERVAL))
{

//Operation 1;

}

if((keyPressTime_panic <= KEY_HOLD_LONG_INTERVAL))

{

Operation 2;

}

-----------------------

#define PSWITCH_BV BV(BIT_1)
#define PSWITCH_SBIT_DDR P1DIR
#define PSWITCH_SBIT P0_1 //change to p0_1 (panic_switch_in) for panic button
#define PSWITCH_POLARITY ACTIVE_LOW

This HAL_PANIC_SWITCH assigned to port 0 and i have a requirement that now, i need to use this HAL_PANIC_SWITCH as interrupt to cc2530 and should do the above operations depending on the time, how much that key is pressed. 

How can i achieve this by considering the HAL_PANIC_SWITCH as interrupt.

  • You can refer to hal_key.c in SampleRemote-EB-EndDevice example which configures P0.1 as GPI and enable ISR.
  • Hi Sir,

    I can configure it as a Rising edge or Falling edge. But for me to calculate the time of switch pressed, how can i do that.. where to calculate the time we need both key pressed and release notification.
  • Hi Sir,

    can u tell me in which stack that example is der? i am having Z-stack Home 1.2.0, In this im not finding that sample application.
  • You can only configure CC2530 GPI as a Rising edge or Falling edge, not both. So, you have to start a periodic event to check if the key is release by keep reading GPI value in periodic event. If you detect the key is released, you can calculate the time of switch pressed.
  • SampleRemote-EB-EndDevice example is in Z-STACK-LIGHTING 1.0.2.

  • Hi Sir,

    If my cc2530 is in sleep mode. can i put key_switch as interrupt and also can i poll the key press both at a time.?
  • What do you mean "can i poll the key press both at a time?"
  • like i need to calculate the key press time.

    If cc2530 is in sleep mode, it can get notified only from external interrupt. so the same switch i will configure it as interrupt and same switch i will poll to calculate , how much time key is pressed.

    Is it possible that whenever a key is pressed, interrupt is also generated and from the polling can i detect the state change in the key.?
  • I have suggested the solution. You have to start a periodic event to check if the key is release by keep reading GPI value in periodic event (this is like the polling to key status). If you detect the key is released, you can calculate the time of switch pressed.
  • Hi sir.

    i am able to polling of the key.
    i wanted one more like, this key should act as a interrupt. whenever key is pressed it should wake up the cc2530.

    following is configuration for the key switch as interrupt.
    //P0_1 as interrupt
    P0SEL &= ~BV(1);
    P0DIR &= ~BV(1);
    P0IFG &= ~BV(1);
    P0IEN |= BV(1);
    IEN1 |= BV(5);
    PICTL |= BV(0);
  • The configurations look fine. What's the problem?
  • Hi Sir,

    I am very sry for late reply.
    i was stuck with some issue.
    so, the problem i am getting here that,
    below is the just sample of ISR for key press interrupt.

    HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR)
    {
    HAL_ENTER_ISR();
    HalLedBlink ( HAL_LED_1, 1, 50, 500 );
    HAL_EXIT_ISR();
    }

    When i press the switch, it is getting into this ISR and HAL_LED_1 is not blinking instead it is getting only on.
  • Hi Sir,

    I am very sry for late reply.
    i was stuck with some issue.
    so, the problem i am getting here that,
    below is the just sample of ISR for key press interrupt.

    HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR)
    {
    HAL_ENTER_ISR();
    HalLedBlink ( HAL_LED_1, 1, 50, 500 );
    HAL_EXIT_ISR();
    }

    When i press the switch, it is getting into this ISR and HAL_LED_1 is not blinking instead it is getting only on.
  • You can't call HalLedBlink in ISR.
  • Hi sir,

    i am not able to toggle LED also in that ISR.

    HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR)
    {
    HAL_ENTER_ISR();
    //HalLedBlink ( HAL_LED_1, 1, 50, 500 );
    HAL_TOGGLE_LED1();
    HAL_EXIT_ISR();
    }

    That LED1 is getting on and when i press the switch again. it is not getting off
  • Hi Yikai sir,

    As u said we cant call halLedblink.
    but as i have configured another port for interrupt and this interrupt is coming from the accelerometer.
    In this interrupt ISR im able to blink the LED.
    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR)
    {
    HAL_ENTER_ISR();
    uint8 data;
    HalLedBlink ( HAL_LED_2, 3, 50, 500 );
    LIS2DH_GetInt1Src(&data);
    P1IFG &= ~BV(5); // Clear source interrupt flag for P1.5
    IRCON2 &= ~BV(3); // Clear CPU interrupt flag for P1
    HAL_EXIT_ISR();
    }
  • After I check the source code, I think it should be fine to call HalLedBlink in ISR. Sorry for wrong information in previous reply.
  • Its ok sir. But In this Key press ISR, y it is not blinking.
    but where as in accelerometer ISR it is blinking.
  • Do you set a breakpoint in HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR) to make sure it hit the breakpoint?
  • Hi sir,

    I am not using it in debug mode, directly im flashing this firmware hex file into my board.
    am i missing any other parameter that need to enable to make this key switch as interrupt to call ISR.
  • Why don't you set a breakpoint and check it with IDE to debug?
  • Hi Yikai Sir

    I am sorry for late reply, i got the solution for my key press interrupt ISR. i forgot to clear the interrupt in ISR, because of this LED is continuously high as it is entering into ISR only one time.
  • No problem and it's good to know you find the problem.
  • Hi Yikai sir

    I have One more doubt that, can we configure P2.3, P2.4 and P2.0 as a interrupt and also as a GPIO. ??
  • Yes, P2.3, P2.4 and P2.0 can be configured as a GPIO.

  • Hi Yikai sir,

    If i set my end device to Power Mode 2/3 manually by setting
    PCON |= 1;
    SLEEPCMD &= ~3;
    SLEEPCMD |= 0x03;

    , that time cc2530 will wakeup only on external interrupts.

    I want to know after waking up, will it be in Power Mode 2/3 or idle/active mode???
  • After waking up, it would be in active mode.
  • Hi Sir,

    as this is a simple, i dont want to put more time on this.

    I am not able to configure the P2.4 port as output.
    I have set the register values as follows
    P2SEL &= ~(BV(2));
    P2DIR |= (BV(4));

    was this correct?
  • The settings are correct.
  • Hi Sir,

    I am adding
    #define PANIC_INIT() st(P0SEL &= ~(P2SEL &= ~BV(2);P2SEL &= ~(BV(0)); P2DIR |= BV(0);P2DIR |= BV(4); \
    P2_4 = 1;P2_0 = 0;);

    above config in hal_board_cfg.h file.

    I am able configure the P2_0, where P2_4 is not configuring.

    In P2_4, continuously it is giving 0.422 v.
  • P2_4 is used as 32K crystal input in Z-Stack. If you want to use is as GPIO, you have to define OSC32K_CRYSTAL_INSTALLED=FALSE in your project compile options.
  • Hi Yikai sir,

    Thank you, It is working now. Now i am able to control the P2_4 port.

    But i stuck at one place that,

    For key press interrupt below is the ISR for that.

    HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR)
    {
    HAL_ENTER_ISR();
    PCON = 0;
    SLEEPCMD = 0x00;
    osal_stop_timerEx( zllSampleLight_TaskID,POWER_MODE_2_SET);

    HAL_TOGGLE_LED1();

    osal_start_timerEx( zllSampleLight_TaskID, POWER_MODE_2_SET,14000 );
    P0IFG &= ~BV(1);
    IRCON &= ~BV(5);
    HAL_EXIT_ISR();
    }

    in zllSampleLight_event_loop( ) function i am adding task event of POWER_MODE_2_SET

    if ( events & POWER_MODE_2_SET )
    {
    HalLedBlink ( HAL_LED_1, 3, 50, 500 );
    PCON |= 1;
    SLEEPCMD &= ~3;
    SLEEPCMD |= 0x03;
    return ( events ^ POWER_MODE_2_SET );
    }

    I am facing issue like im getting into that event and LED is blinking 3 times. but the power modes are not setting.

    but if write power mode settings directly in the ISR, it is getting into power modes.

    can i know what is happening ???
  • Hi Sir,

    Below ISR program is making cc2530 to PM3/PM2

    HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR)
    {
    HAL_ENTER_ISR();
    PCON = 0;
    SLEEPCMD = 0x00;
    //osal_stop_timerEx( zllSampleLight_TaskID,POWER_MODE_2_SET);

    HAL_TOGGLE_LED1();
    //osal_start_timerEx( zllSampleLight_TaskID, POWER_MODE_2_SET,14000 );
    PCON |= 1;
    SLEEPCMD &= ~2;
    SLEEPCMD |= 0x02;
    P0IFG &= ~BV(1);
    IRCON &= ~BV(5);
    HAL_EXIT_ISR();
    }

    but i want this cc2530 to be get into PM2/PM3 after specified time.?
    How to achieve this?
  • When you enable POWER_SAVING in Z-Stack, device would go to PM2 automatically (if there's nothing in Z-Stack task queue). Why do you need cc2530 to be get into PM2/PM3 after specified time?
  • Hi Sir,

    I can configure from the z-stack.
    but my board is having accelerometer, in this for no motion for 12 sec it will give interrupt, bcz of this my cc2530 is awaking.
    this will happen for every 12sec. bcz of this i have to do manual switching to power modes.
  • Even CC2530 is waken up every 12 seconds, it should go back to sleeping mode if there is nothing in task queue.
  • Hi Sir,

    Sry for the late reply.
    i am working on developed firmware. so i dont know which task events are running.. so i just manually making power modes off/on at the time of my task events are running.
    and this is working fine with my use cases.
    it is getting into power modes and coming out from the power modes whenever there is interrupt.

    Sir, I want to how to set sleep timer. so that i can wake up the cc2530 at every 10mins and then sleep again.
  • You can use osal_start_timerEX to create a scheduled event.
  • Hi Sir,

    Will osal_start_timerEX running when cc2530 is in PM2??
  • I won't say osal_start_timerEX would work in PM2. In fact, PM2 can be waken up by timer interrupt and osal_start_timerEX would set timer interrupt. So, it would work perfectly in your application.
  • Hi Sir,

    I have tested with, When cc2530 is in PM2, osal_start_timerEX is not waking up the controller.

    and I didnt get the meaning of osal_start_timerEX would set timer interrupt. ?

    I think in PM2 only sleep timer can wake up the SoC.
    So can u suggest me in configuring the sleep timer to set like every 10 min it wakes up soc if soc is sleeping, and do the related operation.
  • I never see that CC2530 cannot wake up from PM2 using osal_start_timerEx. Can you show me how you use osal_start_timerEx?
  • Hi Sir,

    Actually i am using osal_start_timerEx as normal event handler.

    like
    osal_start_timerEx( zllSampleLight_TaskID, HEART_BEAT_EVERY_PERIOD, 600000);

    In heart_beat_every_period event i putting the usecase.

    if ( events & HEART_BEAT_EVERY_PERIOD )
    {

    {
    //Operation : sending command to gateway.
    }
    osal_start_timerEx( zllSampleLight_TaskID, HEART_BEAT_EVERY_PERIOD, 20000);
    return ( events ^ HEART_BEAT_EVERY_PERIOD );
    }

    I dont know about to wake up the Soc using osal_start_timerEx..

    Please suggest me.

  • Where do you call "osal_start_timerEx( zllSampleLight_TaskID, HEART_BEAT_EVERY_PERIOD, 600000);"? After your application calls it, I suppose HEART_BEAT_EVERY_PERIOD event should be triggered 10 minutes later. Do you set a breakpoint to check it?
  • Hi sir,

    Yes, I am calling in zllSampleLight_Init() function,

    zllSampleLight_Init()
    {

    ....
    ....
    ....
    osal_start_timerEx( zllSampleLight_TaskID, HEART_BEAT_EVERY_PERIOD, 10000);

    .....
    ....
    }

    if ( events & HEART_BEAT_EVERY_PERIOD )
    {
    if(joined == 1){

    HalLedBlink ( HAL_LED_1, 3, 50, 500 );

    }
    osal_start_timerEx( zllSampleLight_TaskID, HEART_BEAT_EVERY_PERIOD, 600000);
    return ( events ^ HEART_BEAT_EVERY_PERIOD );
    }




    -> At start of the application, this will call the event after 10sec, and then later it wil call every 10min. this is happening successfully when cc2530 is not in PM2. but when cc2530 is PM, this is not happening.

    -> and i am having problem that, when i set one or more break point . i am getting a dialog box with a statement "one or more break points are not allowed" ...
    can i know the reason for this?

  • 1. How does SampleLight enter PM2? Do you enable POWER_SAVING to make it or you do it by yourself?
    2. As I remember, you can only set breakpoint up to 4 or 5. You might exceed the max number so you can remove all breakpoints and set it again.
  • Hi Sir,

    1. I am doing that by myself. by configuring PCON and SLEEPCMD registers.
    2. In my project, i am not understanding that .. if i put one break point also i am getting "One or more break points couldn't be set and have been disabled" msg.

    can u suggest me, how to configure the sleep timer so that even in PM2 it should be running and after timer finish it should do some operation and again start the sleep timer.

    that operation should be done at every period of time (like example 10 mins).
  • 1. I don't know what might be wrong since you do PM2 by yourself. As I replied to you, you can enable POWER_SAVING and osal_start_timerEx should work fine.
    2. Try to set Optimizations level to low in C/C++ Compiler options.
  • Hi Sir,

    One small doubt, When we enable the power saving mode, it means that when no task are running then cc2530 will go in PM2 mode.
    as per the document when cc2530 is in PM2 mode, only external interrupts or sleep timer will wake up the cc2530.

    Is osal_start_timerEx acting as sleep timer for cc2530?

    Please guide me if i am wrong, I am new to these topics.
  • Yes, osal_start_timerEx acts as sleep timer for cc2530.