]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/ISSILed/led_scan.c
Code cleanup
[kiibohd-controller.git] / Scan / ISSILed / led_scan.c
1 /* Copyright (C) 2014-2015 by Jacob Alexander
2  *
3  * This file is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This file is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this file.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // ----- Includes -----
18
19 // Compiler Includes
20 #include <Lib/ScanLib.h>
21
22 // Project Includes
23 #include <cli.h>
24 #include <led.h>
25 #include <print.h>
26 #include <led_conf.h> // Located with scan_loop.c
27
28 // Local Includes
29 #include "led_scan.h"
30
31
32
33 // ----- Defines -----
34
35 #define I2C_TxBufferLength 300
36 #define I2C_RxBufferLength 8
37
38 #define LED_BufferLength 144
39
40
41 // ----- Structs -----
42
43 typedef struct I2C_Buffer {
44         uint16_t  head;
45         uint16_t  tail;
46         uint8_t   sequencePos;
47         uint16_t  size;
48         uint8_t  *buffer;
49 } I2C_Buffer;
50
51 typedef struct LED_Buffer {
52         uint8_t buffer[LED_BufferLength];
53 } LED_Buffer;
54
55
56
57 // ----- Function Declarations -----
58
59 // CLI Functions
60 void cliFunc_i2cRecv( char* args );
61 void cliFunc_i2cSend( char* args );
62 void cliFunc_ledPage( char* args );
63 void cliFunc_ledStart( char* args );
64 void cliFunc_ledTest( char* args );
65 void cliFunc_ledZero( char* args );
66
67 uint8_t I2C_TxBufferPop();
68 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer );
69 uint16_t I2C_BufferLen( I2C_Buffer *buffer );
70 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen );
71
72
73
74 // ----- Variables -----
75
76 // Scan Module command dictionary
77 CLIDict_Entry( i2cRecv,     "Send I2C sequence of bytes and expect a reply of 1 byte on the last sequence." NL "\t\tUse |'s to split sequences with a stop." );
78 CLIDict_Entry( i2cSend,     "Send I2C sequence of bytes. Use |'s to split sequences with a stop." );
79 CLIDict_Entry( ledPage,     "Read the given register page." );
80 CLIDict_Entry( ledStart,    "Disable software shutdown." );
81 CLIDict_Entry( ledTest,     "Test out the led pages." );
82 CLIDict_Entry( ledZero,     "Zero out LED register pages (non-configuration)." );
83
84 CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = {
85         CLIDict_Item( i2cRecv ),
86         CLIDict_Item( i2cSend ),
87         CLIDict_Item( ledPage ),
88         CLIDict_Item( ledStart ),
89         CLIDict_Item( ledTest ),
90         CLIDict_Item( ledZero ),
91         { 0, 0, 0 } // Null entry for dictionary end
92 };
93
94
95
96 // Before sending the sequence, I2C_TxBuffer_CurLen is assigned and as each byte is sent, it is decremented
97 // Once I2C_TxBuffer_CurLen reaches zero, a STOP on the I2C bus is sent
98 volatile uint8_t I2C_TxBufferPtr[ I2C_TxBufferLength ];
99 volatile uint8_t I2C_RxBufferPtr[ I2C_TxBufferLength ];
100
101 volatile I2C_Buffer I2C_TxBuffer = { 0, 0, 0, I2C_TxBufferLength, (uint8_t*)I2C_TxBufferPtr };
102 volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_RxBufferPtr };
103
104 LED_Buffer LED_pageBuffer;
105
106 /*
107 // A bit mask determining which LEDs are enabled in the ISSI chip
108 // All channel mask example
109 // 0x00 -> 0x11
110 const uint8_t LED_ledEnableMask[] = {
111 0xE8, // I2C address
112 0x00, // Starting register address
113 0xFF, 0xFF, // C1-1 -> C1-16
114 0xFF, 0xFF, // C2-1 -> C2-16
115 0xFF, 0xFF, // C3-1 -> C3-16
116 0xFF, 0xFF, // C4-1 -> C4-16
117 0xFF, 0xFF, // C5-1 -> C5-16
118 0xFF, 0xFF, // C6-1 -> C6-16
119 0xFF, 0xFF, // C7-1 -> C7-16
120 0xFF, 0xFF, // C8-1 -> C8-16
121 0xFF, 0xFF, // C9-1 -> C9-16
122 };
123 */
124
125 /*
126 // A bit mask determining which LEDs are enabled in the ISSI chip
127 // Infinity ErgoDox full mask
128 // 0x00 -> 0x11
129 const uint8_t LED_ledEnableMask[] = {
130 0xE8, // I2C address
131 0x00, // Starting register address
132 0xFC, 0xFC, // C1-1 -> C1-16
133 0xFB, 0xFB, // C2-1 -> C2-16
134 0xFF, 0xFF, // C3-1 -> C3-16
135 0xFE, 0xFE, // C4-1 -> C4-16
136 0x7F, 0x7F, // C5-1 -> C5-16
137 0xFF, 0xFF, // C6-1 -> C6-16
138 0xCF, 0xCF, // C7-1 -> C7-16
139 0xC7, 0xC7, // C8-1 -> C8-16
140 0x43, 0x43, // C9-1 -> C9-16
141 };
142 */
143
144 /*
145 const uint8_t LED_ledEnableMask[] = {
146 0xE8, // I2C address
147 0x00, // Starting register address
148 0x00, 0x00, // C1-1 -> C1-16
149 //0xEC, 0xEC, // C1-1 -> C1-16
150 0x00, 0x00, // C2-1 -> C2-16
151 0x00, 0x00, // C3-1 -> C3-16
152 0x00, 0x00, // C4-1 -> C4-16
153 0x00, 0x00, // C5-1 -> C5-16
154 0x00, 0x00, // C6-1 -> C6-16
155 0x08, 0x08, // C7-1 -> C7-16
156 0x00, 0x00, // C8-1 -> C8-16
157 0x00, 0x00, // C9-1 -> C9-16
158 };
159 */
160
161
162 /*
163 // XXX Pre-fill example of buffers
164 const uint8_t examplePage[] = {
165 0xE8, // I2C address
166 0x24, // Starting register address
167 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C1-1 -> C1-16
168 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C2-1 -> C2-16
169 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C3-1 -> C3-16
170 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C4-1 -> C4-16
171 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C5-1 -> C5-16
172 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C6-1 -> C6-16
173 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C7-1 -> C7-16
174 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C8-1 -> C8-16
175 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C9-1 -> C9-16
176 };
177 */
178
179 // XXX Pre-fill example of buffers
180 const uint8_t examplePage[] = {
181 0xE8, // I2C address
182 0x24, // Starting register address
183 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, // C1-1 -> C1-16
184 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // C2-1 -> C2-16
185 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, // C3-1 -> C3-16
186 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, // C4-1 -> C4-16
187 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, // C5-1 -> C5-16
188 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, // C6-1 -> C6-16
189 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, // C7-1 -> C7-16
190 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, // C8-1 -> C8-16
191 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, // C9-1 -> C9-16
192 };
193
194
195
196 // ----- Interrupt Functions -----
197
198 void i2c0_isr()
199 {
200         cli(); // Disable Interrupts
201
202         uint8_t status = I2C0_S; // Read I2C Bus status
203
204         // Master Mode Transmit
205         if ( I2C0_C1 & I2C_C1_TX )
206         {
207                 // Check current use of the I2C bus
208                 // Currently sending data
209                 if ( I2C_TxBuffer.sequencePos > 0 )
210                 {
211                         // Make sure slave sent an ACK
212                         if ( status & I2C_S_RXAK )
213                         {
214                                 // NACK Detected, disable interrupt
215                                 erro_print("I2C NAK detected...");
216                                 I2C0_C1 = I2C_C1_IICEN;
217
218                                 // Abort Tx Buffer
219                                 I2C_TxBuffer.head = 0;
220                                 I2C_TxBuffer.tail = 0;
221                                 I2C_TxBuffer.sequencePos = 0;
222                         }
223                         else
224                         {
225                                 // Transmit byte
226                                 I2C0_D = I2C_TxBufferPop();
227                         }
228                 }
229                 // Receiving data
230                 else if ( I2C_RxBuffer.sequencePos > 0 )
231                 {
232                         // Master Receive, addr sent
233                         if ( status & I2C_S_ARBL )
234                         {
235                                 // Arbitration Lost
236                                 erro_print("Arbitration lost...");
237                                 // TODO Abort Rx
238
239                                 I2C0_C1 = I2C_C1_IICEN;
240                                 I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt
241                         }
242                         if ( status & I2C_S_RXAK )
243                         {
244                                 // Slave Address NACK Detected, disable interrupt
245                                 erro_print("Slave Address I2C NAK detected...");
246                                 // TODO Abort Rx
247
248                                 I2C0_C1 = I2C_C1_IICEN;
249                         }
250                         else
251                         {
252                                 dbug_print("Attempting to read byte");
253                                 I2C0_C1 = I2C_RxBuffer.sequencePos == 1
254                                         ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read
255                                         : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read
256                         }
257                 }
258                 else
259                 {
260                         /*
261                         dbug_msg("STOP - ");
262                         printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) );
263                         print(NL);
264                         */
265
266                         // Delay around STOP to make sure it actually happens...
267                         delayMicroseconds( 1 );
268                         I2C0_C1 = I2C_C1_IICEN; // Send STOP
269                         delayMicroseconds( 7 );
270
271                         // If there is another sequence, start sending
272                         if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size )
273                         {
274                                 // Clear status flags
275                                 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
276
277                                 // Wait...till the master dies
278                                 while ( I2C0_S & I2C_S_BUSY );
279
280                                 // Enable I2C interrupt
281                                 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
282
283                                 // Transmit byte
284                                 I2C0_D = I2C_TxBufferPop();
285                         }
286                 }
287         }
288         // Master Mode Receive
289         else
290         {
291                 // XXX Do we need to handle 2nd last byte?
292                 //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv
293
294                 // Last byte
295                 if ( I2C_TxBuffer.sequencePos <= 1 )
296                 {
297                         // Change to Tx mode
298                         I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
299
300                         // Grab last byte
301                         I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
302
303                         delayMicroseconds( 1 ); // Should be enough time before issuing the stop
304                         I2C0_C1 = I2C_C1_IICEN; // Send STOP
305                 }
306                 else
307                 {
308                         // Retrieve data
309                         I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
310                 }
311         }
312
313         I2C0_S = I2C_S_IICIF; // Clear interrupt
314
315         sei(); // Re-enable Interrupts
316 }
317
318
319
320 // ----- Functions -----
321
322 inline void I2C_setup()
323 {
324         // Enable I2C internal clock
325         SIM_SCGC4 |= SIM_SCGC4_I2C0; // Bus 0
326
327         // External pull-up resistor
328         PORTB_PCR0 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
329         PORTB_PCR1 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
330
331         // SCL Frequency Divider
332         // 400kHz -> 120 (0x85) @ 48 MHz F_BUS
333         I2C0_F = 0x85;
334         I2C0_FLT = 4;
335         I2C0_C1 = I2C_C1_IICEN;
336         I2C0_C2 = I2C_C2_HDRS; // High drive select
337
338         // Enable I2C Interrupt
339         NVIC_ENABLE_IRQ( IRQ_I2C0 );
340 }
341
342 void LED_zeroPages( uint8_t startPage, uint8_t numPages, uint8_t startReg, uint8_t endReg )
343 {
344         // Page Setup
345         uint8_t pageSetup[] = { 0xE8, 0xFD, 0x00 };
346
347         // Max length of a page + chip id + reg start
348         uint8_t fullPage[ 0xB4 + 2 ] = { 0 }; // Max size of page
349         fullPage[0] = 0xE8;     // Set chip id
350         fullPage[1] = startReg; // Set start reg
351
352         // Iterate through given pages, zero'ing out the given register regions
353         for ( uint8_t page = startPage; page < startPage + numPages; page++ )
354         {
355                 // Set page
356                 pageSetup[2] = page;
357
358                 // Setup page
359                 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
360                         delay(1);
361
362                 // Zero out page
363                 while ( I2C_Send( fullPage, endReg - startReg + 2, 0 ) == 0 )
364                         delay(1);
365         }
366 }
367
368 void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page )
369 {
370         // Page Setup
371         uint8_t pageSetup[] = { 0xE8, 0xFD, page };
372
373         // Setup page
374         while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
375                 delay(1);
376
377         // Write page to I2C Tx Buffer
378         while ( I2C_Send( buffer, len, 0 ) == 0 )
379                 delay(1);
380
381 }
382
383 void LED_readPage( uint8_t len, uint8_t page )
384 {
385         // Page Setup
386         uint8_t pageSetup[] = { 0xE8, 0xFD, page };
387
388         // Setup page
389         while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
390                 delay(1);
391
392         // Register Setup
393         uint8_t regSetup[] = { 0xE8, 0x00 };
394
395         // Setup starting register
396         while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 )
397                 delay(1);
398
399         // Register Read Command
400         uint8_t regReadCmd[] = { 0xE9 };
401
402         // Read each register in the page
403         for ( uint8_t reg = 0; reg < len; reg++ )
404         {
405                 // Request register data
406                 while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 0 ) == 0 )
407                         delay(1);
408         }
409 }
410
411 void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page )
412 {
413         // Page Setup
414         uint8_t pageSetup[] = { 0xE8, 0xFD, page };
415
416         // Reg Write Setup
417         uint8_t writeData[] = { 0xE8, reg, val };
418
419         // Setup page
420         while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
421                 delay(1);
422
423         while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 )
424                 delay(1);
425 }
426
427 // Setup
428 inline void LED_setup()
429 {
430         // Register Scan CLI dictionary
431         CLI_registerDictionary( ledCLIDict, ledCLIDictName );
432
433         // Initialize I2C
434         I2C_setup();
435
436         // Zero out Frame Registers
437         // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things)
438         LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
439
440         // Disable Hardware shutdown of ISSI chip (pull high)
441         GPIOB_PDDR |= (1<<16);
442         PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
443         GPIOB_PSOR |= (1<<16);
444
445         // Clear LED Pages
446         LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
447
448         // Enable LEDs based upon mask
449         LED_sendPage( (uint8_t*)LED_ledEnableMask, sizeof( LED_ledEnableMask ), 0 );
450
451         // Disable Software shutdown of ISSI chip
452         LED_writeReg( 0x0A, 0x01, 0x0B );
453 }
454
455
456 inline uint8_t I2C_BufferCopy( uint8_t *data, uint8_t sendLen, uint8_t recvLen, I2C_Buffer *buffer )
457 {
458         uint8_t reTurn = 0;
459
460         // If sendLen is greater than buffer fail right away
461         if ( sendLen > buffer->size )
462                 return 0;
463
464         // Calculate new tail to determine if buffer has enough space
465         // The first element specifies the expected number of bytes from the slave (+1)
466         // The second element in the new buffer is the length of the buffer sequence (+1)
467         uint16_t newTail = buffer->tail + sendLen + 2;
468         if ( newTail >= buffer->size )
469                 newTail -= buffer->size;
470
471         if ( I2C_BufferLen( buffer ) < sendLen + 2 )
472                 return 0;
473
474 /*
475         print("|");
476         printHex( sendLen + 2 );
477         print("|");
478         printHex( *tail );
479         print("@");
480         printHex( newTail );
481         print("@");
482 */
483
484         // If buffer is clean, return 1, otherwise 2
485         reTurn = buffer->head == buffer->tail ? 1 : 2;
486
487         // Add to buffer, already know there is enough room (simplifies adding logic)
488         uint8_t bufferHeaderPos = 0;
489         for ( uint16_t c = 0; c < sendLen; c++ )
490         {
491                 // Add data to buffer
492                 switch ( bufferHeaderPos )
493                 {
494                 case 0:
495                         buffer->buffer[ buffer->tail ] = recvLen;
496                         bufferHeaderPos++;
497                         c--;
498                         break;
499
500                 case 1:
501                         buffer->buffer[ buffer->tail ] = sendLen;
502                         bufferHeaderPos++;
503                         c--;
504                         break;
505
506                 default:
507                         buffer->buffer[ buffer->tail ] = data[ c ];
508                         break;
509                 }
510
511                 // Check for wrap-around case
512                 if ( buffer->tail + 1 >= buffer->size )
513                 {
514                         buffer->tail = 0;
515                 }
516                 // Normal case
517                 else
518                 {
519                         buffer->tail++;
520                 }
521         }
522
523         return reTurn;
524 }
525
526
527 inline uint16_t I2C_BufferLen( I2C_Buffer *buffer )
528 {
529         // Tail >= Head
530         if ( buffer->tail >= buffer->head )
531                 return buffer->head + buffer->size - buffer->tail;
532
533         // Head > Tail
534         return buffer->head - buffer->tail;
535 }
536
537
538 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer )
539 {
540         dbug_msg("DATA: ");
541         printHex( byte );
542
543         // Make sure buffer isn't full
544         if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) )
545         {
546                 warn_msg("I2C_BufferPush failed, buffer full: ");
547                 printHex( byte );
548                 print( NL );
549                 return;
550         }
551
552         // Check for wrap-around case
553         if ( buffer->tail + 1 >= buffer->size )
554         {
555                 buffer->tail = 0;
556         }
557         // Normal case
558         else
559         {
560                 buffer->tail++;
561         }
562
563         // Add byte to buffer
564         buffer->buffer[ buffer->tail ] = byte;
565 }
566
567
568 uint8_t I2C_TxBufferPop()
569 {
570         // Return 0xFF if no buffer left (do not rely on this)
571         if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) >= I2C_TxBuffer.size )
572         {
573                 erro_msg("No buffer to pop an entry from... ");
574                 printHex( I2C_TxBuffer.head );
575                 print(" ");
576                 printHex( I2C_TxBuffer.tail );
577                 print(" ");
578                 printHex( I2C_TxBuffer.sequencePos );
579                 print(NL);
580                 return 0xFF;
581         }
582
583         // If there is currently no sequence being sent, the first entry in the RingBuffer is the length
584         if ( I2C_TxBuffer.sequencePos == 0 )
585         {
586                 I2C_TxBuffer.sequencePos = 0xFF; // So this doesn't become an infinite loop
587                 I2C_RxBuffer.sequencePos = I2C_TxBufferPop();
588                 I2C_TxBuffer.sequencePos = I2C_TxBufferPop();
589         }
590
591         uint8_t data = I2C_TxBuffer.buffer[ I2C_TxBuffer.head ];
592
593         // Prune head
594         I2C_TxBuffer.head++;
595
596         // Wrap-around case
597         if ( I2C_TxBuffer.head >= I2C_TxBuffer.size )
598                 I2C_TxBuffer.head = 0;
599
600         // Decrement buffer sequence (until next stop will be sent)
601         I2C_TxBuffer.sequencePos--;
602
603         /*
604         dbug_msg("Popping: ");
605         printHex( data );
606         print(" ");
607         printHex( I2C_TxBuffer.head );
608         print(" ");
609         printHex( I2C_TxBuffer.tail );
610         print(" ");
611         printHex( I2C_TxBuffer.sequencePos );
612         print(NL);
613         */
614         return data;
615 }
616
617
618 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen )
619 {
620         // Check head and tail pointers
621         // If full, return 0
622         // If empty, start up I2C Master Tx
623         // If buffer is non-empty and non-full, just append to the buffer
624         switch ( I2C_BufferCopy( data, sendLen, recvLen, (I2C_Buffer*)&I2C_TxBuffer ) )
625         {
626         // Not enough buffer space...
627         case 0:
628                 /*
629                 erro_msg("Not enough Tx buffer space... ");
630                 printHex( I2C_TxBuffer.head );
631                 print(":");
632                 printHex( I2C_TxBuffer.tail );
633                 print("+");
634                 printHex( sendLen );
635                 print("|");
636                 printHex( I2C_TxBuffer.size );
637                 print( NL );
638                 */
639                 return 0;
640
641         // Empty buffer, initialize I2C
642         case 1:
643                 // Clear status flags
644                 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
645
646                 // Check to see if we already have control of the bus
647                 if ( I2C0_C1 & I2C_C1_MST )
648                 {
649                         // Already the master (ah yeah), send a repeated start
650                         I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
651                 }
652                 // Otherwise, seize control
653                 else
654                 {
655                         // Wait...till the master dies
656                         while ( I2C0_S & I2C_S_BUSY );
657
658                         // Now we're the master (ah yisss), get ready to send stuffs
659                         I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
660                 }
661
662                 // Enable I2C interrupt
663                 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
664
665                 // Depending on what type of transfer, the first byte is configured for R or W
666                 I2C0_D = I2C_TxBufferPop();
667
668                 return 1;
669         }
670
671         // Dirty buffer, I2C already initialized
672         return 2;
673 }
674
675
676
677 // LED State processing loop
678 inline uint8_t LED_scan()
679 {
680
681         // I2C Busy
682         // S & I2C_S_BUSY
683         //I2C_S_BUSY
684
685         return 0;
686 }
687
688
689
690 // ----- CLI Command Functions -----
691
692 void cliFunc_i2cSend( char* args )
693 {
694         char* curArgs;
695         char* arg1Ptr;
696         char* arg2Ptr = args;
697
698         // Buffer used after interpretting the args, will be sent to I2C functions
699         // NOTE: Limited to 8 bytes currently (can be increased if necessary
700         #define i2cSend_BuffLenMax 8
701         uint8_t buffer[ i2cSend_BuffLenMax ];
702         uint8_t bufferLen = 0;
703
704         // No \r\n by default after the command is entered
705         print( NL );
706         info_msg("Sending: ");
707
708         // Parse args until a \0 is found
709         while ( bufferLen < i2cSend_BuffLenMax )
710         {
711                 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
712                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
713
714                 // Stop processing args if no more are found
715                 if ( *arg1Ptr == '\0' )
716                         break;
717
718                 // If | is found, end sequence and start new one
719                 if ( *arg1Ptr == '|' )
720                 {
721                         print("| ");
722                         I2C_Send( buffer, bufferLen, 0 );
723                         bufferLen = 0;
724                         continue;
725                 }
726
727                 // Interpret the argument
728                 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
729
730                 // Print out the arg
731                 dPrint( arg1Ptr );
732                 print(" ");
733         }
734
735         print( NL );
736
737         I2C_Send( buffer, bufferLen, 0 );
738 }
739
740 void cliFunc_i2cRecv( char* args )
741 {
742         char* curArgs;
743         char* arg1Ptr;
744         char* arg2Ptr = args;
745
746         // Buffer used after interpretting the args, will be sent to I2C functions
747         // NOTE: Limited to 8 bytes currently (can be increased if necessary
748         #define i2cSend_BuffLenMax 8
749         uint8_t buffer[ i2cSend_BuffLenMax ];
750         uint8_t bufferLen = 0;
751
752         // No \r\n by default after the command is entered
753         print( NL );
754         info_msg("Sending: ");
755
756         // Parse args until a \0 is found
757         while ( bufferLen < i2cSend_BuffLenMax )
758         {
759                 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
760                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
761
762                 // Stop processing args if no more are found
763                 if ( *arg1Ptr == '\0' )
764                         break;
765
766                 // If | is found, end sequence and start new one
767                 if ( *arg1Ptr == '|' )
768                 {
769                         print("| ");
770                         I2C_Send( buffer, bufferLen, 0 );
771                         bufferLen = 0;
772                         continue;
773                 }
774
775                 // Interpret the argument
776                 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
777
778                 // Print out the arg
779                 dPrint( arg1Ptr );
780                 print(" ");
781         }
782
783         print( NL );
784
785         I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip
786 }
787
788 void cliFunc_ledPage( char* args )
789 {
790         // Parse number from argument
791         //  NOTE: Only first argument is used
792         char* arg1Ptr;
793         char* arg2Ptr;
794         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
795
796         // Default to 0 if no argument is given
797         uint8_t page = 0;
798
799         if ( arg1Ptr[0] != '\0' )
800         {
801                  page = (uint8_t)numToInt( arg1Ptr );
802         }
803
804         // No \r\n by default after the command is entered
805         print( NL );
806
807         LED_readPage( 0xB4, page );
808 }
809
810 void cliFunc_ledStart( char* args )
811 {
812         print( NL ); // No \r\n by default after the command is entered
813         LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
814         //LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
815         LED_writeReg( 0x0A, 0x01, 0x0B );
816         LED_sendPage( (uint8_t*)LED_ledEnableMask, sizeof( LED_ledEnableMask ), 0 );
817
818 }
819
820 void cliFunc_ledTest( char* args )
821 {
822         print( NL ); // No \r\n by default after the command is entered
823         LED_sendPage( (uint8_t*)examplePage, sizeof( examplePage ), 0 );
824 }
825
826 void cliFunc_ledZero( char* args )
827 {
828         print( NL ); // No \r\n by default after the command is entered
829         LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs
830 }
831