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 static inline bool bitmask_is_set(uint8_t byte
, uint8_t mask
) {
38 return (byte
& mask
) == mask
;
41 uint8_t TWI_StartTransmission(TWI_t
*twi
,
42 const uint8_t SlaveAddress
,
43 const uint8_t TimeoutMS
)
45 uint16_t TimeoutRemaining
;
47 twi
->MASTER
.ADDR
= SlaveAddress
;
49 TimeoutRemaining
= (TimeoutMS
* 100);
50 while (TimeoutRemaining
)
52 uint8_t status
= twi
->MASTER
.STATUS
;
53 if (bitmask_is_set(status
, TWI_MASTER_WIF_bm
| TWI_MASTER_ARBLOST_bm
))
55 // Case 1: Arbitration lost. Try again. (or error)
56 twi
->MASTER
.ADDR
= SlaveAddress
;
58 else if (bitmask_is_set(status
, TWI_MASTER_WIF_bm
| TWI_MASTER_RXACK_bm
))
60 // Case 2: No response from slave.
61 // We need to release the bus.
62 TWI_StopTransmission(twi
);
63 return TWI_ERROR_SlaveResponseTimeout
;
65 else if (status
& TWI_MASTER_WIF_bm
)
67 // Case 3: Slave ACK the Write. Ready!
68 return TWI_ERROR_NoError
;
70 else if (status
& TWI_MASTER_RIF_bm
)
72 // Case 4: Slave ACK the Read. Ready! (a byte will be read)
73 return TWI_ERROR_NoError
;
80 if (!(TimeoutRemaining
)) {
81 if (twi
->MASTER
.STATUS
& TWI_MASTER_CLKHOLD_bm
) {
82 // Release the bus if we're holding it.
83 TWI_StopTransmission(twi
);
86 return TWI_ERROR_BusCaptureTimeout
;
89 bool TWI_SendByte(TWI_t
*twi
, const uint8_t Byte
)
91 // We assume we're ready to write!
92 twi
->MASTER
.DATA
= Byte
;
93 while (!(twi
->MASTER
.STATUS
& TWI_MASTER_WIF_bm
));
94 return (twi
->MASTER
.STATUS
& TWI_MASTER_WIF_bm
) && !(twi
->MASTER
.STATUS
& TWI_MASTER_RXACK_bm
);
97 bool TWI_ReceiveByte(TWI_t
*twi
, uint8_t* const Byte
,
100 // If we're here, we should already be reading. Wait if we haven't read yet.
101 if (bitmask_is_set(twi
->MASTER
.STATUS
, TWI_MASTER_BUSERR_bm
| TWI_MASTER_ARBLOST_bm
)) {
104 while (!(twi
->MASTER
.STATUS
& TWI_MASTER_RIF_bm
));
105 *Byte
= twi
->MASTER
.DATA
;
107 twi
->MASTER
.CTRLC
= TWI_MASTER_ACKACT_bm
| TWI_MASTER_CMD_STOP_gc
;
109 twi
->MASTER
.CTRLC
= TWI_MASTER_CMD_RECVTRANS_gc
;
113 uint8_t TWI_ReadPacket(TWI_t
*twi
,
114 const uint8_t SlaveAddress
,
115 const uint8_t TimeoutMS
,
116 const uint8_t* InternalAddress
,
117 uint8_t InternalAddressLen
,
123 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
,
124 TimeoutMS
)) == TWI_ERROR_NoError
)
126 while (InternalAddressLen
--)
128 if (!(TWI_SendByte(twi
, *(InternalAddress
++))))
130 ErrorCode
= TWI_ERROR_SlaveNAK
;
135 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_READ
,
136 TimeoutMS
)) == TWI_ERROR_NoError
)
140 if (!(TWI_ReceiveByte(twi
, Buffer
++, (Length
== 0))))
142 ErrorCode
= TWI_ERROR_SlaveNAK
;
148 TWI_StopTransmission(twi
);
154 uint8_t TWI_WritePacket(TWI_t
*twi
,
155 const uint8_t SlaveAddress
,
156 const uint8_t TimeoutMS
,
157 const uint8_t* InternalAddress
,
158 uint8_t InternalAddressLen
,
159 const uint8_t* Buffer
,
163 if ((ErrorCode
= TWI_StartTransmission(twi
, (SlaveAddress
& TWI_DEVICE_ADDRESS_MASK
) | TWI_ADDRESS_WRITE
,
164 TimeoutMS
)) == TWI_ERROR_NoError
)
166 while (InternalAddressLen
--)
168 if (!(TWI_SendByte(twi
, *(InternalAddress
++))))
170 ErrorCode
= TWI_ERROR_SlaveNAK
;
177 if (!(TWI_SendByte(twi
, *(Buffer
++))))
179 ErrorCode
= TWI_ERROR_SlaveNAK
;
184 TWI_StopTransmission(twi
);