This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Not able to detect GPIO state, reading incorrect GPIO pin value

Hello,

I am working on a custom board using a nRF52840 based module. I am using SDK 14.2

I am running the following simple code to sense when an GPIO goes to a low state(detects SD card insertion) and toggle an LED.

GPIO P0.13 is connected to a SD card detect pin which gets grounded if an SD card is inserted, else it is left floating.

GPIO P0.15 is connected to an anode of an LED. 

int main(void)
{
    nrf_gpio_cfg_input(13, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_output(15);
    uint32_t new_CD;
    while (true)
    {
        new_CD = nrf_gpio_pin_read(13);
	if (new_CD == 0) {
		
                nrf_gpio_pin_toggle(15);
		}
        // Do nothing.
    }
}

However, the controller does not detect a low state on P0.13, when I insert the SD card. I have verified that the pin is indeed grounded using a oscilloscope.

During debugging, the 'new_CD' variable on the watch window is also always '1'.

Why cant I detect a low state on the GPIO correctly?

Thanks

Parents
  • If the pin P0.13 is actually going low when SD card is inserted then I guess the compiler is optimizing away the code around new_CD. I would suggest you to use volatile for new_CD since the way you wrote code gives compiler great flexibility to optimise code. Also set optimizations low or none while testing this.

    Suggestion to change declaration of new_CD like below

       volatile uint32_t new_CD;

Reply
  • If the pin P0.13 is actually going low when SD card is inserted then I guess the compiler is optimizing away the code around new_CD. I would suggest you to use volatile for new_CD since the way you wrote code gives compiler great flexibility to optimise code. Also set optimizations low or none while testing this.

    Suggestion to change declaration of new_CD like below

       volatile uint32_t new_CD;

Children
Related