3 Copyright (C) Dean Camera, 2012.
\r
5 dean [at] fourwalledcubicle [dot] com
\r
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
\r
12 Permission to use, copy, modify, distribute, and sell this
\r
13 software and its documentation for any purpose is hereby granted
\r
14 without fee, provided that the above copyright notice appear in
\r
15 all copies and that both that the copyright notice and this
\r
16 permission notice and warranty disclaimer appear in supporting
\r
17 documentation, and that the name of the author not be used in
\r
18 advertising or publicity pertaining to distribution of the
\r
19 software without specific, written prior permission.
\r
21 The author disclaim all warranties with regard to this
\r
22 software, including all implied warranties of merchantability
\r
23 and fitness. In no event shall the author be liable for any
\r
24 special, indirect or consequential damages or any damages
\r
25 whatsoever resulting from loss of use, data or profits, whether
\r
26 in an action of contract, negligence or other tortious action,
\r
27 arising out of or in connection with the use or performance of
\r
31 #include "../../../Common/Common.h"
\r
32 #if (ARCH == ARCH_AVR8) && defined(TWCR)
\r
34 #define __INCLUDE_FROM_TWI_C
\r
37 uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,
\r
38 const uint8_t TimeoutMS)
\r
42 bool BusCaptured = false;
\r
43 uint16_t TimeoutRemaining;
\r
45 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
\r
47 TimeoutRemaining = (TimeoutMS * 100);
\r
48 while (TimeoutRemaining-- && !(BusCaptured))
\r
50 if (TWCR & (1 << TWINT))
\r
52 switch (TWSR & TW_STATUS_MASK)
\r
58 case TW_MT_ARB_LOST:
\r
59 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
\r
63 return TWI_ERROR_BusFault;
\r
70 if (!(TimeoutRemaining))
\r
73 return TWI_ERROR_BusCaptureTimeout;
\r
76 TWDR = SlaveAddress;
\r
77 TWCR = ((1 << TWINT) | (1 << TWEN));
\r
79 TimeoutRemaining = (TimeoutMS * 100);
\r
80 while (TimeoutRemaining--)
\r
82 if (TWCR & (1 << TWINT))
\r
88 if (!(TimeoutRemaining))
\r
89 return TWI_ERROR_SlaveResponseTimeout;
\r
91 switch (TWSR & TW_STATUS_MASK)
\r
95 return TWI_ERROR_NoError;
\r
97 TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
\r
98 return TWI_ERROR_SlaveNotReady;
\r
103 bool TWI_SendByte(const uint8_t Byte)
\r
106 TWCR = ((1 << TWINT) | (1 << TWEN));
\r
107 while (!(TWCR & (1 << TWINT)));
\r
109 return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);
\r
112 bool TWI_ReceiveByte(uint8_t* const Byte,
\r
113 const bool LastByte)
\r
118 TWCRMask = ((1 << TWINT) | (1 << TWEN));
\r
120 TWCRMask = ((1 << TWINT) | (1 << TWEN) | (1 << TWEA));
\r
123 while (!(TWCR & (1 << TWINT)));
\r
126 uint8_t Status = (TWSR & TW_STATUS_MASK);
\r
128 return ((LastByte) ? (Status == TW_MR_DATA_NACK) : (Status == TW_MR_DATA_ACK));
\r
131 uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
\r
132 const uint8_t TimeoutMS,
\r
133 const uint8_t* InternalAddress,
\r
134 uint8_t InternalAddressLen,
\r
140 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
\r
141 TimeoutMS)) == TWI_ERROR_NoError)
\r
143 while (InternalAddressLen--)
\r
145 if (!(TWI_SendByte(*(InternalAddress++))))
\r
147 ErrorCode = TWI_ERROR_SlaveNAK;
\r
152 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
\r
153 TimeoutMS)) == TWI_ERROR_NoError)
\r
157 if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))
\r
159 ErrorCode = TWI_ERROR_SlaveNAK;
\r
164 TWI_StopTransmission();
\r
171 uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
\r
172 const uint8_t TimeoutMS,
\r
173 const uint8_t* InternalAddress,
\r
174 uint8_t InternalAddressLen,
\r
175 const uint8_t* Buffer,
\r
180 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
\r
181 TimeoutMS)) == TWI_ERROR_NoError)
\r
183 while (InternalAddressLen--)
\r
185 if (!(TWI_SendByte(*(InternalAddress++))))
\r
187 ErrorCode = TWI_ERROR_SlaveNAK;
\r
194 if (!(TWI_SendByte(*(Buffer++))))
\r
196 ErrorCode = TWI_ERROR_SlaveNAK;
\r
201 TWI_StopTransmission();
\r