From 3b6611ae2de2caac0d2baae32f4f05bd2a3bb9b9 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 12 Jun 2010 20:32:58 +0000 Subject: Disabling interrupts while digitalWrite() and pinMode() modify registers (issue #146). Updating revisions. --- cores/arduino/wiring_digital.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'cores/arduino/wiring_digital.c') diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index 1cdbf6c..3529923 100755 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -36,8 +36,17 @@ void pinMode(uint8_t pin, uint8_t mode) // JWS: can I let the optimizer do this? reg = portModeRegister(port); - if (mode == INPUT) *reg &= ~bit; - else *reg |= bit; + if (mode == INPUT) { + uint8_t oldSREG = SREG; + cli(); + *reg &= ~bit; + SREG = oldSREG; + } else { + uint8_t oldSREG = SREG; + cli(); + *reg |= bit; + SREG = oldSREG; + } } // Forcing this inline keeps the callers from having to push their own stuff @@ -90,8 +99,17 @@ void digitalWrite(uint8_t pin, uint8_t val) out = portOutputRegister(port); - if (val == LOW) *out &= ~bit; - else *out |= bit; + if (val == LOW) { + uint8_t oldSREG = SREG; + cli(); + *out &= ~bit; + SREG = oldSREG; + } else { + uint8_t oldSREG = SREG; + cli(); + *out |= bit; + SREG = oldSREG; + } } int digitalRead(uint8_t pin) -- cgit v1.2.3-18-g5258