]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c
Squashed 'tmk_core/' content from commit 05caacc
[tmk_keyboard.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Peripheral / AVR8 / TWI_AVR8.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 disclaim 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 }
69
70 if (!(TimeoutRemaining))
71 {
72 TWCR = (1 << TWEN);
73 return TWI_ERROR_BusCaptureTimeout;
74 }
75
76 TWDR = SlaveAddress;
77 TWCR = ((1 << TWINT) | (1 << TWEN));
78
79 TimeoutRemaining = (TimeoutMS * 100);
80 while (TimeoutRemaining--)
81 {
82 if (TWCR & (1 << TWINT))
83 break;
84
85 _delay_us(10);
86 }
87
88 if (!(TimeoutRemaining))
89 return TWI_ERROR_SlaveResponseTimeout;
90
91 switch (TWSR & TW_STATUS_MASK)
92 {
93 case TW_MT_SLA_ACK:
94 case TW_MR_SLA_ACK:
95 return TWI_ERROR_NoError;
96 default:
97 TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
98 return TWI_ERROR_SlaveNotReady;
99 }
100 }
101 }
102
103 bool TWI_SendByte(const uint8_t Byte)
104 {
105 TWDR = Byte;
106 TWCR = ((1 << TWINT) | (1 << TWEN));
107 while (!(TWCR & (1 << TWINT)));
108
109 return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);
110 }
111
112 bool TWI_ReceiveByte(uint8_t* const Byte,
113 const bool LastByte)
114 {
115 uint8_t TWCRMask;
116
117 if (LastByte)
118 TWCRMask = ((1 << TWINT) | (1 << TWEN));
119 else
120 TWCRMask = ((1 << TWINT) | (1 << TWEN) | (1 << TWEA));
121
122 TWCR = TWCRMask;
123 while (!(TWCR & (1 << TWINT)));
124 *Byte = TWDR;
125
126 uint8_t Status = (TWSR & TW_STATUS_MASK);
127
128 return ((LastByte) ? (Status == TW_MR_DATA_NACK) : (Status == TW_MR_DATA_ACK));
129 }
130
131 uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
132 const uint8_t TimeoutMS,
133 const uint8_t* InternalAddress,
134 uint8_t InternalAddressLen,
135 uint8_t* Buffer,
136 uint8_t Length)
137 {
138 uint8_t ErrorCode;
139
140 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
141 TimeoutMS)) == TWI_ERROR_NoError)
142 {
143 while (InternalAddressLen--)
144 {
145 if (!(TWI_SendByte(*(InternalAddress++))))
146 {
147 ErrorCode = TWI_ERROR_SlaveNAK;
148 break;
149 }
150 }
151
152 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
153 TimeoutMS)) == TWI_ERROR_NoError)
154 {
155 while (Length--)
156 {
157 if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))
158 {
159 ErrorCode = TWI_ERROR_SlaveNAK;
160 break;
161 }
162 }
163
164 TWI_StopTransmission();
165 }
166 }
167
168 return ErrorCode;
169 }
170
171 uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
172 const uint8_t TimeoutMS,
173 const uint8_t* InternalAddress,
174 uint8_t InternalAddressLen,
175 const uint8_t* Buffer,
176 uint8_t Length)
177 {
178 uint8_t ErrorCode;
179
180 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
181 TimeoutMS)) == TWI_ERROR_NoError)
182 {
183 while (InternalAddressLen--)
184 {
185 if (!(TWI_SendByte(*(InternalAddress++))))
186 {
187 ErrorCode = TWI_ERROR_SlaveNAK;
188 break;
189 }
190 }
191
192 while (Length--)
193 {
194 if (!(TWI_SendByte(*(Buffer++))))
195 {
196 ErrorCode = TWI_ERROR_SlaveNAK;
197 break;
198 }
199 }
200
201 TWI_StopTransmission();
202 }
203
204 return ErrorCode;
205 }
206
207 #endif
Imprint / Impressum