AnsweredAssumed Answered

Second Read of Pin with GPIO_PinRead Crashes after PORT_SetPinConfig()

Question asked by James Morrison on Apr 10, 2019
Latest reply on Apr 17, 2019 by Con Verse

Hello,

 

I'm developing some code for my eventual target using the FRDM-KL03Z board as a starting platform.  I'm trying to write some code that will do a quick power-on self-test and make sure that each GPIO pin is not shorted to power or GND (eventually I'll add or any other IO pin).

 

I started with one of the example projects (RTC) and modified from there.  The code seems to work well in general and I can control I/O as expected.  But now I'm messing with the GPIO pin directions and configs and there seems to be a problem.

 

Below is the code of my main.c.  You can see the initialization routines that are called.  What I'm trying to do is take over the GPIO, set them as inputs, turn pull-ups and pull-downs ON and OFF, read the pin state to make sure it is what it should be.  When this is all done I plan to call BOARD_InitPins() again to get the I/O set back to the way I need it for my application.

 

I've had test code that set the pull-up/-down on and off (with a for loop delay between) and I can see the pin toggling with an oscilloscope, even if I do it many times consecutively.  So that seems to work fine.

 

But the second time I call GPIO_PinRead() it seems to crash.  I can't figure out why.  I saw another post saying something about the clock not being active for an unused port, but that doesn't seem to be the case here as the first call works fine.  The crash only seems to happen if I use GPIO_PinRead().  If I don't actually read the pin state then the code runs fine.

 

Any ideas?

 

 

 

    rtc_config_t rtcConfig;
    static int count=0;
    tpm_config_t tpm_config;
    lptmr_config_t lptmr_config;

 

    uint32_t x;

 

    /* Board pin, clock, debug console init */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

 

    LED_GREEN_OFF();
    LED_RED_OFF();
    LED_BLUE_OFF();

 

    // test LED Green pin for shorts
    gpio_pin_config_t input_pin_config =
    {
        kGPIO_DigitalInput,
        0,
    };

 

    GPIO_PinInit(BOARD_INITPINS_LED_GREEN_GPIO, BOARD_INITPINS_LED_GREEN_PIN, &input_pin_config);

 

    port_pin_config_t port_config = {/* Internal pull-up resistor is enabled */
                                                  kPORT_PullUp,
                                                 /* Slow slew rate is configured */
                                                 kPORT_SlowSlewRate,
                                                 /* Passive filter is disabled */
                                                 kPORT_PassiveFilterDisable,
                                                 /* Low drive strength is configured */
                                                 kPORT_LowDriveStrength,
                                                 /* Pin is configured as GPIO */
                                                 kPORT_MuxAsGpio};
    /* configure pin as GPIO with pull-up */
    PORT_SetPinConfig(BOARD_INITPINS_LED_GREEN_PORT, BOARD_INITPINS_LED_GREEN_PIN, &port_config);

 

    // read pin to ensure that it is indeed HIGH, if not, there is an external short on the board
    if( !(GPIO_PinRead(BOARD_INITPINS_LED_GREEN_GPIO, BOARD_INITPINS_LED_GREEN_PIN)) )
    {
        LED_BLUE_ON();
        while(1);
    }

 

    port_pin_config_t pull_down_config = {/* Internal pull-down resistor is enabled */
                                                  kPORT_PullDown,
                                                 /* Slow slew rate is configured */
                                                 kPORT_SlowSlewRate,
                                                 /* Passive filter is disabled */
                                                 kPORT_PassiveFilterDisable,
                                                 /* Low drive strength is configured */
                                                 kPORT_LowDriveStrength,
                                                 /* Pin is configured as GPIO */
                                                 kPORT_MuxAsGpio};

 

//    port_config.pullSelect = kPORT_PullDown;
    /* configure pin as GPIO with pull-down */
    PORT_SetPinConfig(BOARD_INITPINS_LED_GREEN_PORT, BOARD_INITPINS_LED_GREEN_PIN, &pull_down_config);

 

    for(count=0;count<100;count++);

 

    // read pin to ensure that it is indeed HIGH, if not, there is an external short on the board
    if( (GPIO_PinRead(BOARD_INITPINS_LED_GREEN_GPIO, BOARD_INITPINS_LED_GREEN_PIN)) )
    {
        LED_BLUE_ON();
        while(1);
    }

Outcomes