aboutsummaryrefslogtreecommitdiffstats
path: root/LUFA
diff options
context:
space:
mode:
authorDean Camera <dean@fourwalledcubicle.com>2010-02-24 09:49:19 +0000
committerDean Camera <dean@fourwalledcubicle.com>2010-02-24 09:49:19 +0000
commitc0c982df7af34ea0375eda774185f3825c261522 (patch)
tree0d17079345768999a2b1f6272674b251e253ab8a /LUFA
parent14a5a94084180a5186f7c3a796d4a70e785a91b8 (diff)
downloadlufa-c0c982df7af34ea0375eda774185f3825c261522.tar.gz
lufa-c0c982df7af34ea0375eda774185f3825c261522.tar.bz2
lufa-c0c982df7af34ea0375eda774185f3825c261522.zip
Added a timeout value to the TWI_StartTransmission() function, within which the addressed device must respond.
Fixed TWI_StartTransmission() corrupting the contents of the GPIOR0 register.
Diffstat (limited to 'LUFA')
-rw-r--r--LUFA/Drivers/Peripheral/AVRU4U6U7/TWI.h4
-rw-r--r--LUFA/Drivers/Peripheral/TWI.c61
-rw-r--r--LUFA/ManPages/ChangeLog.txt2
-rw-r--r--LUFA/ManPages/MigrationInformation.txt5
4 files changed, 49 insertions, 23 deletions
diff --git a/LUFA/Drivers/Peripheral/AVRU4U6U7/TWI.h b/LUFA/Drivers/Peripheral/AVRU4U6U7/TWI.h
index 13db86e69..2d8e6df1e 100644
--- a/LUFA/Drivers/Peripheral/AVRU4U6U7/TWI.h
+++ b/LUFA/Drivers/Peripheral/AVRU4U6U7/TWI.h
@@ -56,6 +56,7 @@
#include <avr/io.h>
#include <stdbool.h>
#include <util/twi.h>
+ #include <util/delay.h>
/* Enable C linkage for C++ Compilers: */
#if defined(__cplusplus)
@@ -131,10 +132,11 @@
/** Begins a master mode TWI bus communication with the given slave device address.
*
* \param[in] SlaveAddress Address of the slave TWI device to communicate with
+ * \param[in] TimeoutMS Timeout period within which the slave must respond, in milliseconds
*
* \return Boolean true if the device is ready for data, false otherwise
*/
- bool TWI_StartTransmission(uint8_t SlaveAddress);
+ bool TWI_StartTransmission(uint8_t SlaveAddress, uint8_t TimeoutMS);
/* Disable C linkage for C++ Compilers: */
#if defined(__cplusplus)
diff --git a/LUFA/Drivers/Peripheral/TWI.c b/LUFA/Drivers/Peripheral/TWI.c
index 6028bbae9..eb513c454 100644
--- a/LUFA/Drivers/Peripheral/TWI.c
+++ b/LUFA/Drivers/Peripheral/TWI.c
@@ -7,39 +7,58 @@
#include "TWI.h"
-bool TWI_StartTransmission(uint8_t SlaveAddress)
+bool TWI_StartTransmission(uint8_t SlaveAddress, uint8_t TimeoutMS)
{
for (;;)
{
- uint8_t IterationsRemaining = 50;
- bool BusCaptured = false;
+ bool BusCaptured = false;
+ uint16_t TimeoutRemaining;
- while (IterationsRemaining-- && !BusCaptured)
+ TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
+
+ TimeoutRemaining = (TimeoutMS * 100);
+ while (TimeoutRemaining-- && !BusCaptured)
{
- TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
- while (!(TWCR & (1 << TWINT)));
-
- switch (TWSR & TW_STATUS_MASK)
+ if (TWCR & (1 << TWINT))
{
- case TW_START:
- case TW_REP_START:
- BusCaptured = true;
- break;
- case TW_MT_ARB_LOST:
- continue;
- default:
- return false;
+ switch (TWSR & TW_STATUS_MASK)
+ {
+ case TW_START:
+ case TW_REP_START:
+ BusCaptured = true;
+ break;
+ case TW_MT_ARB_LOST:
+ TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
+ continue;
+ default:
+ TWCR = (1 << TWEN);
+ return false;
+ }
}
+
+ _delay_us(10);
}
if (!(BusCaptured))
- return false;
-
+ {
+ TWCR = (1 << TWEN);
+ return false;
+ }
+
TWDR = SlaveAddress;
TWCR = ((1 << TWINT) | (1 << TWEN));
- while (!(TWCR & (1 << TWINT)));
- GPIOR0 = (TWSR & TW_STATUS_MASK);
+ TimeoutRemaining = (TimeoutMS * 100);
+ while (TimeoutRemaining--)
+ {
+ if (TWCR & (1 << TWINT))
+ break;
+
+ _delay_us(10);
+ }
+
+ if (!(TimeoutRemaining))
+ return false;
switch (TWSR & TW_STATUS_MASK)
{
@@ -49,6 +68,6 @@ bool TWI_StartTransmission(uint8_t SlaveAddress)
default:
TWI_StopTransmission();
break;
- }
+ }
}
}
diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index 14f1a2547..155afa099 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -15,9 +15,11 @@
* - AVRISP programmer project now has a more robust timeout system, allowing for a doubling of the software USART speed
* for PDI and TPI programming
* - Increased the speed of both software and hardware TPI/PDI programming modes of the AVRISP project
+ * - Added a timeout value to the TWI_StartTransmission() function, within which the addressed device must respond
*
* <b>Fixed:</b>
* - Fixed software PDI/TPI programming mode in the AVRISP project not correctly toggling just the clock pin
+ * - Fixed TWI_StartTransmission() corrupting the contents of the GPIOR0 register
*
* \section Sec_ChangeLog100219 Version 100219
*
diff --git a/LUFA/ManPages/MigrationInformation.txt b/LUFA/ManPages/MigrationInformation.txt
index 262364070..8467e0019 100644
--- a/LUFA/ManPages/MigrationInformation.txt
+++ b/LUFA/ManPages/MigrationInformation.txt
@@ -12,8 +12,11 @@
*
* \section Sec_MigrationXXXXXX Migrating from 100219 to XXXXXX
*
+ * <b>Non-USB Library Components</b>
+ * - The \ref TWI_StartTransmission() function now takes in a timeout period, expressed in milliseconds, within which the addressed
+ * device must respond or the function will abort.
+ *
* \section Sec_Migration100219 Migrating from 091223 to 100219
- * - (None)
*
* <b>Non-USB Library Components</b>
* - Due to some ADC channels not being identical to their ADC MUX selection masks for single-ended conversions on some AVR models,