|
1 | 1 | /*
|
2 |
| - TwoWire.cpp - TWI/I2C library for Wiring & Arduino |
| 2 | + Wire.cpp - TWI/I2C library for Wiring & Arduino |
3 | 3 | Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
4 | 4 |
|
5 | 5 | This library is free software; you can redistribute it and/or
|
|
19 | 19 | Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
|
20 | 20 | Modified 2017 by Chuck Todd (ctodd@cableone.net) to correct Unconfigured Slave Mode reboot
|
21 | 21 | Modified 2020 by Asuki Kono (asukiaaa@gmail.com) to use TwoWire class for Wire1
|
| 22 | + Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts |
22 | 23 | */
|
23 | 24 |
|
24 | 25 | extern "C" {
|
@@ -122,6 +123,54 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
|
122 | 123 | return read;
|
123 | 124 | }
|
124 | 125 |
|
| 126 | +#if defined(WIRE_TIMEOUT) |
| 127 | +/** |
| 128 | + * Sets the TWI timeout. |
| 129 | + * |
| 130 | + * This limits the maximum time to wait for the TWI hardware. If more time passes, the bus is assumed |
| 131 | + * to have locked up (e.g. due to noise-induced glitches or faulty slaves) and the transaction is aborted. |
| 132 | + * Optionally, the TWI hardware is also reset, which can be required to allow subsequent transactions to |
| 133 | + * succeed in some cases (in particular when noise has made the TWI hardware think there is a second |
| 134 | + * master that has claimed the bus). |
| 135 | + * |
| 136 | + * When a timeout is triggered, a flag is set that can be queried with `getWireTimeoutFlag()` and is cleared |
| 137 | + * when `clearWireTimeoutFlag()` or `setWireTimeoutUs()` is called. |
| 138 | + * |
| 139 | + * Note that this timeout can also trigger while waiting for clock stretching or waiting for a second master |
| 140 | + * to complete its transaction. So make sure to adapt the timeout to accomodate for those cases if needed. |
| 141 | + * A typical timeout would be 25ms (which is the maximum clock stretching allowed by the SMBus protocol), |
| 142 | + * but (much) shorter values will usually also work. |
| 143 | + * |
| 144 | + * In the future, a timeout will be enabled by default, so if you require the timeout to be disabled, it is |
| 145 | + * recommended you disable it by default using `setWireTimeoutUs(0)`, even though that is currently |
| 146 | + * the default. |
| 147 | + * |
| 148 | + * @param timeout a timeout value in microseconds, if zero then timeout checking is disabled |
| 149 | + * @param reset_with_timeout if true then TWI interface will be automatically reset on timeout |
| 150 | + * if false then TWI interface will not be reset on timeout |
| 151 | +
|
| 152 | + */ |
| 153 | +void TwoWire::setWireTimeout(uint32_t timeout, bool reset_with_timeout){ |
| 154 | + twi_setTimeoutInMicros(timeout, reset_with_timeout); |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Returns the TWI timeout flag. |
| 159 | + * |
| 160 | + * @return true if timeout has occured since the flag was last cleared. |
| 161 | + */ |
| 162 | +bool TwoWire::getWireTimeoutFlag(void){ |
| 163 | + return(twi_manageTimeoutFlag(false)); |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * Clears the TWI timeout flag. |
| 168 | + */ |
| 169 | +void TwoWire::clearWireTimeoutFlag(void){ |
| 170 | + twi_manageTimeoutFlag(true); |
| 171 | +} |
| 172 | +#endif // WIRE_TIMEOUT |
| 173 | + |
125 | 174 | uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
|
126 | 175 | return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
|
127 | 176 | }
|
@@ -161,8 +210,8 @@ void TwoWire::beginTransmission(int address)
|
161 | 210 | // Originally, 'endTransmission' was an f(void) function.
|
162 | 211 | // It has been modified to take one parameter indicating
|
163 | 212 | // whether or not a STOP should be performed on the bus.
|
164 |
| -// Calling endTransmission(false) allows a sketch to |
165 |
| -// perform a repeated start. |
| 213 | +// Calling endTransmission(false) allows a sketch to |
| 214 | +// perform a repeated start. |
166 | 215 | //
|
167 | 216 | // WARNING: Nothing in the library keeps track of whether
|
168 | 217 | // the bus tenure has been properly ended with a STOP. It
|
|
0 commit comments