Skip to content

Commit 8d72086

Browse files
committed
Update twi_pins.c
1 parent e6f3f6d commit 8d72086

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

megaavr/libraries/Wire/src/twi_pins.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ bool TWI0_Pins(uint8_t sda_pin, uint8_t scl_pin) {
160160
#if ( (defined(PIN_WIRE_SDA_PINSWAP_1) && PIN_WIRE_SDA_PINSWAP_1 != NOT_A_PIN) || (defined(PIN_WIRE_SDA_PINSWAP_2) && PIN_WIRE_SDA_PINSWAP_2 != NOT_A_PIN) || (defined(PIN_WIRE_SDA_PINSWAP_3) && PIN_WIRE_SDA_PINSWAP_3 != NOT_A_PIN))
161161
// --- Attiny series ---
162162
#if defined(PORTMUX_CTRLB)
163-
#if (defined(PIN_WIRE_SDA_PINSWAP_1) && PIN_WIRE_SDA_PINSWAP_1 != NOT_A_PIN)
163+
#if (defined(PIN_WIRE_SDA_PINSWAP_1) && PIN_WIRE_SDA_PINSWAP_1 != NOT_A_PIN) && (defined(PIN_WIRE_SCL_PINSWAP_2) && PIN_WIRE_SCL_PINSWAP_2 != NOT_A_PIN)
164164
if (sda_pin == PIN_WIRE_SDA_PINSWAP_1 && scl_pin == PIN_WIRE_SCL_PINSWAP_1) {
165165
// Use pin swap
166166
PORTMUX.CTRLB |= PORTMUX_TWI0_bm;
@@ -180,23 +180,24 @@ bool TWI0_Pins(uint8_t sda_pin, uint8_t scl_pin) {
180180
// --- mega0 series ---
181181
#elif defined(PORTMUX_TWISPIROUTEA)
182182
uint8_t twimux = PORTMUX.TWISPIROUTEA & ~PORTMUX_TWI0_gm;
183-
#if (defined(PIN_WIRE_SDA_PINSWAP_2) && PIN_WIRE_SDA_PINSWAP_2 != NOT_A_PIN)
183+
#if (defined(PIN_WIRE_SDA_PINSWAP_2) && PIN_WIRE_SDA_PINSWAP_2 != NOT_A_PIN) && (defined(PIN_WIRE_SCL_PINSWAP_2) && PIN_WIRE_SCL_PINSWAP_2 != NOT_A_PIN)
184184
if (sda_pin == PIN_WIRE_SDA_PINSWAP_2 && scl_pin == PIN_WIRE_SCL_PINSWAP_2) {
185185
twimux |= PORTMUX_TWI0_ALT2_gc;
186186
PORTMUX.TWISPIROUTEA = twimux;
187187
return true;
188-
#endif
189-
/* Can't happen */
190-
#if (defined(PIN_WIRE_SDA_PINSWAP_1) && PIN_WIRE_SDA_PINSWAP_1 != NOT_A_PIN)
188+
}
189+
#endif
190+
/* Can't happen */
191+
#if (defined(PIN_WIRE_SDA_PINSWAP_1) && PIN_WIRE_SDA_PINSWAP_1 != NOT_A_PIN) && (defined(PIN_WIRE_SCL_PINSWAP_1) && PIN_WIRE_SCL_PINSWAP_1 != NOT_A_PIN)
191192
if (sda_pin == PIN_WIRE_SDA_PINSWAP_1 && scl_pin == PIN_WIRE_SCL_PINSWAP_1) {
192193
// Use pin swap
193194
twimux |= PORTMUX_TWI0_ALT1_gc;
194195
PORTMUX.TWISPIROUTEA = twimux;
195196
return true;
196197
}
197-
/* end can'thappen */x
198+
/* end can'thappen */x
198199
#endif
199-
} else if (sda_pin == PIN_WIRE_SDA && scl_pin == PIN_WIRE_SCL) {
200+
else if (sda_pin == PIN_WIRE_SDA && scl_pin == PIN_WIRE_SCL) {
200201
// Use default configuration
201202
twimux &= ~PORTMUX_TWI0_gm;
202203
return true;
@@ -208,13 +209,13 @@ bool TWI0_Pins(uint8_t sda_pin, uint8_t scl_pin) {
208209
// --- Dx series ---
209210
#elif defined(PORTMUX_TWIROUTEA)
210211
uint8_t portmux = (PORTMUX.TWIROUTEA & ~PORTMUX_TWI0_gm);
211-
#if (defined(PIN_WIRE_SDA_PINSWAP_3) && PIN_WIRE_SDA_PINSWAP_3 != NOT_A_PIN)
212+
#if (defined(PIN_WIRE_SDA_PINSWAP_3) && PIN_WIRE_SDA_PINSWAP_3 != NOT_A_PIN) && (defined(PIN_WIRE_SCL_PINSWAP_3) && PIN_WIRE_SCL_PINSWAP_3 != NOT_A_PIN)
212213
if (sda_pin == PIN_WIRE_SDA_PINSWAP_3 && scl_pin == PIN_WIRE_SCL_PINSWAP_3) {
213214
PORTMUX.TWIROUTEA = portmux | PORTMUX_TWI0_ALT3_gc;
214215
return true;
215216
} else
216217
#endif
217-
#if (defined(PIN_WIRE_SDA_PINSWAP_2) && PIN_WIRE_SDA_PINSWAP_2 != NOT_A_PIN)
218+
#if (defined(PIN_WIRE_SDA_PINSWAP_2) && PIN_WIRE_SDA_PINSWAP_2 != NOT_A_PIN) && (defined(PIN_WIRE_SCL_PINSWAP_2) && PIN_WIRE_SCL_PINSWAP_2 != NOT_A_PIN)
218219
#if !defined(ERRATA_TWI0_MUX2)
219220
if (sda_pin == PIN_WIRE_SDA_PINSWAP_2 && scl_pin == PIN_WIRE_SCL_PINSWAP_2) {
220221
PORTMUX.TWIROUTEA = portmux | PORTMUX_TWI0_ALT2_gc;
@@ -224,13 +225,13 @@ bool TWI0_Pins(uint8_t sda_pin, uint8_t scl_pin) {
224225
#endif
225226
} else
226227
#endif
227-
#if (defined(PIN_WIRE_SDA_PINSWAP_1) && PIN_WIRE_SDA_PINSWAP_1 != NOT_A_PIN)
228+
#if (defined(PIN_WIRE_SDA_PINSWAP_1) && PIN_WIRE_SDA_PINSWAP_1 != NOT_A_PIN) && (defined(PIN_WIRE_SCL_PINSWAP_1) && PIN_WIRE_SCL_PINSWAP_1 != NOT_A_PIN)
228229
if (sda_pin == PIN_WIRE_SDA_PINSWAP_1 && scl_pin == PIN_WIRE_SCL_PINSWAP_1) {
229230
PORTMUX.TWIROUTEA = portmux | PORTMUX_TWI0_ALT1_gc;
230231
return true;
231232
} else
232233
#endif
233-
#if defined(PIN_WIRE_SDA)
234+
#if (defined(PIN_WIRE_SDA) && PIN_WIRE_SDA != NOT_A_PIN) && (defined(PIN_WIRE_SCL) && PIN_WIRE_SCL != NOT_A_PIN)
234235
if (sda_pin == PIN_WIRE_SDA && scl_pin == PIN_WIRE_SCL) {
235236
// Use default configuration
236237
PORTMUX.TWIROUTEA = portmux;

0 commit comments

Comments
 (0)