Skip to content

Commit 50b35d1

Browse files
committed
DigitalRead and DigitalWrite can be called before PinMode
Implements checks on DigitalRead and DigitalWrite functions. If the requested pin is not initialized, it is assigned to a new DigitalInOut object configured as: - if INPUT: no pull and 0 as default value - if OUTPUT: no pull and the argument value as init value
1 parent 99c4760 commit 50b35d1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cores/arduino/wiring_digital.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ void digitalWrite(pin_size_t pin, PinStatus val)
9191
if (pin >= PINS_COUNT) {
9292
return;
9393
}
94-
digitalPinToGpio(pin)->write(val);
94+
mbed::DigitalInOut* gpio = digitalPinToGpio(pin);
95+
if (gpio == NULL) {
96+
gpio = new mbed::DigitalInOut(digitalPinToPinName(pin), PIN_OUTPUT, PullNone, val);
97+
digitalPinToGpio(pin) = gpio;
98+
}
99+
gpio->write(val);
95100
}
96101

97102
PinStatus digitalRead(PinName pin)
@@ -108,6 +113,11 @@ PinStatus digitalRead(pin_size_t pin)
108113
{
109114
if (pin >= PINS_COUNT) {
110115
return LOW;
116+
}
117+
mbed::DigitalInOut* gpio = digitalPinToGpio(pin);
118+
if (gpio == NULL) {
119+
gpio = new mbed::DigitalInOut(digitalPinToPinName(pin), PIN_INPUT, PullNone, 0);
120+
digitalPinToGpio(pin) = gpio;
111121
}
112-
return (PinStatus) digitalPinToGpio(pin)->read();
122+
return (PinStatus) gpio->read();
113123
}

0 commit comments

Comments
 (0)