From 89d42ba09d1af6bca605f1cf15d9f5965d204b82 Mon Sep 17 00:00:00 2001
From: Jacob Lessing <jacob_lessing@outlook.com>
Date: Sun, 2 Mar 2025 17:16:57 -0500
Subject: [PATCH] Use DIR register for pin direction checking

Replace DIRSET with DIR register when checking if a pin is
configured as output in digitalWrite(). DIR is the designated
register for reading current pin direction state.

According to the SAMD21 datasheet, DIRSET is designed as a
write-only register without guaranteed read behavior, while
DIR explicitly represents the current direction state.

Fixes #727
---
 cores/arduino/wiring_digital.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c
index f9d8b68b1..8dc1e5f30 100644
--- a/cores/arduino/wiring_digital.c
+++ b/cores/arduino/wiring_digital.c
@@ -83,7 +83,7 @@ void digitalWrite( pin_size_t ulPin, PinStatus ulVal )
   uint32_t pin = g_APinDescription[ulPin].ulPin;
   uint32_t pinMask = (1ul << pin);
 
-  if ( (PORT->Group[port].DIRSET.reg & pinMask) == 0 ) {
+  if ( (PORT->Group[port].DIR.reg & pinMask) == 0 ) {
     // the pin is not an output, disable pull-up if val is LOW, otherwise enable pull-up
     PORT->Group[port].PINCFG[pin].bit.PULLEN = ((ulVal == LOW) ? 0 : 1) ;
   }