]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/meira/TWIlib.c
Add user-overridable callback for cancelling UCIS input (#5564)
[qmk_firmware.git] / keyboards / meira / TWIlib.c
1 /*
2  * TWIlib.c
3  *
4  *  Created: 6/01/2014 10:41:33 PM
5  *  Author: Chris Herring
6  */
7
8 #include <avr/io.h>
9 #include <avr/interrupt.h>
10 #include "TWIlib.h"
11 #include "util/delay.h"
12 #include "print.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
32 //        xprintf("i2c ready\n");
33                 return 1;
34         }
35         else
36         {
37                 if(TWIInfo.mode == Initializing){
38                         switch(TWIInfo.errorCode){
39                         case TWI_SUCCESS:
40                             break;
41                         case TWI_NO_RELEVANT_INFO:
42
43                                 break;
44                                 case TWI_LOST_ARBIT:
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);
49                                 TWIInit();
50                                 break;
51                         default:
52                                 xprintf("Other i2c init error: %d\n", TWIInfo.errorCode);
53                         }
54                 }
55                 return 0;
56         }
57 }
58
59
60 void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking)
61 {
62         // Wait until ready
63         while (!isTWIReady()) {_delay_us(1);}
64         // Reset the I2C stuff
65         TWCR = (1 << TWINT)|(1 << TWSTO);
66         TWIInit();
67         // Set repeated start mode
68         TWIInfo.repStart = repStart;
69         // Copy transmit info to global variables
70         TWITransmitBuffer = (uint8_t *)TXdata;
71         TXBuffLen = dataLen;
72         TXBuffIndex = 0;
73
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)
77         {
78                 TWIInfo.mode = Initializing;
79                 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
80                 TWISendTransmit(); // Send the data
81         }
82         else // Otherwise, just send the normal start signal to begin transmission.
83         {
84                 TWIInfo.mode = Initializing;
85                 TWISendStart();
86         }
87         if(blocking){
88                 // Wait until ready
89                 while (!isTWIReady()){_delay_us(1);}
90         }
91 }
92
93
94 // uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart)
95 // {
96 //      if (dataLen <= TXMAXBUFLEN)
97 //      {
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++)
105 //              {
106 //                      TWITransmitBuffer[i] = data[i];
107 //              }
108 //              // Copy transmit info to global variables
109 //              TXBuffLen = dataLen;
110 //              TXBuffIndex = 0;
111
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)
115 //              {
116 //                      TWIInfo.mode = Initializing;
117 //                      TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
118 //                      TWISendTransmit(); // Send the data
119 //              }
120 //              else // Otherwise, just send the normal start signal to begin transmission.
121 //              {
122 //                      TWIInfo.mode = Initializing;
123 //                      TWISendStart();
124 //              }
125
126 //      }
127 //      else
128 //      {
129 //              return 1; // return an error if data length is longer than buffer
130 //      }
131 //      return 0;
132 // }
133
134 uint8_t TWIReadData(uint8_t TWIaddr, uint8_t bytesToRead, uint8_t repStart)
135 {
136         // Check if number of bytes to read can fit in the RXbuffer
137         if (bytesToRead < RXMAXBUFLEN)
138         {
139                 // Reset buffer index and set RXBuffLen to the number of bytes to read
140                 RXBuffIndex = 0;
141                 RXBuffLen = bytesToRead;
142                 // Create the one value array for the address to be transmitted
143                 uint8_t TXdata[1];
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);
148         }
149         else
150         {
151                 return 0;
152         }
153         return 1;
154 }
155
156 ISR (TWI_vect)
157 {
158         switch (TWI_STATUS)
159         {
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
167                         {
168                                 TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer
169                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
170                                 TWISendTransmit(); // Send the data
171                         }
172                         // This transmission is complete however do not release bus yet
173                         else if (TWIInfo.repStart)
174                         {
175                                 TWIInfo.errorCode = 0xFF;
176                                 TWISendStart();
177                         }
178                         // All transmissions are complete, exit
179                         else
180                         {
181                                 TWIInfo.mode = Ready;
182                                 TWIInfo.errorCode = 0xFF;
183                                 TWISendStop();
184                         }
185                         break;
186
187                 // ----\/ ---- MASTER RECEIVER ----\/ ----  //
188
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)
194                         {
195                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
196                                 TWISendACK();
197                         }
198                         // Otherwise when a data byte (the only data byte) is received, return NACK
199                         else
200                         {
201                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
202                                 TWISendNACK();
203                         }
204                         break;
205
206                 case TWI_MR_DATA_ACK: // Data has been received, ACK has been transmitted.
207
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)
212                         {
213                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
214                                 TWISendACK();
215                         }
216                         // Otherwise when a data byte (the only data byte) is received, return NACK
217                         else
218                         {
219                                 TWIInfo.errorCode = TWI_NO_RELEVANT_INFO;
220                                 TWISendNACK();
221                         }
222                         break;
223
224                 case TWI_MR_DATA_NACK: // Data byte has been received, NACK has been transmitted. End of transmission.
225
226                         /// -- HANDLE DATA BYTE --- ///
227                         TWIReceiveBuffer[RXBuffIndex++] = TWDR;
228                         // This transmission is complete however do not release bus yet
229                         if (TWIInfo.repStart)
230                         {
231                                 TWIInfo.errorCode = 0xFF;
232                                 TWISendStart();
233                         }
234                         // All transmissions are complete, exit
235                         else
236                         {
237                                 TWIInfo.mode = Ready;
238                                 TWIInfo.errorCode = 0xFF;
239                                 TWISendStop();
240                         }
241                         break;
242
243                 // ----\/ ---- MT and MR common ----\/ ---- //
244
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)
251                         {
252                                 TWIInfo.errorCode = TWI_STATUS;
253                                 TWISendStart();
254                         }
255                         // All transmissions are complete, exit
256                         else
257                         {
258                                 TWIInfo.mode = Ready;
259                                 TWIInfo.errorCode = TWI_STATUS;
260                                 TWISendStop();
261                         }
262                         break;
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;
266                         break;
267
268                 // ----\/ ---- SLAVE RECEIVER ----\/ ----  //
269
270                 // TODO  IMPLEMENT SLAVE RECEIVER FUNCTIONALITY
271
272                 // ----\/ ---- SLAVE TRANSMITTER ----\/ ----  //
273
274                 // TODO  IMPLEMENT SLAVE TRANSMITTER FUNCTIONALITY
275
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
279                         break;
280                 case TWI_ILLEGAL_START_STOP: // Illegal START/STOP, abort and return error
281                         TWIInfo.errorCode = TWI_ILLEGAL_START_STOP;
282                         TWIInfo.mode = Ready;
283                         TWISendStop();
284                         break;
285         }
286
287 }