Skip to content

Commit bf454d8

Browse files
committed
Clean up port, pin, and pin mask duplication in digitalWrite
1 parent 0f3f80a commit bf454d8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

cores/arduino/wiring_digital.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,23 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal )
8282
return ;
8383
}
8484

85-
if ( (PORT->Group[g_APinDescription[ulPin].ulPort].DIRSET.reg & (1ul << g_APinDescription[ulPin].ulPin)) == 0 ) {
85+
EPortType port = g_APinDescription[ulPin].ulPort;
86+
uint32_t pin = g_APinDescription[ulPin].ulPin;
87+
uint32_t pinMask = (1ul << pin);
88+
89+
if ( (PORT->Group[port].DIRSET.reg & pinMask) == 0 ) {
8690
// the pin is not an output, disable pull-up if val is LOW, otherwise enable pull-up
87-
PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].bit.PULLEN = (ulVal != LOW) ;
91+
PORT->Group[port].PINCFG[pin].bit.PULLEN = (ulVal != LOW) ;
8892
}
8993

9094
switch ( ulVal )
9195
{
9296
case LOW:
93-
PORT->Group[g_APinDescription[ulPin].ulPort].OUTCLR.reg = (1ul << g_APinDescription[ulPin].ulPin) ;
97+
PORT->Group[port].OUTCLR.reg = pinMask;
9498
break ;
9599

96100
default:
97-
PORT->Group[g_APinDescription[ulPin].ulPort].OUTSET.reg = (1ul << g_APinDescription[ulPin].ulPin) ;
101+
PORT->Group[port].OUTSET.reg = pinMask;
98102
break ;
99103
}
100104

0 commit comments

Comments
 (0)