Skip to content

Commit 602fb66

Browse files
authored
Merge pull request #5 from espreng/api-avr4809-master
Bug fix: check for activity on double bonded pins
2 parents a56c001 + 65ef786 commit 602fb66

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

cores/arduino/wiring_analog.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ void analogReference(uint8_t mode)
6969
int analogRead(uint8_t pin)
7070
{
7171
if(pin > NUM_ANALOG_INPUTS) return NOT_A_PIN;
72+
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;
7277

7378
uint8_t low, high;
7479

@@ -117,6 +122,15 @@ void analogWrite(uint8_t pin, int val)
117122
uint8_t bit_pos = digitalPinToBitPosition(pin);
118123
if(bit_pos == NOT_A_PIN) return;
119124

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;
133+
120134
// We need to make sure the PWM output is enabled for those pins
121135
// that support it, as we turn it off when digitally reading or
122136
// writing with them. Also, make sure the pin is in output mode

cores/arduino/wiring_digital.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ void pinMode(uint8_t pin, PinMode mode)
3131
uint8_t bit_pos = digitalPinToBitPosition(pin);
3232

3333
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;
3443

3544
PORT_t* port = digitalPinToPortStruct(pin);
3645
if(port == NULL) return;
@@ -137,6 +146,15 @@ void digitalWrite(uint8_t pin, PinStatus val)
137146
/* Get bit mask for pin */
138147
uint8_t bit_mask = digitalPinToBitMask(pin);
139148
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;
140158

141159
/* Turn off PWM if applicable */
142160

@@ -210,6 +228,15 @@ PinStatus digitalRead(uint8_t pin)
210228
/* Get bit mask and check valid pin */
211229
uint8_t bit_mask = digitalPinToBitMask(pin);
212230
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;
213240

214241
// If the pin that support PWM output, we need to turn it off
215242
// before getting a digital reading.

0 commit comments

Comments
 (0)