]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/Peripheral/XMEGA/TWI_XMEGA.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / Peripheral / XMEGA / TWI_XMEGA.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_XMEGA)
33
34 #define __INCLUDE_FROM_TWI_C
35 #include "../TWI.h"
36
37 uint8_t TWI_StartTransmission(TWI_t* const 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 uint8_t status = TWI->MASTER.STATUS;
49
50 if ((status & (TWI_MASTER_WIF_bm | TWI_MASTER_ARBLOST_bm)) == (TWI_MASTER_WIF_bm | TWI_MASTER_ARBLOST_bm))
51 {
52 TWI->MASTER.ADDR = SlaveAddress;
53 }
54 else if ((status & (TWI_MASTER_WIF_bm | TWI_MASTER_RXACK_bm)) == (TWI_MASTER_WIF_bm | TWI_MASTER_RXACK_bm))
55 {
56 TWI_StopTransmission(TWI);
57 return TWI_ERROR_SlaveResponseTimeout;
58 }
59 else if (status & (TWI_MASTER_WIF_bm | TWI_MASTER_RIF_bm))
60 {
61 return TWI_ERROR_NoError;
62 }
63
64 _delay_us(10);
65 TimeoutRemaining--;
66 }
67
68 if (!(TimeoutRemaining)) {
69 if (TWI->MASTER.STATUS & TWI_MASTER_CLKHOLD_bm) {
70 TWI_StopTransmission(TWI);
71 }
72 }
73
74 return TWI_ERROR_BusCaptureTimeout;
75 }
76
77 bool TWI_SendByte(TWI_t* const TWI,
78 const uint8_t Byte)
79 {
80 TWI->MASTER.DATA = Byte;
81
82 while (!(TWI->MASTER.STATUS & TWI_MASTER_WIF_bm));
83
84 return (TWI->MASTER.STATUS & TWI_MASTER_WIF_bm) && !(TWI->MASTER.STATUS & TWI_MASTER_RXACK_bm);
85 }
86
87 bool TWI_ReceiveByte(TWI_t* const TWI,
88 uint8_t* const Byte,
89 const bool LastByte)
90 {
91 if ((TWI->MASTER.STATUS & (TWI_MASTER_BUSERR_bm | TWI_MASTER_ARBLOST_bm)) == (TWI_MASTER_BUSERR_bm | TWI_MASTER_ARBLOST_bm)) {
92 return false;
93 }
94
95 while (!(TWI->MASTER.STATUS & TWI_MASTER_RIF_bm));
96
97 *Byte = TWI->MASTER.DATA;
98
99 if (LastByte)
100 TWI->MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc;
101 else
102 TWI->MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
103
104 return true;
105 }
106
107 uint8_t TWI_ReadPacket(TWI_t* const TWI,
108 const uint8_t SlaveAddress,
109 const uint8_t TimeoutMS,
110 const uint8_t* InternalAddress,
111 uint8_t InternalAddressLen,
112 uint8_t* Buffer,
113 uint8_t Length)
114 {
115 uint8_t ErrorCode;
116
117 if ((ErrorCode = TWI_StartTransmission(TWI, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
118 TimeoutMS)) == TWI_ERROR_NoError)
119 {
120 while (InternalAddressLen--)
121 {
122 if (!(TWI_SendByte(TWI, *(InternalAddress++))))
123 {
124 ErrorCode = TWI_ERROR_SlaveNAK;
125 break;
126 }
127 }
128
129 if ((ErrorCode = TWI_StartTransmission(TWI, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
130 TimeoutMS)) == TWI_ERROR_NoError)
131 {
132 while (Length--)
133 {
134 if (!(TWI_ReceiveByte(TWI, Buffer++, (Length == 0))))
135 {
136 ErrorCode = TWI_ERROR_SlaveNAK;
137 break;
138 }
139 }
140 }
141
142 TWI_StopTransmission(TWI);
143 }
144
145 return ErrorCode;
146 }
147
148 uint8_t TWI_WritePacket(TWI_t* const TWI,
149 const uint8_t SlaveAddress,
150 const uint8_t TimeoutMS,
151 const uint8_t* InternalAddress,
152 uint8_t InternalAddressLen,
153 const uint8_t* Buffer,
154 uint8_t Length)
155 {
156 uint8_t ErrorCode;
157
158 if ((ErrorCode = TWI_StartTransmission(TWI, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
159 TimeoutMS)) == TWI_ERROR_NoError)
160 {
161 while (InternalAddressLen--)
162 {
163 if (!(TWI_SendByte(TWI, *(InternalAddress++))))
164 {
165 ErrorCode = TWI_ERROR_SlaveNAK;
166 break;
167 }
168 }
169
170 while (Length--)
171 {
172 if (!(TWI_SendByte(TWI, *(Buffer++))))
173 {
174 ErrorCode = TWI_ERROR_SlaveNAK;
175 break;
176 }
177 }
178
179 TWI_StopTransmission(TWI);
180 }
181
182 return ErrorCode;
183 }
184
185 #endif
Imprint / Impressum