3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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 disclaim 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 #define  __INCLUDE_FROM_TWI_C 
  34 uint8_t TWI_StartTransmission(const uint8_t SlaveAddress
, 
  35                               const uint8_t TimeoutMS
) 
  39                 bool     BusCaptured 
= false; 
  40                 uint16_t TimeoutRemaining
; 
  42                 TWCR 
= ((1 << TWINT
) | (1 << TWSTA
) | (1 << TWEN
)); 
  44                 TimeoutRemaining 
= (TimeoutMS 
* 100); 
  45                 while (TimeoutRemaining
-- && !(BusCaptured
)) 
  47                         if (TWCR 
& (1 << TWINT
)) 
  49                                 switch (TWSR 
& TW_STATUS_MASK
) 
  56                                                 TWCR 
= ((1 << TWINT
) | (1 << TWSTA
) | (1 << TWEN
)); 
  60                                                 return TWI_ERROR_BusFault
; 
  67                 if (!(TimeoutRemaining
)) 
  70                         return TWI_ERROR_BusCaptureTimeout
; 
  74                 TWCR 
= ((1 << TWINT
) | (1 << TWEN
)); 
  76                 TimeoutRemaining 
= (TimeoutMS 
* 100); 
  77                 while (TimeoutRemaining
--) 
  79                         if (TWCR 
& (1 << TWINT
)) 
  85                 if (!(TimeoutRemaining
)) 
  86                   return TWI_ERROR_SlaveResponseTimeout
; 
  88                 switch (TWSR 
& TW_STATUS_MASK
) 
  92                                 return TWI_ERROR_NoError
; 
  94                                 TWCR 
= ((1 << TWINT
) | (1 << TWSTO
) | (1 << TWEN
)); 
  95                                 return TWI_ERROR_SlaveNotReady
; 
 100 uint8_t TWI_ReadPacket(const uint8_t SlaveAddress
, 
 101                        const uint8_t TimeoutMS
, 
 102                        const uint8_t* InternalAddress
, 
 103                        const uint8_t InternalAddressLen
, 
 109         if ((ErrorCode 
= TWI_WritePacket(SlaveAddress
, TimeoutMS
, InternalAddress
, InternalAddressLen
, 
 110                                          NULL
, 0)) != TWI_ERROR_NoError
) 
 115         if ((ErrorCode 
= TWI_StartTransmission((SlaveAddress 
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_READ
, 
 116                                                                                    TimeoutMS
)) == TWI_ERROR_NoError
) 
 120                         if (!(TWI_ReceiveByte(Buffer
++, (Length 
== 0)))) 
 122                                 ErrorCode 
= TWI_ERROR_SlaveNAK
; 
 127                 TWI_StopTransmission(); 
 133 uint8_t TWI_WritePacket(const uint8_t SlaveAddress
, 
 134                         const uint8_t TimeoutMS
, 
 135                         const uint8_t* InternalAddress
, 
 136                         uint8_t InternalAddressLen
, 
 137                         const uint8_t* Buffer
, 
 142         if ((ErrorCode 
= TWI_StartTransmission((SlaveAddress 
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
, 
 143                                                TimeoutMS
)) == TWI_ERROR_NoError
) 
 145                 while (InternalAddressLen
--) 
 147                         if (!(TWI_SendByte(*(InternalAddress
++)))) 
 149                                 ErrorCode 
= TWI_ERROR_SlaveNAK
; 
 156                         if (!(TWI_SendByte(*(Buffer
++)))) 
 158                                 ErrorCode 
= TWI_ERROR_SlaveNAK
; 
 163                 TWI_StopTransmission();