Skip to content

Feature/wire on request more #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add Wire::onRequestMore()
  • Loading branch information
jwagnerhki committed Dec 19, 2023
commit bea41806cd789483353dec65f263995d11c1361f
19 changes: 19 additions & 0 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ uint8_t TwoWire::txBufferLength = 0;

uint8_t TwoWire::transmitting = 0;
void (*TwoWire::user_onRequest)(void);
void (*TwoWire::user_onRequestMore)(void);
void (*TwoWire::user_onReceive)(int);

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

twi_init();
twi_attachSlaveTxEvent(onRequestService); // default callback must exist
twi_attachSlaveTxMoreEvent(onRequestMoreService); // default callback must exist
twi_attachSlaveRxEvent(onReceiveService); // default callback must exist
}

Expand Down Expand Up @@ -360,6 +362,17 @@ void TwoWire::onRequestService(void)
user_onRequest();
}

// behind the scenes function that is called when more data is requested
void TwoWire::onRequestMoreService(void)
{
// don't bother if user hasn't registered a callback
if(!user_onRequestMore){
return;
}
// alert user program
user_onRequestMore();
}

// sets function called on slave write
void TwoWire::onReceive( void (*function)(int) )
{
Expand All @@ -372,6 +385,12 @@ void TwoWire::onRequest( void (*function)(void) )
user_onRequest = function;
}

// sets function called on slave read
void TwoWire::onRequestMore( void (*function)(void) )
{
user_onRequestMore = function;
}

// Preinstantiate Objects //////////////////////////////////////////////////////

TwoWire Wire = TwoWire();
Expand Down
3 changes: 3 additions & 0 deletions libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class TwoWire : public Stream

static uint8_t transmitting;
static void (*user_onRequest)(void);
static void (*user_onRequestMore)(void);
static void (*user_onReceive)(int);
static void onRequestService(void);
static void onRequestMoreService(void);
static void onReceiveService(uint8_t*, int);
public:
TwoWire();
Expand Down Expand Up @@ -75,6 +77,7 @@ class TwoWire : public Stream
virtual void flush(void);
void onReceive( void (*)(int) );
void onRequest( void (*)(void) );
void onRequestMore( void (*)(void) );

inline size_t write(unsigned long n) { return write((uint8_t)n); }
inline size_t write(long n) { return write((uint8_t)n); }
Expand Down
16 changes: 16 additions & 0 deletions libraries/Wire/src/utility/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static volatile bool twi_timed_out_flag = false; // a timeout has been seen
static volatile bool twi_do_reset_on_timeout = false; // reset the TWI registers on timeout

static void (*twi_onSlaveTransmit)(void);
static void (*twi_onSlaveTransmitMore)(void);
static void (*twi_onSlaveReceive)(uint8_t*, int);

static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
Expand Down Expand Up @@ -384,6 +385,17 @@ void twi_attachSlaveTxEvent( void (*function)(void) )
twi_onSlaveTransmit = function;
}

/*
* Function twi_attachSlaveTxMoreEvent
* Desc sets function called before a slave cont'd sequential data write operation
* Input function: callback function to use
* Output none
*/
void twi_attachSlaveTxMoreEvent( void (*function)(void) )
{
twi_onSlaveTransmitMore = function;
}

/*
* Function twi_reply
* Desc sends byte or readys receive line
Expand Down Expand Up @@ -640,6 +652,10 @@ ISR(TWI_vect)
case TW_ST_DATA_ACK: // byte sent, ack returned
// copy data to output register
TWDR = twi_txBuffer[twi_txBufferIndex++];
// if the buffer emptied, request it to be topped up
if (twi_txBufferIndex >= twi_txBufferLength) {
twi_onSlaveTransmitMore();
}
// if there is more to send, ack, otherwise nack
if(twi_txBufferIndex < twi_txBufferLength){
twi_reply(1);
Expand Down
1 change: 1 addition & 0 deletions libraries/Wire/src/utility/twi.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
uint8_t twi_transmit(const uint8_t*, uint8_t);
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
void twi_attachSlaveTxEvent( void (*)(void) );
void twi_attachSlaveTxMoreEvent( void (*)(void) );
void twi_reply(uint8_t);
void twi_stop(void);
void twi_releaseBus(void);
Expand Down