X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Scan%2FISSILed%2Fled_scan.c;h=549fc0d27cb211178d1b950f3caeedfc1e7df3d1;hb=d9ba60b34ee80e073ef708a7e9e58d8b8529a0e2;hp=ef2400a6e4ef798107c1a71cbb10f0c292749fc7;hpb=8942ab63d50b578f39309b9d9c24d000c3698cec;p=kiibohd-controller.git diff --git a/Scan/ISSILed/led_scan.c b/Scan/ISSILed/led_scan.c index ef2400a..549fc0d 100644 --- a/Scan/ISSILed/led_scan.c +++ b/Scan/ISSILed/led_scan.c @@ -21,6 +21,7 @@ // Project Includes #include +#include #include #include @@ -48,6 +49,8 @@ typedef struct I2C_Buffer { } I2C_Buffer; typedef struct LED_Buffer { + uint8_t i2c_addr; + uint8_t reg_addr; uint8_t buffer[LED_BufferLength]; } LED_Buffer; @@ -56,12 +59,14 @@ typedef struct LED_Buffer { // ----- Function Declarations ----- // CLI Functions -void cliFunc_i2cRecv( char* args ); -void cliFunc_i2cSend( char* args ); -void cliFunc_ledPage( char* args ); +void cliFunc_i2cRecv ( char* args ); +void cliFunc_i2cSend ( char* args ); +void cliFunc_ledCtrl ( char* args ); +void cliFunc_ledRPage( char* args ); void cliFunc_ledStart( char* args ); -void cliFunc_ledTest( char* args ); -void cliFunc_ledZero( char* args ); +void cliFunc_ledTest ( char* args ); +void cliFunc_ledWPage( char* args ); +void cliFunc_ledZero ( char* args ); uint8_t I2C_TxBufferPop(); void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer ); @@ -75,17 +80,21 @@ uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen ); // Scan Module command dictionary 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." ); CLIDict_Entry( i2cSend, "Send I2C sequence of bytes. Use |'s to split sequences with a stop." ); -CLIDict_Entry( ledPage, "Read the given register page." ); +CLIDict_Entry( ledCtrl, "Basic LED control. Args: []" ); +CLIDict_Entry( ledRPage, "Read the given register page." ); CLIDict_Entry( ledStart, "Disable software shutdown." ); CLIDict_Entry( ledTest, "Test out the led pages." ); +CLIDict_Entry( ledWPage, "Write to given register page starting at address. i.e. 0x2 0x24 0xF0 0x12" ); CLIDict_Entry( ledZero, "Zero out LED register pages (non-configuration)." ); CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = { CLIDict_Item( i2cRecv ), CLIDict_Item( i2cSend ), - CLIDict_Item( ledPage ), + CLIDict_Item( ledCtrl ), + CLIDict_Item( ledRPage ), CLIDict_Item( ledStart ), CLIDict_Item( ledTest ), + CLIDict_Item( ledWPage ), CLIDict_Item( ledZero ), { 0, 0, 0 } // Null entry for dictionary end }; @@ -102,92 +111,21 @@ volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_ LED_Buffer LED_pageBuffer; -/* // A bit mask determining which LEDs are enabled in the ISSI chip -// All channel mask example -// 0x00 -> 0x11 -const uint8_t LED_ledEnableMask[] = { -0xE8, // I2C address -0x00, // Starting register address -0xFF, 0xFF, // C1-1 -> C1-16 -0xFF, 0xFF, // C2-1 -> C2-16 -0xFF, 0xFF, // C3-1 -> C3-16 -0xFF, 0xFF, // C4-1 -> C4-16 -0xFF, 0xFF, // C5-1 -> C5-16 -0xFF, 0xFF, // C6-1 -> C6-16 -0xFF, 0xFF, // C7-1 -> C7-16 -0xFF, 0xFF, // C8-1 -> C8-16 -0xFF, 0xFF, // C9-1 -> C9-16 +const uint8_t LED_ledEnableMask1[] = { + 0xE8, // I2C address + 0x00, // Starting register address + ISSILedMask1_define }; -*/ -/* -// A bit mask determining which LEDs are enabled in the ISSI chip -// Infinity ErgoDox full mask -// 0x00 -> 0x11 -const uint8_t LED_ledEnableMask[] = { -0xE8, // I2C address -0x00, // Starting register address -0xFC, 0xFC, // C1-1 -> C1-16 -0xFB, 0xFB, // C2-1 -> C2-16 -0xFF, 0xFF, // C3-1 -> C3-16 -0xFE, 0xFE, // C4-1 -> C4-16 -0x7F, 0x7F, // C5-1 -> C5-16 -0xFF, 0xFF, // C6-1 -> C6-16 -0xCF, 0xCF, // C7-1 -> C7-16 -0xC7, 0xC7, // C8-1 -> C8-16 -0x43, 0x43, // C9-1 -> C9-16 -}; -*/ -const uint8_t LED_ledEnableMask[] = { -0xE8, // I2C address -0x00, // Starting register address -0x00, 0x00, // C1-1 -> C1-16 -//0xEC, 0xEC, // C1-1 -> C1-16 -0x00, 0x00, // C2-1 -> C2-16 -0x00, 0x00, // C3-1 -> C3-16 -0x00, 0x00, // C4-1 -> C4-16 -0x00, 0x00, // C5-1 -> C5-16 -0x00, 0x00, // C6-1 -> C6-16 -0x08, 0x08, // C7-1 -> C7-16 -0x00, 0x00, // C8-1 -> C8-16 -0x00, 0x00, // C9-1 -> C9-16 +// Default LED brightness +const uint8_t LED_defaultBrightness1[] = { + 0xE8, // I2C address + 0x24, // Starting register address + ISSILedBrightness1_define }; -// XXX Pre-fill example of buffers -const uint8_t examplePage[] = { -0xE8, // I2C address -0x24, // Starting register address -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C1-1 -> C1-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C2-1 -> C2-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C3-1 -> C3-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C4-1 -> C4-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C5-1 -> C5-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C6-1 -> C6-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C7-1 -> C7-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C8-1 -> C8-16 -0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // C9-1 -> C9-16 -}; - -/* -// XXX Pre-fill example of buffers -const uint8_t examplePage[] = { -0xE8, // I2C address -0x24, // Starting register address -0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, // C1-1 -> C1-16 -0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // C2-1 -> C2-16 -0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, // C3-1 -> C3-16 -0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, // C4-1 -> C4-16 -0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, // C5-1 -> C5-16 -0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, // C6-1 -> C6-16 -0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, // C7-1 -> C7-16 -0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, // C8-1 -> C8-16 -0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, // C9-1 -> C9-16 -}; -*/ - - // ----- Interrupt Functions ----- @@ -245,7 +183,9 @@ void i2c0_isr() } else { - dbug_print("Attempting to read byte"); + dbug_msg("Attempting to read byte - "); + printHex( I2C_RxBuffer.sequencePos ); + print( NL ); I2C0_C1 = I2C_RxBuffer.sequencePos == 1 ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read @@ -376,48 +316,58 @@ void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page ) } -void LED_readPage( uint8_t len, uint8_t page ) +void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page ) { // Page Setup uint8_t pageSetup[] = { 0xE8, 0xFD, page }; + // Reg Write Setup + uint8_t writeData[] = { 0xE8, reg, val }; + // Setup page while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 ) delay(1); - // Register Setup - uint8_t regSetup[] = { 0xE8, 0x00 }; - - // Setup starting register - while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 ) + while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 ) delay(1); - - // Register Read Command - uint8_t regReadCmd[] = { 0xE9 }; - - // Read each register in the page - for ( uint8_t reg = 0; reg < len; reg++ ) - { - // Request register data - while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 0 ) == 0 ) - delay(1); - } } -void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page ) +void LED_readPage( uint8_t len, uint8_t page ) { + // Software shutdown must be enabled to read registers + LED_writeReg( 0x0A, 0x00, 0x0B ); + // Page Setup uint8_t pageSetup[] = { 0xE8, 0xFD, page }; - // Reg Write Setup - uint8_t writeData[] = { 0xE8, reg, val }; - // Setup page while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 ) delay(1); - while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 ) - delay(1); + // Register Setup + uint8_t regSetup[] = { 0xE8, 0x00 }; + + // Read each register in the page + for ( uint8_t reg = 0; reg < len; reg++ ) + { + // Update register to read + regSetup[1] = reg; + + // Configure register + while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 ) + delay(1); + + // Register Read Command + uint8_t regReadCmd[] = { 0xE9 }; + + // Request single register byte + while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 1 ) == 0 ) + delay(1); + dbug_print("NEXT"); + } + + // Disable software shutdown + LED_writeReg( 0x0A, 0x01, 0x0B ); } // Setup @@ -429,25 +379,26 @@ inline void LED_setup() // Initialize I2C I2C_setup(); - /* TODO Make work // Zero out Frame Registers // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things) LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers // Disable Hardware shutdown of ISSI chip (pull high) - GPIOD_PDDR |= (1<<1); - PORTD_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOD_PSOR |= (1<<1); + GPIOB_PDDR |= (1<<16); + PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); + GPIOB_PSOR |= (1<<16); // Clear LED Pages LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers // Enable LEDs based upon mask - LED_sendPage( (uint8_t*)LED_ledEnableMask, sizeof( LED_ledEnableMask ), 0 ); + LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 ); + + // Set default brightness + LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 ); // Disable Software shutdown of ISSI chip LED_writeReg( 0x0A, 0x01, 0x0B ); - */ } @@ -685,8 +636,122 @@ inline uint8_t LED_scan() +// ----- Capabilities ----- + +// Basic LED Control Capability +typedef enum LedControlMode { + // Single LED Modes + LedControlMode_brightness_decrease, + LedControlMode_brightness_increase, + LedControlMode_brightness_set, + // Set all LEDs (index argument not required) + LedControlMode_brightness_decrease_all, + LedControlMode_brightness_increase_all, + LedControlMode_brightness_set_all, +} LedControlMode; + +typedef struct LedControl { + LedControlMode mode; // XXX Make sure to adjust the .kll capability if this variable is larger than 8 bits + uint8_t amount; + uint16_t index; +} LedControl; + +uint8_t LED_control_timer = 0; +void LED_control( LedControl *control ) +{ + // Only send if we've completed all other transactions + if ( I2C_TxBuffer.sequencePos > 0 ) + return; + + // XXX + // ISSI Chip locks up if we spam updates too quickly (might be an I2C bug on this side too -HaaTa) + // Make sure we only send an update every 30 milliseconds at most + // It may be possible to optimize speed even further, but will likely require serious time with a logic analyzer + + uint8_t currentTime = (uint8_t)systick_millis_count; + int8_t compare = (int8_t)(currentTime - LED_control_timer) & 0x7F; + if ( compare < 30 ) + { + return; + } + LED_control_timer = currentTime; + + // Configure based upon the given mode + // TODO Handle multiple issi chips per node + // TODO Perhaps do gamma adjustment? + switch ( control->mode ) + { + case LedControlMode_brightness_decrease: + // Don't worry about rolling over, the cycle is quick + LED_pageBuffer.buffer[ control->index ] -= control->amount; + break; + + case LedControlMode_brightness_increase: + // Don't worry about rolling over, the cycle is quick + LED_pageBuffer.buffer[ control->index ] += control->amount; + break; + + case LedControlMode_brightness_set: + LED_pageBuffer.buffer[ control->index ] = control->amount; + break; + + case LedControlMode_brightness_decrease_all: + for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ ) + { + // Don't worry about rolling over, the cycle is quick + LED_pageBuffer.buffer[ channel ] -= control->amount; + } + break; + + case LedControlMode_brightness_increase_all: + for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ ) + { + // Don't worry about rolling over, the cycle is quick + LED_pageBuffer.buffer[ channel ] += control->amount; + } + break; + + case LedControlMode_brightness_set_all: + for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ ) + { + LED_pageBuffer.buffer[ channel ] = control->amount; + } + break; + } + + // Sync LED buffer with ISSI chip buffer + // TODO Support multiple frames + LED_pageBuffer.i2c_addr = 0xE8; // Chip 1 + LED_pageBuffer.reg_addr = 0x24; // Brightness section + LED_sendPage( (uint8_t*)&LED_pageBuffer, sizeof( LED_Buffer ), 0 ); +} + +void LED_control_capability( uint8_t state, uint8_t stateType, uint8_t *args ) +{ + // Display capability name + if ( stateType == 0xFF && state == 0xFF ) + { + print("LED_control_capability(mode,amount,index)"); + return; + } + + // Only use capability on press + // TODO Analog + if ( stateType == 0x00 && state == 0x03 ) // Not on release + return; + + // Set the input structure + LedControl *control = (LedControl*)args; + + // TODO broadcast to rest of interconnect nodes if necessary + LED_control( control ); +} + + + // ----- CLI Command Functions ----- +// TODO Currently not working correctly void cliFunc_i2cSend( char* args ) { char* curArgs; @@ -783,7 +848,8 @@ void cliFunc_i2cRecv( char* args ) I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip } -void cliFunc_ledPage( char* args ) +// TODO Currently not working correctly +void cliFunc_ledRPage( char* args ) { // Parse number from argument // NOTE: Only first argument is used @@ -796,13 +862,62 @@ void cliFunc_ledPage( char* args ) if ( arg1Ptr[0] != '\0' ) { - page = (uint8_t)numToInt( arg1Ptr ); + page = (uint8_t)numToInt( arg1Ptr ); } // No \r\n by default after the command is entered print( NL ); - LED_readPage( 0xB4, page ); + LED_readPage( 0x1, page ); + //LED_readPage( 0xB4, page ); +} + +void cliFunc_ledWPage( char* args ) +{ + char* curArgs; + char* arg1Ptr; + char* arg2Ptr = args; + + // First process page and starting address + curArgs = arg2Ptr; + CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr ); + + // Stop processing args if no more are found + if ( *arg1Ptr == '\0' ) + return; + uint8_t page[] = { 0xE8, 0xFD, numToInt( arg1Ptr ) }; + + curArgs = arg2Ptr; + CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr ); + + // Stop processing args if no more are found + if ( *arg1Ptr == '\0' ) + return; + uint8_t data[] = { 0xE8, numToInt( arg1Ptr ), 0 }; + + // Set the register page + while ( I2C_Send( page, sizeof( page ), 0 ) == 0 ) + delay(1); + + // Process all args + for ( ;; ) + { + curArgs = arg2Ptr; + CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr ); + + // Stop processing args if no more are found + if ( *arg1Ptr == '\0' ) + break; + + data[2] = numToInt( arg1Ptr ); + + // Write register location and data to I2C + while ( I2C_Send( data, sizeof( data ), 0 ) == 0 ) + delay(1); + + // Increment address + data[1]++; + } } void cliFunc_ledStart( char* args ) @@ -811,14 +926,14 @@ void cliFunc_ledStart( char* args ) LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers //LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers LED_writeReg( 0x0A, 0x01, 0x0B ); - LED_sendPage( (uint8_t*)LED_ledEnableMask, sizeof( LED_ledEnableMask ), 0 ); + LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 ); } void cliFunc_ledTest( char* args ) { print( NL ); // No \r\n by default after the command is entered - LED_sendPage( (uint8_t*)examplePage, sizeof( examplePage ), 0 ); + LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 ); } void cliFunc_ledZero( char* args ) @@ -827,3 +942,40 @@ void cliFunc_ledZero( char* args ) LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs } +void cliFunc_ledCtrl( char* args ) +{ + char* curArgs; + char* arg1Ptr; + char* arg2Ptr = args; + LedControl control; + + // First process mode + curArgs = arg2Ptr; + CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr ); + + // Stop processing args if no more are found + if ( *arg1Ptr == '\0' ) + return; + control.mode = numToInt( arg1Ptr ); + + + // Next process amount + curArgs = arg2Ptr; + CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr ); + + // Stop processing args if no more are found + if ( *arg1Ptr == '\0' ) + return; + control.amount = numToInt( arg1Ptr ); + + + // Finally process led index, if it exists + // Default to 0 + curArgs = arg2Ptr; + CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr ); + control.index = *arg1Ptr == '\0' ? 0 : numToInt( arg1Ptr ); + + // Process request + LED_control( &control ); +} +