1 /* Copyright (C) 2014-2015 by Jacob Alexander
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.
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.
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/>.
17 // ----- Includes -----
20 #include <Lib/ScanLib.h>
33 // ----- Defines -----
35 #define I2C_TxBufferLength 300
36 #define I2C_RxBufferLength 8
38 #define LED_BufferLength 144
41 // ----- Structs -----
43 typedef struct I2C_Buffer {
51 typedef struct LED_Buffer {
52 uint8_t buffer[LED_BufferLength];
57 // ----- Function Declarations -----
60 void cliFunc_i2cRecv ( char* args );
61 void cliFunc_i2cSend ( char* args );
62 void cliFunc_ledRPage( char* args );
63 void cliFunc_ledStart( char* args );
64 void cliFunc_ledTest ( char* args );
65 void cliFunc_ledWPage( char* args );
66 void cliFunc_ledZero ( char* args );
68 uint8_t I2C_TxBufferPop();
69 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer );
70 uint16_t I2C_BufferLen( I2C_Buffer *buffer );
71 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen );
75 // ----- Variables -----
77 // Scan Module command dictionary
78 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." );
79 CLIDict_Entry( i2cSend, "Send I2C sequence of bytes. Use |'s to split sequences with a stop." );
80 CLIDict_Entry( ledRPage, "Read the given register page." );
81 CLIDict_Entry( ledStart, "Disable software shutdown." );
82 CLIDict_Entry( ledTest, "Test out the led pages." );
83 CLIDict_Entry( ledWPage, "Write to given register page starting at address. i.e. 0x2 0x24 0xF0 0x12" );
84 CLIDict_Entry( ledZero, "Zero out LED register pages (non-configuration)." );
86 CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = {
87 CLIDict_Item( i2cRecv ),
88 CLIDict_Item( i2cSend ),
89 CLIDict_Item( ledRPage ),
90 CLIDict_Item( ledStart ),
91 CLIDict_Item( ledTest ),
92 CLIDict_Item( ledWPage ),
93 CLIDict_Item( ledZero ),
94 { 0, 0, 0 } // Null entry for dictionary end
99 // Before sending the sequence, I2C_TxBuffer_CurLen is assigned and as each byte is sent, it is decremented
100 // Once I2C_TxBuffer_CurLen reaches zero, a STOP on the I2C bus is sent
101 volatile uint8_t I2C_TxBufferPtr[ I2C_TxBufferLength ];
102 volatile uint8_t I2C_RxBufferPtr[ I2C_TxBufferLength ];
104 volatile I2C_Buffer I2C_TxBuffer = { 0, 0, 0, I2C_TxBufferLength, (uint8_t*)I2C_TxBufferPtr };
105 volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_RxBufferPtr };
107 LED_Buffer LED_pageBuffer;
109 // A bit mask determining which LEDs are enabled in the ISSI chip
110 const uint8_t LED_ledEnableMask1[] = {
112 0x00, // Starting register address
116 // Default LED brightness
117 const uint8_t LED_defaultBrightness1[] = {
119 0x24, // Starting register address
120 ISSILedBrightness1_define
125 // ----- Interrupt Functions -----
129 cli(); // Disable Interrupts
131 uint8_t status = I2C0_S; // Read I2C Bus status
133 // Master Mode Transmit
134 if ( I2C0_C1 & I2C_C1_TX )
136 // Check current use of the I2C bus
137 // Currently sending data
138 if ( I2C_TxBuffer.sequencePos > 0 )
140 // Make sure slave sent an ACK
141 if ( status & I2C_S_RXAK )
143 // NACK Detected, disable interrupt
144 erro_print("I2C NAK detected...");
145 I2C0_C1 = I2C_C1_IICEN;
148 I2C_TxBuffer.head = 0;
149 I2C_TxBuffer.tail = 0;
150 I2C_TxBuffer.sequencePos = 0;
155 I2C0_D = I2C_TxBufferPop();
159 else if ( I2C_RxBuffer.sequencePos > 0 )
161 // Master Receive, addr sent
162 if ( status & I2C_S_ARBL )
165 erro_print("Arbitration lost...");
168 I2C0_C1 = I2C_C1_IICEN;
169 I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt
171 if ( status & I2C_S_RXAK )
173 // Slave Address NACK Detected, disable interrupt
174 erro_print("Slave Address I2C NAK detected...");
177 I2C0_C1 = I2C_C1_IICEN;
181 dbug_print("Attempting to read byte");
182 I2C0_C1 = I2C_RxBuffer.sequencePos == 1
183 ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read
184 : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read
191 printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) );
195 // Delay around STOP to make sure it actually happens...
196 delayMicroseconds( 1 );
197 I2C0_C1 = I2C_C1_IICEN; // Send STOP
198 delayMicroseconds( 7 );
200 // If there is another sequence, start sending
201 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size )
203 // Clear status flags
204 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
206 // Wait...till the master dies
207 while ( I2C0_S & I2C_S_BUSY );
209 // Enable I2C interrupt
210 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
213 I2C0_D = I2C_TxBufferPop();
217 // Master Mode Receive
220 // XXX Do we need to handle 2nd last byte?
221 //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv
224 if ( I2C_TxBuffer.sequencePos <= 1 )
227 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
230 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
232 delayMicroseconds( 1 ); // Should be enough time before issuing the stop
233 I2C0_C1 = I2C_C1_IICEN; // Send STOP
238 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
242 I2C0_S = I2C_S_IICIF; // Clear interrupt
244 sei(); // Re-enable Interrupts
249 // ----- Functions -----
251 inline void I2C_setup()
253 // Enable I2C internal clock
254 SIM_SCGC4 |= SIM_SCGC4_I2C0; // Bus 0
256 // External pull-up resistor
257 PORTB_PCR0 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
258 PORTB_PCR1 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
260 // SCL Frequency Divider
261 // 400kHz -> 120 (0x85) @ 48 MHz F_BUS
264 I2C0_C1 = I2C_C1_IICEN;
265 I2C0_C2 = I2C_C2_HDRS; // High drive select
267 // Enable I2C Interrupt
268 NVIC_ENABLE_IRQ( IRQ_I2C0 );
271 void LED_zeroPages( uint8_t startPage, uint8_t numPages, uint8_t startReg, uint8_t endReg )
274 uint8_t pageSetup[] = { 0xE8, 0xFD, 0x00 };
276 // Max length of a page + chip id + reg start
277 uint8_t fullPage[ 0xB4 + 2 ] = { 0 }; // Max size of page
278 fullPage[0] = 0xE8; // Set chip id
279 fullPage[1] = startReg; // Set start reg
281 // Iterate through given pages, zero'ing out the given register regions
282 for ( uint8_t page = startPage; page < startPage + numPages; page++ )
288 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
292 while ( I2C_Send( fullPage, endReg - startReg + 2, 0 ) == 0 )
297 void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page )
300 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
303 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
306 // Write page to I2C Tx Buffer
307 while ( I2C_Send( buffer, len, 0 ) == 0 )
312 void LED_readPage( uint8_t len, uint8_t page )
315 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
318 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
322 uint8_t regSetup[] = { 0xE8, 0x00 };
324 // Setup starting register
325 while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 )
328 // Register Read Command
329 uint8_t regReadCmd[] = { 0xE9 };
331 // Read each register in the page
332 for ( uint8_t reg = 0; reg < len; reg++ )
334 // Request register data
335 while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 0 ) == 0 )
340 void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page )
343 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
346 uint8_t writeData[] = { 0xE8, reg, val };
349 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
352 while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 )
357 inline void LED_setup()
359 // Register Scan CLI dictionary
360 CLI_registerDictionary( ledCLIDict, ledCLIDictName );
365 // Zero out Frame Registers
366 // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things)
367 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
369 // Disable Hardware shutdown of ISSI chip (pull high)
370 GPIOB_PDDR |= (1<<16);
371 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
372 GPIOB_PSOR |= (1<<16);
375 LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
377 // Enable LEDs based upon mask
378 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
380 // Set default brightness
381 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
383 // Disable Software shutdown of ISSI chip
384 LED_writeReg( 0x0A, 0x01, 0x0B );
388 inline uint8_t I2C_BufferCopy( uint8_t *data, uint8_t sendLen, uint8_t recvLen, I2C_Buffer *buffer )
392 // If sendLen is greater than buffer fail right away
393 if ( sendLen > buffer->size )
396 // Calculate new tail to determine if buffer has enough space
397 // The first element specifies the expected number of bytes from the slave (+1)
398 // The second element in the new buffer is the length of the buffer sequence (+1)
399 uint16_t newTail = buffer->tail + sendLen + 2;
400 if ( newTail >= buffer->size )
401 newTail -= buffer->size;
403 if ( I2C_BufferLen( buffer ) < sendLen + 2 )
408 printHex( sendLen + 2 );
416 // If buffer is clean, return 1, otherwise 2
417 reTurn = buffer->head == buffer->tail ? 1 : 2;
419 // Add to buffer, already know there is enough room (simplifies adding logic)
420 uint8_t bufferHeaderPos = 0;
421 for ( uint16_t c = 0; c < sendLen; c++ )
423 // Add data to buffer
424 switch ( bufferHeaderPos )
427 buffer->buffer[ buffer->tail ] = recvLen;
433 buffer->buffer[ buffer->tail ] = sendLen;
439 buffer->buffer[ buffer->tail ] = data[ c ];
443 // Check for wrap-around case
444 if ( buffer->tail + 1 >= buffer->size )
459 inline uint16_t I2C_BufferLen( I2C_Buffer *buffer )
462 if ( buffer->tail >= buffer->head )
463 return buffer->head + buffer->size - buffer->tail;
466 return buffer->head - buffer->tail;
470 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer )
475 // Make sure buffer isn't full
476 if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) )
478 warn_msg("I2C_BufferPush failed, buffer full: ");
484 // Check for wrap-around case
485 if ( buffer->tail + 1 >= buffer->size )
495 // Add byte to buffer
496 buffer->buffer[ buffer->tail ] = byte;
500 uint8_t I2C_TxBufferPop()
502 // Return 0xFF if no buffer left (do not rely on this)
503 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) >= I2C_TxBuffer.size )
505 erro_msg("No buffer to pop an entry from... ");
506 printHex( I2C_TxBuffer.head );
508 printHex( I2C_TxBuffer.tail );
510 printHex( I2C_TxBuffer.sequencePos );
515 // If there is currently no sequence being sent, the first entry in the RingBuffer is the length
516 if ( I2C_TxBuffer.sequencePos == 0 )
518 I2C_TxBuffer.sequencePos = 0xFF; // So this doesn't become an infinite loop
519 I2C_RxBuffer.sequencePos = I2C_TxBufferPop();
520 I2C_TxBuffer.sequencePos = I2C_TxBufferPop();
523 uint8_t data = I2C_TxBuffer.buffer[ I2C_TxBuffer.head ];
529 if ( I2C_TxBuffer.head >= I2C_TxBuffer.size )
530 I2C_TxBuffer.head = 0;
532 // Decrement buffer sequence (until next stop will be sent)
533 I2C_TxBuffer.sequencePos--;
536 dbug_msg("Popping: ");
539 printHex( I2C_TxBuffer.head );
541 printHex( I2C_TxBuffer.tail );
543 printHex( I2C_TxBuffer.sequencePos );
550 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen )
552 // Check head and tail pointers
554 // If empty, start up I2C Master Tx
555 // If buffer is non-empty and non-full, just append to the buffer
556 switch ( I2C_BufferCopy( data, sendLen, recvLen, (I2C_Buffer*)&I2C_TxBuffer ) )
558 // Not enough buffer space...
561 erro_msg("Not enough Tx buffer space... ");
562 printHex( I2C_TxBuffer.head );
564 printHex( I2C_TxBuffer.tail );
568 printHex( I2C_TxBuffer.size );
573 // Empty buffer, initialize I2C
575 // Clear status flags
576 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
578 // Check to see if we already have control of the bus
579 if ( I2C0_C1 & I2C_C1_MST )
581 // Already the master (ah yeah), send a repeated start
582 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
584 // Otherwise, seize control
587 // Wait...till the master dies
588 while ( I2C0_S & I2C_S_BUSY );
590 // Now we're the master (ah yisss), get ready to send stuffs
591 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
594 // Enable I2C interrupt
595 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
597 // Depending on what type of transfer, the first byte is configured for R or W
598 I2C0_D = I2C_TxBufferPop();
603 // Dirty buffer, I2C already initialized
609 // LED State processing loop
610 inline uint8_t LED_scan()
622 // ----- CLI Command Functions -----
624 void cliFunc_i2cSend( char* args )
628 char* arg2Ptr = args;
630 // Buffer used after interpretting the args, will be sent to I2C functions
631 // NOTE: Limited to 8 bytes currently (can be increased if necessary
632 #define i2cSend_BuffLenMax 8
633 uint8_t buffer[ i2cSend_BuffLenMax ];
634 uint8_t bufferLen = 0;
636 // No \r\n by default after the command is entered
638 info_msg("Sending: ");
640 // Parse args until a \0 is found
641 while ( bufferLen < i2cSend_BuffLenMax )
643 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
644 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
646 // Stop processing args if no more are found
647 if ( *arg1Ptr == '\0' )
650 // If | is found, end sequence and start new one
651 if ( *arg1Ptr == '|' )
654 I2C_Send( buffer, bufferLen, 0 );
659 // Interpret the argument
660 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
669 I2C_Send( buffer, bufferLen, 0 );
672 void cliFunc_i2cRecv( char* args )
676 char* arg2Ptr = args;
678 // Buffer used after interpretting the args, will be sent to I2C functions
679 // NOTE: Limited to 8 bytes currently (can be increased if necessary
680 #define i2cSend_BuffLenMax 8
681 uint8_t buffer[ i2cSend_BuffLenMax ];
682 uint8_t bufferLen = 0;
684 // No \r\n by default after the command is entered
686 info_msg("Sending: ");
688 // Parse args until a \0 is found
689 while ( bufferLen < i2cSend_BuffLenMax )
691 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
692 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
694 // Stop processing args if no more are found
695 if ( *arg1Ptr == '\0' )
698 // If | is found, end sequence and start new one
699 if ( *arg1Ptr == '|' )
702 I2C_Send( buffer, bufferLen, 0 );
707 // Interpret the argument
708 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
717 I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip
720 void cliFunc_ledRPage( char* args )
722 // Parse number from argument
723 // NOTE: Only first argument is used
726 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
728 // Default to 0 if no argument is given
731 if ( arg1Ptr[0] != '\0' )
733 page = (uint8_t)numToInt( arg1Ptr );
736 // No \r\n by default after the command is entered
739 LED_readPage( 0xB4, page );
742 void cliFunc_ledWPage( char* args )
746 char* arg2Ptr = args;
748 // First process page and starting address
750 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
752 // Stop processing args if no more are found
753 if ( *arg1Ptr == '\0' )
755 uint8_t page[] = { 0xE8, 0xFD, numToInt( arg1Ptr ) };
758 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
760 // Stop processing args if no more are found
761 if ( *arg1Ptr == '\0' )
763 uint8_t data[] = { 0xE8, numToInt( arg1Ptr ), 0 };
765 // Set the register page
766 while ( I2C_Send( page, sizeof( page ), 0 ) == 0 )
773 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
775 // Stop processing args if no more are found
776 if ( *arg1Ptr == '\0' )
779 data[2] = numToInt( arg1Ptr );
781 // Write register location and data to I2C
782 while ( I2C_Send( data, sizeof( data ), 0 ) == 0 )
790 void cliFunc_ledStart( char* args )
792 print( NL ); // No \r\n by default after the command is entered
793 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
794 //LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
795 LED_writeReg( 0x0A, 0x01, 0x0B );
796 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
800 void cliFunc_ledTest( char* args )
802 print( NL ); // No \r\n by default after the command is entered
803 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
806 void cliFunc_ledZero( char* args )
808 print( NL ); // No \r\n by default after the command is entered
809 LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs