Skip to content

Commit

Permalink
Fix compatibility issue with Wire library
Browse files Browse the repository at this point in the history
requestFrom() called with a uint8_t as the address and an unsigned int
as the second argument would confuse the compiler. Now it doesn't.
  • Loading branch information
SpenceKonde committed Sep 22, 2019
1 parent 03d3b4b commit ea4d5a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
52 changes: 26 additions & 26 deletions megaavr/libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
Modified 2017 by Chuck Todd ([email protected]) to correct Unconfigured Slave Mode reboot
*/
Expand Down Expand Up @@ -62,7 +62,7 @@ void TwoWire::begin(void)
txBufferIndex = 0;
txBufferLength = 0;

TWI_MasterInit(DEFAULT_FREQUENCY);
TWI_MasterInit(DEFAULT_FREQUENCY);
}

void TwoWire::begin(uint8_t address)
Expand All @@ -72,12 +72,12 @@ void TwoWire::begin(uint8_t address)

txBufferIndex = 0;
txBufferLength = 0;

TWI_SlaveInit(address);

TWI_attachSlaveTxEvent(onRequestService, txBuffer); // default callback must exist
TWI_attachSlaveRxEvent(onReceiveService, rxBuffer, BUFFER_LENGTH); // default callback must exist

}

void TwoWire::begin(int address)
Expand All @@ -95,13 +95,13 @@ void TwoWire::setClock(uint32_t clock)
TWI_MasterSetBaud(clock);
}

uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop) {
uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop) {
if(quantity > BUFFER_LENGTH){
quantity = BUFFER_LENGTH;
}

uint8_t bytes_read = TWI_MasterRead(address, rxBuffer, quantity, sendStop);

/* Initialize read variables */
rxBufferIndex = 0;
rxBufferLength = bytes_read;
Expand All @@ -114,14 +114,14 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity)
return requestFrom(address, quantity, true);
}

uint8_t TwoWire::requestFrom(int address, int quantity)
uint8_t TwoWire::requestFrom(int address, size_t quantity)
{
return requestFrom((uint8_t)address, (size_t)quantity, true);
return requestFrom((uint8_t)address, quantity, true);
}

uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
uint8_t TwoWire::requestFrom(int address, size_t quantity, int sendStop)
{
return requestFrom((uint8_t)address, (size_t)quantity, (bool)sendStop);
return requestFrom((uint8_t)address, quantity, (bool)sendStop);
}

void TwoWire::beginTransmission(uint8_t address)
Expand All @@ -144,8 +144,8 @@ 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.
// 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
Expand All @@ -157,14 +157,14 @@ uint8_t TwoWire::endTransmission(bool sendStop)
{
// transmit buffer (blocking)
uint8_t status = TWI_MasterWrite(txAddress, txBuffer, txBufferLength, sendStop);

// reset tx buffer iterator vars
txBufferIndex = 0;
txBufferLength = 0;

// indicate that we are done transmitting
transmitting = 0;

return status;
}

Expand Down Expand Up @@ -193,7 +193,7 @@ size_t TwoWire::write(uint8_t data)

/* Update buffer length */
txBufferLength = txBufferIndex;

return 1;
}

Expand Down Expand Up @@ -224,7 +224,7 @@ int TwoWire::available(void)
int TwoWire::read(void)
{
int value = -1;

// get each successive byte on each call
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
Expand All @@ -240,15 +240,15 @@ int TwoWire::read(void)
int TwoWire::peek(void)
{
int value = -1;

if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
}

return value;
}

// can be used to get out of an error state in TWI module
// can be used to get out of an error state in TWI module
// e.g. when MDATA regsiter is written before MADDR
void TwoWire::flush(void)
{
Expand All @@ -257,13 +257,13 @@ void TwoWire::flush(void)
// txBuffer[i] = 0;
// rxBuffer[i] = 0;
// }
//
//
// /* Clear buffer variables */
// txBufferIndex = 0;
// txBufferLength = 0;
// rxBufferIndex = 0;
// rxBufferLength = 0;
//
//
// /* Turn off and on TWI module */
// TWI_Flush();
}
Expand All @@ -285,7 +285,7 @@ void TwoWire::onReceiveService(int numBytes)
// set rx iterator vars
rxBufferIndex = 0;
rxBufferLength = numBytes;

// alert user program
user_onReceive(numBytes);
}
Expand All @@ -297,11 +297,11 @@ uint8_t TwoWire::onRequestService(void)
if(!user_onRequest){
return 0;
}

// reset slave write buffer iterator var
txBufferIndex = 0;
txBufferLength = 0;

// alert user program
user_onRequest();

Expand Down
4 changes: 2 additions & 2 deletions megaavr/libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class TwoWire : public HardwareI2C
uint8_t endTransmission(bool);
uint8_t requestFrom(uint8_t, size_t);
uint8_t requestFrom(uint8_t, size_t, bool);
uint8_t requestFrom(int, int);
uint8_t requestFrom(int, int, int);
uint8_t requestFrom(int, size_t);
uint8_t requestFrom(int, size_t, int);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *, size_t);
virtual int available(void);
Expand Down

0 comments on commit ea4d5a8

Please sign in to comment.