X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/88d022a75245e7492ecd11a5e1ea5c553acf0b2c..7cd9e0dbc45afa4b047711d5dc255f4850e80c6b:/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c diff --git a/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c b/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c index d6db37d6e..e7629d5b7 100644 --- a/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c +++ b/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2011. + Copyright (C) Dean Camera, 2013. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -18,7 +18,7 @@ advertising or publicity pertaining to distribution of the software without specific, written prior permission. - The author disclaim all warranties with regard to this + The author disclaims all warranties with regard to this software, including all implied warranties of merchantability and fitness. In no event shall the author be liable for any special, indirect or consequential damages or any damages @@ -28,6 +28,9 @@ this software. */ +#include "../../../Common/Common.h" +#if (ARCH == ARCH_AVR8) && defined(TWCR) + #define __INCLUDE_FROM_TWI_C #include "../TWI.h" @@ -42,7 +45,7 @@ uint8_t TWI_StartTransmission(const uint8_t SlaveAddress, TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN)); TimeoutRemaining = (TimeoutMS * 100); - while (TimeoutRemaining-- && !(BusCaptured)) + while (TimeoutRemaining && !(BusCaptured)) { if (TWCR & (1 << TWINT)) { @@ -62,6 +65,7 @@ uint8_t TWI_StartTransmission(const uint8_t SlaveAddress, } _delay_us(10); + TimeoutRemaining--; } if (!(TimeoutRemaining)) @@ -74,12 +78,13 @@ uint8_t TWI_StartTransmission(const uint8_t SlaveAddress, TWCR = ((1 << TWINT) | (1 << TWEN)); TimeoutRemaining = (TimeoutMS * 100); - while (TimeoutRemaining--) + while (TimeoutRemaining) { if (TWCR & (1 << TWINT)) break; _delay_us(10); + TimeoutRemaining--; } if (!(TimeoutRemaining)) @@ -119,9 +124,9 @@ bool TWI_ReceiveByte(uint8_t* const Byte, TWCR = TWCRMask; while (!(TWCR & (1 << TWINT))); *Byte = TWDR; - + uint8_t Status = (TWSR & TW_STATUS_MASK); - + return ((LastByte) ? (Status == TW_MR_DATA_NACK) : (Status == TW_MR_DATA_ACK)); } @@ -133,19 +138,19 @@ uint8_t TWI_ReadPacket(const uint8_t SlaveAddress, uint8_t Length) { uint8_t ErrorCode; - + if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE, TimeoutMS)) == TWI_ERROR_NoError) { while (InternalAddressLen--) { if (!(TWI_SendByte(*(InternalAddress++)))) - { + { ErrorCode = TWI_ERROR_SlaveNAK; break; } } - + if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ, TimeoutMS)) == TWI_ERROR_NoError) { @@ -157,11 +162,11 @@ uint8_t TWI_ReadPacket(const uint8_t SlaveAddress, break; } } - + TWI_StopTransmission(); } } - + return ErrorCode; } @@ -180,7 +185,7 @@ uint8_t TWI_WritePacket(const uint8_t SlaveAddress, while (InternalAddressLen--) { if (!(TWI_SendByte(*(InternalAddress++)))) - { + { ErrorCode = TWI_ERROR_SlaveNAK; break; } @@ -194,9 +199,11 @@ uint8_t TWI_WritePacket(const uint8_t SlaveAddress, break; } } - + TWI_StopTransmission(); } - + return ErrorCode; } + +#endif