Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef4482a

Browse files
edbaafifacchinm
authored andcommitted
Wire library support for TWI General Call
Squash and rebase of arduino/Arduino#49
1 parent 6c861d8 commit ef4482a

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
lines changed

libraries/Wire/src/Wire.cpp

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ uint8_t TwoWire::txBufferLength = 0;
4242
uint8_t TwoWire::transmitting = 0;
4343
void (*TwoWire::user_onRequest)(void);
4444
void (*TwoWire::user_onReceive)(int);
45+
void (*TwoWire::user_onGcallReceive)(int);
4546

4647
// Constructors ////////////////////////////////////////////////////////////////
4748

@@ -273,12 +274,22 @@ void TwoWire::flush(void)
273274
}
274275

275276
// behind the scenes function that is called when data is received
276-
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
277+
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes, uint8_t isGeneralCall)
277278
{
278-
// don't bother if user hasn't registered a callback
279-
if(!user_onReceive){
280-
return;
281-
}
279+
//check if general call or normal receive
280+
if(isGeneralCall){
281+
// don't bother if user hasn't registered a general call callback
282+
if(!user_onGcallReceive){
283+
return;
284+
}
285+
}
286+
else{
287+
// don't bother if user hasn't registered a callback
288+
if(!user_onReceive){
289+
return;
290+
}
291+
}
292+
282293
// don't bother if rx buffer is in use by a master requestFrom() op
283294
// i know this drops data, but it allows for slight stupidity
284295
// meaning, they may not have read all the master requestFrom() data yet
@@ -293,8 +304,15 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
293304
// set rx iterator vars
294305
rxBufferIndex = 0;
295306
rxBufferLength = numBytes;
296-
// alert user program
297-
user_onReceive(numBytes);
307+
308+
if(isGeneralCall){
309+
// alert user's general call receive callback
310+
user_onGcallReceive(numBytes);
311+
}
312+
else{
313+
// alert user's receive callback
314+
user_onReceive(numBytes);
315+
}
298316
}
299317

300318
// behind the scenes function that is called when data is requested
@@ -312,16 +330,27 @@ void TwoWire::onRequestService(void)
312330
user_onRequest();
313331
}
314332

315-
// sets function called on slave write
316-
void TwoWire::onReceive( void (*function)(int) )
333+
// sets function called when slave receives data from master at slave's address
334+
void TwoWire::onReceive( void (*recFunction)(int) )
335+
{
336+
user_onReceive = recFunction;
337+
}
338+
339+
// sets function called when slave receives data from master at slaves address
340+
// and another function called when slave receives data from a general call
341+
void TwoWire::onReceive( void (*recFunction)(int), void (*gcallRecFunction)(int) )
317342
{
318-
user_onReceive = function;
343+
user_onReceive = recFunction;
344+
user_onGcallReceive = gcallRecFunction;
345+
346+
//enable general call addressing
347+
twi_enableGenCall();
319348
}
320349

321-
// sets function called on slave read
322-
void TwoWire::onRequest( void (*function)(void) )
350+
// sets function called when master requests data from slave
351+
void TwoWire::onRequest( void (*reqFunction)(void) )
323352
{
324-
user_onRequest = function;
353+
user_onRequest = reqFunction;
325354
}
326355

327356
// Preinstantiate Objects //////////////////////////////////////////////////////

libraries/Wire/src/Wire.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class TwoWire : public Stream
4545
static uint8_t transmitting;
4646
static void (*user_onRequest)(void);
4747
static void (*user_onReceive)(int);
48+
static void (*user_onGcallReceive)(int);
4849
static void onRequestService(void);
49-
static void onReceiveService(uint8_t*, int);
50+
static void onReceiveService(uint8_t*, int,uint8_t);
5051
public:
5152
TwoWire();
5253
void begin();
@@ -70,6 +71,7 @@ class TwoWire : public Stream
7071
virtual int peek(void);
7172
virtual void flush(void);
7273
void onReceive( void (*)(int) );
74+
void onReceive( void (*)(int), void (*)(int) );
7375
void onRequest( void (*)(void) );
7476

7577
inline size_t write(unsigned long n) { return write((uint8_t)n); }

libraries/Wire/src/utility/twi.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static volatile uint8_t twi_sendStop; // should the transaction end with a sto
4444
static volatile uint8_t twi_inRepStart; // in the middle of a repeated start
4545

4646
static void (*twi_onSlaveTransmit)(void);
47-
static void (*twi_onSlaveReceive)(uint8_t*, int);
47+
static void (*twi_onSlaveReceive)(uint8_t*, int, uint8_t);
4848

4949
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
5050
static volatile uint8_t twi_masterBufferIndex;
@@ -58,6 +58,8 @@ static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
5858
static volatile uint8_t twi_rxBufferIndex;
5959

6060
static volatile uint8_t twi_error;
61+
static volatile uint8_t twi_gcall_data;
62+
6163

6264
/*
6365
* Function twi_init
@@ -114,8 +116,8 @@ void twi_disable(void)
114116
*/
115117
void twi_setAddress(uint8_t address)
116118
{
117-
// set twi slave address (skip over TWGCE bit)
118-
TWAR = address << 1;
119+
// set twi slave address and leave TWGCE bit (LSB)
120+
TWAR = (address << TWA0) | ( TWAR & (1 << TWGCE) ) ;
119121
}
120122

121123
/*
@@ -134,6 +136,18 @@ void twi_setFrequency(uint32_t frequency)
134136
It is 72 for a 16mhz Wiring board with 100kHz TWI */
135137
}
136138

139+
/*
140+
* Function twi_enableGenCall
141+
* Desc enables general call address for slave
142+
* Input none
143+
* Output none
144+
*/
145+
void twi_enableGenCall(void)
146+
{
147+
// set TWGCE bit (LSB) and leave rest of address
148+
TWAR |= 1 << TWGCE;
149+
}
150+
137151
/*
138152
* Function twi_readFrom
139153
* Desc attempts to become twi bus master and read a
@@ -328,7 +342,7 @@ uint8_t twi_transmit(const uint8_t* data, uint8_t length)
328342
* Input function: callback function to use
329343
* Output none
330344
*/
331-
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
345+
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int, uint8_t) )
332346
{
333347
twi_onSlaveReceive = function;
334348
}
@@ -481,10 +495,15 @@ ISR(TWI_vect)
481495
twi_state = TWI_SRX;
482496
// indicate that rx buffer can be overwritten and ack
483497
twi_rxBufferIndex = 0;
484-
twi_reply(1);
498+
499+
//indicate that we have not received any gcall data
500+
twi_gcall_data = 0;
501+
502+
twi_reply(1);
485503
break;
486-
case TW_SR_DATA_ACK: // data received, returned ack
487504
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
505+
twi_gcall_data = 1; //indicate that we received some gcall data - need to send flag to receiver
506+
case TW_SR_DATA_ACK: // data received, returned ack
488507
// if there is still room in the rx buffer
489508
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
490509
// put byte in buffer and ack
@@ -503,7 +522,7 @@ ISR(TWI_vect)
503522
twi_rxBuffer[twi_rxBufferIndex] = '\0';
504523
}
505524
// callback to user defined callback
506-
twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
525+
twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex,twi_gcall_data);
507526
// since we submit rx buffer to "wire" library, we can reset it
508527
twi_rxBufferIndex = 0;
509528
break;

libraries/Wire/src/utility/twi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@
4141
void twi_init(void);
4242
void twi_disable(void);
4343
void twi_setAddress(uint8_t);
44+
void twi_enableGenCall(void);
4445
void twi_setFrequency(uint32_t);
4546
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t);
4647
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t);
4748
uint8_t twi_transmit(const uint8_t*, uint8_t);
48-
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
49+
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int, uint8_t) );
4950
void twi_attachSlaveTxEvent( void (*)(void) );
5051
void twi_reply(uint8_t);
5152
void twi_stop(void);

0 commit comments

Comments
 (0)