4 * Created: 6/01/2014 10:38:42 PM
5 * Author: Chris Herring
12 #define TWI_FREQ 400000
14 #define TWI_STATUS (TWSR & 0xF8)
15 // Transmit buffer length
16 #define TXMAXBUFLEN 20
17 // Receive buffer length
18 #define RXMAXBUFLEN 20
19 // Global transmit buffer
20 volatile uint8_t *TWITransmitBuffer;
21 // Global receive buffer
22 volatile uint8_t TWIReceiveBuffer[RXMAXBUFLEN];
24 volatile int TXBuffIndex; // Index of the transmit buffer. Is volatile, can change at any time.
25 int RXBuffIndex; // Current index in the receive buffer
27 int TXBuffLen; // The total length of the transmit buffer
28 int RXBuffLen; // The total number of bytes to read (should be less than RXMAXBUFFLEN)
40 typedef struct TWIInfoStruct{
45 TWIInfoStruct TWIInfo;
49 #define TWI_START_SENT 0x08 // Start sent
50 #define TWI_REP_START_SENT 0x10 // Repeated Start sent
51 // Master Transmitter Mode
52 #define TWI_MT_SLAW_ACK 0x18 // SLA+W sent and ACK received
53 #define TWI_MT_SLAW_NACK 0x20 // SLA+W sent and NACK received
54 #define TWI_MT_DATA_ACK 0x28 // DATA sent and ACK received
55 #define TWI_MT_DATA_NACK 0x30 // DATA sent and NACK received
56 // Master Receiver Mode
57 #define TWI_MR_SLAR_ACK 0x40 // SLA+R sent, ACK received
58 #define TWI_MR_SLAR_NACK 0x48 // SLA+R sent, NACK received
59 #define TWI_MR_DATA_ACK 0x50 // Data received, ACK returned
60 #define TWI_MR_DATA_NACK 0x58 // Data received, NACK returned
62 // Miscellaneous States
63 #define TWI_LOST_ARBIT 0x38 // Arbitration has been lost
64 #define TWI_NO_RELEVANT_INFO 0xF8 // No relevant information available
65 #define TWI_ILLEGAL_START_STOP 0x00 // Illegal START or STOP condition has been detected
66 #define TWI_SUCCESS 0xFF // Successful transfer, this state is impossible from TWSR as bit2 is 0 and read only
69 #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.
70 #define TWISendStop() (TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN)|(1<<TWIE)) // Send the STOP signal, enable interrupts and TWI, clear TWINT flag.
71 #define TWISendTransmit() (TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWIE)) // Used to resume a transfer, clear TWINT and ensure that TWI and interrupts are enabled.
72 #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.
73 #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.
75 // Function declarations
76 void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking);
78 uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart);
79 uint8_t isTWIReady(void);