forked from mirror/qmk_firmware
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.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user