Skip to content

Commit

Permalink
Update Wire libraries
Browse files Browse the repository at this point in the history
Remove dot_a_linkage (may cause problems with PlatformIO), Fix formatting and restructured the definition of TWI buffers. This makes it much easier to redefine the buffer size in the boards.txt file or in platformio.ini
  • Loading branch information
MCUdude committed May 30, 2019
1 parent 99e8f6e commit e27fbe6
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 138 deletions.
1 change: 0 additions & 1 deletion avr/libraries/Wire/library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name=Wire
version=1.0
author=Arduino
maintainer=MCUdude
dot_a_linkgage=true
sentence=Allows the communication between devices or sensors connected via Two Wire Interface Bus.
paragraph=
category=Communication
Expand Down
36 changes: 18 additions & 18 deletions avr/libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ extern "C" {

// Initialize Class Variables //////////////////////////////////////////////////

uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
uint8_t TwoWire::rxBuffer[TWI_BUFFER_SIZE];
uint8_t TwoWire::rxBufferIndex = 0;
uint8_t TwoWire::rxBufferLength = 0;

uint8_t TwoWire::txAddress = 0;
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
uint8_t TwoWire::txBuffer[TWI_BUFFER_SIZE];
uint8_t TwoWire::txBufferIndex = 0;
uint8_t TwoWire::txBufferLength = 0;

Expand Down Expand Up @@ -106,8 +106,8 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
}

// clamp to buffer length
if(quantity > BUFFER_LENGTH){
quantity = BUFFER_LENGTH;
if(quantity > TWI_BUFFER_SIZE){
quantity = TWI_BUFFER_SIZE;
}
// perform blocking read into buffer
uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
Expand All @@ -119,7 +119,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
}

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
}

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
Expand Down Expand Up @@ -154,17 +154,17 @@ void TwoWire::beginTransmission(int address)
}

//
// Originally, 'endTransmission' was an f(void) function.
// It has been modified to take one parameter indicating
// whether or not a STOP should be performed on the bus.
// Calling endTransmission(false) allows a sketch to
// perform a repeated start.
// Originally, 'endTransmission' was an f(void) function.
// It has been modified to take one parameter indicating
// whether or not a STOP should be performed on the bus.
// Calling endTransmission(false) allows a sketch to
// perform a repeated start.
//
// WARNING: Nothing in the library keeps track of whether
// the bus tenure has been properly ended with a STOP. It
// is very possible to leave the bus in a hung state if
// no call to endTransmission(true) is made. Some I2C
// devices will behave oddly if they do not see a STOP.
// WARNING: Nothing in the library keeps track of whether
// the bus tenure has been properly ended with a STOP. It
// is very possible to leave the bus in a hung state if
// no call to endTransmission(true) is made. Some I2C
// devices will behave oddly if they do not see a STOP.
//
uint8_t TwoWire::endTransmission(uint8_t sendStop)
{
Expand All @@ -178,8 +178,8 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
return ret;
}

// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
//
uint8_t TwoWire::endTransmission(void)
{
Expand All @@ -194,7 +194,7 @@ size_t TwoWire::write(uint8_t data)
if(transmitting){
// in master transmitter mode
// don't bother if buffer is full
if(txBufferLength >= BUFFER_LENGTH){
if(txBufferLength >= TWI_BUFFER_SIZE){
setWriteError();
return 0;
}
Expand Down
8 changes: 1 addition & 7 deletions avr/libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
#include <inttypes.h>
#include "Stream.h"

// Only include Wire1 if two i2c channels are available
#if defined(TWI1_vect)
#include <Wire1.h>
#endif

#define BUFFER_LENGTH 32

// WIRE_HAS_END means Wire has end()
#define WIRE_HAS_END 1
Expand Down Expand Up @@ -66,7 +60,7 @@ class TwoWire : public Stream
uint8_t endTransmission(uint8_t);
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t);
uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t);
uint8_t requestFrom(int, int);
uint8_t requestFrom(int, int, int);
virtual size_t write(uint8_t);
Expand Down
84 changes: 42 additions & 42 deletions avr/libraries/Wire/src/utility/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@

static volatile uint8_t twi_state;
static volatile uint8_t twi_slarw;
static volatile uint8_t twi_sendStop; // should the transaction end with a stop
static volatile uint8_t twi_inRepStart; // in the middle of a repeated start
static volatile uint8_t twi_sendStop; // should the transaction end with a stop
static volatile uint8_t twi_inRepStart; // in the middle of a repeated start

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[TWI_BUFFER_SIZE];
static volatile uint8_t twi_masterBufferIndex;
static volatile uint8_t twi_masterBufferLength;

static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
static uint8_t twi_txBuffer[TWI_BUFFER_SIZE];
static volatile uint8_t twi_txBufferIndex;
static volatile uint8_t twi_txBufferLength;

static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
static uint8_t twi_rxBuffer[TWI_BUFFER_SIZE];
static volatile uint8_t twi_rxBufferIndex;

static volatile uint8_t twi_error;
Expand All @@ -69,7 +69,7 @@ void twi_init(void)
{
// initialize state
twi_state = TWI_READY;
twi_sendStop = true; // default value
twi_sendStop = true; // default value
twi_inRepStart = false;

// activate internal pullups for twi.
Expand Down Expand Up @@ -133,7 +133,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
uint8_t i;

// ensure data will fit into buffer
if(TWI_BUFFER_LENGTH < length){
if(TWI_BUFFER_SIZE < length){
return 0;
}

Expand Down Expand Up @@ -166,11 +166,11 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
// since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
// up. Also, don't enable the START interrupt. There may be one pending from the
// repeated start that we sent ourselves, and that would really confuse things.
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
do {
TWDR = twi_slarw;
} while(TWCR & _BV(TWWC));
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
}
else
// send start condition
Expand All @@ -188,7 +188,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen
for(i = 0; i < length; ++i){
data[i] = twi_masterBuffer[i];
}

return length;
}

Expand All @@ -212,7 +212,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
uint8_t i;

// ensure data will fit into buffer
if(TWI_BUFFER_LENGTH < length){
if(TWI_BUFFER_SIZE < length){
return 1;
}

Expand Down Expand Up @@ -248,29 +248,29 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
// since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
// up. Also, don't enable the START interrupt. There may be one pending from the
// repeated start that we sent outselves, and that would really confuse things.
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
twi_inRepStart = false; // remember, we're dealing with an ASYNC ISR
do {
TWDR = twi_slarw;
TWDR = twi_slarw;
} while(TWCR & _BV(TWWC));
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE); // enable INTs, but not START
}
else
// send start condition
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA); // enable INTs
TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA); // enable INTs

// wait for write operation to complete
while(wait && (TWI_MTX == twi_state)){
continue;
}

if (twi_error == 0xFF)
return 0; // success
return 0; // success
else if (twi_error == TW_MT_SLA_NACK)
return 2; // error: address send, nack received
return 2; // error: address send, nack received
else if (twi_error == TW_MT_DATA_NACK)
return 3; // error: data send, nack received
return 3; // error: data send, nack received
else
return 4; // other twi error
return 4; // other twi error
}

/*
Expand All @@ -288,7 +288,7 @@ uint8_t twi_transmit(const uint8_t* data, uint8_t length)
uint8_t i;

// ensure data will fit into buffer
if(TWI_BUFFER_LENGTH < length){
if(TWI_BUFFER_SIZE < length){
return 1;
}

Expand Down Expand Up @@ -340,7 +340,7 @@ void twi_reply(uint8_t ack)
if(ack){
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
}else{
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
}
}

Expand Down Expand Up @@ -400,16 +400,16 @@ ISR(TWI_vect)
TWDR = twi_masterBuffer[twi_masterBufferIndex++];
twi_reply(1);
}else{
if (twi_sendStop)
if (twi_sendStop)
twi_stop();
else {
twi_inRepStart = true; // we're gonna send the START
// don't enable the interrupt. We'll generate the start, but we
// avoid handling the interrupt until we're in the next transaction,
// at the point where we would normally issue the start.
TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
twi_state = TWI_READY;
}
else {
twi_inRepStart = true; // we're gonna send the START
// don't enable the interrupt. We'll generate the start, but we
// avoid handling the interrupt until we're in the next transaction,
// at the point where we would normally issue the start.
TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
twi_state = TWI_READY;
}
}
break;
case TW_MT_SLA_NACK: // address sent, nack received
Expand Down Expand Up @@ -440,17 +440,17 @@ ISR(TWI_vect)
case TW_MR_DATA_NACK: // data received, nack sent
// put final byte into buffer
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
if (twi_sendStop)
if (twi_sendStop)
twi_stop();
else {
twi_inRepStart = true; // we're gonna send the START
// don't enable the interrupt. We'll generate the start, but we
// avoid handling the interrupt until we're in the next transaction,
// at the point where we would normally issue the start.
TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
twi_state = TWI_READY;
}
break;
else {
twi_inRepStart = true; // we're gonna send the START
// don't enable the interrupt. We'll generate the start, but we
// avoid handling the interrupt until we're in the next transaction,
// at the point where we would normally issue the start.
TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
twi_state = TWI_READY;
}
break;
case TW_MR_SLA_NACK: // address sent, nack received
twi_stop();
break;
Expand All @@ -470,7 +470,7 @@ ISR(TWI_vect)
case TW_SR_DATA_ACK: // data received, returned ack
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
// if there is still room in the rx buffer
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
if(twi_rxBufferIndex < TWI_BUFFER_SIZE){
// put byte in buffer and ack
twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
twi_reply(1);
Expand All @@ -483,7 +483,7 @@ ISR(TWI_vect)
// ack future responses and leave slave receiver state
twi_releaseBus();
// put a null char after data if there's room
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
if(twi_rxBufferIndex < TWI_BUFFER_SIZE){
twi_rxBuffer[twi_rxBufferIndex] = '\0';
}
// callback to user defined callback
Expand Down
4 changes: 2 additions & 2 deletions avr/libraries/Wire/src/utility/twi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#define TWI_FREQ 100000L
#endif

#ifndef TWI_BUFFER_LENGTH
#define TWI_BUFFER_LENGTH 32
#ifndef TWI_BUFFER_SIZE
#define TWI_BUFFER_SIZE 32
#endif

#define TWI_READY 0
Expand Down
1 change: 0 additions & 1 deletion avr/libraries/Wire1/library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name=Wire1
version=1.0
author=Arduino
maintainer=MCUdude
dot_a_linkgage=true
sentence=This library allows you to communicate with I2C and Two Wire Interface devices.
paragraph=It allows the communication with I2C devices like temperature sensors, realtime clocks and many others using SDA1 (Data Line) and SCL1 (Clock Line).
category=Communication
Expand Down
Loading

0 comments on commit e27fbe6

Please sign in to comment.