]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Peripheral/AVR8/TWI_AVR8.c
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Peripheral / AVR8 / TWI_AVR8.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #include "../../../Common/Common.h"\r
32 #if (ARCH == ARCH_AVR8) && defined(TWCR)\r
33 \r
34 #define  __INCLUDE_FROM_TWI_C\r
35 #include "../TWI.h"\r
36 \r
37 uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,\r
38                               const uint8_t TimeoutMS)\r
39 {\r
40         for (;;)\r
41         {\r
42                 bool     BusCaptured = false;\r
43                 uint16_t TimeoutRemaining;\r
44 \r
45                 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));\r
46 \r
47                 TimeoutRemaining = (TimeoutMS * 100);\r
48                 while (TimeoutRemaining-- && !(BusCaptured))\r
49                 {\r
50                         if (TWCR & (1 << TWINT))\r
51                         {\r
52                                 switch (TWSR & TW_STATUS_MASK)\r
53                                 {\r
54                                         case TW_START:\r
55                                         case TW_REP_START:\r
56                                                 BusCaptured = true;\r
57                                                 break;\r
58                                         case TW_MT_ARB_LOST:\r
59                                                 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));\r
60                                                 continue;\r
61                                         default:\r
62                                                 TWCR = (1 << TWEN);\r
63                                                 return TWI_ERROR_BusFault;\r
64                                 }\r
65                         }\r
66 \r
67                         _delay_us(10);\r
68                 }\r
69 \r
70                 if (!(TimeoutRemaining))\r
71                 {\r
72                         TWCR = (1 << TWEN);\r
73                         return TWI_ERROR_BusCaptureTimeout;\r
74                 }\r
75 \r
76                 TWDR = SlaveAddress;\r
77                 TWCR = ((1 << TWINT) | (1 << TWEN));\r
78 \r
79                 TimeoutRemaining = (TimeoutMS * 100);\r
80                 while (TimeoutRemaining--)\r
81                 {\r
82                         if (TWCR & (1 << TWINT))\r
83                           break;\r
84 \r
85                         _delay_us(10);\r
86                 }\r
87 \r
88                 if (!(TimeoutRemaining))\r
89                   return TWI_ERROR_SlaveResponseTimeout;\r
90 \r
91                 switch (TWSR & TW_STATUS_MASK)\r
92                 {\r
93                         case TW_MT_SLA_ACK:\r
94                         case TW_MR_SLA_ACK:\r
95                                 return TWI_ERROR_NoError;\r
96                         default:\r
97                                 TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));\r
98                                 return TWI_ERROR_SlaveNotReady;\r
99                 }\r
100         }\r
101 }\r
102 \r
103 bool TWI_SendByte(const uint8_t Byte)\r
104 {\r
105         TWDR = Byte;\r
106         TWCR = ((1 << TWINT) | (1 << TWEN));\r
107         while (!(TWCR & (1 << TWINT)));\r
108 \r
109         return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);\r
110 }\r
111 \r
112 bool TWI_ReceiveByte(uint8_t* const Byte,\r
113                                          const bool LastByte)\r
114 {\r
115         uint8_t TWCRMask;\r
116 \r
117         if (LastByte)\r
118           TWCRMask = ((1 << TWINT) | (1 << TWEN));\r
119         else\r
120           TWCRMask = ((1 << TWINT) | (1 << TWEN) | (1 << TWEA));\r
121 \r
122         TWCR = TWCRMask;\r
123         while (!(TWCR & (1 << TWINT)));\r
124         *Byte = TWDR;\r
125 \r
126         uint8_t Status = (TWSR & TW_STATUS_MASK);\r
127 \r
128         return ((LastByte) ? (Status == TW_MR_DATA_NACK) : (Status == TW_MR_DATA_ACK));\r
129 }\r
130 \r
131 uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,\r
132                        const uint8_t TimeoutMS,\r
133                        const uint8_t* InternalAddress,\r
134                        uint8_t InternalAddressLen,\r
135                        uint8_t* Buffer,\r
136                        uint8_t Length)\r
137 {\r
138         uint8_t ErrorCode;\r
139 \r
140         if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,\r
141                                                TimeoutMS)) == TWI_ERROR_NoError)\r
142         {\r
143                 while (InternalAddressLen--)\r
144                 {\r
145                         if (!(TWI_SendByte(*(InternalAddress++))))\r
146                         {\r
147                                 ErrorCode = TWI_ERROR_SlaveNAK;\r
148                                 break;\r
149                         }\r
150                 }\r
151 \r
152                 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,\r
153                                                                                            TimeoutMS)) == TWI_ERROR_NoError)\r
154                 {\r
155                         while (Length--)\r
156                         {\r
157                                 if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))\r
158                                 {\r
159                                         ErrorCode = TWI_ERROR_SlaveNAK;\r
160                                         break;\r
161                                 }\r
162                         }\r
163 \r
164                         TWI_StopTransmission();\r
165                 }\r
166         }\r
167 \r
168         return ErrorCode;\r
169 }\r
170 \r
171 uint8_t TWI_WritePacket(const uint8_t SlaveAddress,\r
172                         const uint8_t TimeoutMS,\r
173                         const uint8_t* InternalAddress,\r
174                         uint8_t InternalAddressLen,\r
175                         const uint8_t* Buffer,\r
176                         uint8_t Length)\r
177 {\r
178         uint8_t ErrorCode;\r
179 \r
180         if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,\r
181                                                TimeoutMS)) == TWI_ERROR_NoError)\r
182         {\r
183                 while (InternalAddressLen--)\r
184                 {\r
185                         if (!(TWI_SendByte(*(InternalAddress++))))\r
186                         {\r
187                                 ErrorCode = TWI_ERROR_SlaveNAK;\r
188                                 break;\r
189                         }\r
190                 }\r
191 \r
192                 while (Length--)\r
193                 {\r
194                         if (!(TWI_SendByte(*(Buffer++))))\r
195                         {\r
196                                 ErrorCode = TWI_ERROR_SlaveNAK;\r
197                                 break;\r
198                         }\r
199                 }\r
200 \r
201                 TWI_StopTransmission();\r
202         }\r
203 \r
204         return ErrorCode;\r
205 }\r
206 \r
207 #endif\r