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
*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 if (twi
->MASTER
.STATUS
& (TWI_MASTER_WIF_bm
| TWI_MASTER_ARBLOST_bm
))
50 // Case 1: Arbitration lost. Try again. (or error)
51 twi
->MASTER
.ADDR
= SlaveAddress
;
53 else if (twi
->MASTER
.STATUS
& (TWI_MASTER_WIF_bm
| TWI_MASTER_RXACK_bm
))
55 // Case 2: No response from slave.
56 return TWI_ERROR_SlaveResponseTimeout
;
58 else if (twi
->MASTER
.STATUS
& (TWI_MASTER_WIF_bm
))
60 // Case 3: Slave ACK the Write. Ready!
61 return TWI_ERROR_NoError
;
63 else if (twi
->MASTER
.STATUS
& (TWI_MASTER_RIF_bm
))
65 // Case 4: Slave ACK the Read. Ready! (a byte will be read)
66 return TWI_ERROR_NoError
;
73 if (!(TimeoutRemaining
)) {
74 if (twi
->MASTER
.STATUS
& TWI_MASTER_CLKHOLD_bm
) {
75 // Release the bus if we're holding it.
76 twi
->MASTER
.CTRLC
= TWI_MASTER_CMD_STOP_gc
;
79 return TWI_ERROR_BusCaptureTimeout
;
82 bool TWI_SendByte(TWI_t
*twi
, const uint8_t Byte
)
84 // We assume we're ready to write!
85 twi
->MASTER
.DATA
= Byte
;
86 while (!(twi
->MASTER
.STATUS
& TWI_MASTER_WIF_bm
));
87 return (twi
->MASTER
.STATUS
& TWI_MASTER_WIF_bm
) & !(twi
->MASTER
.STATUS
& TWI_MASTER_RXACK_bm
);
90 bool TWI_ReceiveByte(TWI_t
*twi
, uint8_t* const Byte
,
93 // If we're here, we should already be reading. Wait if we haven't read yet.
94 if (twi
->MASTER
.STATUS
& (TWI_MASTER_BUSERR_bm
| TWI_MASTER_ARBLOST_bm
)) {
97 while (!(twi
->MASTER
.STATUS
& TWI_MASTER_RIF_bm
));
98 *Byte
= twi
->MASTER
.DATA
;
100 twi
->MASTER
.CTRLC
= TWI_MASTER_ACKACT_bm
| TWI_MASTER_CMD_RECVTRANS_gc
;
102 twi
->MASTER
.CTRLC
= TWI_MASTER_CMD_RECVTRANS_gc
;
106 uint8_t TWI_ReadPacket(TWI_t
*twi
,
107 const uint8_t SlaveAddress
,
108 const uint8_t TimeoutMS
,
109 const uint8_t* InternalAddress
,
110 uint8_t InternalAddressLen
,
116 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
,
117 TimeoutMS
)) == TWI_ERROR_NoError
)
119 while (InternalAddressLen
--)
121 if (!(TWI_SendByte(twi
, *(InternalAddress
++))))
123 ErrorCode
= TWI_ERROR_SlaveNAK
;
128 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_READ
,
129 TimeoutMS
)) == TWI_ERROR_NoError
)
133 if (!(TWI_ReceiveByte(twi
, Buffer
++, (Length
== 0))))
135 ErrorCode
= TWI_ERROR_SlaveNAK
;
140 TWI_StopTransmission(twi
);
147 uint8_t TWI_WritePacket(TWI_t
*twi
,
148 const uint8_t SlaveAddress
,
149 const uint8_t TimeoutMS
,
150 const uint8_t* InternalAddress
,
151 uint8_t InternalAddressLen
,
152 const uint8_t* Buffer
,
157 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
,
158 TimeoutMS
)) == TWI_ERROR_NoError
)
160 while (InternalAddressLen
--)
162 if (!(TWI_SendByte(twi
, *(InternalAddress
++))))
164 ErrorCode
= TWI_ERROR_SlaveNAK
;
171 if (!(TWI_SendByte(twi
, *(Buffer
++))))
173 ErrorCode
= TWI_ERROR_SlaveNAK
;
178 TWI_StopTransmission(twi
);