Skip to content

Commit bea4180

Browse files
committed
add Wire::onRequestMore()
1 parent 6309212 commit bea4180

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

libraries/Wire/src/Wire.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ uint8_t TwoWire::txBufferLength = 0;
4343

4444
uint8_t TwoWire::transmitting = 0;
4545
void (*TwoWire::user_onRequest)(void);
46+
void (*TwoWire::user_onRequestMore)(void);
4647
void (*TwoWire::user_onReceive)(int);
4748

4849
// Constructors ////////////////////////////////////////////////////////////////
@@ -63,6 +64,7 @@ void TwoWire::begin(void)
6364

6465
twi_init();
6566
twi_attachSlaveTxEvent(onRequestService); // default callback must exist
67+
twi_attachSlaveTxMoreEvent(onRequestMoreService); // default callback must exist
6668
twi_attachSlaveRxEvent(onReceiveService); // default callback must exist
6769
}
6870

@@ -360,6 +362,17 @@ void TwoWire::onRequestService(void)
360362
user_onRequest();
361363
}
362364

365+
// behind the scenes function that is called when more data is requested
366+
void TwoWire::onRequestMoreService(void)
367+
{
368+
// don't bother if user hasn't registered a callback
369+
if(!user_onRequestMore){
370+
return;
371+
}
372+
// alert user program
373+
user_onRequestMore();
374+
}
375+
363376
// sets function called on slave write
364377
void TwoWire::onReceive( void (*function)(int) )
365378
{
@@ -372,6 +385,12 @@ void TwoWire::onRequest( void (*function)(void) )
372385
user_onRequest = function;
373386
}
374387

388+
// sets function called on slave read
389+
void TwoWire::onRequestMore( void (*function)(void) )
390+
{
391+
user_onRequestMore = function;
392+
}
393+
375394
// Preinstantiate Objects //////////////////////////////////////////////////////
376395

377396
TwoWire Wire = TwoWire();

libraries/Wire/src/Wire.h

+3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ class TwoWire : public Stream
4545

4646
static uint8_t transmitting;
4747
static void (*user_onRequest)(void);
48+
static void (*user_onRequestMore)(void);
4849
static void (*user_onReceive)(int);
4950
static void onRequestService(void);
51+
static void onRequestMoreService(void);
5052
static void onReceiveService(uint8_t*, int);
5153
public:
5254
TwoWire();
@@ -75,6 +77,7 @@ class TwoWire : public Stream
7577
virtual void flush(void);
7678
void onReceive( void (*)(int) );
7779
void onRequest( void (*)(void) );
80+
void onRequestMore( void (*)(void) );
7881

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

libraries/Wire/src/utility/twi.c

+16
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static volatile bool twi_timed_out_flag = false; // a timeout has been seen
5656
static volatile bool twi_do_reset_on_timeout = false; // reset the TWI registers on timeout
5757

5858
static void (*twi_onSlaveTransmit)(void);
59+
static void (*twi_onSlaveTransmitMore)(void);
5960
static void (*twi_onSlaveReceive)(uint8_t*, int);
6061

6162
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
@@ -384,6 +385,17 @@ void twi_attachSlaveTxEvent( void (*function)(void) )
384385
twi_onSlaveTransmit = function;
385386
}
386387

388+
/*
389+
* Function twi_attachSlaveTxMoreEvent
390+
* Desc sets function called before a slave cont'd sequential data write operation
391+
* Input function: callback function to use
392+
* Output none
393+
*/
394+
void twi_attachSlaveTxMoreEvent( void (*function)(void) )
395+
{
396+
twi_onSlaveTransmitMore = function;
397+
}
398+
387399
/*
388400
* Function twi_reply
389401
* Desc sends byte or readys receive line
@@ -640,6 +652,10 @@ ISR(TWI_vect)
640652
case TW_ST_DATA_ACK: // byte sent, ack returned
641653
// copy data to output register
642654
TWDR = twi_txBuffer[twi_txBufferIndex++];
655+
// if the buffer emptied, request it to be topped up
656+
if (twi_txBufferIndex >= twi_txBufferLength) {
657+
twi_onSlaveTransmitMore();
658+
}
643659
// if there is more to send, ack, otherwise nack
644660
if(twi_txBufferIndex < twi_txBufferLength){
645661
twi_reply(1);

libraries/Wire/src/utility/twi.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
uint8_t twi_transmit(const uint8_t*, uint8_t);
5050
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
5151
void twi_attachSlaveTxEvent( void (*)(void) );
52+
void twi_attachSlaveTxMoreEvent( void (*)(void) );
5253
void twi_reply(uint8_t);
5354
void twi_stop(void);
5455
void twi_releaseBus(void);

0 commit comments

Comments
 (0)