Skip to content

Commit 650d351

Browse files
sandeepmistryfacchinm
authored andcommitted
Add buffer arg to TWI_MasterRead and avoid using master_readData in Wire.cpp
1 parent d8c5624 commit 650d351

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

libraries/Wire/src/Wire.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop) {
100100
quantity = BUFFER_LENGTH;
101101
}
102102

103-
uint8_t bytes_read = TWI_MasterRead(address, quantity, sendStop);
104-
105-
/* Copy read buffer from lower layer */
106-
for(uint8_t i = 0; i < bytes_read; i++){
107-
rxBuffer[i] = master_readData[i];
108-
}
103+
uint8_t bytes_read = TWI_MasterRead(address, rxBuffer, quantity, sendStop);
109104

110105
/* Initialize read variables */
111106
rxBufferIndex = 0;

libraries/Wire/src/utility/twi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,12 @@ uint8_t TWI_MasterWrite(uint8_t slave_address,
213213
* \retval false If transaction could not be started.
214214
*/
215215
uint8_t TWI_MasterRead(uint8_t slave_address,
216-
uint8_t bytes_to_read,
216+
uint8_t* read_data,
217+
uint8_t bytes_to_read,
217218
uint8_t send_stop)
218219
{
220+
master_readData = read_data;
221+
219222
uint8_t bytes_read = TWI_MasterWriteRead(slave_address,
220223
0,
221224
0,
@@ -252,9 +255,6 @@ uint8_t TWI_MasterWriteRead(uint8_t slave_address,
252255
if (bytes_to_write > TWI_BUFFER_SIZE) {
253256
return 1;
254257
}
255-
if (bytes_to_read > TWI_BUFFER_SIZE) {
256-
return 0;
257-
}
258258

259259
/*Initiate transaction if bus is ready. */
260260
if (master_trans_status == TWIM_STATUS_READY) {
@@ -443,7 +443,7 @@ void TWI_MasterWriteHandler()
443443
void TWI_MasterReadHandler()
444444
{
445445
/* Fetch data if bytes to be read. */
446-
if (master_bytesRead < TWI_BUFFER_SIZE) {
446+
if (master_bytesRead < master_bytesToRead) {
447447
uint8_t data = TWI0.MDATA;
448448
master_readData[master_bytesRead] = data;
449449
master_bytesRead++;

libraries/Wire/src/utility/twi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ typedef enum TWI_MODE_enum {
7575
/* Master variables */
7676
register8_t master_slaveAddress; /*!< Slave address */
7777
register8_t master_writeData[TWI_BUFFER_SIZE]; /*!< Data to write */
78-
register8_t master_readData[TWI_BUFFER_SIZE]; /*!< Read data */
78+
register8_t* master_readData; /*!< Read data */
7979
register8_t master_bytesToWrite; /*!< Number of bytes to write */
8080
register8_t master_bytesToRead; /*!< Number of bytes to read */
8181
register8_t master_bytesWritten; /*!< Number of bytes written */
@@ -116,6 +116,7 @@ uint8_t TWI_MasterWrite(uint8_t slave_address,
116116
uint8_t bytes_to_write,
117117
uint8_t send_stop);
118118
uint8_t TWI_MasterRead(uint8_t slave_address,
119+
uint8_t* read_data,
119120
uint8_t bytes_to_read,
120121
uint8_t send_stop);
121122
uint8_t TWI_MasterWriteRead(uint8_t slave_address,

0 commit comments

Comments
 (0)