Skip to content

Commit 9cb152b

Browse files
committed
Added multi address slave
Added begin and setAddressAndMask warnings if called with mask on a uC without mask support
1 parent 0510efd commit 9cb152b

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

Diff for: hardware/arduino/avr/libraries/Wire/Wire.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
2020
Modified 2014 by Nicola Corna ([email protected])
2121
Moved pullups enable from twi.c to Wire.cpp
22+
Added multi address slave
2223
*/
2324

2425
extern "C" {
@@ -83,6 +84,32 @@ void TwoWire::begin(int address)
8384
begin((uint8_t)address);
8485
}
8586

87+
void TwoWire::begin(uint8_t address, uint8_t mask)
88+
{
89+
#ifdef TWAMR
90+
twi_setAddressAndMask(address, mask);
91+
#else
92+
twi_setAddress(address); //Just to show the function warning only once
93+
#endif
94+
twi_attachSlaveTxEvent(onRequestService);
95+
twi_attachSlaveRxEvent(onReceiveService);
96+
begin();
97+
}
98+
99+
void TwoWire::begin(int address, int mask)
100+
{
101+
#ifdef TWAMR
102+
begin((uint8_t)address, (uint8_t)mask);
103+
#else
104+
begin((uint8_t)address); //Just to show the function warning only once
105+
#endif
106+
}
107+
108+
uint8_t TwoWire::slaveAddress()
109+
{
110+
return twi_slaveAddress();
111+
}
112+
86113
void TwoWire::setClock(uint32_t frequency)
87114
{
88115
TWBR = ((F_CPU / frequency) - 16) / 2;

Diff for: hardware/arduino/avr/libraries/Wire/Wire.h

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
20+
Modified 2014 by Nicola Corna ([email protected])
21+
Added multi address slave
2022
*/
2123

2224
#ifndef TwoWire_h
@@ -49,6 +51,14 @@ class TwoWire : public Stream
4951
void begin();
5052
void begin(uint8_t);
5153
void begin(int);
54+
#ifdef TWAMR
55+
void begin(uint8_t, uint8_t);
56+
void begin(int, int);
57+
#else
58+
void begin(uint8_t, uint8_t) __attribute__((warning("I2C address masking is unsupported on this microcontroller. Mask 0x00 will be used.")));
59+
void begin(int, int) __attribute__((warning("I2C address masking is unsupported on this microcontroller. Mask 0x00 will be used.")));
60+
#endif
61+
uint8_t slaveAddress();
5262
void setClock(uint32_t);
5363
void beginTransmission(uint8_t);
5464
void beginTransmission(int);

Diff for: hardware/arduino/avr/libraries/Wire/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#######################################
1212

1313
begin KEYWORD2
14+
slaveAddress KEYWORD2
1415
setClock KEYWORD2
1516
beginTransmission KEYWORD2
1617
endTransmission KEYWORD2

Diff for: hardware/arduino/avr/libraries/Wire/utility/twi.c

+54-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
Modified 2014 by Nicola Corna ([email protected])
2121
Moved pullups enable from twi.c to Wire.cpp
2222
Updated deprecated include <compat/twi.c>
23+
Added multi address slave
2324
*/
2425

2526
#include <math.h>
2627
#include <stdlib.h>
2728
#include <stdbool.h>
2829
#include <inttypes.h>
29-
#include <avr/io.h>
3030
#include <avr/interrupt.h>
3131
#include <util/twi.h>
3232

@@ -61,6 +61,10 @@ static volatile uint8_t twi_rxBufferIndex;
6161

6262
static volatile uint8_t twi_error;
6363

64+
#ifdef TWAMR
65+
static volatile uint8_t twi_lastSlaveAddress;
66+
#endif
67+
6468
/*
6569
* Function twi_init
6670
* Desc readys twi pins and sets twi bitrate
@@ -86,11 +90,16 @@ void twi_init(void)
8690

8791
// enable twi module, acks, and twi interrupt
8892
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
93+
94+
#ifdef TWAMR
95+
// initialize to 0
96+
twi_lastSlaveAddress = 0x00;
97+
#endif
8998
}
9099

91100
/*
92-
* Function twi_slaveInit
93-
* Desc sets slave address and enables interrupt
101+
* Function twi_setAddress
102+
* Desc set the slave address
94103
* Input none
95104
* Output none
96105
*/
@@ -100,6 +109,40 @@ void twi_setAddress(uint8_t address)
100109
TWAR = address << 1;
101110
}
102111

112+
/*
113+
* Function twi_setAddressAndMask
114+
* Desc sets slave address and slave address mask
115+
* Input address: slave address
116+
* mask: slave address mask
117+
* Output none
118+
*/
119+
void twi_setAddressAndMask(uint8_t address, uint8_t mask)
120+
{
121+
// set twi slave address (skip over TWGCE bit)
122+
TWAR = address << 1;
123+
#ifdef TWAMR
124+
//set twi slave address mask
125+
TWAMR = mask << 1;
126+
#endif
127+
}
128+
129+
/*
130+
* Function twi_slaveAddress
131+
* Desc return the last called slave address
132+
* Input none
133+
* Output the slave address
134+
*/
135+
uint8_t twi_slaveAddress(void)
136+
{
137+
//If address masking is not supported, returns the only address
138+
//supported (saved in TWAR)
139+
#ifdef TWAMR
140+
return twi_lastSlaveAddress;
141+
#else
142+
return TWAR >> 1;
143+
#endif
144+
}
145+
103146
/*
104147
* Function twi_readFrom
105148
* Desc attempts to become twi bus master and read a
@@ -441,6 +484,10 @@ ISR(TWI_vect)
441484
case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
442485
// enter slave receiver mode
443486
twi_state = TWI_SRX;
487+
#ifdef TWAMR
488+
// save the current TWDR (slave address)
489+
twi_lastSlaveAddress = TWDR >> 1;
490+
#endif
444491
// indicate that rx buffer can be overwritten and ack
445492
twi_rxBufferIndex = 0;
446493
twi_reply(1);
@@ -482,6 +529,10 @@ ISR(TWI_vect)
482529
case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
483530
// enter slave transmitter mode
484531
twi_state = TWI_STX;
532+
#ifdef TWAMR
533+
// save the current TWDR (slave address)
534+
twi_lastSlaveAddress = TWDR >> 1;
535+
#endif
485536
// ready the tx buffer index for iteration
486537
twi_txBufferIndex = 0;
487538
// set tx buffer length to be zero, to verify if user changes it

Diff for: hardware/arduino/avr/libraries/Wire/utility/twi.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
You should have received a copy of the GNU Lesser General Public
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
Modified 2014 by Nicola Corna ([email protected])
20+
Added multi address slave
1821
*/
1922

2023
#ifndef twi_h
2124
#define twi_h
2225

2326
#include <inttypes.h>
24-
27+
#include <avr/io.h>
28+
2529
//#define ATMEGA8
2630

2731
#ifndef TWI_FREQ
@@ -40,6 +44,12 @@
4044

4145
void twi_init(void);
4246
void twi_setAddress(uint8_t);
47+
#ifdef TWAMR
48+
void twi_setAddressAndMask(uint8_t, uint8_t);
49+
#else
50+
void twi_setAddressAndMask(uint8_t, uint8_t) __attribute__((warning("I2C address masking is unsupported on this microcontroller. Mask 0x00 will be used.")));
51+
#endif
52+
uint8_t twi_slaveAddress(void);
4353
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t);
4454
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t);
4555
uint8_t twi_transmit(const uint8_t*, uint8_t);

0 commit comments

Comments
 (0)