From c87bc7ba9a94cbeca3b652ea068c4bdb38c76b6c Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 11 Dec 2018 00:01:57 +0100 Subject: [PATCH 01/11] Add USBDevice.isSuspended() Based on code originally by Rob van der Veer , this adds USBDevice.isSuspended(), so user sketches can run custom code in their `loop` methods after checking if the device is suspended or not. Signed-off-by: Gergely Nagy --- cores/arduino/USBAPI.h | 2 ++ cores/arduino/USBCore.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h index 479ced9c8..701a14f78 100644 --- a/cores/arduino/USBAPI.h +++ b/cores/arduino/USBAPI.h @@ -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; diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index 81f689d24..c0a4c7cc0 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -855,4 +855,10 @@ bool USBDevice_::wakeupHost() return false; } +bool USBDevice_::isSuspended() +{ + return (_usbSuspendState & (1 << SUSPI)); +} + + #endif /* if defined(USBCON) */ From a901b081aed0c60e97777c9c92350d354b721094 Mon Sep 17 00:00:00 2001 From: per1234 Date: Wed, 19 Dec 2018 01:12:26 -0800 Subject: [PATCH 02/11] Revert "Updated Arduino as ISP setting" This reverts commit b084848f2eaf9ccb3ac9a64ac5492d91df4706bf. --- programmers.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/programmers.txt b/programmers.txt index d0d2cc175..c100e84bc 100644 --- a/programmers.txt +++ b/programmers.txt @@ -43,9 +43,9 @@ parallel.program.extra_params=-F arduinoasisp.name=Arduino as ISP arduinoasisp.communication=serial -arduinoasisp.protocol=arduino +arduinoasisp.protocol=stk500v1 arduinoasisp.speed=19200 -arduinoasisp.program.protocol=arduino +arduinoasisp.program.protocol=stk500v1 arduinoasisp.program.speed=19200 arduinoasisp.program.tool=avrdude arduinoasisp.program.extra_params=-P{serial.port} -b{program.speed} From 98c8a138c18592e7385feb800e30165a403802d9 Mon Sep 17 00:00:00 2001 From: per1234 Date: Wed, 19 Dec 2018 01:45:56 -0800 Subject: [PATCH 03/11] Add ATmega32U4-compatible Arduino as ISP programmer Use of the stk500v1 protocol for Arduino as ISP does not work with native USB boards on Windows. The arduino protocol does. However, the arduino protocol makes it more likely that boards with an external USB interface chip will require the auto-reset circuitry to be disabled to allow them to be used as Arduino as ISP. That adds extra complexity to a process already difficult for the average Arduino user. For this reason, a new programmer using the arduino protocol is added specifically for using native USB boards as Arduino as ISP and the previous Arduino as ISP configuration is retained for use with all other boards. --- programmers.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/programmers.txt b/programmers.txt index c100e84bc..69ddf692c 100644 --- a/programmers.txt +++ b/programmers.txt @@ -50,6 +50,15 @@ arduinoasisp.program.speed=19200 arduinoasisp.program.tool=avrdude arduinoasisp.program.extra_params=-P{serial.port} -b{program.speed} +arduinoasispatmega32u4.name=Arduino as ISP (ATmega32U4) +arduinoasispatmega32u4.communication=serial +arduinoasispatmega32u4.protocol=arduino +arduinoasispatmega32u4.speed=19200 +arduinoasispatmega32u4.program.protocol=arduino +arduinoasispatmega32u4.program.speed=19200 +arduinoasispatmega32u4.program.tool=avrdude +arduinoasispatmega32u4.program.extra_params=-P{serial.port} -b{program.speed} + usbGemma.name=Arduino Gemma usbGemma.protocol=arduinogemma usbGemma.program.tool=avrdude From c27aef0ed6638a635d92fcb97412dbd3e65e8c31 Mon Sep 17 00:00:00 2001 From: Paulo Costa Date: Fri, 4 Jan 2019 15:41:14 -0200 Subject: [PATCH 04/11] Remove commented out code for I2C interrupts on WInterrupts.c These are currently implemented by the Wire library, on twi.c --- cores/arduino/WInterrupts.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index cef1106e0..ac72dda44 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -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) { @@ -274,11 +273,6 @@ void detachInterrupt(uint8_t interruptNum) { } } -/* -void attachInterruptTwi(void (*userFunc)(void) ) { - twiIntFunc = userFunc; -} -*/ #define IMPLEMENT_ISR(vect, interrupt) \ ISR(vect) { \ @@ -314,11 +308,3 @@ IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_2) #endif #endif - -/* -ISR(TWI_vect) { - if(twiIntFunc) - twiIntFunc(); -} -*/ - From a8ef5c0a73cc54f83e421c9c5684c8d2b9d00d4a Mon Sep 17 00:00:00 2001 From: Witold Markowski Date: Wed, 28 Feb 2018 20:59:45 +0000 Subject: [PATCH 05/11] #1476 Extend API by adding a possibility to set a global timeout in milliseconds. --- libraries/Wire/src/Wire.cpp | 5 +++++ libraries/Wire/src/Wire.h | 1 + libraries/Wire/src/utility/twi.c | 6 ++++++ libraries/Wire/src/utility/twi.h | 1 + 4 files changed, 13 insertions(+) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 58916ce93..5186373a7 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -86,6 +86,11 @@ 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) { diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 702f37d64..b3ce61a3a 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -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); diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 171af7303..d5cacdfd1 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -42,6 +42,7 @@ 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_timeout_ms = 0; static void (*twi_onSlaveTransmit)(void); static void (*twi_onSlaveReceive)(uint8_t*, int); @@ -396,6 +397,11 @@ void twi_releaseBus(void) twi_state = TWI_READY; } +void twi_setTimeoutInMillis(uint8_t timeout) +{ + twi_timeout_ms = timeout; +} + ISR(TWI_vect) { switch(TW_STATUS){ diff --git a/libraries/Wire/src/utility/twi.h b/libraries/Wire/src/utility/twi.h index d27325ea7..d52b5d8a8 100644 --- a/libraries/Wire/src/utility/twi.h +++ b/libraries/Wire/src/utility/twi.h @@ -50,6 +50,7 @@ void twi_reply(uint8_t); void twi_stop(void); void twi_releaseBus(void); + void twi_setTimeoutInMillis(uint8_t); #endif From 1e7fc4d3d1446434fce307d5626a795904fbe08f Mon Sep 17 00:00:00 2001 From: Witold Markowski Date: Thu, 1 Mar 2018 13:49:04 +0000 Subject: [PATCH 06/11] #1476. Introduce timeout when checking twi_state variable. --- libraries/Wire/src/utility/twi.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index d5cacdfd1..27c54e66a 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -25,7 +25,7 @@ #include #include #include -#include "Arduino.h" // for digitalWrite +#include "Arduino.h" // for digitalWrite and millis #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) @@ -155,7 +155,14 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen } // wait until twi is ready, become master receiver + uint32_t startMillis = millis(); while(TWI_READY != twi_state){ + if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { + //timeout + twi_init(); + + return 0; + } continue; } twi_state = TWI_MRX; @@ -194,7 +201,14 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // wait for read operation to complete + startMillis = millis(); while(TWI_MRX == twi_state){ + if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { + //timeout + twi_init(); + + return 0; + } continue; } @@ -234,7 +248,14 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait } // wait until twi is ready, become master transmitter + uint32_t startMillis = millis(); while(TWI_READY != twi_state){ + if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { + //timeout + twi_init(); + + return 4; + } continue; } twi_state = TWI_MTX; @@ -276,7 +297,14 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA); // enable INTs // wait for write operation to complete + startMillis = millis(); while(wait && (TWI_MTX == twi_state)){ + if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { + //timeout + twi_init(); + + return 4; + } continue; } From 14230ea3cb1b9540b1dee1cd127ed3ebb51c23e3 Mon Sep 17 00:00:00 2001 From: Witold Markowski Date: Sat, 3 Mar 2018 21:11:28 +0000 Subject: [PATCH 07/11] #1476 Add timout in twi_stop(void) method. --- libraries/Wire/src/utility/twi.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 27c54e66a..15d3afe6b 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -402,7 +402,16 @@ void twi_stop(void) // wait for stop condition to be exectued on bus // TWINT is not set after a stop condition! + uint32_t counter = 0; while(TWCR & _BV(TWSTO)){ + counter++; + if((twi_timeout_ms > 0) && (counter >= 25000)) { + // timeout + twi_init(); + + return; + } + continue; } From 7fcb4d42263e1e7e102ae2c0b12f76636a56d5a6 Mon Sep 17 00:00:00 2001 From: Witold Markowski Date: Mon, 5 Mar 2018 20:35:25 +0000 Subject: [PATCH 08/11] #1476 Disable TWI before init. --- libraries/Wire/src/utility/twi.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 15d3afe6b..c06064327 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -159,6 +159,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen while(TWI_READY != twi_state){ if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { //timeout + twi_disable(); twi_init(); return 0; @@ -205,6 +206,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen while(TWI_MRX == twi_state){ if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { //timeout + twi_disable(); twi_init(); return 0; @@ -252,6 +254,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait while(TWI_READY != twi_state){ if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { //timeout + twi_disable(); twi_init(); return 4; @@ -301,6 +304,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait while(wait && (TWI_MTX == twi_state)){ if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { //timeout + twi_disable(); twi_init(); return 4; @@ -407,6 +411,7 @@ void twi_stop(void) counter++; if((twi_timeout_ms > 0) && (counter >= 25000)) { // timeout + twi_disable(); twi_init(); return; From 282faf067b0ac4e46f3018119273db1e55f10094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20L=C3=B8vdal?= Date: Sun, 17 Feb 2019 13:31:29 +0100 Subject: [PATCH 09/11] Reduce code duplication with BUSYWAIT_WITH_TIMEOUT_UNTIL macro --- libraries/Wire/src/utility/twi.c | 105 +++++++++++++++---------------- 1 file changed, 49 insertions(+), 56 deletions(-) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index c06064327..5b0d685a0 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -38,6 +38,33 @@ #include "pins_arduino.h" #include "twi.h" +#define BUSYWAIT_WITH_TIMEOUT_UNTIL(exit_condition, timedout_label) \ + do { \ + uint32_t startMillis = millis(); \ + while (!(exit_condition)) { \ + if ((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { \ + goto timedout_label; \ + } \ + } \ + } while (0) + +// Same as BUSYWAIT_WITH_TIMEOUT_UNTIL but intended to be used in code +// executed in interrupt service routines where millis() cannot be used. +// https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/: +// "millis() relies on interrupts to count, so it will never increment inside an ISR." +// TODO: Document calculations behind the 25000 upper counter limit. +// TODO: Make the upper counter limit reflect the twi_timeout_ms value. +#define IRS_BUSYWAIT_WITH_TIMEOUT_UNTIL(exit_condition, timedout_label) \ + do { \ + uint32_t counter = 0; \ + while (!(exit_condition)) { \ + counter++; \ + if ((twi_timeout_ms > 0) && (counter >= 25000)) { \ + goto timedout_label; \ + } \ + } \ + } while (0) + 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 @@ -155,17 +182,8 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen } // wait until twi is ready, become master receiver - uint32_t startMillis = millis(); - while(TWI_READY != twi_state){ - if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { - //timeout - twi_disable(); - twi_init(); - - return 0; - } - continue; - } + BUSYWAIT_WITH_TIMEOUT_UNTIL(twi_state == TWI_READY, waiting_timedout); + twi_state = TWI_MRX; twi_sendStop = sendStop; // reset error state (0xFF.. no error occured) @@ -202,17 +220,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // wait for read operation to complete - startMillis = millis(); - while(TWI_MRX == twi_state){ - if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { - //timeout - twi_disable(); - twi_init(); - - return 0; - } - continue; - } + BUSYWAIT_WITH_TIMEOUT_UNTIL(twi_state != TWI_MRX, waiting_timedout); if (twi_masterBufferIndex < length) length = twi_masterBufferIndex; @@ -223,6 +231,11 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen } return length; + +waiting_timedout: + twi_disable(); + twi_init(); + return 0; } /* @@ -250,17 +263,8 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait } // wait until twi is ready, become master transmitter - uint32_t startMillis = millis(); - while(TWI_READY != twi_state){ - if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { - //timeout - twi_disable(); - twi_init(); - - return 4; - } - continue; - } + BUSYWAIT_WITH_TIMEOUT_UNTIL(twi_state == TWI_READY, waiting_timedout); + twi_state = TWI_MTX; twi_sendStop = sendStop; // reset error state (0xFF.. no error occured) @@ -300,16 +304,8 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA); // enable INTs // wait for write operation to complete - startMillis = millis(); - while(wait && (TWI_MTX == twi_state)){ - if((twi_timeout_ms > 0) && (millis() - startMillis > twi_timeout_ms)) { - //timeout - twi_disable(); - twi_init(); - - return 4; - } - continue; + if (wait) { + BUSYWAIT_WITH_TIMEOUT_UNTIL(twi_state != TWI_MTX, waiting_timedout); } if (twi_error == 0xFF) @@ -320,6 +316,11 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait return 3; // error: data send, nack received else return 4; // other twi error + +waiting_timedout: + twi_disable(); + twi_init(); + return 4; } /* @@ -406,22 +407,14 @@ void twi_stop(void) // wait for stop condition to be exectued on bus // TWINT is not set after a stop condition! - uint32_t counter = 0; - while(TWCR & _BV(TWSTO)){ - counter++; - if((twi_timeout_ms > 0) && (counter >= 25000)) { - // timeout - twi_disable(); - twi_init(); - - return; - } - - continue; - } + IRS_BUSYWAIT_WITH_TIMEOUT_UNTIL((TWCR & _BV(TWSTO)) == 0, waiting_timedout); // update twi state twi_state = TWI_READY; + +waiting_timedout: + twi_disable(); + twi_init(); } /* From 40404739d1c449293c1c96119be39c838d4c89da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20L=C3=B8vdal?= Date: Sun, 17 Feb 2019 13:48:35 +0100 Subject: [PATCH 10/11] Remove trailing whitespace --- libraries/Wire/src/Wire.cpp | 14 +++---- libraries/Wire/src/utility/twi.c | 64 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 5186373a7..f93cb1162 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -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 (todd@krein.org) to implement repeated starts Modified 2017 by Chuck Todd (ctodd@cableone.net) to correct Unconfigured Slave Mode reboot */ @@ -163,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 @@ -207,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 @@ -249,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]; @@ -265,7 +265,7 @@ int TwoWire::read(void) int TwoWire::peek(void) { int value = -1; - + if(rxBufferIndex < rxBufferLength){ value = rxBuffer[rxBufferIndex]; } @@ -294,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; diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 5b0d685a0..bad9c8aab 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -87,7 +87,7 @@ static volatile uint8_t twi_rxBufferIndex; static volatile uint8_t twi_error; -/* +/* * Function twi_init * Desc readys twi pins and sets twi bitrate * Input none @@ -99,7 +99,7 @@ void twi_init(void) twi_state = TWI_READY; twi_sendStop = true; // default value twi_inRepStart = false; - + // activate internal pullups for twi. digitalWrite(SDA, 1); digitalWrite(SCL, 1); @@ -118,7 +118,7 @@ void twi_init(void) TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); } -/* +/* * Function twi_disable * Desc disables twi pins * Input none @@ -134,7 +134,7 @@ void twi_disable(void) digitalWrite(SCL, 0); } -/* +/* * Function twi_slaveInit * Desc sets slave address and enables interrupt * Input none @@ -146,7 +146,7 @@ void twi_setAddress(uint8_t address) TWAR = address << 1; } -/* +/* * Function twi_setClock * Desc sets twi bit rate * Input Clock Frequency @@ -155,14 +155,14 @@ void twi_setAddress(uint8_t address) void twi_setFrequency(uint32_t frequency) { TWBR = ((F_CPU / frequency) - 16) / 2; - + /* twi bit rate formula from atmega128 manual pg 204 SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR)) note: TWBR should be 10 or higher for master mode It is 72 for a 16mhz Wiring board with 100kHz TWI */ } -/* +/* * Function twi_readFrom * Desc attempts to become twi bus master and read a * series of bytes from a device on the bus @@ -193,7 +193,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen twi_masterBufferIndex = 0; twi_masterBufferLength = length-1; // This is not intuitive, read on... // On receive, the previously configured ACK/NACK setting is transmitted in - // response to the received byte before the interrupt is signalled. + // response to the received byte before the interrupt is signalled. // Therefor we must actually set NACK when the _next_ to last byte is // received, causing that NACK to be sent in response to receiving the last // expected byte of data. @@ -207,7 +207,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen // (@@@ we hope), and the TWI statemachine is just waiting for the address byte. // We need to remove ourselves from the repeated start state before we enable interrupts, // 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 + // 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 do { @@ -229,7 +229,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; waiting_timedout: @@ -238,7 +238,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sen return 0; } -/* +/* * Function twi_writeTo * Desc attempts to become twi bus master and write a * series of bytes to a device on the bus @@ -273,16 +273,16 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait // initialize buffer iteration vars 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; - + // if we're in a repeated start, then we've already sent the START // in the ISR. Don't do it again. // @@ -291,11 +291,11 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait // (@@@ we hope), and the TWI statemachine is just waiting for the address byte. // We need to remove ourselves from the repeated start state before we enable interrupts, // 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 + // 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 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 } @@ -307,7 +307,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait if (wait) { BUSYWAIT_WITH_TIMEOUT_UNTIL(twi_state != TWI_MTX, waiting_timedout); } - + if (twi_error == 0xFF) return 0; // success else if (twi_error == TW_MT_SLA_NACK) @@ -323,7 +323,7 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait return 4; } -/* +/* * Function twi_transmit * Desc fills slave tx buffer with data * must be called in slave tx event callback @@ -341,22 +341,22 @@ uint8_t twi_transmit(const uint8_t* data, uint8_t length) if(TWI_BUFFER_LENGTH < (twi_txBufferLength+length)){ return 1; } - + // ensure we are currently a slave transmitter if(TWI_STX != twi_state){ return 2; } - + // set length and copy data into tx buffer for(i = 0; i < length; ++i){ twi_txBuffer[twi_txBufferLength+i] = data[i]; } twi_txBufferLength += length; - + return 0; } -/* +/* * Function twi_attachSlaveRxEvent * Desc sets function called before a slave read operation * Input function: callback function to use @@ -367,7 +367,7 @@ void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) ) twi_onSlaveReceive = function; } -/* +/* * Function twi_attachSlaveTxEvent * Desc sets function called before a slave write operation * Input function: callback function to use @@ -378,7 +378,7 @@ void twi_attachSlaveTxEvent( void (*function)(void) ) twi_onSlaveTransmit = function; } -/* +/* * Function twi_reply * Desc sends byte or readys receive line * Input ack: byte indicating to ack or to nack @@ -394,7 +394,7 @@ void twi_reply(uint8_t ack) } } -/* +/* * Function twi_stop * Desc relinquishes bus master status * Input none @@ -417,7 +417,7 @@ void twi_stop(void) twi_init(); } -/* +/* * Function twi_releaseBus * Desc releases bus control * Input none @@ -451,7 +451,7 @@ ISR(TWI_vect) // Master Transmitter case TW_MT_SLA_ACK: // slave receiver acked address case TW_MT_DATA_ACK: // slave receiver acked data - // if there is data to send, send it, otherwise stop + // if there is data to send, send it, otherwise stop if(twi_masterBufferIndex < twi_masterBufferLength){ // copy data to output register and ack TWDR = twi_masterBuffer[twi_masterBufferIndex++]; @@ -461,7 +461,7 @@ ISR(TWI_vect) twi_stop(); else { twi_inRepStart = true; // we're gonna send the START - // don't enable the interrupt. We'll generate the start, but we + // 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) ; @@ -501,12 +501,12 @@ ISR(TWI_vect) twi_stop(); else { twi_inRepStart = true; // we're gonna send the START - // don't enable the interrupt. We'll generate the start, but we + // 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(); @@ -553,7 +553,7 @@ ISR(TWI_vect) // nack back at master twi_reply(0); break; - + // Slave Transmitter case TW_ST_SLA_ACK: // addressed, returned ack case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack @@ -582,7 +582,7 @@ ISR(TWI_vect) twi_reply(0); } break; - case TW_ST_DATA_NACK: // received nack, we are done + case TW_ST_DATA_NACK: // received nack, we are done case TW_ST_LAST_DATA: // received ack, but we are done already! // ack future responses twi_reply(1); From dd34343225afeec8683fed3ea4541928512f8b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20L=C3=B8vdal?= Date: Sun, 17 Feb 2019 13:54:45 +0100 Subject: [PATCH 11/11] Fix indentation --- libraries/Wire/src/Wire.cpp | 24 +++++++++--------- libraries/Wire/src/utility/twi.c | 42 ++++++++++++++++---------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index f93cb1162..a13481acc 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -94,21 +94,21 @@ void TwoWire::setTimeoutInMillis(uint8_t 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 diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index bad9c8aab..8a43e58f4 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -390,7 +390,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); } } @@ -434,7 +434,7 @@ void twi_releaseBus(void) void twi_setTimeoutInMillis(uint8_t timeout) { - twi_timeout_ms = timeout; + twi_timeout_ms = timeout; } ISR(TWI_vect) @@ -457,16 +457,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 @@ -497,17 +497,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;