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.

Newbie needs help with CC2540 firmware setting input pin for button

Other Parts Discussed in Thread: CC2540

Hello,

I have a custom PCB with a CC2540 and I am trying to modify SimpleBLEPeripheral.  I have two buttons on the board at P1.2 and P1.3.  The SimpleBLEPeripheral firmware with the Keyfob build has two buttons, one will switch advertising on and off.  The Keyfob buttons are connected to P0.0 and P0.1.  I'm unsure how to change the code to set the buttons up on P1.2 and P1.3 instead?

  P0SEL = 0; // Configure Port 0 as GPIO
  P1SEL = 0; // Configure Port 1 as GPIO
  P2SEL = 0; // Configure Port 2 as GPIO

  P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons), all others (P0.2-P0.7) as output
  P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output
  P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output

  P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons)
  P1 = 0;   // All pins on port 1 to low
  P2 = 0;   // All pins on port 2 to low

I'm guessing I need to change this code but where do the 0xFC, 0xFF etc. come from?  How can I work out what 0xXX I need to have P1.2 and P1.3 set as input?  I have spent hours reading the developers guides and searching the forums but I'm lost.

I am new to firmware coding (as you can probably tell!) so thanks for helping out a newbie!

Cheers

Edit with the pic of the schematic

  • To config P1.2 and P1.3 as GPI, you need to set P1DIR=0xF3

  • Thanks YiKai, but where does that 0xF3 come from?  How can I find this myself for future?

    Also what do I need to set the P1 = code to?

  • Okay I've just realised the hex to binary connection.  So 0xF3 = 11110011 which is all pins except 2 and 3 set to 1 for output and 0 for input.  I took that into account with setting the pin power too.

    So this is what I've ended up with:

      P0SEL = 0; // Configure Port 0 as GPIO
      P1SEL = 0; // Configure Port 1 as GPIO
      P2SEL = 0; // Configure Port 2 as GPIO
    
      P0DIR = 0xFF; // All output
      P1DIR = 0xF3; // Port 1 pins P1.2, P1.3 as input (buttons), all others output
      P2DIR = 0xFF; // All output
    
      P0 = 0;      // All pins on port 0 to low
      P1 = 0x0C;   // All pins on port 1 to low except for P1.2 and P1.3 (buttons)
      P2 = 0;      // All pins on port 2 to low

    I've also changed the following in hal_key.c but my buttons aren't working yet:

    /* SW_1 is at P1.2 */
    #define HAL_KEY_SW_1_PORT   P1
    #define HAL_KEY_SW_1_BIT    BV(2)
    #define HAL_KEY_SW_1_SEL    P1SEL
    #define HAL_KEY_SW_1_DIR    P1DIR
    
    /* SW_2 is at P1.3 */
    #define HAL_KEY_SW_2_PORT   P1
    #define HAL_KEY_SW_2_BIT    BV(3)
    #define HAL_KEY_SW_2_SEL    P1SEL
    #define HAL_KEY_SW_2_DIR    P1DIR
    
    #define HAL_KEY_SW_1_IEN      IEN1  /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT  BV(2) /* P1IEN - P1.2 enable/disable bit */
    #define HAL_KEY_SW_1_IENBIT   BV(4) /* Mask bit for all of Port_0 */ 
    #define HAL_KEY_SW_1_PXIFG    P1IFG /* Interrupt flag at source */
    
    #define HAL_KEY_SW_2_IEN      IEN1  /* CPU interrupt mask register */
    #define HAL_KEY_SW_2_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_2_ICTLBIT  BV(3) /* P1IEN - P1.3 enable/disable bit */
    #define HAL_KEY_SW_2_IENBIT   BV(4) /* Mask bit for all of Port_0 */ 
    #define HAL_KEY_SW_2_PXIFG    P1IFG /* Interrupt flag at source */
    
    #define HAL_KEY_SW_1_EDGEBIT  BV(0) 

    Does this look alright?  What else do I need to change?

  • To know more details about CC254x register settings, you can download user guild from http://www.ti.com/lit/ug/swru191e/swru191e.pdf . The following line in your code need to revise to IEN2

    #define HAL_KEY_SW_1_IEN      IEN1      ---> this is for port 0 interrupt

    #define HAL_KEY_SW_2_IEN      IEN1      ---> this is for port 0 interrupt

    So you need to change them to

    #define HAL_KEY_SW_1_IEN      IEN2      ---> this is for port 1 interrupt

    #define HAL_KEY_SW_2_IEN      IEN2      ---> this is for port 1 interrupt

  • Okay I have it working now.  To summarise in case someone else sees this in future, here's what I did:

    simpleBLEPeripheral.c

    Changes:

      P0DIR = 0xFF; // All output
      P1DIR = 0xF3; // Port 1 pins P1.2, P1.3 as input (buttons), all others output
      P2DIR = 0xFF; // All output
    
      P0 = 0;      // All pins on port 0 to low
      P1 = 0x0C;   // All pins on port 1 to low except for P1.2 and P1.3 (buttons)
      P2 = 0;      // All pins on port 2 to low

    hal_key.c

    Add:

    #define HAL_KEY_CPU_PORT_1_IF P1IF

    Changes:

    /* SW_1 is at P1.2 */
    #define HAL_KEY_SW_1_PORT   P1
    #define HAL_KEY_SW_1_BIT    BV(2)
    #define HAL_KEY_SW_1_SEL    P1SEL
    #define HAL_KEY_SW_1_DIR    P1DIR
    
    /* SW_2 is at P1.3 */
    #define HAL_KEY_SW_2_PORT   P1
    #define HAL_KEY_SW_2_BIT    BV(3)
    #define HAL_KEY_SW_2_SEL    P1SEL
    #define HAL_KEY_SW_2_DIR    P1DIR
    
    #define HAL_KEY_SW_1_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_1_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_1_ICTLBIT  BV(2) /* P1IEN - P1.2 enable/disable bit */
    #define HAL_KEY_SW_1_IENBIT   BV(4) /* Mask bit for all of Port_0 */ 
    #define HAL_KEY_SW_1_PXIFG    P1IFG /* Interrupt flag at source */
    
    #define HAL_KEY_SW_2_IEN      IEN2  /* CPU interrupt mask register */
    #define HAL_KEY_SW_2_ICTL     P1IEN /* Port Interrupt Control register */
    #define HAL_KEY_SW_2_ICTLBIT  BV(3) /* P1IEN - P1.3 enable/disable bit */
    #define HAL_KEY_SW_2_IENBIT   BV(4) /* Mask bit for all of Port_0 */ 
    #define HAL_KEY_SW_2_PXIFG    P1IFG /* Interrupt flag at source */
    
    #define HAL_KEY_SW_1_EDGEBIT  BV(1)

    Change:

    HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )

    To:

    HAL_ISR_FUNCTION( halKeyPort1Isr, P1INT_VECTOR )

    Change (in HAL_ISR_FUNCTION):

    HAL_KEY_CPU_PORT_0_IF = 0;

    To:

    HAL_KEY_CPU_PORT_1_IF = 0;

    hal_key.h:

    Change:

    #define HAL_KEY_SW_1 0x01
    #define HAL_KEY_SW_2 0x02

    To:

    #define HAL_KEY_SW_1 0x04 
    #define HAL_KEY_SW_2 0x08

    If there is anything I've forgotten please reply to this post.

  • Hello,

    First of all, thanks for this post that help me figure out how to set up interrupts for other port/s.

    I tried this and it works well, but i have something to confirm about the code snippet below you shared:

    Change:

    #define HAL_KEY_SW_1 0x01
    #define HAL_KEY_SW_2 0x02

    To:

    #define HAL_KEY_SW_1 0x04 
    #define HAL_KEY_SW_2 0x08

    If i understand it correctly, you set HAL_KEY_SW_1 and HAL_KEY_SW_2 to the corresponding pin you want to use as input (from bit 0 and bit 1 to bit 2 and bit 3 respectively) and i also did that.

    But why is it even i didn't made the changes, the interrupt (in my case at P1_2) still works?

    That is all.

    Thanks.

  • You don't have to change

    #define HAL_KEY_SW_1 0x01
    #define HAL_KEY_SW_2 0x02

    to

    #define HAL_KEY_SW_1 0x04
    #define HAL_KEY_SW_2 0x08