Skip to content

Commit b07c7f1

Browse files
committed
Don't waste RAM in the low-level TWI Wire drivers.
Since twi_readFrom() and twi_writeTo() are both blocking functions, there is no need to allocate a special twi_masterBuffer. Doing so wastes valuable RAM, uses extra time to copy the data to the secondary buffer, and limits the transfer size to TWI_BUFFER_SIZE. Instead, it only needs a pointer to the buffer for the IRQ to use for the transfer. And, if asynchronous non-blocking functions are ever added, which will require a different API and callbacks, etc., then the existing txBuffer and rxBuffer for slave mode can just be used there too, since master mode and slave mode can't both be active at the same time.
1 parent 342f4d1 commit b07c7f1

File tree

2 files changed

+6
-30
lines changed
  • avr/libraries

2 files changed

+6
-30
lines changed

avr/libraries/Wire/src/utility/twi.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static volatile bool twi_do_reset_on_timeout = false; // reset the TWI register
5959
static void (*twi_onSlaveTransmit)(void);
6060
static void (*twi_onSlaveReceive)(uint8_t*, int);
6161

62-
static uint8_t twi_masterBuffer[TWI_BUFFER_SIZE];
62+
static uint8_t *twi_masterBuffer;
6363
static volatile uint8_t twi_masterBufferIndex;
6464
static volatile uint8_t twi_masterBufferLength;
6565

@@ -159,8 +159,6 @@ void twi_setFrequency(uint32_t frequency)
159159
*/
160160
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
161161
{
162-
uint8_t i;
163-
164162
// ensure data will fit into buffer
165163
if(TWI_BUFFER_SIZE < length){
166164
return 0;
@@ -186,6 +184,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
186184
twi_error = 0xFF;
187185

188186
// initialize buffer iteration vars
187+
twi_masterBuffer = data;
189188
twi_masterBufferIndex = 0;
190189
twi_masterBufferLength = length-1; // This is not intuitive, read on...
191190
// On receive, the previously configured ACK/NACK setting is transmitted in
@@ -244,11 +243,6 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
244243
if (twi_masterBufferIndex < length)
245244
length = twi_masterBufferIndex;
246245

247-
// copy twi buffer to data
248-
for(i = 0; i < length; ++i){
249-
data[i] = twi_masterBuffer[i];
250-
}
251-
252246
return length;
253247
}
254248

@@ -270,8 +264,6 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
270264
*/
271265
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
272266
{
273-
uint8_t i;
274-
275267
// ensure data will fit into buffer
276268
if(TWI_BUFFER_SIZE < length){
277269
return 1;
@@ -298,14 +290,10 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
298290
twi_error = 0xFF;
299291

300292
// initialize buffer iteration vars
293+
twi_masterBuffer = data;
301294
twi_masterBufferIndex = 0;
302295
twi_masterBufferLength = length;
303296

304-
// copy data to twi buffer
305-
for(i = 0; i < length; ++i){
306-
twi_masterBuffer[i] = data[i];
307-
}
308-
309297
// build sla+w, slave device address + w bit
310298
twi_slarw = TW_WRITE;
311299
twi_slarw |= address << 1;

avr/libraries/Wire1/src/utility/twi1.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static volatile uint8_t twi_inRepStart; // in the middle of a repeated start
4646
static void (*twi_onSlaveTransmit)(void);
4747
static void (*twi_onSlaveReceive)(uint8_t*, int);
4848

49-
static uint8_t twi_masterBuffer[TWI1_BUFFER_SIZE];
49+
static uint8_t *twi_masterBuffer;
5050
static volatile uint8_t twi_masterBufferIndex;
5151
static volatile uint8_t twi_masterBufferLength;
5252

@@ -146,8 +146,6 @@ void twi_setFrequency1(uint32_t frequency)
146146
*/
147147
uint8_t twi_readFrom1(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
148148
{
149-
uint8_t i;
150-
151149
// ensure data will fit into buffer
152150
if(TWI1_BUFFER_SIZE < length){
153151
return 0;
@@ -163,6 +161,7 @@ uint8_t twi_readFrom1(uint8_t address, uint8_t* data, uint8_t length, uint8_t se
163161
twi_error = 0xFF;
164162

165163
// initialize buffer iteration vars
164+
twi_masterBuffer = data;
166165
twi_masterBufferIndex = 0;
167166
twi_masterBufferLength = length-1; // This is not intuitive, read on...
168167
// On receive, the previously configured ACK/NACK setting is transmitted in
@@ -200,11 +199,6 @@ uint8_t twi_readFrom1(uint8_t address, uint8_t* data, uint8_t length, uint8_t se
200199
if (twi_masterBufferIndex < length)
201200
length = twi_masterBufferIndex;
202201

203-
// copy twi buffer to data
204-
for(i = 0; i < length; ++i){
205-
data[i] = twi_masterBuffer[i];
206-
}
207-
208202
return length;
209203
}
210204

@@ -225,8 +219,6 @@ uint8_t twi_readFrom1(uint8_t address, uint8_t* data, uint8_t length, uint8_t se
225219
*/
226220
uint8_t twi_writeTo1(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
227221
{
228-
uint8_t i;
229-
230222
// ensure data will fit into buffer
231223
if(TWI1_BUFFER_SIZE < length){
232224
return 1;
@@ -242,14 +234,10 @@ uint8_t twi_writeTo1(uint8_t address, uint8_t* data, uint8_t length, uint8_t wai
242234
twi_error = 0xFF;
243235

244236
// initialize buffer iteration vars
237+
twi_masterBuffer = data;
245238
twi_masterBufferIndex = 0;
246239
twi_masterBufferLength = length;
247240

248-
// copy data to twi buffer
249-
for(i = 0; i < length; ++i){
250-
twi_masterBuffer[i] = data[i];
251-
}
252-
253241
// build sla+w, slave device address + w bit
254242
twi_slarw = TW_WRITE;
255243
twi_slarw |= address << 1;

0 commit comments

Comments
 (0)