4 * Created: 6/01/2014 10:41:33 PM
5 * Author: Chris Herring
9 #include <avr/interrupt.h>
11 #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) )
35 if(TWIInfo.mode == Initializing){
36 switch(TWIInfo.errorCode){
38 case TWI_NO_RELEVANT_INFO:
41 case TWI_MT_DATA_NACK:
42 // Some kind of I2C error, reset and re-init
43 xprintf("I2C init error: %d\n", TWIInfo.errorCode);
44 TWCR = (1 << TWINT)|(1 << TWSTO);
48 xprintf("Other i2c init error: %d\n", TWIInfo.errorCode);
56 void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking)
59 while (!isTWIReady()) {_delay_us(1);}
60 // Reset the I2C stuff
61 TWCR = (1 << TWINT)|(1 << TWSTO);
63 // Set repeated start mode
64 TWIInfo.repStart = repStart;
65 // Copy transmit info to global variables
66 TWITransmitBuffer = (uint8_t *)TXdata;
70 // If a repeated start has been sent, then devices are already listening for an address
71 // and another start does not need to be sent.
72 if (TWIInfo.mode == RepeatedStartSent)
74 TWIInfo.mode = Initializing;
75 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
76 TWISendTransmit(); // Send the data
78 else // Otherwise, just send the normal start signal to begin transmission.
80 TWIInfo.mode = Initializing;
85 while (!isTWIReady()){_delay_us(1);}
90 // uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart)
92 // if (dataLen <= TXMAXBUFLEN)
94 // // Wait until ready
95 // while (!isTWIReady()) {_delay_us(1);}
96 // // Set repeated start mode
97 // TWIInfo.repStart = repStart;
98 // // Copy data into the transmit buffer
99 // uint8_t *data = (uint8_t *)TXdata;
100 // for (int i = 0; i < dataLen; i++)
102 // TWITransmitBuffer[i] = data[i];
104 // // Copy transmit info to global variables
105 // TXBuffLen = dataLen;
108 // // If a repeated start has been sent, then devices are already listening for an address
109 // // and another start does not need to be sent.
110 // if (TWIInfo.mode == RepeatedStartSent)
112 // TWIInfo.mode = Initializing;
113 // TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
114 // TWISendTransmit(); // Send the data
116 // else // Otherwise, just send the normal start signal to begin transmission.
118 // TWIInfo.mode = Initializing;
125 // return 1; // return an error if data length is longer than buffer
130 uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart)
132 // Check if number of bytes to read can fit in the RXbuffer
133 if (bytesToRead < RXMAXBUFLEN)
135 // Reset buffer index and set RXBuffLen to the number of bytes to read
137 RXBuffLen = bytesToRead;
138 // Create the one value array for the address to be transmitted
140 // Shift the address and AND a 1 into the read write bit (set to write mode)
141 TXdata[0] = (TWIaddr << 1) | 0x01;
142 // Use the TWITransmitData function to initialize the transfer and address the slave
143 TWITransmitData(TXdata, 1, repStart, 0);
156 // ----\/ ---- MASTER TRANSMITTER OR WRITING ADDRESS ----\/ ---- //
157 case TWI_MT_SLAW_ACK: // SLA+W transmitted and ACK received
158 // Set mode to Master Transmitter
159 TWIInfo.mode = MasterTransmitter;
160 case TWI_START_SENT: // Start condition has been transmitted
161 case TWI_MT_DATA_ACK: // Data byte has been transmitted, ACK received
162 if (TXBuffIndex < TXBuffLen) // If there is more data to send
164 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
165 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
166 TWISendTransmit(); // Send the data
168 // This transmission is complete however do not release bus yet
169 else if (TWIInfo.repStart)
171 TWIInfo.errorCode = 0xFF;
174 // All transmissions are complete, exit
177 TWIInfo.mode = Ready;
178 TWIInfo.errorCode = 0xFF;
183 // ----\/ ---- MASTER RECEIVER ----\/ ---- //
185 case TWI_MR_SLAR_ACK: // SLA+R has been transmitted, ACK has been received
186 // Switch to Master Receiver mode
187 TWIInfo.mode = MasterReceiver;
188 // If there is more than one byte to be read, receive data byte and return an ACK
189 if (RXBuffIndex < RXBuffLen-1)
191 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
194 // Otherwise when a data byte (the only data byte) is received, return NACK
197 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
202 case TWI_MR_DATA_ACK: // Data has been received, ACK has been transmitted.
204 /// -- HANDLE DATA BYTE --- ///
205 TWIReceiveBuffer[RXBuffIndex++] = TWDR;
206 // If there is more than one byte to be read, receive data byte and return an ACK
207 if (RXBuffIndex < RXBuffLen-1)
209 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
212 // Otherwise when a data byte (the only data byte) is received, return NACK
215 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
220 case TWI_MR_DATA_NACK: // Data byte has been received, NACK has been transmitted. End of transmission.
222 /// -- HANDLE DATA BYTE --- ///
223 TWIReceiveBuffer[RXBuffIndex++] = TWDR;
224 // This transmission is complete however do not release bus yet
225 if (TWIInfo.repStart)
227 TWIInfo.errorCode = 0xFF;
230 // All transmissions are complete, exit
233 TWIInfo.mode = Ready;
234 TWIInfo.errorCode = 0xFF;
239 // ----\/ ---- MT and MR common ----\/ ---- //
241 case TWI_MR_SLAR_NACK: // SLA+R transmitted, NACK received
242 case TWI_MT_SLAW_NACK: // SLA+W transmitted, NACK received
243 case TWI_MT_DATA_NACK: // Data byte has been transmitted, NACK received
244 case TWI_LOST_ARBIT: // Arbitration has been lost
245 // Return error and send stop and set mode to ready
246 if (TWIInfo.repStart)
248 TWIInfo.errorCode = TWI_STATUS;
251 // All transmissions are complete, exit
254 TWIInfo.mode = Ready;
255 TWIInfo.errorCode = TWI_STATUS;
259 case TWI_REP_START_SENT: // Repeated start has been transmitted
260 // Set the mode but DO NOT clear TWINT as the next data is not yet ready
261 TWIInfo.mode = RepeatedStartSent;
264 // ----\/ ---- SLAVE RECEIVER ----\/ ---- //
266 // TODO IMPLEMENT SLAVE RECEIVER FUNCTIONALITY
268 // ----\/ ---- SLAVE TRANSMITTER ----\/ ---- //
270 // TODO IMPLEMENT SLAVE TRANSMITTER FUNCTIONALITY
272 // ----\/ ---- MISCELLANEOUS STATES ----\/ ---- //
273 case TWI_NO_RELEVANT_INFO: // It is not really possible to get into this ISR on this condition
274 // Rather, it is there to be manually set between operations
276 case TWI_ILLEGAL_START_STOP: // Illegal START/STOP, abort and return error
277 TWIInfo.errorCode = TWI_ILLEGAL_START_STOP;
278 TWIInfo.mode = Ready;