]> git.donarmstrong.com Git - qmk_firmware.git/blob - drivers/avr/TWIlib.c
Adds IS31FL3731 RGB Matrix Implementation (#2910)
[qmk_firmware.git] / drivers / avr / TWIlib.c
1 /*
2  * TWIlib.c
3  *
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/
7  */ 
8
9 #include <avr/io.h>
10 #include <avr/interrupt.h>
11 #include "TWIlib.h"
12 #include "util/delay.h"
13
14 void TWIInit()
15 {
16         TWIInfo.mode = Ready;
17         TWIInfo.errorCode = 0xFF;
18         TWIInfo.repStart = 0;
19         // Set pre-scalers (no pre-scaling)
20         TWSR = 0;
21         // Set bit rate
22         TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
23         // Enable TWI and interrupt
24         TWCR = (1 << TWIE) | (1 << TWEN);
25 }
26
27 uint8_t isTWIReady()
28 {
29         if ( (TWIInfo.mode == Ready) | (TWIInfo.mode == RepeatedStartSent) )
30         {
31                 return 1;
32         }
33         else
34         {
35                 return 0;
36         }
37 }
38
39 uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart)
40 {
41         if (dataLen <= TXMAXBUFLEN)
42         {
43                 // Wait until ready
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++)
50                 {
51                         TWITransmitBuffer[i] = data[i];
52                 }
53                 // Copy transmit info to global variables
54                 TXBuffLen = dataLen;
55                 TXBuffIndex = 0;
56                 
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)
60                 {
61                         TWIInfo.mode = Initializing;
62                         TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
63                         TWISendTransmit(); // Send the data
64                 }
65                 else // Otherwise, just send the normal start signal to begin transmission.
66                 {
67                         TWIInfo.mode = Initializing;
68                         TWISendStart();
69                 }
70                 
71         }
72         else
73         {
74                 return 1; // return an error if data length is longer than buffer
75         }
76         return 0;
77 }
78
79 uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart)
80 {
81         // Check if number of bytes to read can fit in the RXbuffer
82         if (bytesToRead < RXMAXBUFLEN)
83         {
84                 // Reset buffer index and set RXBuffLen to the number of bytes to read
85                 RXBuffIndex = 0;
86                 RXBuffLen = bytesToRead;
87                 // Create the one value array for the address to be transmitted
88                 uint8_t TXdata[1];
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);
93         }
94         else
95         {
96                 return 0;
97         }
98         return 1;
99 }
100
101 ISR (TWI_vect)
102 {
103         switch (TWI_STATUS)
104         {
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
112                         {
113                                 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
114                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
115                                 TWISendTransmit(); // Send the data
116                         }
117                         // This transmission is complete however do not release bus yet
118                         else if (TWIInfo.repStart)
119                         {
120                                 TWIInfo.errorCode = 0xFF;
121                                 TWISendStart();
122                         }
123                         // All transmissions are complete, exit
124                         else
125                         {
126                                 TWIInfo.mode = Ready;
127                                 TWIInfo.errorCode = 0xFF;
128                                 TWISendStop();
129                         }
130                         break;
131                 
132                 // ----\/ ---- MASTER RECEIVER ----\/ ----  //
133                 
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)
139                         {
140                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
141                                 TWISendACK();
142                         }
143                         // Otherwise when a data byte (the only data byte) is received, return NACK
144                         else
145                         {
146                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
147                                 TWISendNACK();
148                         }
149                         break;
150                 
151                 case TWI_MR_DATA_ACK: // Data has been received, ACK has been transmitted.
152                 
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)
157                         {
158                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
159                                 TWISendACK();
160                         }
161                         // Otherwise when a data byte (the only data byte) is received, return NACK
162                         else
163                         {
164                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
165                                 TWISendNACK();
166                         }
167                         break;
168                 
169                 case TWI_MR_DATA_NACK: // Data byte has been received, NACK has been transmitted. End of transmission.
170                 
171                         /// -- HANDLE DATA BYTE --- ///
172                         TWIReceiveBuffer[RXBuffIndex++] = TWDR; 
173                         // This transmission is complete however do not release bus yet
174                         if (TWIInfo.repStart)
175                         {
176                                 TWIInfo.errorCode = 0xFF;
177                                 TWISendStart();
178                         }
179                         // All transmissions are complete, exit
180                         else
181                         {
182                                 TWIInfo.mode = Ready;
183                                 TWIInfo.errorCode = 0xFF;
184                                 TWISendStop();
185                         }
186                         break;
187                 
188                 // ----\/ ---- MT and MR common ----\/ ---- //
189                 
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)
196                         {                               
197                                 TWIInfo.errorCode = TWI_STATUS;
198                                 TWISendStart();
199                         }
200                         // All transmissions are complete, exit
201                         else
202                         {
203                                 TWIInfo.mode = Ready;
204                                 TWIInfo.errorCode = TWI_STATUS;
205                                 TWISendStop();
206                         }
207                         break;
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;
211                         break;
212                 
213                 // ----\/ ---- SLAVE RECEIVER ----\/ ----  //
214                 
215                 // TODO  IMPLEMENT SLAVE RECEIVER FUNCTIONALITY
216                 
217                 // ----\/ ---- SLAVE TRANSMITTER ----\/ ----  //
218                 
219                 // TODO  IMPLEMENT SLAVE TRANSMITTER FUNCTIONALITY
220                 
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
224                         break;
225                 case TWI_ILLEGAL_START_STOP: // Illegal START/STOP, abort and return error
226                         TWIInfo.errorCode = TWI_ILLEGAL_START_STOP;
227                         TWIInfo.mode = Ready;
228                         TWISendStop();
229                         break;
230         }
231         
232 }