]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / LUFA / Drivers / Peripheral / AVR8 / TWI_AVR8.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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_AVR8) && defined(TWCR)
33
34 #define __INCLUDE_FROM_TWI_C
35 #include "../TWI.h"
36
37 uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,
38 const uint8_t TimeoutMS)
39 {
40 for (;;)
41 {
42 bool BusCaptured = false;
43 uint16_t TimeoutRemaining;
44
45 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
46
47 TimeoutRemaining = (TimeoutMS * 100);
48 while (TimeoutRemaining && !(BusCaptured))
49 {
50 if (TWCR & (1 << TWINT))
51 {
52 switch (TWSR & TW_STATUS_MASK)
53 {
54 case TW_START:
55 case TW_REP_START:
56 BusCaptured = true;
57 break;
58 case TW_MT_ARB_LOST:
59 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
60 continue;
61 default:
62 TWCR = (1 << TWEN);
63 return TWI_ERROR_BusFault;
64 }
65 }
66
67 _delay_us(10);
68 TimeoutRemaining--;
69 }
70
71 if (!(TimeoutRemaining))
72 {
73 TWCR = (1 << TWEN);
74 return TWI_ERROR_BusCaptureTimeout;
75 }
76
77 TWDR = SlaveAddress;
78 TWCR = ((1 << TWINT) | (1 << TWEN));
79
80 TimeoutRemaining = (TimeoutMS * 100);
81 while (TimeoutRemaining)
82 {
83 if (TWCR & (1 << TWINT))
84 break;
85
86 _delay_us(10);
87 TimeoutRemaining--;
88 }
89
90 if (!(TimeoutRemaining))
91 return TWI_ERROR_SlaveResponseTimeout;
92
93 switch (TWSR & TW_STATUS_MASK)
94 {
95 case TW_MT_SLA_ACK:
96 case TW_MR_SLA_ACK:
97 return TWI_ERROR_NoError;
98 default:
99 TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
100 return TWI_ERROR_SlaveNotReady;
101 }
102 }
103 }
104
105 bool TWI_SendByte(const uint8_t Byte)
106 {
107 TWDR = Byte;
108 TWCR = ((1 << TWINT) | (1 << TWEN));
109 while (!(TWCR & (1 << TWINT)));
110
111 return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);
112 }
113
114 bool TWI_ReceiveByte(uint8_t* const Byte,
115 const bool LastByte)
116 {
117 uint8_t TWCRMask;
118
119 if (LastByte)
120 TWCRMask = ((1 << TWINT) | (1 << TWEN));
121 else
122 TWCRMask = ((1 << TWINT) | (1 << TWEN) | (1 << TWEA));
123
124 TWCR = TWCRMask;
125 while (!(TWCR & (1 << TWINT)));
126 *Byte = TWDR;
127
128 uint8_t Status = (TWSR & TW_STATUS_MASK);
129
130 return ((LastByte) ? (Status == TW_MR_DATA_NACK) : (Status == TW_MR_DATA_ACK));
131 }
132
133 uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
134 const uint8_t TimeoutMS,
135 const uint8_t* InternalAddress,
136 uint8_t InternalAddressLen,
137 uint8_t* Buffer,
138 uint8_t Length)
139 {
140 uint8_t ErrorCode;
141
142 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
143 TimeoutMS)) == TWI_ERROR_NoError)
144 {
145 while (InternalAddressLen--)
146 {
147 if (!(TWI_SendByte(*(InternalAddress++))))
148 {
149 ErrorCode = TWI_ERROR_SlaveNAK;
150 break;
151 }
152 }
153
154 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
155 TimeoutMS)) == TWI_ERROR_NoError)
156 {
157 while (Length--)
158 {
159 if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))
160 {
161 ErrorCode = TWI_ERROR_SlaveNAK;
162 break;
163 }
164 }
165
166 TWI_StopTransmission();
167 }
168 }
169
170 return ErrorCode;
171 }
172
173 uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
174 const uint8_t TimeoutMS,
175 const uint8_t* InternalAddress,
176 uint8_t InternalAddressLen,
177 const uint8_t* Buffer,
178 uint8_t Length)
179 {
180 uint8_t ErrorCode;
181
182 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
183 TimeoutMS)) == TWI_ERROR_NoError)
184 {
185 while (InternalAddressLen--)
186 {
187 if (!(TWI_SendByte(*(InternalAddress++))))
188 {
189 ErrorCode = TWI_ERROR_SlaveNAK;
190 break;
191 }
192 }
193
194 while (Length--)
195 {
196 if (!(TWI_SendByte(*(Buffer++))))
197 {
198 ErrorCode = TWI_ERROR_SlaveNAK;
199 break;
200 }
201 }
202
203 TWI_StopTransmission();
204 }
205
206 return ErrorCode;
207 }
208
209 #endif
Imprint / Impressum