Skip to content

Commit 92efe52

Browse files
committed
Get rid of twi_masterBuffer in the TWI code.
The TWI Master Read/Write functions are blocking anyway, so why copy it to a local buffer first? It wastes memory, plus limits transmission size to the buffer size.
1 parent 42fa4a1 commit 92efe52

File tree

1 file changed

+3
-15
lines changed
  • libraries/Wire/src/utility

1 file changed

+3
-15
lines changed

Diff for: libraries/Wire/src/utility/twi.c

+3-15
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static volatile bool twi_do_reset_on_timeout = false; // reset the TWI register
5858
static void (*twi_onSlaveTransmit)(void);
5959
static void (*twi_onSlaveReceive)(uint8_t*, int);
6060

61-
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
61+
static uint8_t *twi_masterBuffer;
6262
static volatile uint8_t twi_masterBufferIndex;
6363
static volatile uint8_t twi_masterBufferLength;
6464

@@ -158,8 +158,6 @@ void twi_setFrequency(uint32_t frequency)
158158
*/
159159
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
160160
{
161-
uint8_t i;
162-
163161
// ensure data will fit into buffer
164162
if(TWI_BUFFER_LENGTH < length){
165163
return 0;
@@ -179,6 +177,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
179177
twi_error = 0xFF;
180178

181179
// initialize buffer iteration vars
180+
twi_masterBuffer = data;
182181
twi_masterBufferIndex = 0;
183182
twi_masterBufferLength = length-1; // This is not intuitive, read on...
184183
// On receive, the previously configured ACK/NACK setting is transmitted in
@@ -226,11 +225,6 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
226225
length = twi_masterBufferIndex;
227226
}
228227

229-
// copy twi buffer to data
230-
for(i = 0; i < length; ++i){
231-
data[i] = twi_masterBuffer[i];
232-
}
233-
234228
return length;
235229
}
236230

@@ -252,8 +246,6 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
252246
*/
253247
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
254248
{
255-
uint8_t i;
256-
257249
// ensure data will fit into buffer
258250
if(TWI_BUFFER_LENGTH < length){
259251
return 1;
@@ -273,14 +265,10 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
273265
twi_error = 0xFF;
274266

275267
// initialize buffer iteration vars
268+
twi_masterBuffer = data;
276269
twi_masterBufferIndex = 0;
277270
twi_masterBufferLength = length;
278271

279-
// copy data to twi buffer
280-
for(i = 0; i < length; ++i){
281-
twi_masterBuffer[i] = data[i];
282-
}
283-
284272
// build sla+w, slave device address + w bit
285273
twi_slarw = TW_WRITE;
286274
twi_slarw |= address << 1;

0 commit comments

Comments
 (0)