Skip to content

Commit f9a60fb

Browse files
committed
Move DoubleBonded handling into variant
If the board does not use double bonding the weak (empty) function is called (no need to redeclare it in the variant)
1 parent c2398dd commit f9a60fb

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

cores/arduino/Arduino.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ extern const uint8_t PROGMEM digital_pin_to_interrupt[];
117117
#define TIMERB2 4
118118
#define TIMERB3 5
119119

120-
void setup_timers() __attribute__((weak));
120+
void setup_timers();
121+
bool isDoubleBondedActive(uint8_t pin);
121122

122123
#define digitalPinToPort(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_port + pin) : NOT_A_PIN )
123124
#define digitalPinToBitPosition(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_position + pin) : NOT_A_PIN )

cores/arduino/wiring.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,6 @@ void init()
378378
/*************************** ENABLE GLOBAL INTERRUPTS *************************/
379379

380380
sei();
381-
}
381+
}
382+
383+
void setup_timers(void) __attribute__((weak));

cores/arduino/wiring_analog.c

+5-14
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ int analogRead(uint8_t pin)
7070
{
7171
if(pin > NUM_ANALOG_INPUTS) return NOT_A_PIN;
7272

73-
/* Check if TWI is operating on double bonded pin (Master Enable is high
74-
in both Master and Slave mode for bus error detection, so this can
75-
indicate an active state for Wire) */
76-
if(((pin == PIN_A4) || (pin == PIN_A5)) && (TWI0.MCTRLA & TWI_ENABLE_bm)) return 0;
73+
/* Check if TWI is operating on double bonded pin (Master Enable is high
74+
in both Master and Slave mode for bus error detection, so this can
75+
indicate an active state for Wire) */
76+
if(isDoubleBondedActive(pin)) return 0;
7777

7878
uint8_t low, high;
7979

@@ -120,16 +120,7 @@ void analogWrite(uint8_t pin, int val)
120120
{
121121

122122
uint8_t bit_pos = digitalPinToBitPosition(pin);
123-
if(bit_pos == NOT_A_PIN) return;
124-
125-
/* Special check for SPI_SS double bonded pin -- no action if SPI is active
126-
(Using SPI Enable bit as indicator of SPI activity) */
127-
if((pin == 10) && (SPI0.CTRLA & SPI_ENABLE_bm)) return;
128-
129-
/* Check if TWI is operating on double bonded pin (Master Enable is high
130-
in both Master and Slave mode for bus error detection, so this can
131-
indicate an active state for Wire) */
132-
if(((pin == PIN_A4) || (pin == PIN_A5)) && (TWI0.MCTRLA & TWI_ENABLE_bm)) return;
123+
if(bit_pos == NOT_A_PIN || isDoubleBondedActive(pin)) return;
133124

134125
// We need to make sure the PWM output is enabled for those pins
135126
// that support it, as we turn it off when digitally reading or

cores/arduino/wiring_digital.c

+5-32
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,13 @@
2626
#include "wiring_private.h"
2727
#include "pins_arduino.h"
2828

29+
bool isDoubleBondedActive(uint8_t pin) __attribute__((weak));
30+
2931
void 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.

variants/uno2018/variant.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include "pins_arduino.h"
2+
#include <stdbool.h>
3+
4+
#define FORCE_INLINE __attribute__((always_inline)) inline
25

36
void setup_timers() {
47

@@ -73,4 +76,17 @@ void setup_timers() {
7376
// timer_B++;
7477
//
7578
// }
76-
}
79+
}
80+
81+
FORCE_INLINE bool isDoubleBondedActive(uint8_t pin) {
82+
/* Check if TWI is operating on double bonded pin (Master Enable is high
83+
in both Master and Slave mode for bus error detection, so this can
84+
indicate an active state for Wire) */
85+
if(((pin == PIN_A4) || (pin == PIN_A5)) && (TWI0.MCTRLA & TWI_ENABLE_bm)) return true;
86+
87+
/* Special check for SPI_SS double bonded pin -- no action if SPI is active
88+
(Using SPI Enable bit as indicator of SPI activity) */
89+
if((pin == 10) && (SPI0.CTRLA & SPI_ENABLE_bm)) return true;
90+
91+
return false;
92+
}

0 commit comments

Comments
 (0)