4 * Created: 6/01/2014 10:41:33 PM
5 * Author: Chris Herring
6 * http://www.chrisherring.net/all/tutorial-interrupt-driven-twi-interface-for-avr-part1/
10 #include <avr/interrupt.h>
12 #include "util/delay.h"
17 TWIInfo.errorCode = 0xFF;
19 // Set pre-scalers (no pre-scaling)
22 TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
23 // Enable TWI and interrupt
24 TWCR = (1 << TWIE) | (1 << TWEN);
29 if ( (TWIInfo.mode == Ready) | (TWIInfo.mode == RepeatedStartSent) )
39 uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart)
41 if (dataLen <= TXMAXBUFLEN)
44 while (!isTWIReady()) {_delay_us(1);}
45 // Set repeated start mode
46 TWIInfo.repStart = repStart;
47 // Copy data into the transmit buffer
48 uint8_t *data = (uint8_t *)TXdata;
49 for (int i = 0; i < dataLen; i++)
51 TWITransmitBuffer[i] = data[i];
53 // Copy transmit info to global variables
57 // If a repeated start has been sent, then devices are already listening for an address
58 // and another start does not need to be sent.
59 if (TWIInfo.mode == RepeatedStartSent)
61 TWIInfo.mode = Initializing;
62 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
63 TWISendTransmit(); // Send the data
65 else // Otherwise, just send the normal start signal to begin transmission.
67 TWIInfo.mode = Initializing;
74 return 1; // return an error if data length is longer than buffer
79 uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart)
81 // Check if number of bytes to read can fit in the RXbuffer
82 if (bytesToRead < RXMAXBUFLEN)
84 // Reset buffer index and set RXBuffLen to the number of bytes to read
86 RXBuffLen = bytesToRead;
87 // Create the one value array for the address to be transmitted
89 // Shift the address and AND a 1 into the read write bit (set to write mode)
90 TXdata[0] = (TWIaddr << 1) | 0x01;
91 // Use the TWITransmitData function to initialize the transfer and address the slave
92 TWITransmitData(TXdata, 1, repStart);
105 // ----\/ ---- MASTER TRANSMITTER OR WRITING ADDRESS ----\/ ---- //
106 case TWI_MT_SLAW_ACK: // SLA+W transmitted and ACK received
107 // Set mode to Master Transmitter
108 TWIInfo.mode = MasterTransmitter;
109 case TWI_START_SENT: // Start condition has been transmitted
110 case TWI_MT_DATA_ACK: // Data byte has been transmitted, ACK received
111 if (TXBuffIndex < TXBuffLen) // If there is more data to send
113 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
114 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
115 TWISendTransmit(); // Send the data
117 // This transmission is complete however do not release bus yet
118 else if (TWIInfo.repStart)
120 TWIInfo.errorCode = 0xFF;
123 // All transmissions are complete, exit
126 TWIInfo.mode = Ready;
127 TWIInfo.errorCode = 0xFF;
132 // ----\/ ---- MASTER RECEIVER ----\/ ---- //
134 case TWI_MR_SLAR_ACK: // SLA+R has been transmitted, ACK has been received
135 // Switch to Master Receiver mode
136 TWIInfo.mode = MasterReceiver;
137 // If there is more than one byte to be read, receive data byte and return an ACK
138 if (RXBuffIndex < RXBuffLen-1)
140 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
143 // Otherwise when a data byte (the only data byte) is received, return NACK
146 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
151 case TWI_MR_DATA_ACK: // Data has been received, ACK has been transmitted.
153 /// -- HANDLE DATA BYTE --- ///
154 TWIReceiveBuffer[RXBuffIndex++] = TWDR;
155 // If there is more than one byte to be read, receive data byte and return an ACK
156 if (RXBuffIndex < RXBuffLen-1)
158 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
161 // Otherwise when a data byte (the only data byte) is received, return NACK
164 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
169 case TWI_MR_DATA_NACK: // Data byte has been received, NACK has been transmitted. End of transmission.
171 /// -- HANDLE DATA BYTE --- ///
172 TWIReceiveBuffer[RXBuffIndex++] = TWDR;
173 // This transmission is complete however do not release bus yet
174 if (TWIInfo.repStart)
176 TWIInfo.errorCode = 0xFF;
179 // All transmissions are complete, exit
182 TWIInfo.mode = Ready;
183 TWIInfo.errorCode = 0xFF;
188 // ----\/ ---- MT and MR common ----\/ ---- //
190 case TWI_MR_SLAR_NACK: // SLA+R transmitted, NACK received
191 case TWI_MT_SLAW_NACK: // SLA+W transmitted, NACK received
192 case TWI_MT_DATA_NACK: // Data byte has been transmitted, NACK received
193 case TWI_LOST_ARBIT: // Arbitration has been lost
194 // Return error and send stop and set mode to ready
195 if (TWIInfo.repStart)
197 TWIInfo.errorCode = TWI_STATUS;
200 // All transmissions are complete, exit
203 TWIInfo.mode = Ready;
204 TWIInfo.errorCode = TWI_STATUS;
208 case TWI_REP_START_SENT: // Repeated start has been transmitted
209 // Set the mode but DO NOT clear TWINT as the next data is not yet ready
210 TWIInfo.mode = RepeatedStartSent;
213 // ----\/ ---- SLAVE RECEIVER ----\/ ---- //
215 // TODO IMPLEMENT SLAVE RECEIVER FUNCTIONALITY
217 // ----\/ ---- SLAVE TRANSMITTER ----\/ ---- //
219 // TODO IMPLEMENT SLAVE TRANSMITTER FUNCTIONALITY
221 // ----\/ ---- MISCELLANEOUS STATES ----\/ ---- //
222 case TWI_NO_RELEVANT_INFO: // It is not really possible to get into this ISR on this condition
223 // Rather, it is there to be manually set between operations
225 case TWI_ILLEGAL_START_STOP: // Illegal START/STOP, abort and return error
226 TWIInfo.errorCode = TWI_ILLEGAL_START_STOP;
227 TWIInfo.mode = Ready;