Skip to content

Commit

Permalink
Improve Wire library
Browse files Browse the repository at this point in the history
Lots on improvements have been borrowed from megaTinyCore
  • Loading branch information
MCUdude committed May 4, 2020
1 parent 9271e6c commit 1746f4d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 124 deletions.
76 changes: 48 additions & 28 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 @@ -119,23 +119,43 @@ void TwoWire::begin(void)
TWI_MasterInit(DEFAULT_FREQUENCY);
}

void TwoWire::begin(uint8_t address)
void TwoWire::begin(uint8_t address, bool receive_broadcast, uint8_t second_address)
{
rxBufferIndex = 0;
rxBufferLength = 0;

txBufferIndex = 0;
txBufferLength = 0;

TWI_SlaveInit(address);
TWI_SlaveInit(address, receive_broadcast, second_address);

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

void TwoWire::begin(int address, bool receive_broadcast, uint8_t second_address)
{
begin((uint8_t)address, receive_broadcast, second_address);
}

void TwoWire::begin(uint8_t address, bool receive_broadcast)
{
begin(address, receive_broadcast, 0);
}

void TwoWire::begin(int address, bool receive_broadcast)
{
begin((uint8_t)address, receive_broadcast, 0);
}

void TwoWire::begin(uint8_t address)
{
begin(address, 0, 0);
}

void TwoWire::begin(int address)
{
begin((uint8_t)address);
begin((uint8_t)address, 0, 0);
}

void TwoWire::end(void)
Expand Down Expand Up @@ -196,17 +216,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(bool sendStop)
{
Expand All @@ -223,8 +243,8 @@ uint8_t TwoWire::endTransmission(bool sendStop)
return status;
}

// 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 Down Expand Up @@ -310,20 +330,20 @@ int TwoWire::peek(void)
// e.g. when MDATA regsiter is written before MADDR
void TwoWire::flush(void)
{
// /* Clear buffers */
// for(uint8_t i = 0; i < BUFFER_LENGTH; i++){
// txBuffer[i] = 0;
// rxBuffer[i] = 0;
// }
// /* Clear buffers */
// for(uint8_t i = 0; i < BUFFER_LENGTH; i++){
// txBuffer[i] = 0;
// rxBuffer[i] = 0;
// }
//
// /* Clear buffer variables */
// txBufferIndex = 0;
// txBufferLength = 0;
// rxBufferIndex = 0;
// rxBufferLength = 0;
// /* Clear buffer variables */
// txBufferIndex = 0;
// txBufferLength = 0;
// rxBufferIndex = 0;
// rxBufferLength = 0;
//
// /* Turn off and on TWI module */
// TWI_Flush();
// /* Turn off and on TWI module */
// TWI_Flush();
}

// behind the scenes function that is called when data is received
Expand Down
17 changes: 15 additions & 2 deletions megaavr/libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@

#include <Arduino.h>

#if ((RAMEND - RAMSTART) < 1023)
#define BUFFER_LENGTH 16
#elif ((RAMEND - RAMSTART) < 4095)
#define BUFFER_LENGTH 32
#elif ((RAMEND - RAMSTART) < 8191)
#define BUFFER_LENGTH 64
#else
#define BUFFER_LENGTH 128
#endif

// WIRE_HAS_END means Wire has end()
#define WIRE_HAS_END 1
Expand All @@ -45,13 +53,18 @@ class TwoWire : public Stream
static void (*user_onReceive)(int);
static uint8_t onRequestService(void);
static void onReceiveService(int);

public:
TwoWire();
bool pins(uint8_t sda_pin, uint8_t scl_pin);
bool swap(uint8_t state = 1);
void begin();
void begin(uint8_t);
void begin(int);
void begin(uint8_t, bool, uint8_t);
void begin(int, bool, uint8_t);
void begin(uint8_t, bool);
void begin(int, bool);
void end();
void setClock(uint32_t);
void beginTransmission(uint8_t);
Expand All @@ -68,8 +81,8 @@ class TwoWire : public Stream
virtual int read(void);
virtual int peek(void);
virtual void flush(void);
void onReceive( void (*)(int) );
void onRequest( void (*)(void) );
void onReceive(void (*)(int));
void onRequest(void (*)(void));

inline size_t write(unsigned long n) { return write((uint8_t)n); }
inline size_t write(long n) { return write((uint8_t)n); }
Expand Down
Loading

0 comments on commit 1746f4d

Please sign in to comment.