|
18 | 18 | |
19 | 19 | Modified 2012 by Todd Krein ([email protected]) to implement repeated starts |
20 | 20 | Modified 2017 by Chuck Todd ([email protected]) to correct Unconfigured Slave Mode reboot |
| 21 | + Modified 2020 by Greyson Christoforo ([email protected]) to implement timeouts |
21 | 22 | */ |
22 | 23 |
|
23 | 24 | extern "C" { |
@@ -86,6 +87,52 @@ void TwoWire::setClock(uint32_t clock) |
86 | 87 | twi_setFrequency(clock); |
87 | 88 | } |
88 | 89 |
|
| 90 | +/*** |
| 91 | + * Sets the TWI timeout. |
| 92 | + * |
| 93 | + * This limits the maximum time to wait for the TWI hardware. If more time passes, the bus is assumed |
| 94 | + * to have locked up (e.g. due to noise-induced glitches or faulty slaves) and the transaction is aborted. |
| 95 | + * Optionally, the TWI hardware is also reset, which can be required to allow subsequent transactions to |
| 96 | + * succeed in some cases (in particular when noise has made the TWI hardware think there is a second |
| 97 | + * master that has claimed the bus). |
| 98 | + * |
| 99 | + * When a timeout is triggered, a flag is set that can be queried with `getWireTimeoutFlag()` and is cleared |
| 100 | + * when `clearWireTimeoutFlag()` or `setWireTimeoutUs()` is called. |
| 101 | + * |
| 102 | + * Note that this timeout can also trigger while waiting for clock stretching or waiting for a second master |
| 103 | + * to complete its transaction. So make sure to adapt the timeout to accomodate for those cases if needed. |
| 104 | + * A typical timeout would be 25ms (which is the maximum clock stretching allowed by the SMBus protocol), |
| 105 | + * but (much) shorter values will usually also work. |
| 106 | + * |
| 107 | + * In the future, a timeout will be enabled by default, so if you require the timeout to be disabled, it is |
| 108 | + * recommended you disable it by default using `setWireTimeoutUs(0)`, even though that is currently |
| 109 | + * the default. |
| 110 | + * |
| 111 | + * @param timeout a timeout value in microseconds, if zero then timeout checking is disabled |
| 112 | + * @param reset_with_timeout if true then TWI interface will be automatically reset on timeout |
| 113 | + * if false then TWI interface will not be reset on timeout |
| 114 | +
|
| 115 | + */ |
| 116 | +void TwoWire::setWireTimeout(uint32_t timeout, bool reset_with_timeout){ |
| 117 | + twi_setTimeoutInMicros(timeout, reset_with_timeout); |
| 118 | +} |
| 119 | + |
| 120 | +/*** |
| 121 | + * Returns the TWI timeout flag. |
| 122 | + * |
| 123 | + * @return true if timeout has occured since the flag was last cleared. |
| 124 | + */ |
| 125 | +bool TwoWire::getWireTimeoutFlag(void){ |
| 126 | + return(twi_manageTimeoutFlag(false)); |
| 127 | +} |
| 128 | + |
| 129 | +/*** |
| 130 | + * Clears the TWI timeout flag. |
| 131 | + */ |
| 132 | +void TwoWire::clearWireTimeoutFlag(void){ |
| 133 | + twi_manageTimeoutFlag(true); |
| 134 | +} |
| 135 | + |
89 | 136 | uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop) |
90 | 137 | { |
91 | 138 | if (isize > 0) { |
|
0 commit comments