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