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_XMEGA)
34 #define __INCLUDE_FROM_TWI_C
37 uint8_t TWI_StartTransmission(TWI_t
* const TWI
,
38 const uint8_t SlaveAddress
,
39 const uint8_t TimeoutMS
)
41 uint16_t TimeoutRemaining
;
43 TWI
->MASTER
.ADDR
= SlaveAddress
;
45 TimeoutRemaining
= (TimeoutMS
* 100);
46 while (TimeoutRemaining
)
48 uint8_t status
= TWI
->MASTER
.STATUS
;
50 if ((status
& (TWI_MASTER_WIF_bm
| TWI_MASTER_ARBLOST_bm
)) == (TWI_MASTER_WIF_bm
| TWI_MASTER_ARBLOST_bm
))
52 TWI
->MASTER
.ADDR
= SlaveAddress
;
54 else if ((status
& (TWI_MASTER_WIF_bm
| TWI_MASTER_RXACK_bm
)) == (TWI_MASTER_WIF_bm
| TWI_MASTER_RXACK_bm
))
56 TWI_StopTransmission(twi
);
57 return TWI_ERROR_SlaveResponseTimeout
;
59 else if (status
& (TWI_MASTER_WIF_bm
| TWI_MASTER_RIF_bm
))
61 return TWI_ERROR_NoError
;
68 if (!(TimeoutRemaining
)) {
69 if (TWI
->MASTER
.STATUS
& TWI_MASTER_CLKHOLD_bm
) {
70 TWI_StopTransmission(twi
);
74 return TWI_ERROR_BusCaptureTimeout
;
77 bool TWI_SendByte(TWI_t
* const TWI
,
80 TWI
->MASTER
.DATA
= Byte
;
82 while (!(TWI
->MASTER
.STATUS
& TWI_MASTER_WIF_bm
));
84 return (TWI
->MASTER
.STATUS
& TWI_MASTER_WIF_bm
) && !(TWI
->MASTER
.STATUS
& TWI_MASTER_RXACK_bm
);
87 bool TWI_ReceiveByte(TWI_t
* const TWI
,
91 if ((TWI
->MASTER
.STATUS
& (TWI_MASTER_BUSERR_bm
| TWI_MASTER_ARBLOST_bm
)) == (TWI_MASTER_BUSERR_bm
| TWI_MASTER_ARBLOST_bm
)) {
95 while (!(TWI
->MASTER
.STATUS
& TWI_MASTER_RIF_bm
));
97 *Byte
= TWI
->MASTER
.DATA
;
100 TWI
->MASTER
.CTRLC
= TWI_MASTER_ACKACT_bm
| TWI_MASTER_CMD_STOP_gc
;
102 TWI
->MASTER
.CTRLC
= TWI_MASTER_CMD_RECVTRANS_gc
;
107 uint8_t TWI_ReadPacket(TWI_t
* const TWI
,
108 const uint8_t SlaveAddress
,
109 const uint8_t TimeoutMS
,
110 const uint8_t* InternalAddress
,
111 uint8_t InternalAddressLen
,
117 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
,
118 TimeoutMS
)) == TWI_ERROR_NoError
)
120 while (InternalAddressLen
--)
122 if (!(TWI_SendByte(twi
, *(InternalAddress
++))))
124 ErrorCode
= TWI_ERROR_SlaveNAK
;
129 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_READ
,
130 TimeoutMS
)) == TWI_ERROR_NoError
)
134 if (!(TWI_ReceiveByte(twi
, Buffer
++, (Length
== 0))))
136 ErrorCode
= TWI_ERROR_SlaveNAK
;
142 TWI_StopTransmission(twi
);
148 uint8_t TWI_WritePacket(TWI_t
* const twi
,
149 const uint8_t SlaveAddress
,
150 const uint8_t TimeoutMS
,
151 const uint8_t* InternalAddress
,
152 uint8_t InternalAddressLen
,
153 const uint8_t* Buffer
,
158 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
,
159 TimeoutMS
)) == TWI_ERROR_NoError
)
161 while (InternalAddressLen
--)
163 if (!(TWI_SendByte(twi
, *(InternalAddress
++))))
165 ErrorCode
= TWI_ERROR_SlaveNAK
;
172 if (!(TWI_SendByte(twi
, *(Buffer
++))))
174 ErrorCode
= TWI_ERROR_SlaveNAK
;
179 TWI_StopTransmission(twi
);