2626#include "wiring_private.h"
2727#include "pins_arduino.h"
2828
29+ bool isDoubleBondedActive (uint8_t pin ) __attribute__((weak ));
30+
2931void pinMode (uint8_t pin , PinMode mode )
3032{
3133 uint8_t bit_pos = digitalPinToBitPosition (pin );
3234
33- if ((bit_pos == NOT_A_PIN )|| (mode > INPUT_PULLUP )) return ;
34-
35- /* Check if TWI is operating on double bonded pin (Master Enable is high
36- in both Master and Slave mode for bus error detection, so this can
37- indicate an active state for Wire) */
38- if (((pin == PIN_A4 ) || (pin == PIN_A5 )) && (TWI0 .MCTRLA & TWI_ENABLE_bm )) return ;
39-
40- /* Special check for SPI_SS double bonded pin -- no action if SPI is active
41- (Using SPI Enable bit as indicator of SPI activity) */
42- if ((pin == 10 ) && (SPI0 .CTRLA & SPI_ENABLE_bm )) return ;
35+ if ((bit_pos == NOT_A_PIN ) || (mode > INPUT_PULLUP ) || isDoubleBondedActive (pin )) return ;
4336
4437 PORT_t * port = digitalPinToPortStruct (pin );
4538 if (port == NULL ) return ;
@@ -145,16 +138,7 @@ void digitalWrite(uint8_t pin, PinStatus val)
145138{
146139 /* Get bit mask for pin */
147140 uint8_t bit_mask = digitalPinToBitMask (pin );
148- if (bit_mask == NOT_A_PIN ) return ;
149-
150- /* Check if TWI is operating on double bonded pin (Master Enable is high
151- in both Master and Slave mode for bus error detection, so this can
152- indicate an active state for Wire) */
153- if (((pin == PIN_A4 ) || (pin == PIN_A5 )) && (TWI0 .MCTRLA & TWI_ENABLE_bm )) return ;
154-
155- /* Special check for SPI_SS double bonded pin -- no action if SPI is active
156- (Using SPI Enable bit as indicator of SPI activity) */
157- if ((pin == 10 ) && (SPI0 .CTRLA & SPI_ENABLE_bm )) return ;
141+ if (bit_mask == NOT_A_PIN || isDoubleBondedActive (pin )) return ;
158142
159143 /* Turn off PWM if applicable */
160144
@@ -189,7 +173,6 @@ void digitalWrite(uint8_t pin, PinStatus val)
189173 /* Restore system status */
190174 SREG = status ;
191175
192-
193176 /* Input direction */
194177 } else {
195178 /* Old implementation has side effect when pin set as input -
@@ -218,7 +201,6 @@ void digitalWrite(uint8_t pin, PinStatus val)
218201
219202 /* Restore system status */
220203 SREG = status ;
221-
222204 }
223205
224206}
@@ -227,16 +209,7 @@ PinStatus digitalRead(uint8_t pin)
227209{
228210 /* Get bit mask and check valid pin */
229211 uint8_t bit_mask = digitalPinToBitMask (pin );
230- if (bit_mask == NOT_A_PIN ) return LOW ;
231-
232- /* Check if TWI is operating on double bonded pin (Master Enable is high
233- in both Master and Slave mode for bus error detection, so this can
234- indicate an active state for Wire) */
235- if (((pin == PIN_A4 ) || (pin == PIN_A5 )) && (TWI0 .MCTRLA & TWI_ENABLE_bm )) return LOW ;
236-
237- /* Special check for SPI_SS double bonded pin -- no action if SPI is active
238- (Using SPI Enable bit as indicator of SPI activity) */
239- if ((pin == 10 ) && (SPI0 .CTRLA & SPI_ENABLE_bm )) return ;
212+ if (bit_mask == NOT_A_PIN || isDoubleBondedActive (pin )) return LOW ;
240213
241214 // If the pin that support PWM output, we need to turn it off
242215 // before getting a digital reading.
0 commit comments