Skip to content

Don't waste RAM in the low-level TWI Wire driver #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions libraries/Wire/src/utility/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static volatile bool twi_do_reset_on_timeout = false; // reset the TWI register
static void (*twi_onSlaveTransmit)(void);
static void (*twi_onSlaveReceive)(uint8_t*, int);

static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
static uint8_t *twi_masterBuffer;
static volatile uint8_t twi_masterBufferIndex;
static volatile uint8_t twi_masterBufferLength;

Expand Down Expand Up @@ -158,8 +158,6 @@ void twi_setFrequency(uint32_t frequency)
*/
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
{
uint8_t i;

// ensure data will fit into buffer
if(TWI_BUFFER_LENGTH < length){
return 0;
Expand All @@ -179,6 +177,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
twi_error = 0xFF;

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

// copy twi buffer to data
for(i = 0; i < length; ++i){
data[i] = twi_masterBuffer[i];
}

return length;
}

Expand All @@ -252,8 +246,6 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
*/
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
{
uint8_t i;

// ensure data will fit into buffer
if(TWI_BUFFER_LENGTH < length){
return 1;
Expand All @@ -273,14 +265,10 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
twi_error = 0xFF;

// initialize buffer iteration vars
twi_masterBuffer = data;
twi_masterBufferIndex = 0;
twi_masterBufferLength = length;

// copy data to twi buffer
for(i = 0; i < length; ++i){
twi_masterBuffer[i] = data[i];
}

// build sla+w, slave device address + w bit
twi_slarw = TW_WRITE;
twi_slarw |= address << 1;
Expand Down