Fix incorrect macro guard in the UC3 EndpointStream header file.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / AVR8 / TWI_AVR8.c
1 /*
2 Copyright (C) Dean Camera, 2011.
3
4 dean [at] fourwalledcubicle [dot] com
5 www.lufa-lib.org
6 */
7
8 #define __INCLUDE_FROM_TWI_C
9 #include "../TWI.h"
10
11 uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,
12 const uint8_t TimeoutMS)
13 {
14 for (;;)
15 {
16 bool BusCaptured = false;
17 uint16_t TimeoutRemaining;
18
19 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
20
21 TimeoutRemaining = (TimeoutMS * 100);
22 while (TimeoutRemaining-- && !(BusCaptured))
23 {
24 if (TWCR & (1 << TWINT))
25 {
26 switch (TWSR & TW_STATUS_MASK)
27 {
28 case TW_START:
29 case TW_REP_START:
30 BusCaptured = true;
31 break;
32 case TW_MT_ARB_LOST:
33 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
34 continue;
35 default:
36 TWCR = (1 << TWEN);
37 return TWI_ERROR_BusFault;
38 }
39 }
40
41 _delay_us(10);
42 }
43
44 if (!(TimeoutRemaining))
45 {
46 TWCR = (1 << TWEN);
47 return TWI_ERROR_BusCaptureTimeout;
48 }
49
50 TWDR = SlaveAddress;
51 TWCR = ((1 << TWINT) | (1 << TWEN));
52
53 TimeoutRemaining = (TimeoutMS * 100);
54 while (TimeoutRemaining--)
55 {
56 if (TWCR & (1 << TWINT))
57 break;
58
59 _delay_us(10);
60 }
61
62 if (!(TimeoutRemaining))
63 return TWI_ERROR_SlaveResponseTimeout;
64
65 switch (TWSR & TW_STATUS_MASK)
66 {
67 case TW_MT_SLA_ACK:
68 case TW_MR_SLA_ACK:
69 return TWI_ERROR_NoError;
70 default:
71 TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
72 return TWI_ERROR_SlaveNotReady;
73 }
74 }
75 }
76
77 uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
78 const uint8_t TimeoutMS,
79 const uint8_t* InternalAddress,
80 uint8_t InternalAddressLen,
81 uint8_t* Buffer,
82 uint8_t Length)
83 {
84 uint8_t ErrorCode;
85
86 if ((ErrorCode = TWI_WritePacket(SlaveAddress, TimeoutMS, InternalAddress, InternalAddressLen,
87 NULL, 0)) != TWI_ERROR_NoError)
88 {
89 return ErrorCode;
90 }
91
92 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
93 TimeoutMS)) == TWI_ERROR_NoError)
94 {
95 while (Length--)
96 {
97 if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))
98 {
99 ErrorCode = TWI_ERROR_SlaveNAK;
100 break;
101 }
102 }
103
104 TWI_StopTransmission();
105 }
106
107 return ErrorCode;
108 }
109
110 uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
111 const uint8_t TimeoutMS,
112 const uint8_t* InternalAddress,
113 uint8_t InternalAddressLen,
114 const uint8_t* Buffer,
115 uint8_t Length)
116 {
117 uint8_t ErrorCode;
118
119 if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
120 TimeoutMS)) == TWI_ERROR_NoError)
121 {
122 while (InternalAddressLen--)
123 {
124 if (!(TWI_SendByte(*(InternalAddress++))))
125 {
126 ErrorCode = TWI_ERROR_SlaveNAK;
127 break;
128 }
129 }
130
131 while (Length--)
132 {
133 if (!(TWI_SendByte(*(Buffer++))))
134 {
135 ErrorCode = TWI_ERROR_SlaveNAK;
136 break;
137 }
138 }
139
140 TWI_StopTransmission();
141 }
142
143 return ErrorCode;
144 }