3 Replies Latest reply on Jul 2, 2019 2:54 AM by BragadeeshV_41

    Dynamically change CapSense pins to Digital Inputs/Outputs


      I'm working on a project where we have 6 CapSense buttons. Our pads consist of an outer ring and a center pad with a metal dome on the outside ring. During CapSense Mode, I would like the outer ring/dome to act as a CapSense button which I have working fine. However, I would like to dynamically change the mode of the CapSense pins to be inputs so the buttons could then become mechanical buttons (The dome gets pressed down and shorts the outer ring with the inner pad).


      Is this something that would be possible? If so, how could I go about implementing this? I know you can change input/output pin configurations/modes on the fly, but I can't find anything on changing from CapSense to input/output pins.

        • 1. Re: Dynamically change CapSense pins to Digital Inputs/Outputs

          Hi TyFo_1975986,


          This can be done by modifying the GPIO_PRTx_PC , HSIOM_PORT_SELx and GPIO_PRTx_PCx registers accordingly.


          When a pin is configured as CapSense button, the pin is set to the following:

          1. Drive mode -> Analog High -Z

          2. HSIOM -.> CapSense sensor CSD

          3. Input buffer forcefully disabled by writing '1' in the GPIO_PRTx_PCx register


          CapSense_ScanAllWidgets() takes care of setting this register every time a sensor is scanned. Therefore calling CapSense_ScanAllWidgets() after modifying the registers (to serve as input or output pin) will be overwritten by CapSense_ScanAllWidgets().


          To modify the CapSense pin to act as digital input pin do the following:


          1. Make sure that there is no active scan. Modify the registers only when there is no scan happening.

          2.  Modify the drive mode to Digital HI Z/  Resitive pull down/ resistive pull up  using GPIO_PRTx_PC register.

          3.  Change the HSIOM using HSIOM_PORT_SELx  register depending on your requirement

          4. Make sure the input buffer is not disabled using GPIO_PRTx_PC2 register.

          5. Read the status of the pin using CYREG_GPIO_PRTx_PS register


          These 3 registers needs to be modified according to whether you want them to be input pin/ output pin/ analog pin.

          Please refer to the architecture TRM and register TRM for more details on when to set what.


          Below is a sample code to convert capsense pin to digital input pin:


          //Pin P2.7 is used here

          #define PIN_NUM (7u)

          #define PIN_MASK (1u << PIN_NUM )


          #define DRIVE_MODE_SHIFT (3u)

          #define DIGITAL_HIZ (1u)


          #define HSIOM_SHIFT (4u)

          #define HSIOM_MODE_GPIO (0u)


          #define PC2_SHIFT (1u)


          void ConfigurePinAsInput()


              /*Change the drive mode to Digital HIZ*/ //Or what you need

            uint32 regVal;


              regVal = CY_GET_REG32(CYREG_GPIO_PRT2_PC);

              regVal = ((regVal &(~(7u << PIN_NUM * DRIVE_MODE_SHIFT))) | (DIGITAL_HIZ << PIN_NUM * DRIVE_MODE_SHIFT));

              CY_SET_REG32(CYREG_GPIO_PRT2_PC, regVal);


            /*Change the HSIOM */

              regVal = CY_GET_REG32(CYREG_HSIOM_PORT_SEL2);

              regVal = ((regVal &(~(15u << PIN_NUM * HSIOM_SHIFT))) | (HSIOM_MODE_GPIO << PIN_NUM * HSIOM_SHIFT));

              CY_SET_REG32(CYREG_HSIOM_PORT_SEL2, regVal);


              /*Change PC2 to enable input buffer*/


              regVal = CY_GET_REG32(CYREG_GPIO_PRT2_PC2);

              regVal = ((regVal &(~(1u << PIN_NUM * PC2_SHIFT))) | (0u << PIN_NUM * PC2_SHIFT));

              CY_SET_REG32(CYREG_GPIO_PRT2_PC2, regVal);



          I've also attached a sample code that swtich between CapSense sensor and input sensor based on a button press in 4100 S device.

          Please let us know your observation.




          • 2. Re: Dynamically change CapSense pins to Digital Inputs/Outputs



            I'm able to configure the pins as a resistive pull-ups like I would like through code. However, It doesn't work after I do CapSense_Start(). Would there be a difference in how this is done between a 4100S and a 4200M device?

            • 3. Re: Dynamically change CapSense pins to Digital Inputs/Outputs

              Hi TyFo_1975986,


              Please let us know what doesn't work.