2      Copyright (C) Dean Camera, 2011. 
   4   dean [at] fourwalledcubicle [dot] com 
   8 #define  __INCLUDE_FROM_TWI_C 
  11 uint8_t TWI_StartTransmission(const uint8_t SlaveAddress
, 
  12                               const uint8_t TimeoutMS
) 
  16                 bool     BusCaptured 
= false; 
  17                 uint16_t TimeoutRemaining
; 
  19                 TWCR 
= ((1 << TWINT
) | (1 << TWSTA
) | (1 << TWEN
)); 
  21                 TimeoutRemaining 
= (TimeoutMS 
* 100); 
  22                 while (TimeoutRemaining
-- && !(BusCaptured
)) 
  24                         if (TWCR 
& (1 << TWINT
)) 
  26                                 switch (TWSR 
& TW_STATUS_MASK
) 
  33                                                 TWCR 
= ((1 << TWINT
) | (1 << TWSTA
) | (1 << TWEN
)); 
  37                                                 return TWI_ERROR_BusFault
; 
  44                 if (!(TimeoutRemaining
)) 
  47                         return TWI_ERROR_BusCaptureTimeout
; 
  51                 TWCR 
= ((1 << TWINT
) | (1 << TWEN
)); 
  53                 TimeoutRemaining 
= (TimeoutMS 
* 100); 
  54                 while (TimeoutRemaining
--) 
  56                         if (TWCR 
& (1 << TWINT
)) 
  62                 if (!(TimeoutRemaining
)) 
  63                   return TWI_ERROR_SlaveResponseTimeout
; 
  65                 switch (TWSR 
& TW_STATUS_MASK
) 
  69                                 return TWI_ERROR_NoError
; 
  71                                 TWCR 
= ((1 << TWINT
) | (1 << TWSTO
) | (1 << TWEN
)); 
  72                                 return TWI_ERROR_SlaveNotReady
; 
  77 uint8_t TWI_ReadPacket(const uint8_t SlaveAddress
, 
  78                        const uint8_t TimeoutMS
, 
  79                        const uint8_t* InternalAddress
, 
  80                        uint8_t InternalAddressLen
, 
  86         if ((ErrorCode 
= TWI_WritePacket(SlaveAddress
, TimeoutMS
, InternalAddress
, InternalAddressLen
, 
  87                                          NULL
, 0)) != TWI_ERROR_NoError
) 
  92         if ((ErrorCode 
= TWI_StartTransmission((SlaveAddress 
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_READ
, 
  93                                                                                    TimeoutMS
)) == TWI_ERROR_NoError
) 
  97                         if (!(TWI_ReceiveByte(Buffer
++, (Length 
== 0)))) 
  99                                 ErrorCode 
= TWI_ERROR_SlaveNAK
; 
 104                 TWI_StopTransmission(); 
 110 uint8_t TWI_WritePacket(const uint8_t SlaveAddress
, 
 111                         const uint8_t TimeoutMS
, 
 112                         const uint8_t* InternalAddress
, 
 113                         uint8_t InternalAddressLen
, 
 114                         const uint8_t* Buffer
, 
 119         if ((ErrorCode 
= TWI_StartTransmission((SlaveAddress 
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
, 
 120                                                TimeoutMS
)) == TWI_ERROR_NoError
) 
 122                 while (InternalAddressLen
--) 
 124                         if (!(TWI_SendByte(*(InternalAddress
++)))) 
 126                                 ErrorCode 
= TWI_ERROR_SlaveNAK
; 
 133                         if (!(TWI_SendByte(*(Buffer
++)))) 
 135                                 ErrorCode 
= TWI_ERROR_SlaveNAK
; 
 140                 TWI_StopTransmission();