1 /* Copyright (C) 2014-2016 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>
28 // Interconnect module if compiled in
29 #if defined(ConnectEnabled_define)
30 #include <connect_scan.h>
38 // ----- Defines -----
40 #define I2C_TxBufferLength 300
41 #define I2C_RxBufferLength 8
43 #define LED_BufferLength 144
45 // TODO Needs to be defined per keyboard
46 #define LED_TotalChannels 144
50 // ----- Structs -----
52 typedef struct I2C_Buffer {
60 typedef struct LED_Buffer {
63 uint8_t buffer[LED_BufferLength];
68 // ----- Function Declarations -----
71 void cliFunc_i2cRecv ( char* args );
72 void cliFunc_i2cSend ( char* args );
73 void cliFunc_ledCtrl ( char* args );
74 void cliFunc_ledRPage( char* args );
75 void cliFunc_ledStart( char* args );
76 void cliFunc_ledTest ( char* args );
77 void cliFunc_ledWPage( char* args );
78 void cliFunc_ledZero ( char* args );
80 uint8_t I2C_TxBufferPop();
81 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer );
82 uint16_t I2C_BufferLen( I2C_Buffer *buffer );
83 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen );
87 // ----- Variables -----
89 // Scan Module command dictionary
90 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." );
91 CLIDict_Entry( i2cSend, "Send I2C sequence of bytes. Use |'s to split sequences with a stop." );
92 CLIDict_Entry( ledCtrl, "Basic LED control. Args: <mode> <amount> [<index>]" );
93 CLIDict_Entry( ledRPage, "Read the given register page." );
94 CLIDict_Entry( ledStart, "Disable software shutdown." );
95 CLIDict_Entry( ledTest, "Test out the led pages." );
96 CLIDict_Entry( ledWPage, "Write to given register page starting at address. i.e. 0x2 0x24 0xF0 0x12" );
97 CLIDict_Entry( ledZero, "Zero out LED register pages (non-configuration)." );
99 CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = {
100 CLIDict_Item( i2cRecv ),
101 CLIDict_Item( i2cSend ),
102 CLIDict_Item( ledCtrl ),
103 CLIDict_Item( ledRPage ),
104 CLIDict_Item( ledStart ),
105 CLIDict_Item( ledTest ),
106 CLIDict_Item( ledWPage ),
107 CLIDict_Item( ledZero ),
108 { 0, 0, 0 } // Null entry for dictionary end
113 // Before sending the sequence, I2C_TxBuffer_CurLen is assigned and as each byte is sent, it is decremented
114 // Once I2C_TxBuffer_CurLen reaches zero, a STOP on the I2C bus is sent
115 volatile uint8_t I2C_TxBufferPtr[ I2C_TxBufferLength ];
116 volatile uint8_t I2C_RxBufferPtr[ I2C_TxBufferLength ];
118 volatile I2C_Buffer I2C_TxBuffer = { 0, 0, 0, I2C_TxBufferLength, (uint8_t*)I2C_TxBufferPtr };
119 volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_RxBufferPtr };
121 LED_Buffer LED_pageBuffer;
123 // A bit mask determining which LEDs are enabled in the ISSI chip
124 const uint8_t LED_ledEnableMask1[] = {
126 0x00, // Starting register address
130 // Default LED brightness
131 const uint8_t LED_defaultBrightness1[] = {
133 0x24, // Starting register address
134 ISSILedBrightness1_define
139 // ----- Interrupt Functions -----
143 cli(); // Disable Interrupts
145 uint8_t status = I2C0_S; // Read I2C Bus status
147 // Master Mode Transmit
148 if ( I2C0_C1 & I2C_C1_TX )
150 // Check current use of the I2C bus
151 // Currently sending data
152 if ( I2C_TxBuffer.sequencePos > 0 )
154 // Make sure slave sent an ACK
155 if ( status & I2C_S_RXAK )
157 // NACK Detected, disable interrupt
158 erro_print("I2C NAK detected...");
159 I2C0_C1 = I2C_C1_IICEN;
162 I2C_TxBuffer.head = 0;
163 I2C_TxBuffer.tail = 0;
164 I2C_TxBuffer.sequencePos = 0;
169 I2C0_D = I2C_TxBufferPop();
173 else if ( I2C_RxBuffer.sequencePos > 0 )
175 // Master Receive, addr sent
176 if ( status & I2C_S_ARBL )
179 erro_print("Arbitration lost...");
182 I2C0_C1 = I2C_C1_IICEN;
183 I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt
185 if ( status & I2C_S_RXAK )
187 // Slave Address NACK Detected, disable interrupt
188 erro_print("Slave Address I2C NAK detected...");
191 I2C0_C1 = I2C_C1_IICEN;
195 dbug_msg("Attempting to read byte - ");
196 printHex( I2C_RxBuffer.sequencePos );
198 I2C0_C1 = I2C_RxBuffer.sequencePos == 1
199 ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read
200 : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read
207 printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) );
211 // Delay around STOP to make sure it actually happens...
212 delayMicroseconds( 1 );
213 I2C0_C1 = I2C_C1_IICEN; // Send STOP
214 delayMicroseconds( 7 );
216 // If there is another sequence, start sending
217 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size )
219 // Clear status flags
220 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
222 // Wait...till the master dies
223 while ( I2C0_S & I2C_S_BUSY );
225 // Enable I2C interrupt
226 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
229 I2C0_D = I2C_TxBufferPop();
233 // Master Mode Receive
236 // XXX Do we need to handle 2nd last byte?
237 //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv
240 if ( I2C_TxBuffer.sequencePos <= 1 )
243 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
246 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
248 delayMicroseconds( 1 ); // Should be enough time before issuing the stop
249 I2C0_C1 = I2C_C1_IICEN; // Send STOP
254 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
258 I2C0_S = I2C_S_IICIF; // Clear interrupt
260 sei(); // Re-enable Interrupts
265 // ----- Functions -----
267 inline void I2C_setup()
269 // Enable I2C internal clock
270 SIM_SCGC4 |= SIM_SCGC4_I2C0; // Bus 0
272 // External pull-up resistor
273 PORTB_PCR0 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
274 PORTB_PCR1 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
276 // SCL Frequency Divider
277 // 400kHz -> 120 (0x85) @ 48 MHz F_BUS
280 I2C0_C1 = I2C_C1_IICEN;
281 I2C0_C2 = I2C_C2_HDRS; // High drive select
283 // Enable I2C Interrupt
284 NVIC_ENABLE_IRQ( IRQ_I2C0 );
287 void LED_zeroPages( uint8_t startPage, uint8_t numPages, uint8_t startReg, uint8_t endReg )
290 uint8_t pageSetup[] = { 0xE8, 0xFD, 0x00 };
292 // Max length of a page + chip id + reg start
293 uint8_t fullPage[ 0xB4 + 2 ] = { 0 }; // Max size of page
294 fullPage[0] = 0xE8; // Set chip id
295 fullPage[1] = startReg; // Set start reg
297 // Iterate through given pages, zero'ing out the given register regions
298 for ( uint8_t page = startPage; page < startPage + numPages; page++ )
304 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
308 while ( I2C_Send( fullPage, endReg - startReg + 2, 0 ) == 0 )
313 void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page )
316 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
319 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
322 // Write page to I2C Tx Buffer
323 while ( I2C_Send( buffer, len, 0 ) == 0 )
328 void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page )
331 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
334 uint8_t writeData[] = { 0xE8, reg, val };
337 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
340 while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 )
344 void LED_readPage( uint8_t len, uint8_t page )
346 // Software shutdown must be enabled to read registers
347 LED_writeReg( 0x0A, 0x00, 0x0B );
350 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
353 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
357 uint8_t regSetup[] = { 0xE8, 0x00 };
359 // Read each register in the page
360 for ( uint8_t reg = 0; reg < len; reg++ )
362 // Update register to read
365 // Configure register
366 while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 )
369 // Register Read Command
370 uint8_t regReadCmd[] = { 0xE9 };
372 // Request single register byte
373 while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 1 ) == 0 )
378 // Disable software shutdown
379 LED_writeReg( 0x0A, 0x01, 0x0B );
383 inline void LED_setup()
385 // Register Scan CLI dictionary
386 CLI_registerDictionary( ledCLIDict, ledCLIDictName );
391 // Zero out Frame Registers
392 // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things)
393 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
395 // Disable Hardware shutdown of ISSI chip (pull high)
396 GPIOB_PDDR |= (1<<16);
397 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
398 GPIOB_PSOR |= (1<<16);
401 LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
403 // Enable LEDs based upon mask
404 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
406 // Set default brightness
407 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
409 // Do not disable software shutdown of ISSI chip unless current is high enough
410 // Require at least 150 mA
411 // May be enabled/disabled at a later time
412 if ( Output_current_available() >= 150 )
414 // Disable Software shutdown of ISSI chip
415 LED_writeReg( 0x0A, 0x01, 0x0B );
420 inline uint8_t I2C_BufferCopy( uint8_t *data, uint8_t sendLen, uint8_t recvLen, I2C_Buffer *buffer )
424 // If sendLen is greater than buffer fail right away
425 if ( sendLen > buffer->size )
428 // Calculate new tail to determine if buffer has enough space
429 // The first element specifies the expected number of bytes from the slave (+1)
430 // The second element in the new buffer is the length of the buffer sequence (+1)
431 uint16_t newTail = buffer->tail + sendLen + 2;
432 if ( newTail >= buffer->size )
433 newTail -= buffer->size;
435 if ( I2C_BufferLen( buffer ) < sendLen + 2 )
440 printHex( sendLen + 2 );
448 // If buffer is clean, return 1, otherwise 2
449 reTurn = buffer->head == buffer->tail ? 1 : 2;
451 // Add to buffer, already know there is enough room (simplifies adding logic)
452 uint8_t bufferHeaderPos = 0;
453 for ( uint16_t c = 0; c < sendLen; c++ )
455 // Add data to buffer
456 switch ( bufferHeaderPos )
459 buffer->buffer[ buffer->tail ] = recvLen;
465 buffer->buffer[ buffer->tail ] = sendLen;
471 buffer->buffer[ buffer->tail ] = data[ c ];
475 // Check for wrap-around case
476 if ( buffer->tail + 1 >= buffer->size )
491 inline uint16_t I2C_BufferLen( I2C_Buffer *buffer )
494 if ( buffer->tail >= buffer->head )
495 return buffer->head + buffer->size - buffer->tail;
498 return buffer->head - buffer->tail;
502 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer )
507 // Make sure buffer isn't full
508 if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) )
510 warn_msg("I2C_BufferPush failed, buffer full: ");
516 // Check for wrap-around case
517 if ( buffer->tail + 1 >= buffer->size )
527 // Add byte to buffer
528 buffer->buffer[ buffer->tail ] = byte;
532 uint8_t I2C_TxBufferPop()
534 // Return 0xFF if no buffer left (do not rely on this)
535 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) >= I2C_TxBuffer.size )
537 erro_msg("No buffer to pop an entry from... ");
538 printHex( I2C_TxBuffer.head );
540 printHex( I2C_TxBuffer.tail );
542 printHex( I2C_TxBuffer.sequencePos );
547 // If there is currently no sequence being sent, the first entry in the RingBuffer is the length
548 if ( I2C_TxBuffer.sequencePos == 0 )
550 I2C_TxBuffer.sequencePos = 0xFF; // So this doesn't become an infinite loop
551 I2C_RxBuffer.sequencePos = I2C_TxBufferPop();
552 I2C_TxBuffer.sequencePos = I2C_TxBufferPop();
555 uint8_t data = I2C_TxBuffer.buffer[ I2C_TxBuffer.head ];
561 if ( I2C_TxBuffer.head >= I2C_TxBuffer.size )
562 I2C_TxBuffer.head = 0;
564 // Decrement buffer sequence (until next stop will be sent)
565 I2C_TxBuffer.sequencePos--;
568 dbug_msg("Popping: ");
571 printHex( I2C_TxBuffer.head );
573 printHex( I2C_TxBuffer.tail );
575 printHex( I2C_TxBuffer.sequencePos );
582 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen )
584 // Check head and tail pointers
586 // If empty, start up I2C Master Tx
587 // If buffer is non-empty and non-full, just append to the buffer
588 switch ( I2C_BufferCopy( data, sendLen, recvLen, (I2C_Buffer*)&I2C_TxBuffer ) )
590 // Not enough buffer space...
593 erro_msg("Not enough Tx buffer space... ");
594 printHex( I2C_TxBuffer.head );
596 printHex( I2C_TxBuffer.tail );
600 printHex( I2C_TxBuffer.size );
605 // Empty buffer, initialize I2C
607 // Clear status flags
608 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
610 // Check to see if we already have control of the bus
611 if ( I2C0_C1 & I2C_C1_MST )
613 // Already the master (ah yeah), send a repeated start
614 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
616 // Otherwise, seize control
619 // Wait...till the master dies
620 while ( I2C0_S & I2C_S_BUSY );
622 // Now we're the master (ah yisss), get ready to send stuffs
623 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
626 // Enable I2C interrupt
627 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
629 // Depending on what type of transfer, the first byte is configured for R or W
630 I2C0_D = I2C_TxBufferPop();
635 // Dirty buffer, I2C already initialized
641 // LED State processing loop
642 inline uint8_t LED_scan()
653 // Called by parent Scan Module whenver the available current has changed
655 void LED_currentChange( unsigned int current )
657 // TODO dim LEDs in low power mode instead of shutting off
660 // Enabled Software shutdown of ISSI chip
661 LED_writeReg( 0x0A, 0x00, 0x0B );
665 // Disable Software shutdown of ISSI chip
666 LED_writeReg( 0x0A, 0x01, 0x0B );
672 // ----- Capabilities -----
674 // Basic LED Control Capability
675 typedef enum LedControlMode {
677 LedControlMode_brightness_decrease,
678 LedControlMode_brightness_increase,
679 LedControlMode_brightness_set,
680 // Set all LEDs (index argument not required)
681 LedControlMode_brightness_decrease_all,
682 LedControlMode_brightness_increase_all,
683 LedControlMode_brightness_set_all,
686 typedef struct LedControl {
687 LedControlMode mode; // XXX Make sure to adjust the .kll capability if this variable is larger than 8 bits
692 void LED_control( LedControl *control )
694 // Only send if we've completed all other transactions
696 if ( I2C_TxBuffer.sequencePos > 0 )
700 // Configure based upon the given mode
701 // TODO Perhaps do gamma adjustment?
702 switch ( control->mode )
704 case LedControlMode_brightness_decrease:
705 // Don't worry about rolling over, the cycle is quick
706 LED_pageBuffer.buffer[ control->index ] -= control->amount;
709 case LedControlMode_brightness_increase:
710 // Don't worry about rolling over, the cycle is quick
711 LED_pageBuffer.buffer[ control->index ] += control->amount;
714 case LedControlMode_brightness_set:
715 LED_pageBuffer.buffer[ control->index ] = control->amount;
718 case LedControlMode_brightness_decrease_all:
719 for ( uint8_t channel = 0; channel < LED_TotalChannels; channel++ )
721 // Don't worry about rolling over, the cycle is quick
722 LED_pageBuffer.buffer[ channel ] -= control->amount;
726 case LedControlMode_brightness_increase_all:
727 for ( uint8_t channel = 0; channel < LED_TotalChannels; channel++ )
729 // Don't worry about rolling over, the cycle is quick
730 LED_pageBuffer.buffer[ channel ] += control->amount;
734 case LedControlMode_brightness_set_all:
735 for ( uint8_t channel = 0; channel < LED_TotalChannels; channel++ )
737 LED_pageBuffer.buffer[ channel ] = control->amount;
742 // Sync LED buffer with ISSI chip buffer
743 // TODO Support multiple frames
744 LED_pageBuffer.i2c_addr = 0xE8; // Chip 1
745 LED_pageBuffer.reg_addr = 0x24; // Brightness section
746 LED_sendPage( (uint8_t*)&LED_pageBuffer, sizeof( LED_Buffer ), 0 );
749 uint8_t LED_control_timer = 0;
750 void LED_control_capability( uint8_t state, uint8_t stateType, uint8_t *args )
752 // Display capability name
753 if ( stateType == 0xFF && state == 0xFF )
755 print("LED_control_capability(mode,amount,index)");
759 // Only use capability on press
761 if ( stateType == 0x00 && state == 0x03 ) // Not on release
765 // ISSI Chip locks up if we spam updates too quickly (might be an I2C bug on this side too -HaaTa)
766 // Make sure we only send an update every 30 milliseconds at most
767 // It may be possible to optimize speed even further, but will likely require serious time with a logic analyzer
769 uint8_t currentTime = (uint8_t)systick_millis_count;
770 int8_t compare = (int8_t)(currentTime - LED_control_timer) & 0x7F;
775 LED_control_timer = currentTime;
777 // Set the input structure
778 LedControl *control = (LedControl*)args;
780 // Interconnect broadcasting
781 #if defined(ConnectEnabled_define)
782 uint8_t send_packet = 0;
783 uint8_t ignore_node = 0;
785 // By default send to the *next* node, which will determine where to go next
786 extern uint8_t Connect_id; // connect_scan.c
787 uint8_t addr = Connect_id + 1;
789 switch ( control->mode )
791 // Calculate the led address to send
792 // If greater than the Total hannels
793 // Set address - Total channels
795 case LedControlMode_brightness_decrease:
796 case LedControlMode_brightness_increase:
797 case LedControlMode_brightness_set:
798 // Ignore if led is on this node
799 if ( control->index < LED_TotalChannels )
802 // Calculate new led index
803 control->index -= LED_TotalChannels;
809 // Broadcast to all nodes
810 // XXX Do not set broadcasting address
811 // Will send command twice
812 case LedControlMode_brightness_decrease_all:
813 case LedControlMode_brightness_increase_all:
814 case LedControlMode_brightness_set_all:
819 // Only send interconnect remote capability packet if necessary
823 extern const Capability CapabilitiesList[];
825 // Broadcast layerStackExact remote capability (0xFF is the broadcast id)
826 Connect_send_RemoteCapability(
828 LED_control_capability_index,
831 CapabilitiesList[ LED_control_capability_index ].argCount,
836 // If there is nothing to do on this node, ignore
841 // Modify led state of this node
842 LED_control( control );
847 // ----- CLI Command Functions -----
849 // TODO Currently not working correctly
850 void cliFunc_i2cSend( char* args )
854 char* arg2Ptr = args;
856 // Buffer used after interpretting the args, will be sent to I2C functions
857 // NOTE: Limited to 8 bytes currently (can be increased if necessary
858 #define i2cSend_BuffLenMax 8
859 uint8_t buffer[ i2cSend_BuffLenMax ];
860 uint8_t bufferLen = 0;
862 // No \r\n by default after the command is entered
864 info_msg("Sending: ");
866 // Parse args until a \0 is found
867 while ( bufferLen < i2cSend_BuffLenMax )
869 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
870 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
872 // Stop processing args if no more are found
873 if ( *arg1Ptr == '\0' )
876 // If | is found, end sequence and start new one
877 if ( *arg1Ptr == '|' )
880 I2C_Send( buffer, bufferLen, 0 );
885 // Interpret the argument
886 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
895 I2C_Send( buffer, bufferLen, 0 );
898 void cliFunc_i2cRecv( char* args )
902 char* arg2Ptr = args;
904 // Buffer used after interpretting the args, will be sent to I2C functions
905 // NOTE: Limited to 8 bytes currently (can be increased if necessary
906 #define i2cSend_BuffLenMax 8
907 uint8_t buffer[ i2cSend_BuffLenMax ];
908 uint8_t bufferLen = 0;
910 // No \r\n by default after the command is entered
912 info_msg("Sending: ");
914 // Parse args until a \0 is found
915 while ( bufferLen < i2cSend_BuffLenMax )
917 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
918 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
920 // Stop processing args if no more are found
921 if ( *arg1Ptr == '\0' )
924 // If | is found, end sequence and start new one
925 if ( *arg1Ptr == '|' )
928 I2C_Send( buffer, bufferLen, 0 );
933 // Interpret the argument
934 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
943 I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip
946 // TODO Currently not working correctly
947 void cliFunc_ledRPage( char* args )
949 // Parse number from argument
950 // NOTE: Only first argument is used
953 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
955 // Default to 0 if no argument is given
958 if ( arg1Ptr[0] != '\0' )
960 page = (uint8_t)numToInt( arg1Ptr );
963 // No \r\n by default after the command is entered
966 LED_readPage( 0x1, page );
967 //LED_readPage( 0xB4, page );
970 void cliFunc_ledWPage( char* args )
974 char* arg2Ptr = args;
976 // First process page and starting address
978 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
980 // Stop processing args if no more are found
981 if ( *arg1Ptr == '\0' )
983 uint8_t page[] = { 0xE8, 0xFD, numToInt( arg1Ptr ) };
986 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
988 // Stop processing args if no more are found
989 if ( *arg1Ptr == '\0' )
991 uint8_t data[] = { 0xE8, numToInt( arg1Ptr ), 0 };
993 // Set the register page
994 while ( I2C_Send( page, sizeof( page ), 0 ) == 0 )
1001 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1003 // Stop processing args if no more are found
1004 if ( *arg1Ptr == '\0' )
1007 data[2] = numToInt( arg1Ptr );
1009 // Write register location and data to I2C
1010 while ( I2C_Send( data, sizeof( data ), 0 ) == 0 )
1013 // Increment address
1018 void cliFunc_ledStart( char* args )
1020 print( NL ); // No \r\n by default after the command is entered
1021 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
1022 //LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
1023 LED_writeReg( 0x0A, 0x01, 0x0B );
1024 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
1028 void cliFunc_ledTest( char* args )
1030 print( NL ); // No \r\n by default after the command is entered
1031 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
1034 void cliFunc_ledZero( char* args )
1036 print( NL ); // No \r\n by default after the command is entered
1037 LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs
1040 void cliFunc_ledCtrl( char* args )
1044 char* arg2Ptr = args;
1047 // First process mode
1049 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1051 // Stop processing args if no more are found
1052 if ( *arg1Ptr == '\0' )
1054 control.mode = numToInt( arg1Ptr );
1057 // Next process amount
1059 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1061 // Stop processing args if no more are found
1062 if ( *arg1Ptr == '\0' )
1064 control.amount = numToInt( arg1Ptr );
1067 // Finally process led index, if it exists
1070 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
1071 control.index = *arg1Ptr == '\0' ? 0 : numToInt( arg1Ptr );
1074 LED_control( &control );