Skip to content

Commit bf88db8

Browse files
committed
Statically allocating buffers in Wire library (issue #351).
1 parent 27cfd22 commit bf88db8

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed

libraries/Wire/Wire.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ extern "C" {
2828

2929
// Initialize Class Variables //////////////////////////////////////////////////
3030

31-
uint8_t* TwoWire::rxBuffer = 0;
31+
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
3232
uint8_t TwoWire::rxBufferIndex = 0;
3333
uint8_t TwoWire::rxBufferLength = 0;
3434

3535
uint8_t TwoWire::txAddress = 0;
36-
uint8_t* TwoWire::txBuffer = 0;
36+
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
3737
uint8_t TwoWire::txBufferIndex = 0;
3838
uint8_t TwoWire::txBufferLength = 0;
3939

@@ -51,13 +51,9 @@ TwoWire::TwoWire()
5151

5252
void TwoWire::begin(void)
5353
{
54-
// init buffer for reads
55-
rxBuffer = (uint8_t*) calloc(BUFFER_LENGTH, sizeof(uint8_t));
5654
rxBufferIndex = 0;
5755
rxBufferLength = 0;
5856

59-
// init buffer for writes
60-
txBuffer = (uint8_t*) calloc(BUFFER_LENGTH, sizeof(uint8_t));
6157
txBufferIndex = 0;
6258
txBufferLength = 0;
6359

libraries/Wire/Wire.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
class TwoWire
2828
{
2929
private:
30-
static uint8_t* rxBuffer;
30+
static uint8_t rxBuffer[];
3131
static uint8_t rxBufferIndex;
3232
static uint8_t rxBufferLength;
3333

3434
static uint8_t txAddress;
35-
static uint8_t* txBuffer;
35+
static uint8_t txBuffer[];
3636
static uint8_t txBufferIndex;
3737
static uint8_t txBufferLength;
3838

libraries/Wire/utility/twi.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ static uint8_t twi_slarw;
4040
static void (*twi_onSlaveTransmit)(void);
4141
static void (*twi_onSlaveReceive)(uint8_t*, int);
4242

43-
static uint8_t* twi_masterBuffer;
43+
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
4444
static volatile uint8_t twi_masterBufferIndex;
4545
static uint8_t twi_masterBufferLength;
4646

47-
static uint8_t* twi_txBuffer;
47+
static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
4848
static volatile uint8_t twi_txBufferIndex;
4949
static volatile uint8_t twi_txBufferLength;
5050

51-
static uint8_t* twi_rxBuffer;
51+
static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
5252
static volatile uint8_t twi_rxBufferIndex;
5353

5454
static volatile uint8_t twi_error;
@@ -88,11 +88,6 @@ void twi_init(void)
8888

8989
// enable twi module, acks, and twi interrupt
9090
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
91-
92-
// allocate buffers
93-
twi_masterBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t));
94-
twi_txBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t));
95-
twi_rxBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t));
9691
}
9792

9893
/*

0 commit comments

Comments
 (0)