166427943f28f422f18f86d3419a7a934202c319
[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 uint8_t TWI_StartTransmission(TWI_t *twi,
38 const uint8_t SlaveAddress,
39 const uint8_t TimeoutMS)
40 {
41 uint16_t TimeoutRemaining;
42
43 twi->MASTER.ADDR = SlaveAddress;
44
45 TimeoutRemaining = (TimeoutMS * 100);
46 while (TimeoutRemaining)
47 {
48 if (twi->MASTER.STATUS & (TWI_MASTER_WIF_bm | TWI_MASTER_ARBLOST_bm))
49 {
50 // Case 1: Arbitration lost. Try again. (or error)
51 twi->MASTER.ADDR = SlaveAddress;
52 }
53 else if (twi->MASTER.STATUS & (TWI_MASTER_WIF_bm | TWI_MASTER_RXACK_bm))
54 {
55 // Case 2: No response from slave.
56 return TWI_ERROR_SlaveResponseTimeout;
57 }
58 else if (twi->MASTER.STATUS & (TWI_MASTER_WIF_bm))
59 {
60 // Case 3: Slave ACK the Write. Ready!
61 return TWI_ERROR_NoError;
62 }
63 else if (twi->MASTER.STATUS & (TWI_MASTER_RIF_bm))
64 {
65 // Case 4: Slave ACK the Read. Ready! (a byte will be read)
66 return TWI_ERROR_NoError;
67 }
68 // Still waiting..
69 _delay_us(10);
70 TimeoutRemaining--;
71 }
72
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;
77 }
78 }
79 return TWI_ERROR_BusCaptureTimeout;
80 }
81
82 bool TWI_SendByte(TWI_t *twi, const uint8_t Byte)
83 {
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);
88 }
89
90 bool TWI_ReceiveByte(TWI_t *twi, uint8_t* const Byte,
91 const bool LastByte)
92 {
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)) {
95 return false;
96 }
97 while (!(twi->MASTER.STATUS & TWI_MASTER_RIF_bm));
98 *Byte = twi->MASTER.DATA;
99 if (LastByte)
100 twi->MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_RECVTRANS_gc;
101 else
102 twi->MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
103 return true;
104 }
105
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,
111 uint8_t* Buffer,
112 uint8_t Length)
113 {
114 uint8_t ErrorCode;
115
116 if ((ErrorCode = TWI_StartTransmission(twi, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
117 TimeoutMS)) == TWI_ERROR_NoError)
118 {
119 while (InternalAddressLen--)
120 {
121 if (!(TWI_SendByte(twi, *(InternalAddress++))))
122 {
123 ErrorCode = TWI_ERROR_SlaveNAK;
124 break;
125 }
126 }
127
128 if ((ErrorCode = TWI_StartTransmission(twi, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
129 TimeoutMS)) == TWI_ERROR_NoError)
130 {
131 while (Length--)
132 {
133 if (!(TWI_ReceiveByte(twi, Buffer++, (Length == 0))))
134 {
135 ErrorCode = TWI_ERROR_SlaveNAK;
136 break;
137 }
138 }
139
140 TWI_StopTransmission(twi);
141 }
142 }
143
144 return ErrorCode;
145 }
146
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,
153 uint8_t Length)
154 {
155 uint8_t ErrorCode;
156
157 if ((ErrorCode = TWI_StartTransmission(twi, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
158 TimeoutMS)) == TWI_ERROR_NoError)
159 {
160 while (InternalAddressLen--)
161 {
162 if (!(TWI_SendByte(twi, *(InternalAddress++))))
163 {
164 ErrorCode = TWI_ERROR_SlaveNAK;
165 break;
166 }
167 }
168
169 while (Length--)
170 {
171 if (!(TWI_SendByte(twi, *(Buffer++))))
172 {
173 ErrorCode = TWI_ERROR_SlaveNAK;
174 break;
175 }
176 }
177
178 TWI_StopTransmission(twi);
179 }
180
181 return ErrorCode;
182 }
183
184 #endif