Skip to content
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

Reduce code duplication for timeout handling in twi.c #1

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions cores/arduino/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class USBDevice_
void detach(); // Serial port goes down too...
void poll();
bool wakeupHost(); // returns false, when wakeup cannot be processed

bool isSuspended();
};
extern USBDevice_ USBDevice;

Expand Down
6 changes: 6 additions & 0 deletions cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,10 @@ bool USBDevice_::wakeupHost()
return false;
}

bool USBDevice_::isSuspended()
{
return (_usbSuspendState & (1 << SUSPI));
}


#endif /* if defined(USBCON) */
14 changes: 0 additions & 14 deletions cores/arduino/WInterrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS] = {
nothing,
#endif
};
// volatile static voidFuncPtr twiIntFunc;

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
Expand Down Expand Up @@ -274,11 +273,6 @@ void detachInterrupt(uint8_t interruptNum) {
}
}

/*
void attachInterruptTwi(void (*userFunc)(void) ) {
twiIntFunc = userFunc;
}
*/

#define IMPLEMENT_ISR(vect, interrupt) \
ISR(vect) { \
Expand Down Expand Up @@ -314,11 +308,3 @@ IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_2)
#endif

#endif

/*
ISR(TWI_vect) {
if(twiIntFunc)
twiIntFunc();
}
*/

43 changes: 24 additions & 19 deletions 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 @@ -86,24 +86,29 @@ void TwoWire::setClock(uint32_t clock)
twi_setFrequency(clock);
}

void TwoWire::setTimeoutInMillis(uint8_t timeout)
{
twi_setTimeoutInMillis(timeout);
}

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
{
if (isize > 0) {
// send internal address; this mode allows sending a repeated start to access
// some devices' internal registers. This function is executed by the hardware
// TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
// send internal address; this mode allows sending a repeated start to access
// some devices' internal registers. This function is executed by the hardware
// TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)

beginTransmission(address);
beginTransmission(address);

// the maximum size of internal address is 3 bytes
if (isize > 3){
isize = 3;
}
// the maximum size of internal address is 3 bytes
if (isize > 3){
isize = 3;
}

// write internal register address - most significant byte first
while (isize-- > 0)
write((uint8_t)(iaddress >> (isize*8)));
endTransmission(false);
// write internal register address - most significant byte first
while (isize-- > 0)
write((uint8_t)(iaddress >> (isize*8)));
endTransmission(false);
}

// clamp to buffer length
Expand Down Expand Up @@ -158,8 +163,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 Down Expand Up @@ -202,7 +207,7 @@ size_t TwoWire::write(uint8_t data)
// put byte in tx buffer
txBuffer[txBufferIndex] = data;
++txBufferIndex;
// update amount in buffer
// update amount in buffer
txBufferLength = txBufferIndex;
}else{
// in slave send mode
Expand Down Expand Up @@ -244,7 +249,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 @@ -260,7 +265,7 @@ int TwoWire::read(void)
int TwoWire::peek(void)
{
int value = -1;

if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
}
Expand Down Expand Up @@ -289,7 +294,7 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
// copy twi rx buffer into local read buffer
// this enables new reads to happen in parallel
for(uint8_t i = 0; i < numBytes; ++i){
rxBuffer[i] = inBytes[i];
rxBuffer[i] = inBytes[i];
}
// set rx iterator vars
rxBufferIndex = 0;
Expand Down
1 change: 1 addition & 0 deletions libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TwoWire : public Stream
void begin(int);
void end();
void setClock(uint32_t);
void setTimeoutInMillis(uint8_t);
void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
Expand Down
Loading