Be doubly-certain that the incomming CDC class driver's endpoint/pipe is flushed...
[pub/USBasp.git] / LUFA / Drivers / Peripheral / TWI.c
1 /*
2 Copyright (C) Dean Camera, 2010.
3
4 dean [at] fourwalledcubicle [dot] com
5 www.fourwalledcubicle.com
6 */
7
8 #include "TWI.h"
9
10 bool TWI_StartTransmission(uint8_t SlaveAddress)
11 {
12 for (;;)
13 {
14 uint8_t IterationsRemaining = 50;
15 bool BusCaptured = false;
16
17 while (IterationsRemaining-- && !BusCaptured)
18 {
19 TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
20 while (!(TWCR & (1 << TWINT)));
21
22 switch (TWSR & TW_STATUS_MASK)
23 {
24 case TW_START:
25 case TW_REP_START:
26 BusCaptured = true;
27 break;
28 case TW_MT_ARB_LOST:
29 continue;
30 default:
31 return false;
32 }
33 }
34
35 if (!(BusCaptured))
36 return false;
37
38 TWDR = SlaveAddress;
39 TWCR = ((1 << TWINT) | (1 << TWEN));
40 while (!(TWCR & (1 << TWINT)));
41
42 GPIOR0 = (TWSR & TW_STATUS_MASK);
43
44 switch (TWSR & TW_STATUS_MASK)
45 {
46 case TW_MT_SLA_ACK:
47 case TW_MR_SLA_ACK:
48 return true;
49 default:
50 TWI_StopTransmission();
51 break;
52 }
53 }
54 }