4 * Created: 6/01/2014 10:38:42 PM
5 * Author: Chris Herring
6 * http://www.chrisherring.net/all/tutorial-interrupt-driven-twi-interface-for-avr-part1/
12 // TWI bit rate (was 100000)
13 #define TWI_FREQ 400000
15 #define TWI_STATUS (TWSR & 0xF8)
16 // Transmit buffer length
17 #define TXMAXBUFLEN 20
18 // Receive buffer length
19 #define RXMAXBUFLEN 20
20 // Global transmit buffer
21 uint8_t TWITransmitBuffer[TXMAXBUFLEN];
22 // Global receive buffer
23 volatile uint8_t TWIReceiveBuffer[RXMAXBUFLEN];
25 volatile int TXBuffIndex; // Index of the transmit buffer. Is volatile, can change at any time.
26 int RXBuffIndex; // Current index in the receive buffer
28 int TXBuffLen; // The total length of the transmit buffer
29 int RXBuffLen; // The total number of bytes to read (should be less than RXMAXBUFFLEN)
41 typedef struct TWIInfoStruct{
46 TWIInfoStruct TWIInfo;
50 #define TWI_START_SENT 0x08 // Start sent
51 #define TWI_REP_START_SENT 0x10 // Repeated Start sent
52 // Master Transmitter Mode
53 #define TWI_MT_SLAW_ACK 0x18 // SLA+W sent and ACK received
54 #define TWI_MT_SLAW_NACK 0x20 // SLA+W sent and NACK received
55 #define TWI_MT_DATA_ACK 0x28 // DATA sent and ACK received
56 #define TWI_MT_DATA_NACK 0x30 // DATA sent and NACK received
57 // Master Receiver Mode
58 #define TWI_MR_SLAR_ACK 0x40 // SLA+R sent, ACK received
59 #define TWI_MR_SLAR_NACK 0x48 // SLA+R sent, NACK received
60 #define TWI_MR_DATA_ACK 0x50 // Data received, ACK returned
61 #define TWI_MR_DATA_NACK 0x58 // Data received, NACK returned
63 // Miscellaneous States
64 #define TWI_LOST_ARBIT 0x38 // Arbitration has been lost
65 #define TWI_NO_RELEVANT_INFO 0xF8 // No relevant information available
66 #define TWI_ILLEGAL_START_STOP 0x00 // Illegal START or STOP condition has been detected
67 #define TWI_SUCCESS 0xFF // Successful transfer, this state is impossible from TWSR as bit2 is 0 and read only
70 #define TWISendStart() (TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)|(1<<TWIE)) // Send the START signal, enable interrupts and TWI, clear TWINT flag to resume transfer.
71 #define TWISendStop() (TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN)|(1<<TWIE)) // Send the STOP signal, enable interrupts and TWI, clear TWINT flag.
72 #define TWISendTransmit() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)) // Used to resume a transfer, clear TWINT and ensure that TWI and interrupts are enabled.
73 #define TWISendACK() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)|(1<<TWEA)) // FOR MR mode. Resume a transfer, ensure that TWI and interrupts are enabled and respond with an ACK if the device is addressed as a slave or after it receives a byte.
74 #define TWISendNACK() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)) // FOR MR mode. Resume a transfer, ensure that TWI and interrupts are enabled but DO NOT respond with an ACK if the device is addressed as a slave or after it receives a byte.
76 // Function declarations
77 uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart);
79 uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart);
80 uint8_t isTWIReady(void);