Skip to content

Commit 0a174fe

Browse files
sandeepmistryfacchinm
authored andcommitted
Make slave_readData a point and pass buffer + size into TWI_attachSlaveRxEvent
1 parent e444f3c commit 0a174fe

File tree

4 files changed

+13
-19
lines changed

4 files changed

+13
-19
lines changed

libraries/Wire/src/Wire.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void TwoWire::begin(uint8_t address)
7676
TWI_SlaveInit(address);
7777

7878
TWI_attachSlaveTxEvent(onRequestService); // default callback must exist
79-
TWI_attachSlaveRxEvent(onReceiveService); // default callback must exist
79+
TWI_attachSlaveRxEvent(onReceiveService, rxBuffer, BUFFER_LENGTH); // default callback must exist
8080

8181
}
8282

@@ -269,7 +269,7 @@ void TwoWire::flush(void)
269269
}
270270

271271
// behind the scenes function that is called when data is received
272-
void TwoWire::onReceiveService(volatile uint8_t* inBytes, int numBytes)
272+
void TwoWire::onReceiveService(int numBytes)
273273
{
274274
// don't bother if user hasn't registered a callback
275275
if(!user_onReceive){
@@ -281,13 +281,7 @@ void TwoWire::onReceiveService(volatile uint8_t* inBytes, int numBytes)
281281
if(rxBufferIndex < rxBufferLength){
282282
return;
283283
}
284-
285-
// copy twi rx buffer into local read buffer
286-
// this enables new reads to happen in parallel
287-
for(uint8_t i = 0; i < numBytes; ++i){
288-
rxBuffer[i] = inBytes[i];
289-
}
290-
284+
291285
// set rx iterator vars
292286
rxBufferIndex = 0;
293287
rxBufferLength = numBytes;

libraries/Wire/src/Wire.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TwoWire : public HardwareI2C
4545
static void (*user_onRequest)(void);
4646
static void (*user_onReceive)(int);
4747
static void onRequestService(void);
48-
static void onReceiveService(volatile uint8_t*, int);
48+
static void onReceiveService(int);
4949
public:
5050
TwoWire();
5151
void begin();

libraries/Wire/src/utility/twi.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ void TWI_SlaveInterruptHandler(){
512512
* This should be hit when there is a STOP or REPSTART
513513
*/
514514
if(slave_callUserReceive == 1){
515-
TWI_onSlaveReceive(slave_readData, slave_bytesRead);
515+
TWI_onSlaveReceive(slave_bytesRead);
516516
slave_callUserReceive = 0;
517517
}
518518

@@ -673,7 +673,7 @@ void TWI_SlaveWriteHandler(){
673673
void TWI_SlaveReadHandler(){
674674

675675
/* If free space in buffer */
676-
if(slave_bytesRead < TWI_BUFFER_SIZE){
676+
if(slave_bytesRead < slave_bytesToRead){
677677

678678
/* Fetch data */
679679
uint8_t data = TWI0.SDATA;
@@ -697,8 +697,10 @@ void TWI_SlaveReadHandler(){
697697
* Input function: callback function to use
698698
* Output none
699699
*/
700-
void TWI_attachSlaveRxEvent( void (*function)(volatile uint8_t*, int) ){
700+
void TWI_attachSlaveRxEvent( void (*function)(int), uint8_t *read_data, uint8_t bytes_to_read ){
701701
TWI_onSlaveReceive = function;
702+
slave_readData = read_data;
703+
slave_bytesToRead = bytes_to_read;
702704
}
703705

704706
/*

libraries/Wire/src/utility/twi.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ typedef enum TWI_MODE_enum {
6565
TWI_MODE_SLAVE_RECEIVE = 6
6666
} TWI_MODE_t;
6767

68-
/*! Buffer size define */
69-
#define TWI_BUFFER_SIZE 128
70-
7168
/*! For adding R/_W bit to address */
7269
#define ADD_READ_BIT(address) (address | 0x01)
7370
#define ADD_WRITE_BIT(address) (address & ~0x01)
@@ -86,11 +83,12 @@ register8_t master_result; /*!< Result of transactio
8683

8784
/* Slave variables */
8885
static void (*TWI_onSlaveTransmit)(void) __attribute__((unused));
89-
static void (*TWI_onSlaveReceive)(volatile uint8_t*, int) __attribute__((unused));
86+
static void (*TWI_onSlaveReceive)(int) __attribute__((unused));
9087
register8_t* slave_writeData;
91-
register8_t slave_readData[TWI_BUFFER_SIZE];
88+
register8_t* slave_readData;
9289
register8_t slave_bytesToWrite;
9390
register8_t slave_bytesWritten;
91+
register8_t slave_bytesToRead;
9492
register8_t slave_bytesRead;
9593
register8_t slave_trans_status;
9694
register8_t slave_result;
@@ -136,7 +134,7 @@ void TWI_SlaveStopHandler(void);
136134
void TWI_SlaveDataHandler(void);
137135
void TWI_SlaveWriteHandler(void);
138136
void TWI_SlaveReadHandler(void);
139-
void TWI_attachSlaveRxEvent( void (*function)(volatile uint8_t*, int) );
137+
void TWI_attachSlaveRxEvent( void (*function)(int), uint8_t *read_data, uint8_t bytes_to_read );
140138
void TWI_attachSlaveTxEvent( void (*function)(void) );
141139
void TWI_SlaveTransactionFinished(uint8_t result);
142140
/*! TWI master interrupt service routine.

0 commit comments

Comments
 (0)