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

nrf_gpio_pin_read() return always 0 with a GPIO as output

Hi all,

i need to read the status on my GPIO where there is a led.

I have configured it like a:

     nrf_drv_gpiote_out_config_t configLed = GPIOTE_CONFIG_OUT_SIMPLE(true);
      nrf_drv_gpiote_out_init(LED_2, &configLed);
      nrf_drv_gpiote_out_init(LED_1, &configLed);
      nrf_drv_gpiote_out_init(LED_3, &configLed);

but if i try to read the value with nrf_gpio_pin_read() return always 0.

What does it means "

 * @brief Function for reading the input level of a GPIO pin.
  Note that the pin must have input connected for the value
  returned from this function to be valid."
  • If pin got damaged due to ex. sourcing too much current (i don't know if that is possible, probably it is), You won't know that by reading that register anyway. Reading NRF_GPIO->OUT register just tells you the state of the register.

  • ok thanks, you are right. But if the register is set and led not switch on means that or that gpio is broken ore led, but not the entire micro

  • That is why I said that test does not have any sense. If the entire micro is broken, it does not run any code lol.

  • The scope is understand if the gpio or led is broken or entire micro. So it has sense, from my point of view. And i think that can be useful reading the gpio output level

  • Hello Annapalu

    As Wojtek already stated you cannot use the nrf_gpio_pin_read to read a pin set as an output. If you look at figure 21, page 112 in the nRF52832 product specification you can see the PIN(0).IN register, it is this register that is read by the gpio_pin_read function. When a pin is set as an output the PIN(0).CNF.DIR switch will close on the output side and open on the input side, meaning it is physically impossible to read the actual output value of the pin with the input register.

    Wojtek is also correct that the NRF_GPIO->OUT only shows whether you have set the register, and not the actual pin state.

    To achieve what you want you could connect a second GPIO, configured as an input, to the same circuit. This would tell you whether the voltage is high or low.

    If you need to see a more accurate reading of the voltage level, to detect deviation, you could use the ADC or Comparator.

    EDIT: 23.03.17 14:40

    I have looked further into this and it seems I was a bit hasty in my reply. The product specification shows the control signal of the two above mentioned switches are different. PIN(0).CNF.DIR for the output and PIN(0).CNF.INPUT for the input buffer.So it seems it is possible to read the output value (high or low) of an output pin using the nrf_gpio_pin_read function, if you both set the direction of the pin to output, and connect the input buffer. This can be done with the nrf_gpio_cfg() function. Note that the nrf_gpio_cfg_output() function disconnects the input buffer.

    I have tested it on the PCA10040 both with a led toggling based on it's own pin_read value, and with 4 leds switching based on the input value of one of the other led pins, all are configured as outputs.

    Best regards

    Jørn Frøysa

Related