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
6262static 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
0 commit comments