void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer ) { dbug_msg("DATA: "); printHex( byte ); // Make sure buffer isn't full if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) ) { warn_msg("I2C_BufferPush failed, buffer full: "); printHex( byte ); print( NL ); return; } // Check for wrap-around case if ( buffer->tail + 1 >= buffer->size ) { buffer->tail = 0; } // Normal case else { buffer->tail++; } // Add byte to buffer buffer->buffer[ buffer->tail ] = byte; }
inline void LED_scan() { // Latency measurement start Latency_start_time( ledLatencyResource ); // Check for current change event if ( LED_currentEvent ) { // Turn LEDs off in low power mode if ( LED_currentEvent < 150 ) { LED_enable_current = 0; // Pause animations and clear display Pixel_setAnimationControl( AnimationControl_WipePause ); } else { LED_enable_current = 1; // Start animations Pixel_setAnimationControl( AnimationControl_Forward ); } LED_currentEvent = 0; } // Check if an LED_pause is set // Some ISSI operations need a clear buffer, but still have the chip running if ( LED_pause ) goto led_finish_scan; // Check enable state if ( LED_enable && LED_enable_current ) { // Disable Hardware shutdown of ISSI chips (pull high) GPIO_Ctrl( hardware_shutdown_pin, GPIO_Type_DriveHigh, GPIO_Config_Pullup ); } // Only write pages to I2C if chip is enabled (i.e. Hardware shutdown is disabled) else { // Enable hardware shutdown GPIO_Ctrl( hardware_shutdown_pin, GPIO_Type_DriveLow, GPIO_Config_Pullup ); goto led_finish_scan; } // Check if any I2C buses have errored // Reset the buses and restart the Frame State if ( i2c_error() ) { i2c_reset(); Pixel_FrameState = FrameState_Update; } // Only start if we haven't already // And if we've finished updating the buffers if ( Pixel_FrameState == FrameState_Sending ) goto led_finish_scan; // Only send frame to ISSI chip if buffers are ready if ( Pixel_FrameState != FrameState_Ready ) goto led_finish_scan; // Adjust frame rate (i.e. delay and do something else for a bit) Time duration = Time_duration( LED_timePrev ); if ( duration.ms < LED_framerate ) goto led_finish_scan; // FPS Display if ( LED_displayFPS ) { // Show frame calculation dbug_msg("1frame/"); printInt32( Time_ms( duration ) ); print("ms + "); printInt32( duration.ticks ); print(" ticks"); // Check if we're not meeting frame rate if ( duration.ms > LED_framerate ) { print(" - Could not meet framerate: "); printInt32( LED_framerate ); } print( NL ); } // Emulated brightness control // Lower brightness by LED_brightness #if ISSI_Chip_31FL3731_define == 1 for ( uint8_t chip = 0; chip < ISSI_Chips_define; chip++ ) { for ( uint8_t ch = 0; ch < LED_EnableBufferLength; ch++ ) { LED_pageBuffer_brightness[ chip ].ledctrl[ ch ] = LED_pageBuffer[ chip ].ledctrl[ ch ]; } for ( uint8_t ch = 0; ch < LED_BufferLength; ch++ ) { // Don't modify is 0 if ( LED_pageBuffer[ chip ].buffer[ ch ] == 0 || LED_brightness == 0 ) { LED_pageBuffer_brightness[ chip ].buffer[ ch ] = 0; continue; } // XXX (HaaTa) Yes, this is a bit slow, but it's pretty accurate LED_pageBuffer_brightness[ chip ].buffer[ ch ] = (LED_pageBuffer[ chip ].buffer[ ch ] * LED_brightness) / 0xFF; } } #endif // Update frame start time LED_timePrev = Time_now(); // Set the page of all the ISSI chips // This way we can easily link the buffers to send the brightnesses in the background for ( uint8_t ch = 0; ch < ISSI_Chips_define; ch++ ) { uint8_t bus = LED_ChannelMapping[ ch ].bus; // Page Setup LED_setupPage( bus, LED_ChannelMapping[ ch ].addr, ISSI_LEDPwmPage ); } // Send current set of buffers // Uses interrupts to send to all the ISSI chips // Pixel_FrameState will be updated when complete LED_chipSend = 0; // Start with chip 0 LED_linkedSend(); led_finish_scan: // Latency measurement end Latency_end_time( ledLatencyResource ); }
uint8_t Connect_receive_ScanCode( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num ) { // Check the directionality if ( uart_num == UART_Master ) { erro_print("Invalid ScanCode direction..."); } // Master node, trigger scan codes if ( Connect_master ) switch ( (*pending_bytes)-- ) { // Byte count always starts at 0xFFFF case 0xFFFF: // Device Id Connect_receive_ScanCodeDeviceId = byte; break; case 0xFFFE: // Number of TriggerGuides in bytes (byte * 3) *pending_bytes = byte * sizeof( TriggerGuide ); Connect_receive_ScanCodeBufferPos = 0; break; default: // Set the specific TriggerGuide entry ((uint8_t*)&Connect_receive_ScanCodeBuffer)[ Connect_receive_ScanCodeBufferPos++ ] = byte; // Reset the BufferPos if higher than sizeof TriggerGuide // And send the TriggerGuide to the Macro Module if ( Connect_receive_ScanCodeBufferPos >= sizeof( TriggerGuide ) ) { Connect_receive_ScanCodeBufferPos = 0; // Adjust ScanCode offset if ( Connect_receive_ScanCodeDeviceId > 0 ) { // Check if this node is too large if ( Connect_receive_ScanCodeDeviceId >= InterconnectNodeMax ) { warn_msg("Not enough interconnect layout nodes configured: "); printHex( Connect_receive_ScanCodeDeviceId ); print( NL ); break; } // This variable is in generatedKeymaps.h extern uint8_t InterconnectOffsetList[]; Connect_receive_ScanCodeBuffer.scanCode = Connect_receive_ScanCodeBuffer.scanCode + InterconnectOffsetList[ Connect_receive_ScanCodeDeviceId - 1 ]; } // ScanCode receive debug if ( Connect_debug ) { dbug_msg(""); printHex( Connect_receive_ScanCodeBuffer.type ); print(" "); printHex( Connect_receive_ScanCodeBuffer.state ); print(" "); printHex( Connect_receive_ScanCodeBuffer.scanCode ); print( NL ); } // Send ScanCode to macro module Macro_interconnectAdd( &Connect_receive_ScanCodeBuffer ); } break; } // Propagate ScanCode packet // XXX It would be safer to buffer the scancodes first, before transmitting the packet -Jacob // The current method is the more efficient/aggressive, but could cause issues if there were errors during transmission else switch ( (*pending_bytes)-- ) { // Byte count always starts at 0xFFFF case 0xFFFF: // Device Id { Connect_receive_ScanCodeDeviceId = byte; // Lock the master Tx buffer uart_lockTx( UART_Master ); // Send header + Id byte uint8_t header[] = { 0x16, 0x01, ScanCode, byte }; Connect_addBytes( header, sizeof( header ), UART_Master ); break; } case 0xFFFE: // Number of TriggerGuides in bytes *pending_bytes = byte * sizeof( TriggerGuide ); Connect_receive_ScanCodeBufferPos = 0; // Pass through byte Connect_addBytes( &byte, 1, UART_Master ); break; default: // Pass through byte Connect_addBytes( &byte, 1, UART_Master ); // Unlock Tx Buffer after sending last byte if ( *pending_bytes == 0 ) uart_unlockTx( UART_Master ); break; } // Check whether the scan codes have finished sending return *pending_bytes == 0 ? 1 : 0; }
uint8_t Connect_receive_CableCheck( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num ) { // Check if this is the first byte if ( *pending_bytes == 0xFFFF ) { *pending_bytes = byte; if ( Connect_debug ) { dbug_msg("PENDING SET -> "); printHex( byte ); print(" "); printHex( *pending_bytes ); print( NL ); } } // Verify byte else { (*pending_bytes)--; // The argument bytes are always 0xD2 (11010010) if ( byte != 0xD2 ) { warn_print("Cable Fault!"); // Check which side of the chain if ( uart_num == UART_Slave ) { Connect_cableFaultsSlave++; Connect_cableOkSlave = 0; print(" Slave "); } else { Connect_cableFaultsMaster++; Connect_cableOkMaster = 0; print(" Master "); } printHex( byte ); print( NL ); // Signal that the command should wait for a SYN again return 1; } else { // Check which side of the chain if ( uart_num == UART_Slave ) { Connect_cableChecksSlave++; } else { Connect_cableChecksMaster++; } } } // If cable check was successful, set cable ok if ( *pending_bytes == 0 ) { if ( uart_num == UART_Slave ) { Connect_cableOkSlave = 1; } else { Connect_cableOkMaster = 1; } } if ( Connect_debug ) { dbug_msg("CABLECHECK RECEIVE - "); printHex( byte ); print(" "); printHex( *pending_bytes ); print( NL ); } // Check whether the cable check has finished return *pending_bytes == 0 ? 1 : 0; }
// send the contents of keyboard_keys and keyboard_modifier_keys void usb_keyboard_send() { uint32_t wait_count = 0; usb_packet_t *tx_packet; // Wait till ready while ( 1 ) { if ( !usb_configuration ) { erro_print("USB not configured..."); return; } if ( USBKeys_Protocol == 0 ) // Boot Mode { if ( usb_tx_packet_count( NKRO_KEYBOARD_ENDPOINT ) < TX_PACKET_LIMIT ) { tx_packet = usb_malloc(); if ( tx_packet ) break; } } else if ( USBKeys_Protocol == 1 ) // NKRO Mode { if ( usb_tx_packet_count( KEYBOARD_ENDPOINT ) < TX_PACKET_LIMIT ) { tx_packet = usb_malloc(); if ( tx_packet ) break; } } if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout ) { transmit_previous_timeout = 1; warn_print("USB Transmit Timeout..."); return; } yield(); } // Pointer to USB tx packet buffer uint8_t *tx_buf = tx_packet->buf; switch ( USBKeys_Protocol ) { // Send boot keyboard interrupt packet(s) case 0: // USB Boot Mode debug output if ( Output_DebugMode ) { dbug_msg("Boot USB: "); printHex_op( USBKeys_Modifiers, 2 ); print(" "); printHex( 0 ); print(" "); printHex_op( USBKeys_Keys[0], 2 ); printHex_op( USBKeys_Keys[1], 2 ); printHex_op( USBKeys_Keys[2], 2 ); printHex_op( USBKeys_Keys[3], 2 ); printHex_op( USBKeys_Keys[4], 2 ); printHex_op( USBKeys_Keys[5], 2 ); print( NL ); } // Boot Mode *tx_buf++ = USBKeys_Modifiers; *tx_buf++ = 0; memcpy( tx_buf, USBKeys_Keys, USB_BOOT_MAX_KEYS ); tx_packet->len = 8; // Send USB Packet usb_tx( KEYBOARD_ENDPOINT, tx_packet ); USBKeys_Changed = USBKeyChangeState_None; break; // Send NKRO keyboard interrupts packet(s) case 1: if ( Output_DebugMode ) { dbug_msg("NKRO USB: "); } // Check system control keys if ( USBKeys_Changed & USBKeyChangeState_System ) { if ( Output_DebugMode ) { print("SysCtrl["); printHex_op( USBKeys_SysCtrl, 2 ); print( "] " NL ); } *tx_buf++ = 0x02; // ID *tx_buf = USBKeys_SysCtrl; tx_packet->len = 2; // Send USB Packet usb_tx( NKRO_KEYBOARD_ENDPOINT, tx_packet ); USBKeys_Changed &= ~USBKeyChangeState_System; // Mark sent } // Check consumer control keys if ( USBKeys_Changed & USBKeyChangeState_Consumer ) { if ( Output_DebugMode ) { print("ConsCtrl["); printHex_op( USBKeys_ConsCtrl, 2 ); print( "] " NL ); } *tx_buf++ = 0x03; // ID *tx_buf++ = (uint8_t)(USBKeys_ConsCtrl & 0x00FF); *tx_buf = (uint8_t)(USBKeys_ConsCtrl >> 8); tx_packet->len = 3; // Send USB Packet usb_tx( NKRO_KEYBOARD_ENDPOINT, tx_packet ); USBKeys_Changed &= ~USBKeyChangeState_Consumer; // Mark sent } // Standard HID Keyboard if ( USBKeys_Changed ) { // USB NKRO Debug output if ( Output_DebugMode ) { printHex_op( USBKeys_Modifiers, 2 ); print(" "); for ( uint8_t c = 0; c < 6; c++ ) printHex_op( USBKeys_Keys[ c ], 2 ); print(" "); for ( uint8_t c = 6; c < 20; c++ ) printHex_op( USBKeys_Keys[ c ], 2 ); print(" "); printHex_op( USBKeys_Keys[20], 2 ); print(" "); for ( uint8_t c = 21; c < 27; c++ ) printHex_op( USBKeys_Keys[ c ], 2 ); print( NL ); } tx_packet->len = 0; // Modifiers *tx_buf++ = 0x01; // ID *tx_buf++ = USBKeys_Modifiers; tx_packet->len += 2; // 4-49 (first 6 bytes) memcpy( tx_buf, USBKeys_Keys, 6 ); tx_buf += 6; tx_packet->len += 6; // 51-155 (Middle 14 bytes) memcpy( tx_buf, USBKeys_Keys + 6, 14 ); tx_buf += 14; tx_packet->len += 14; // 157-164 (Next byte) memcpy( tx_buf, USBKeys_Keys + 20, 1 ); tx_buf += 1; tx_packet->len += 1; // 176-221 (last 6 bytes) memcpy( tx_buf, USBKeys_Keys + 21, 6 ); tx_packet->len += 6; // Send USB Packet usb_tx( NKRO_KEYBOARD_ENDPOINT, tx_packet ); USBKeys_Changed = USBKeyChangeState_None; // Mark sent } break; } return; }
void i2c0_isr() { cli(); // Disable Interrupts uint8_t status = I2C0_S; // Read I2C Bus status // Master Mode Transmit if ( I2C0_C1 & I2C_C1_TX ) { // Check current use of the I2C bus // Currently sending data if ( I2C_TxBuffer.sequencePos > 0 ) { // Make sure slave sent an ACK if ( status & I2C_S_RXAK ) { // NACK Detected, disable interrupt erro_print("I2C NAK detected..."); I2C0_C1 = I2C_C1_IICEN; // Abort Tx Buffer I2C_TxBuffer.head = 0; I2C_TxBuffer.tail = 0; I2C_TxBuffer.sequencePos = 0; } else { // Transmit byte I2C0_D = I2C_TxBufferPop(); } } // Receiving data else if ( I2C_RxBuffer.sequencePos > 0 ) { // Master Receive, addr sent if ( status & I2C_S_ARBL ) { // Arbitration Lost erro_print("Arbitration lost..."); // TODO Abort Rx I2C0_C1 = I2C_C1_IICEN; I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt } if ( status & I2C_S_RXAK ) { // Slave Address NACK Detected, disable interrupt erro_print("Slave Address I2C NAK detected..."); // TODO Abort Rx I2C0_C1 = I2C_C1_IICEN; } else { 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 } } else { /* dbug_msg("STOP - "); printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) ); print(NL); */ // Delay around STOP to make sure it actually happens... delayMicroseconds( 1 ); I2C0_C1 = I2C_C1_IICEN; // Send STOP delayMicroseconds( 7 ); // If there is another sequence, start sending if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size ) { // Clear status flags I2C0_S = I2C_S_IICIF | I2C_S_ARBL; // Wait...till the master dies while ( I2C0_S & I2C_S_BUSY ); // Enable I2C interrupt I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX; // Transmit byte I2C0_D = I2C_TxBufferPop(); } } } // Master Mode Receive else { // XXX Do we need to handle 2nd last byte? //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv // Last byte if ( I2C_TxBuffer.sequencePos <= 1 ) { // Change to Tx mode I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX; // Grab last byte I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer ); delayMicroseconds( 1 ); // Should be enough time before issuing the stop I2C0_C1 = I2C_C1_IICEN; // Send STOP } else { // Retrieve data I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer ); } } I2C0_S = I2C_S_IICIF; // Clear interrupt sei(); // Re-enable Interrupts }
uint8_t Connect_receive_CableCheck( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num ) { // Check if this is the first byte if ( *pending_bytes == BYTE_COUNT_START ) { *pending_bytes = byte; if ( Connect_debug ) { dbug_msg("PENDING SET -> "); printHex( byte ); print(" "); printHex( *pending_bytes ); print( NL ); } } // Verify byte else { (*pending_bytes)--; // The argument bytes are always 0xD2 (11010010) if ( byte != CABLE_CHECK_ARG ) { warn_print("Cable Fault!"); // Check which side of the chain if ( uart_num == UART_Slave ) { Connect_cableFaultsSlave++; Connect_cableOkSlave = 0; print(" Slave "); } else { // Lower current requirement during errors // Half of USB negotiation minimum (50 mA) // Only if this is not the master node if ( Connect_id != 0 ) { Output_update_external_current( 50 ); } Connect_cableFaultsMaster++; Connect_cableOkMaster = 0; print(" Master "); } printHex( byte ); print( NL ); // Signal that the command should wait for a SYN again return 1; } else { // Check which side of the chain if ( uart_num == UART_Slave ) { Connect_cableChecksSlave++; } else { // If we already have an Id, then set max current again if ( Connect_id != 255 && Connect_id != 0 ) { Output_update_external_current( Output_current_available() ); } Connect_cableChecksMaster++; } } } // If cable check was successful, set cable ok if ( *pending_bytes == 0 ) { if ( uart_num == UART_Slave ) { Connect_cableOkSlave = 1; } else { Connect_cableOkMaster = 1; } } if ( Connect_debug ) { dbug_msg("CABLECHECK RECEIVE - "); printHex( byte ); print(" "); printHex( *pending_bytes ); print( NL ); } // Check whether the cable check has finished return *pending_bytes == 0 ? 1 : 0; }
inline void LED_scan() { // Latency measurement start Latency_start_time( ledLatencyResource ); // Check for current change event if ( LED_currentEvent ) { // Turn LEDs off in low power mode if ( LED_currentEvent < 150 ) { LED_enable = 0; // Pause animations and clear display Pixel_setAnimationControl( AnimationControl_WipePause ); } else { LED_enable = 1; // Start animations Pixel_setAnimationControl( AnimationControl_Forward ); } LED_currentEvent = 0; } // Check if an LED_pause is set // Some ISSI operations need a clear buffer, but still have the chip running if ( LED_pause ) goto led_finish_scan; // Check enable state if ( LED_enable ) { // Disable Hardware shutdown of ISSI chips (pull high) GPIOB_PSOR |= (1<<16); } // Only write pages to I2C if chip is enabled (i.e. Hardware shutdown is disabled) else { // Enable hardware shutdown GPIOB_PCOR |= (1<<16); goto led_finish_scan; } // Only start if we haven't already // And if we've finished updating the buffers if ( Pixel_FrameState == FrameState_Sending ) goto led_finish_scan; // Only send frame to ISSI chip if buffers are ready if ( Pixel_FrameState != FrameState_Ready ) goto led_finish_scan; // Adjust frame rate (i.e. delay and do something else for a bit) Time duration = Time_duration( LED_timePrev ); if ( duration.ms < LED_framerate ) goto led_finish_scan; // FPS Display if ( LED_displayFPS ) { // Show frame calculation dbug_msg("1frame/"); printInt32( Time_ms( duration ) ); print("ms + "); printInt32( duration.ticks ); print(" ticks"); // Check if we're not meeting frame rate if ( duration.ms > LED_framerate ) { print(" - Could not meet framerate: "); printInt32( LED_framerate ); } print( NL ); } // Emulated brightness control // Lower brightness by LED_brightness #if ISSI_Chip_31FL3731_define == 1 uint8_t inverse_brightness = 0xFF - LED_brightness; for ( uint8_t chip = 0; chip < ISSI_Chips_define; chip++ ) { for ( uint8_t ch = 0; ch < LED_BufferLength; ch++ ) { // Don't modify is 0 if ( LED_pageBuffer[ chip ].buffer[ ch ] == 0 ) { LED_pageBuffer_brightness[ chip ].buffer[ ch ] = 0; continue; } LED_pageBuffer_brightness[ chip ].buffer[ ch ] = LED_pageBuffer[ chip ].buffer[ ch ] - inverse_brightness < 0 ? 0x0 : LED_pageBuffer[ chip ].buffer[ ch ] - inverse_brightness; } } #endif // Update frame start time LED_timePrev = Time_now(); // Set the page of all the ISSI chips // This way we can easily link the buffers to send the brightnesses in the background for ( uint8_t ch = 0; ch < ISSI_Chips_define; ch++ ) { uint8_t bus = LED_ChannelMapping[ ch ].bus; // Page Setup LED_setupPage( bus, LED_ChannelMapping[ ch ].addr, ISSI_LEDPwmPage ); #if ISSI_Chip_31FL3731_define == 1 || ISSI_Chip_31FL3732_define == 1 // Reset LED enable mask // XXX At high speeds, the IS31FL3732 seems to have random bit flips // To get around this, just re-set the enable mask before each send // XXX Might be sufficient to do this every N frames though while ( i2c_send( bus, (uint16_t*)&LED_ledEnableMask[ ch ], sizeof( LED_EnableBuffer ) / 2 ) == -1 ) delay_us( ISSI_SendDelay ); #endif } // Send current set of buffers // Uses interrupts to send to all the ISSI chips // Pixel_FrameState will be updated when complete LED_chipSend = 0; // Start with chip 0 LED_linkedSend(); led_finish_scan: // Latency measurement end Latency_end_time( ledLatencyResource ); }