You are here

More PDL Examples - Wiggling Pins | Cypress Semiconductor

More PDL Examples - Wiggling Pins

Let's see how many ways we can wiggle a pin! OK, there are too many, so let's limit the discussion to firmware-driven pins and just mess about with some more PDL functions instead! I'll point out a few tips and tricks along the way.

I'll start with the humble output pin and I'll wiggle pin 7 on port 13 because it is connected to LED9 (the red one) on the Pioneer kit. The full-featured, no-holds-barred, control-freaks-and-experts-only method is to use the Cy_GPIO_Pin_Init() function. It looks innocent enough, with just three arguments for the port, pin number, and a pointer to a configuration struct.

cy_en_gpio_status_t Cy_GPIO_Pin_Init(
    GPIO_PRT_Type * base,
    uint32_t pinNum,
    const cy_stc_gpio_pin_config_t * config );

The first two are just telling the function which pin to initialize. But that configuration guy is a bit mean!

typedef struct {
    uint32_t outVal;         /**< Pin output state */
    uint32_t driveMode;      /**< Drive mode */
    en_hsiom_sel_t hsiom;    /**< HSIOM selection */
    uint32_t intEdge;        /**< Interrupt Edge type */
    uint32_t intMask;        /**< Interrupt enable mask */
    uint32_t vtrip;          /**< Input buffer voltage trip type */
    uint32_t slewRate;       /**< Output buffer slew rate */
    uint32_t driveSel;       /**< Drive strength */
    uint32_t vregEn;         /**< SIO pair output buffer mode */
    uint32_t ibufMode;       /**< SIO pair input buffer mode */
    uint32_t vtripSel;       /**< SIO pair input buffer trip point */
    uint32_t vrefSel;        /**< SIO pair reference voltage for input buffer trip point */
    uint32_t vohSel;         /**< SIO pair regulated voltage output level */
} cy_stc_gpio_pin_config_t;

OK! Breathe! Don't be scared! I told you this was the expert method! Actually it is easier than it looks and this program should illustrate that.

    cy_stc_gpio_pin_config_t pinConfig =
    {
        .outVal =       1UL,                    
        .driveMode =    CY_GPIO_DM_STRONG,      
        .hsiom =        0,                      
        .intEdge =      CY_GPIO_INTR_DISABLE,
        .intMask =      0UL,
        .vtrip =        CY_GPIO_VTRIP_CMOS,
        .slewRate =     CY_GPIO_SLEW_FAST,
        .driveSel =     CY_GPIO_DRIVE_FULL,
        .vregEn =       0UL,                 
        .ibufMode =     0UL,                   
        .vtripSel =     0UL,                 
        .vrefSel =      0UL,                   
        .vohSel =       0UL               
    };
    
    Cy_GPIO_Pin_Init( P13_7_PORT, P13_7_NUM, &pinConfig );
    for(;;)
    {
        Cy_GPIO_Write( P13_7_PORT, P13_7_NUM, ! Cy_GPIO_Read( P13_7_PORT, P13_7_NUM ) );
        Cy_SysLib_Delay( 100 );
    }

Walking through the code, I initialize the pinConfig struct as follows.

  • outVal is the initial state of the pin (what do I want it to be after initialization and before the wiggling starts)
  • driveMode determines whether it is an input or output and whatr to do when it is coonnected to external hardware (strong means it will control the external LED)
  • hsiom is the internal connection, which I am not using because this is a firmware-controlled pin
  • intEdge is the interrupt type for an input pin
  • intMask enables the port interrupt for this pin
  • vtrip determines the voltage at which an input pin changes state
  • slewRate is the speed at which the pin responds to a change - either fast or slow
  • driveSel determines the strength to drive the pin

Most of that is pretty obvious really. After driveSel there are a bunch of SIO-specific options that do not apply to simple GPIOs.

So, I have initialized my pin and then I wiggle it by reading its state, inverting that, writing it back to the pin and then waiting for 100 milliseconds before repeating the process. Blinky! Now, before you despair of all the code you are going to need for your pins, let's make this easier. There is a very friendly alternative to the Cy_GPIO_Pin_Init() function called Cy_GPIO_Pin_FastInit(). This guy hides all the difficult stuff that most of us never use and just asks you for the drive mode and the connection. Here is my simplified blinky.

    Cy_GPIO_Pin_FastInit( P13_7_PORT, P13_7_NUM, CY_GPIO_DM_STRONG, 1, HSIOM_SEL_GPIO );
    for(;;)
    {
        Cy_GPIO_Write( P13_7_PORT, P13_7_NUM, ! Cy_GPIO_Read( P13_7_PORT, P13_7_NUM ) );
        Cy_SysLib_Delay( 100 );
    }

All that configuration data has gone away, which I like, but I still have that ugly read-modify-write code with Cy_GPIO_Read() and Cy_GPIO_Write(). PSoC 6 pins have a handy "invert" bit and the PDL gives us a handy Cy_GPIO_Inv() function that writes to it and makes my program even simpler.

    Cy_GPIO_Pin_FastInit( P13_7_PORT, P13_7_NUM, CY_GPIO_DM_STRONG, 1, HSIOM_SEL_GPIO );
    for(;;)
    {
        Cy_GPIO_Inv( P13_7_PORT, P13_7_NUM );
        Cy_SysLib_Delay( 100 );
    }

I think this is a really compact program but I can make it even simpler and more maintainable (is that a real word?). You may have noticed that I have not used the schematic file yet. My next step is to use a pin component (GPIO_PDL) and lock it to P13.7 in the resources file.
 

PSoC Output Pin Customizer

PSoC Creator Pin Selection

By doing this, PSoC Creator initializes the pin for me and so my FastInit call goes away. It also creates macros for the port and pin, so my new program looks like this.

    for(;;)
    {
        Cy_GPIO_Inv( LED9_PORT, LED9_NUM );
        Cy_SysLib_Delay( 100 );
    }

There is another reason for working this way - maintainability (yeah, it's a word!). In the first three examples I write directly to the pin registers from firmware. That's fine but if I ever want to wiggle a different pin I need to edit all the code. I can make that better by defining a couple of macros (e.g. #define LED9_PORT P13_7_PORT) and then I can change the pin with a simple edit and rebuild. That works really well but you need to remember all the pins you are accessing in firmware this way and be sure not to map another function to them in the PSoC Creator resource file. Your results will not be super if you try to write to a pin that is being driven from a PWM!!!

Before I go, there is one more function I want to tell you about - Cy_GPIO_ReadOut(). It is a lot like Cy_GPIO_Read() except it reads the state of output driver instead of the input buffer. This is important because the input buffer only refelects the state of the output if the pin is configured to be both and input and an output.
 

PSoC Creator Customizer Output and Input


If I do not check that extra box then Cy_GPIO_Read() will keep reading the same value, and my LED will not blink, whereas Cy_GPIO_ReadOut() always behaves nicely. Just for fun, here is one last version of blinky.

    for(;;)
    {
        Cy_GPIO_Write( LED9_PORT, LED9_NUM, ! Cy_GPIO_ReadOut( LED9_PORT, LED9_NUM ) );
        Cy_SysLib_Delay( 100 );
    }

At the start of this blog I intended to cover both output and input GPIO. But I think I've filled your heads up, so I'll stop now and will write about inputs next week.
 

Blog: 

Comments

marthawilliam008_3278421's picture

nice

vxh01011_3284531's picture

This is introducing psoc creator issued news and information about pdl example wiggling. The instructions you have to read for pay someone to do your assignment australia assignments and essays for all of them.

ianmugoya18_3285061's picture

Me thinks more information would help the few of us with little awareness of the same.

acrepairslasvegas.com

 

antonia.cummins22_3287486's picture

In this manner, relaxin may expand the sum and rate of tooth development through its impact on the periodontal tendon (PDL). The motivation behind this examination was to quantify the impact of relaxin on orthodontic Write My Essay For Me development and PDL structures. In-vitro motorized challenging and tooth adaptability evaluations showed that the PDL of the mandibular fangs in the relaxin preserved rats had lessened yield load, strain, and firmness. The greater part of this examination has concentrated on operators that impact bone digestion, for example, parathyroid hormone, estrogen, and phosphodiesterase.

mariiykedo_3304631's picture

That is an interesting approach. However I https://paperwritingservice.reviews/penmypaper-review/ still do not understand what should stay or go. Of course language is influenced by different factors every time period, but anyway it is a significant feature of every people.

tomaswilson18_3321431's picture

Thanks for sharing this information.. these metrics will come in handy when i am making my tankless water heater Useful in every sense. Thank you.

tomaswilson18_3321431's picture

Abit complicated for me but i appreciate the effort. As most ac repair near me involve less complicated calculations. 

ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.