81fbd0784bf86c262893dbc0d6dc262ecae87038
[pub/USBasp.git] / LUFA / Drivers / Peripheral / XMEGA / TWI_XMEGA.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #include "../../../Common/Common.h"
32 #if (ARCH == ARCH_XMEGA)
33
34 #define __INCLUDE_FROM_TWI_C
35 #include "../TWI.h"
36
37 static inline bool bitmask_is_set(uint8_t byte, uint8_t mask) {
38 return (byte & mask) == mask;
39 }
40
41 uint8_t TWI_StartTransmission(TWI_t *twi,
42 const uint8_t SlaveAddress,
43 const uint8_t TimeoutMS)
44 {
45 uint16_t TimeoutRemaining;
46
47 twi->MASTER.ADDR = SlaveAddress;
48
49 TimeoutRemaining = (TimeoutMS * 100);
50 while (TimeoutRemaining)
51 {
52 uint8_t status = twi->MASTER.STATUS;
53 if (bitmask_is_set(status, TWI_MASTER_WIF_bm | TWI_MASTER_ARBLOST_bm))
54 {
55 // Case 1: Arbitration lost. Try again. (or error)
56 twi->MASTER.ADDR = SlaveAddress;
57 }
58 else if (bitmask_is_set(status, TWI_MASTER_WIF_bm | TWI_MASTER_RXACK_bm))
59 {
60 // Case 2: No response from slave.
61 // We need to release the bus.
62 TWI_StopTransmission(twi);
63 return TWI_ERROR_SlaveResponseTimeout;
64 }
65 else if (status & TWI_MASTER_WIF_bm)
66 {
67 // Case 3: Slave ACK the Write. Ready!
68 return TWI_ERROR_NoError;
69 }
70 else if (status & TWI_MASTER_RIF_bm)
71 {
72 // Case 4: Slave ACK the Read. Ready! (a byte will be read)
73 return TWI_ERROR_NoError;
74 }
75 // Still waiting..
76 _delay_us(10);
77 TimeoutRemaining--;
78 }
79
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);
84 }
85 }
86 return TWI_ERROR_BusCaptureTimeout;
87 }
88
89 bool TWI_SendByte(TWI_t *twi, const uint8_t Byte)
90 {
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);
95 }
96
97 bool TWI_ReceiveByte(TWI_t *twi, uint8_t* const Byte,
98 const bool LastByte)
99 {
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)) {
102 return false;
103 }
104 while (!(twi->MASTER.STATUS & TWI_MASTER_RIF_bm));
105 *Byte = twi->MASTER.DATA;
106 if (LastByte)
107 twi->MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc;
108 else
109 twi->MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
110 return true;
111 }
112
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,
118 uint8_t* Buffer,
119 uint8_t Length)
120 {
121 uint8_t ErrorCode;
122
123 if ((ErrorCode = TWI_StartTransmission(twi, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
124 TimeoutMS)) == TWI_ERROR_NoError)
125 {
126 while (InternalAddressLen--)
127 {
128 if (!(TWI_SendByte(twi, *(InternalAddress++))))
129 {
130 ErrorCode = TWI_ERROR_SlaveNAK;
131 break;
132 }
133 }
134
135 if ((ErrorCode = TWI_StartTransmission(twi, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
136 TimeoutMS)) == TWI_ERROR_NoError)
137 {
138 while (Length--)
139 {
140 if (!(TWI_ReceiveByte(twi, Buffer++, (Length == 0))))
141 {
142 ErrorCode = TWI_ERROR_SlaveNAK;
143 break;
144 }
145 }
146
147 }
148 TWI_StopTransmission(twi);
149 }
150
151 return ErrorCode;
152 }
153
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,
160 uint8_t Length)
161 {
162 uint8_t ErrorCode;
163 if ((ErrorCode = TWI_StartTransmission(twi, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
164 TimeoutMS)) == TWI_ERROR_NoError)
165 {
166 while (InternalAddressLen--)
167 {
168 if (!(TWI_SendByte(twi, *(InternalAddress++))))
169 {
170 ErrorCode = TWI_ERROR_SlaveNAK;
171 break;
172 }
173 }
174
175 while (Length--)
176 {
177 if (!(TWI_SendByte(twi, *(Buffer++))))
178 {
179 ErrorCode = TWI_ERROR_SlaveNAK;
180 break;
181 }
182 }
183
184 TWI_StopTransmission(twi);
185 }
186
187 return ErrorCode;
188 }
189
190 #endif