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) )
32 // xprintf("i2c ready\n");
37 if(TWIInfo.mode == Initializing){
38 switch(TWIInfo.errorCode){
41 case TWI_NO_RELEVANT_INFO:
45 case TWI_MT_DATA_NACK:
46 // Some kind of I2C error, reset and re-init
47 xprintf("I2C init error: %d\n", TWIInfo.errorCode);
48 TWCR = (1 << TWINT)|(1 << TWSTO);
52 xprintf("Other i2c init error: %d\n", TWIInfo.errorCode);
60 void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking)
63 while (!isTWIReady()) {_delay_us(1);}
64 // Reset the I2C stuff
65 TWCR = (1 << TWINT)|(1 << TWSTO);
67 // Set repeated start mode
68 TWIInfo.repStart = repStart;
69 // Copy transmit info to global variables
70 TWITransmitBuffer = (uint8_t *)TXdata;
74 // If a repeated start has been sent, then devices are already listening for an address
75 // and another start does not need to be sent.
76 if (TWIInfo.mode == RepeatedStartSent)
78 TWIInfo.mode = Initializing;
79 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
80 TWISendTransmit(); // Send the data
82 else // Otherwise, just send the normal start signal to begin transmission.
84 TWIInfo.mode = Initializing;
89 while (!isTWIReady()){_delay_us(1);}
94 // uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart)
96 // if (dataLen <= TXMAXBUFLEN)
98 // // Wait until ready
99 // while (!isTWIReady()) {_delay_us(1);}
100 // // Set repeated start mode
101 // TWIInfo.repStart = repStart;
102 // // Copy data into the transmit buffer
103 // uint8_t *data = (uint8_t *)TXdata;
104 // for (int i = 0; i < dataLen; i++)
106 // TWITransmitBuffer[i] = data[i];
108 // // Copy transmit info to global variables
109 // TXBuffLen = dataLen;
112 // // If a repeated start has been sent, then devices are already listening for an address
113 // // and another start does not need to be sent.
114 // if (TWIInfo.mode == RepeatedStartSent)
116 // TWIInfo.mode = Initializing;
117 // TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
118 // TWISendTransmit(); // Send the data
120 // else // Otherwise, just send the normal start signal to begin transmission.
122 // TWIInfo.mode = Initializing;
129 // return 1; // return an error if data length is longer than buffer
134 uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart)
136 // Check if number of bytes to read can fit in the RXbuffer
137 if (bytesToRead < RXMAXBUFLEN)
139 // Reset buffer index and set RXBuffLen to the number of bytes to read
141 RXBuffLen = bytesToRead;
142 // Create the one value array for the address to be transmitted
144 // Shift the address and AND a 1 into the read write bit (set to write mode)
145 TXdata[0] = (TWIaddr << 1) | 0x01;
146 // Use the TWITransmitData function to initialize the transfer and address the slave
147 TWITransmitData(TXdata, 1, repStart, 0);
160 // ----\/ ---- MASTER TRANSMITTER OR WRITING ADDRESS ----\/ ---- //
161 case TWI_MT_SLAW_ACK: // SLA+W transmitted and ACK received
162 // Set mode to Master Transmitter
163 TWIInfo.mode = MasterTransmitter;
164 case TWI_START_SENT: // Start condition has been transmitted
165 case TWI_MT_DATA_ACK: // Data byte has been transmitted, ACK received
166 if (TXBuffIndex < TXBuffLen) // If there is more data to send
168 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
169 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
170 TWISendTransmit(); // Send the data
172 // This transmission is complete however do not release bus yet
173 else if (TWIInfo.repStart)
175 TWIInfo.errorCode = 0xFF;
178 // All transmissions are complete, exit
181 TWIInfo.mode = Ready;
182 TWIInfo.errorCode = 0xFF;
187 // ----\/ ---- MASTER RECEIVER ----\/ ---- //
189 case TWI_MR_SLAR_ACK: // SLA+R has been transmitted, ACK has been received
190 // Switch to Master Receiver mode
191 TWIInfo.mode = MasterReceiver;
192 // If there is more than one byte to be read, receive data byte and return an ACK
193 if (RXBuffIndex < RXBuffLen-1)
195 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
198 // Otherwise when a data byte (the only data byte) is received, return NACK
201 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
206 case TWI_MR_DATA_ACK: // Data has been received, ACK has been transmitted.
208 /// -- HANDLE DATA BYTE --- ///
209 TWIReceiveBuffer[RXBuffIndex++] = TWDR;
210 // If there is more than one byte to be read, receive data byte and return an ACK
211 if (RXBuffIndex < RXBuffLen-1)
213 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
216 // Otherwise when a data byte (the only data byte) is received, return NACK
219 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
224 case TWI_MR_DATA_NACK: // Data byte has been received, NACK has been transmitted. End of transmission.
226 /// -- HANDLE DATA BYTE --- ///
227 TWIReceiveBuffer[RXBuffIndex++] = TWDR;
228 // This transmission is complete however do not release bus yet
229 if (TWIInfo.repStart)
231 TWIInfo.errorCode = 0xFF;
234 // All transmissions are complete, exit
237 TWIInfo.mode = Ready;
238 TWIInfo.errorCode = 0xFF;
243 // ----\/ ---- MT and MR common ----\/ ---- //
245 case TWI_MR_SLAR_NACK: // SLA+R transmitted, NACK received
246 case TWI_MT_SLAW_NACK: // SLA+W transmitted, NACK received
247 case TWI_MT_DATA_NACK: // Data byte has been transmitted, NACK received
248 case TWI_LOST_ARBIT: // Arbitration has been lost
249 // Return error and send stop and set mode to ready
250 if (TWIInfo.repStart)
252 TWIInfo.errorCode = TWI_STATUS;
255 // All transmissions are complete, exit
258 TWIInfo.mode = Ready;
259 TWIInfo.errorCode = TWI_STATUS;
263 case TWI_REP_START_SENT: // Repeated start has been transmitted
264 // Set the mode but DO NOT clear TWINT as the next data is not yet ready
265 TWIInfo.mode = RepeatedStartSent;
268 // ----\/ ---- SLAVE RECEIVER ----\/ ---- //
270 // TODO IMPLEMENT SLAVE RECEIVER FUNCTIONALITY
272 // ----\/ ---- SLAVE TRANSMITTER ----\/ ---- //
274 // TODO IMPLEMENT SLAVE TRANSMITTER FUNCTIONALITY
276 // ----\/ ---- MISCELLANEOUS STATES ----\/ ---- //
277 case TWI_NO_RELEVANT_INFO: // It is not really possible to get into this ISR on this condition
278 // Rather, it is there to be manually set between operations
280 case TWI_ILLEGAL_START_STOP: // Illegal START/STOP, abort and return error
281 TWIInfo.errorCode = TWI_ILLEGAL_START_STOP;
282 TWIInfo.mode = Ready;