Is there a way to address GPIO pins by using an index with the pin numbers in an array on a PSoC6?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Chva_1477726
Level 4
Level 4
25 sign-ins 10 sign-ins First solution authored

Hi

Because inputs (or outputs for that matter) are typically scattered over several ports, it is painful to access all used inputs as you need to get their status one-by-one.

Since the PDL in PSoC Creator for PS0C6 works different to PSoC5 in terms of getting a GPIO pin status, I am wondering if one can use a structure in an array and then address the structure from within the PDL function to access the pins? The idea is to have the same line of code with an index in it to read all the needed pins.

Would this be possible, or more importantly, has anyone done it? If so, can the code be shared?

I need to debounce several inputs and usually do it byte or word wide. With MCU's with UDB 's, you can take your pins to a Status register and read the register to achieve the above goal, but the PS0C6 I need to use, there are no UDB's.

Any useful advice will be appreciated and some working code even more!!

Regards

Chris

0 Likes
1 Solution
MeenakshiR_71
Employee
Employee
100 likes received 50 likes received 25 likes received

Chris,

If you can design/select the pins such that if they all fit in one port, then you can use the Port registers (GPIO_PRT) directly to read the status or update the pin output. This will not only reduce the complexity but also improves efficiency.

If you cannot design/select the pins in the same port, then the PDL does not provide much options. As bob suggested, you can use an array of functions to get the status or update the pin output by indexing the array based on your pin number as index. You can even define your own function to get the combined status (something like below). Or in similar fashion, define APIs for all your needs - set output, drive mode, interrupt status etc. You can even define interrupt APIs and use the IRQ15 (GPIO all ports interrupt) for executing interrupt related processes.

#define NUMBER_OF_PINS  5

typedef struct {

    GPIO_PRT_Type *port;

    uint32 pin;

} gpio_pins;

gpio_pins pinsArray[NUMBER_OF_PINS] =

{

    {GPIO_PRT0, 0},

    {GPIO_PRT1, 1},

    {GPIO_PRT9, 2},

    {GPIO_PRT6, 3},

    {GPIO_PRT10, 4}

};

uint32 Custom_GetAllPinStatus(void)

{

    uint32 retStatus = 0;

   

    for(uint32 index = 0; index < NUMBER_OF_PINS; index++)

    {

        retStatus |= ((Cy_GPIO_Read(pinsArray[index].port, pinsArray[index].pin))<<index);

    }

   

    return retStatus;

}

Let me know if this helps or you need further clarifications.

Regards,

Meenakshi Sundaram R

View solution in original post

0 Likes
4 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Easiest approach will be to use a const array of functions to access the pins. This has the advantage that you can span ports.

Bob

0 Likes
MeenakshiR_71
Employee
Employee
100 likes received 50 likes received 25 likes received

Chris,

If you can design/select the pins such that if they all fit in one port, then you can use the Port registers (GPIO_PRT) directly to read the status or update the pin output. This will not only reduce the complexity but also improves efficiency.

If you cannot design/select the pins in the same port, then the PDL does not provide much options. As bob suggested, you can use an array of functions to get the status or update the pin output by indexing the array based on your pin number as index. You can even define your own function to get the combined status (something like below). Or in similar fashion, define APIs for all your needs - set output, drive mode, interrupt status etc. You can even define interrupt APIs and use the IRQ15 (GPIO all ports interrupt) for executing interrupt related processes.

#define NUMBER_OF_PINS  5

typedef struct {

    GPIO_PRT_Type *port;

    uint32 pin;

} gpio_pins;

gpio_pins pinsArray[NUMBER_OF_PINS] =

{

    {GPIO_PRT0, 0},

    {GPIO_PRT1, 1},

    {GPIO_PRT9, 2},

    {GPIO_PRT6, 3},

    {GPIO_PRT10, 4}

};

uint32 Custom_GetAllPinStatus(void)

{

    uint32 retStatus = 0;

   

    for(uint32 index = 0; index < NUMBER_OF_PINS; index++)

    {

        retStatus |= ((Cy_GPIO_Read(pinsArray[index].port, pinsArray[index].pin))<<index);

    }

   

    return retStatus;

}

Let me know if this helps or you need further clarifications.

Regards,

Meenakshi Sundaram R

0 Likes

Hi Meenakshi

I have already done what you are suggesting on the array of structure. It works very well. I have to make provision for not being able to place all inputs on one port. For developing on a CY8CKIT-062 it is especially important as many pins are already allocated to certain functions. Unless you want to butcher the kit to re-assign the pins by removing and adding components.

Great minds think alike.

Thank you for your response.

Regards

Chris