Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

digitalRead only works with INPUT mode #10

Closed
microbuilder opened this issue Jan 17, 2017 · 1 comment
Closed

digitalRead only works with INPUT mode #10

microbuilder opened this issue Jan 17, 2017 · 1 comment
Labels

Comments

@microbuilder
Copy link
Contributor

digitalRead (wiring_digital.c) only works when the pin is configured as an input, but should also work checking the state of an output pin.

Code

The following code will always return '0' (low) even though the GPIO pin is clearly being toggled:

#include <Arduino.h>

const int baudrate = 115200;

void setup() {
  Serial.begin (baudrate);
  
  pinMode(PIN_LED2, OUTPUT);
}

void loop() {
  int val = 0;

  digitalWrite(PIN_LED2, HIGH);
  val = digitalRead(PIN_LED2);
  Serial.println(val, HEX);
  delay(500);

  digitalWrite(PIN_LED2, LOW);
  val = digitalRead(PIN_LED2);
  Serial.println(val, HEX);
  delay(500);
}

Issue

The nRF5x has separate NRF_GPIO->IN and NRF_GPIO->OUT registers. The current implementation of digitalRead assumes the pins is always configured as an input when we read it, but while this is true 95% of the time we should detect the pin mode and if it is an OUTPUT use the OUT register:

int digitalRead( uint32_t ulPin )
{
  if (ulPin >= PINS_COUNT) {
    return 0;
  }

  ulPin = g_ADigitalPinMap[ulPin];

  return ((NRF_GPIO->IN >> ulPin) & 1UL) ? HIGH : LOW ;
}
@hathach
Copy link
Member

hathach commented Jan 18, 2017

I encounter this bug as well when trying to add digitalToggle(). Will fix and also open issue on sandeep repo as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants