- Fixed cli reflash mode set - Cleaned up debugging code - 4 500 000 baud seems to be reliable - Fixed master selection (assumes slave node unless USB enumerates)connect
printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 ); | printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 ); | ||||
print( NL ); | print( NL ); | ||||
// XXX REMOVEME | |||||
/* | |||||
GPIOB_PDDR |= (1<<16); | |||||
PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOB_PSOR |= (1<<16); | |||||
// RST | |||||
GPIOC_PDDR |= (1<<8); | |||||
PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PSOR |= (1<<8); | |||||
// CS1B | |||||
GPIOC_PDDR |= (1<<4); | |||||
PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PCOR |= (1<<4); | |||||
*/ | |||||
// Backlight | |||||
/* | |||||
GPIOC_PDDR |= (1<<1); | |||||
PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PCOR |= (1<<1); | |||||
GPIOC_PDDR |= (1<<2); | |||||
PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PCOR |= (1<<2); | |||||
GPIOC_PDDR |= (1<<3); | |||||
PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PCOR |= (1<<3); | |||||
*/ | |||||
#ifdef FLASH_DEBUG | #ifdef FLASH_DEBUG | ||||
for ( uint8_t sector = 0; sector < 3; sector++ ) | for ( uint8_t sector = 0; sector < 3; sector++ ) | ||||
sector_print( &_app_rom, sector, 16 ); | sector_print( &_app_rom, sector, 16 ); |
/* Teensyduino Core Library | /* Teensyduino Core Library | ||||
* http://www.pjrc.com/teensy/ | * http://www.pjrc.com/teensy/ | ||||
* Copyright (c) 2013 PJRC.COM, LLC. | * Copyright (c) 2013 PJRC.COM, LLC. | ||||
* Modifications by Jacob Alexander (2013-2014) | |||||
* Modifications by Jacob Alexander (2013-2015) | |||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
print("CONFIGURE - "); | print("CONFIGURE - "); | ||||
#endif | #endif | ||||
usb_configuration = setup.wValue; | usb_configuration = setup.wValue; | ||||
Output_Available = usb_configuration; | |||||
reg = &USB0_ENDPT1; | reg = &USB0_ENDPT1; | ||||
cfg = usb_endpoint_config_table; | cfg = usb_endpoint_config_table; | ||||
// clear all BDT entries, free any allocated memory... | // clear all BDT entries, free any allocated memory... | ||||
SOFTWARE_RESET(); | SOFTWARE_RESET(); | ||||
} | } | ||||
// Kiibohd mk20dx256vlh7 | |||||
#elif defined(_mk20dx256vlh7_) | |||||
// Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode | |||||
for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ ) | |||||
(&VBAT)[ pos ] = sys_reset_to_loader_magic[ pos ]; | |||||
SOFTWARE_RESET(); | |||||
// Teensy 3.0 and 3.1 | // Teensy 3.0 and 3.1 | ||||
#else | #else | ||||
asm volatile("bkpt"); | asm volatile("bkpt"); | ||||
print("USB INIT"NL); | print("USB INIT"NL); | ||||
#endif | #endif | ||||
// If no USB cable is attached, do not initialize usb | |||||
// XXX Test -HaaTa | |||||
//if ( USB0_OTGISTAT & USB_OTGSTAT_ID ) | |||||
// return 0; | |||||
// Clear out endpoints table | // Clear out endpoints table | ||||
for ( int i = 0; i <= NUM_ENDPOINTS * 4; i++ ) | for ( int i = 0; i <= NUM_ENDPOINTS * 4; i++ ) | ||||
{ | { |
// Which modifier keys are currently pressed | // Which modifier keys are currently pressed | ||||
// 1=left ctrl, 2=left shift, 4=left alt, 8=left gui | // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui | ||||
// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui | // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui | ||||
uint8_t USBKeys_Modifiers = 0; | |||||
uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer | |||||
uint8_t USBKeys_Modifiers = 0; | |||||
uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer | |||||
// Currently pressed keys, max is defined by USB_MAX_KEY_SEND | // Currently pressed keys, max is defined by USB_MAX_KEY_SEND | ||||
uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS]; | |||||
uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer | |||||
uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS]; | |||||
uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer | |||||
// System Control and Consumer Control 1KRO containers | // System Control and Consumer Control 1KRO containers | ||||
uint8_t USBKeys_SysCtrl; | |||||
uint16_t USBKeys_ConsCtrl; | |||||
uint8_t USBKeys_SysCtrl; | |||||
uint16_t USBKeys_ConsCtrl; | |||||
// The number of keys sent to the usb in the array | // The number of keys sent to the usb in the array | ||||
uint8_t USBKeys_Sent = 0; | |||||
uint8_t USBKeys_SentCLI = 0; | |||||
uint8_t USBKeys_Sent = 0; | |||||
uint8_t USBKeys_SentCLI = 0; | |||||
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana | // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana | ||||
volatile uint8_t USBKeys_LEDs = 0; | volatile uint8_t USBKeys_LEDs = 0; | ||||
// the idle configuration, how often we send the report to the | // the idle configuration, how often we send the report to the | ||||
// host (ms * 4) even when it hasn't changed | // host (ms * 4) even when it hasn't changed | ||||
uint8_t USBKeys_Idle_Config = 125; | |||||
uint8_t USBKeys_Idle_Config = 125; | |||||
// count until idle timeout | // count until idle timeout | ||||
uint8_t USBKeys_Idle_Count = 0; | |||||
uint8_t USBKeys_Idle_Count = 0; | |||||
// Indicates whether the Output module is fully functional | // Indicates whether the Output module is fully functional | ||||
// 0 - Not fully functional, 1 - Fully functional | // 0 - Not fully functional, 1 - Fully functional | ||||
// 0 is often used to show that a USB cable is not plugged in (but has power) | // 0 is often used to show that a USB cable is not plugged in (but has power) | ||||
uint8_t Output_Available = 0; | |||||
volatile uint8_t Output_Available = 0; | |||||
// Debug control variable for Output modules | // Debug control variable for Output modules | ||||
// 0 - Debug disabled (default) | // 0 - Debug disabled (default) | ||||
// 1 - Debug enabled | // 1 - Debug enabled | ||||
uint8_t Output_DebugMode = 0; | |||||
uint8_t Output_DebugMode = 0; | |||||
// USB Module Setup | // USB Module Setup | ||||
inline void Output_setup() | inline void Output_setup() | ||||
{ | { | ||||
// Initialize the USB, and then wait for the host to set configuration. | |||||
// This will hang forever if USB does not initialize | |||||
// If no USB cable is attached, does not try and initialize USB | |||||
if ( usb_init() ) | |||||
{ | |||||
while ( !usb_configured() ); | |||||
} | |||||
// Initialize the USB | |||||
// If a USB connection does not exist, just ignore it | |||||
// All usb related functions will non-fatally fail if called | |||||
// If the USB initialization is delayed, then functionality will just be delayed | |||||
usb_init(); | |||||
// Register USB Output CLI dictionary | // Register USB Output CLI dictionary | ||||
CLI_registerDictionary( outputCLIDict, outputCLIDictName ); | CLI_registerDictionary( outputCLIDict, outputCLIDictName ); |
extern USBKeyChangeState USBKeys_Changed; | extern USBKeyChangeState USBKeys_Changed; | ||||
extern uint8_t Output_Available; // 0 - Output module not fully functional, 1 - Output module working | |||||
extern volatile uint8_t Output_Available; // 0 - Output module not fully functional, 1 - Output module working | |||||
extern uint8_t Output_DebugMode; // 0 - Debug disabled, 1 - Debug enabled | extern uint8_t Output_DebugMode; // 0 - Debug disabled, 1 - Debug enabled | ||||
// ----- Function Declarations ----- | // ----- Function Declarations ----- | ||||
void cliFunc_kbdProtocol( char* args ); | void cliFunc_kbdProtocol( char* args ); | ||||
void cliFunc_outputDebug( char* args ); | |||||
void cliFunc_readLEDs ( char* args ); | void cliFunc_readLEDs ( char* args ); | ||||
void cliFunc_readUART ( char* args ); | void cliFunc_readUART ( char* args ); | ||||
void cliFunc_sendKeys ( char* args ); | void cliFunc_sendKeys ( char* args ); | ||||
// Output Module command dictionary | // Output Module command dictionary | ||||
CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode" ); | CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode" ); | ||||
CLIDict_Entry( outputDebug, "Toggle Output Debug mode." ); | |||||
CLIDict_Entry( readLEDs, "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." ); | CLIDict_Entry( readLEDs, "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." ); | ||||
CLIDict_Entry( readUART, "Read UART buffer until empty." ); | CLIDict_Entry( readUART, "Read UART buffer until empty." ); | ||||
CLIDict_Entry( sendKeys, "Send the prepared list of USB codes and modifier byte." ); | CLIDict_Entry( sendKeys, "Send the prepared list of USB codes and modifier byte." ); | ||||
CLIDict_Def( outputCLIDict, "USB Module Commands" ) = { | CLIDict_Def( outputCLIDict, "USB Module Commands" ) = { | ||||
CLIDict_Item( kbdProtocol ), | CLIDict_Item( kbdProtocol ), | ||||
CLIDict_Item( outputDebug ), | |||||
CLIDict_Item( readLEDs ), | CLIDict_Item( readLEDs ), | ||||
CLIDict_Item( readUART ), | CLIDict_Item( readUART ), | ||||
CLIDict_Item( sendKeys ), | CLIDict_Item( sendKeys ), | ||||
// Which modifier keys are currently pressed | // Which modifier keys are currently pressed | ||||
// 1=left ctrl, 2=left shift, 4=left alt, 8=left gui | // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui | ||||
// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui | // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui | ||||
uint8_t USBKeys_Modifiers = 0; | |||||
uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer | |||||
uint8_t USBKeys_Modifiers = 0; | |||||
uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer | |||||
// Currently pressed keys, max is defined by USB_MAX_KEY_SEND | // Currently pressed keys, max is defined by USB_MAX_KEY_SEND | ||||
uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS]; | |||||
uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer | |||||
uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS]; | |||||
uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer | |||||
// System Control and Consumer Control 1KRO containers | // System Control and Consumer Control 1KRO containers | ||||
uint8_t USBKeys_SysCtrl; | |||||
uint16_t USBKeys_ConsCtrl; | |||||
uint8_t USBKeys_SysCtrl; | |||||
uint16_t USBKeys_ConsCtrl; | |||||
// The number of keys sent to the usb in the array | // The number of keys sent to the usb in the array | ||||
uint8_t USBKeys_Sent = 0; | |||||
uint8_t USBKeys_SentCLI = 0; | |||||
uint8_t USBKeys_Sent = 0; | |||||
uint8_t USBKeys_SentCLI = 0; | |||||
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana | // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana | ||||
volatile uint8_t USBKeys_LEDs = 0; | volatile uint8_t USBKeys_LEDs = 0; | ||||
// Protocol setting from the host. | // Protocol setting from the host. | ||||
// 0 - Boot Mode | // 0 - Boot Mode | ||||
// 1 - NKRO Mode (Default, unless set by a BIOS or boot interface) | // 1 - NKRO Mode (Default, unless set by a BIOS or boot interface) | ||||
volatile uint8_t USBKeys_Protocol = 0; | |||||
volatile uint8_t USBKeys_Protocol = 1; | |||||
// Indicate if USB should send update | // Indicate if USB should send update | ||||
// OS only needs update if there has been a change in state | // OS only needs update if there has been a change in state | ||||
// the idle configuration, how often we send the report to the | // the idle configuration, how often we send the report to the | ||||
// host (ms * 4) even when it hasn't changed | // host (ms * 4) even when it hasn't changed | ||||
uint8_t USBKeys_Idle_Config = 125; | |||||
uint8_t USBKeys_Idle_Config = 125; | |||||
// count until idle timeout | // count until idle timeout | ||||
uint8_t USBKeys_Idle_Count = 0; | |||||
uint8_t USBKeys_Idle_Count = 0; | |||||
// Indicates whether the Output module is fully functional | // Indicates whether the Output module is fully functional | ||||
// 0 - Not fully functional, 1 - Fully functional | // 0 - Not fully functional, 1 - Fully functional | ||||
// 0 is often used to show that a USB cable is not plugged in (but has power) | // 0 is often used to show that a USB cable is not plugged in (but has power) | ||||
uint8_t Output_Available = 0; | |||||
volatile uint8_t Output_Available = 0; | |||||
// Debug control variable for Output modules | // Debug control variable for Output modules | ||||
// 0 - Debug disabled (default) | // 0 - Debug disabled (default) | ||||
// 1 - Debug enabled | // 1 - Debug enabled | ||||
uint8_t Output_DebugMode = 0; | |||||
uint8_t Output_DebugMode = 0; | |||||
// Only send keypresses if press or hold state | // Only send keypresses if press or hold state | ||||
if ( stateType == 0x00 && state == 0x03 ) // Release state | if ( stateType == 0x00 && state == 0x03 ) // Release state | ||||
{ | |||||
USBKeys_ConsCtrl = 0; | |||||
return; | return; | ||||
} | |||||
// Set consumer control code | // Set consumer control code | ||||
USBKeys_ConsCtrl = *(uint16_t*)(&args[0]); | USBKeys_ConsCtrl = *(uint16_t*)(&args[0]); | ||||
// Only send keypresses if press or hold state | // Only send keypresses if press or hold state | ||||
if ( stateType == 0x00 && state == 0x03 ) // Release state | if ( stateType == 0x00 && state == 0x03 ) // Release state | ||||
{ | |||||
USBKeys_SysCtrl = 0; | |||||
return; | return; | ||||
} | |||||
// Set system control code | // Set system control code | ||||
USBKeys_SysCtrl = args[0]; | USBKeys_SysCtrl = args[0]; | ||||
// Depending on which mode the keyboard is in, USBKeys_Keys array is used differently | // Depending on which mode the keyboard is in, USBKeys_Keys array is used differently | ||||
// Boot mode - Maximum of 6 byte codes | // Boot mode - Maximum of 6 byte codes | ||||
// NKRO mode - Each bit of the 26 byte corresponds to a key | // NKRO mode - Each bit of the 26 byte corresponds to a key | ||||
// Bits 0 - 160 (first 20 bytes) correspond to USB Codes 4 - 164 | |||||
// Bits 161 - 205 (last 6 bytes) correspond to USB Codes 176 - 221 | |||||
// Bits 206 - 208 (last byte) correspond to the 3 padded bits in USB (unused) | |||||
// Bits 0 - 45 (bytes 0 - 5) correspond to USB Codes 4 - 49 (Main) | |||||
// Bits 48 - 161 (bytes 6 - 20) correspond to USB Codes 51 - 164 (Secondary) | |||||
// Bits 168 - 213 (bytes 21 - 26) correspond to USB Codes 176 - 221 (Tertiary) | |||||
// Bits 214 - 216 unused | |||||
uint8_t bytePosition = 0; | uint8_t bytePosition = 0; | ||||
uint8_t byteShift = 0; | uint8_t byteShift = 0; | ||||
switch ( USBKeys_Protocol ) | switch ( USBKeys_Protocol ) | ||||
USBKeys_Changed |= USBKeyChangeState_Modifiers; | USBKeys_Changed |= USBKeyChangeState_Modifiers; | ||||
break; | break; | ||||
} | } | ||||
// First 20 bytes | |||||
else if ( key >= 4 && key <= 164 ) | |||||
// First 6 bytes | |||||
else if ( key >= 4 && key <= 49 ) | |||||
{ | { | ||||
// Lookup (otherwise division or multiple checks are needed to do alignment) | // Lookup (otherwise division or multiple checks are needed to do alignment) | ||||
uint8_t keyPos = key - 4; // Starting position in array | |||||
// Starting at 0th position, each byte has 8 bits, starting at 4th bit | |||||
uint8_t keyPos = key + (0 * 8 - 4); // Starting position in array, Ignoring 4 keys | |||||
switch ( keyPos ) | switch ( keyPos ) | ||||
{ | { | ||||
byteLookup( 0 ); | byteLookup( 0 ); | ||||
byteLookup( 3 ); | byteLookup( 3 ); | ||||
byteLookup( 4 ); | byteLookup( 4 ); | ||||
byteLookup( 5 ); | byteLookup( 5 ); | ||||
} | |||||
USBKeys_Changed |= USBKeyChangeState_MainKeys; | |||||
} | |||||
// Next 14 bytes | |||||
else if ( key >= 51 && key <= 155 ) | |||||
{ | |||||
// Lookup (otherwise division or multiple checks are needed to do alignment) | |||||
// Starting at 6th byte position, each byte has 8 bits, starting at 51st bit | |||||
uint8_t keyPos = key + (6 * 8 - 51); // Starting position in array | |||||
switch ( keyPos ) | |||||
{ | |||||
byteLookup( 6 ); | byteLookup( 6 ); | ||||
byteLookup( 7 ); | byteLookup( 7 ); | ||||
byteLookup( 8 ); | byteLookup( 8 ); | ||||
byteLookup( 19 ); | byteLookup( 19 ); | ||||
} | } | ||||
USBKeys_Changed |= USBKeyChangeState_MainKeys; | |||||
USBKeys_Changed |= USBKeyChangeState_SecondaryKeys; | |||||
} | |||||
// Next byte | |||||
else if ( key >= 157 && key <= 164 ) | |||||
{ | |||||
// Lookup (otherwise division or multiple checks are needed to do alignment) | |||||
uint8_t keyPos = key + (20 * 8 - 157); // Starting position in array, Ignoring 6 keys | |||||
switch ( keyPos ) | |||||
{ | |||||
byteLookup( 20 ); | |||||
} | |||||
USBKeys_Changed |= USBKeyChangeState_TertiaryKeys; | |||||
} | } | ||||
// Last 6 bytes | // Last 6 bytes | ||||
else if ( key >= 176 && key <= 221 ) | else if ( key >= 176 && key <= 221 ) | ||||
{ | { | ||||
// Lookup (otherwise division or multiple checks are needed to do alignment) | // Lookup (otherwise division or multiple checks are needed to do alignment) | ||||
uint8_t keyPos = key - 176; // Starting position in array | |||||
uint8_t keyPos = key + (21 * 8 - 176); // Starting position in array | |||||
switch ( keyPos ) | switch ( keyPos ) | ||||
{ | { | ||||
byteLookup( 20 ); | |||||
byteLookup( 21 ); | byteLookup( 21 ); | ||||
byteLookup( 22 ); | byteLookup( 22 ); | ||||
byteLookup( 23 ); | byteLookup( 23 ); | ||||
byteLookup( 24 ); | byteLookup( 24 ); | ||||
byteLookup( 25 ); | byteLookup( 25 ); | ||||
byteLookup( 26 ); | |||||
} | } | ||||
USBKeys_Changed |= USBKeyChangeState_SecondaryKeys; | |||||
USBKeys_Changed |= USBKeyChangeState_QuartiaryKeys; | |||||
} | |||||
// Received 0x00 | |||||
// This is a special USB Code that internally indicates a "break" | |||||
// It is used to send "nothing" in order to break up sequences of USB Codes | |||||
else if ( key == 0x00 ) | |||||
{ | |||||
USBKeys_Changed |= USBKeyChangeState_MainKeys; | |||||
// Also flush out buffers just in case | |||||
Output_flushBuffers(); | |||||
break; | |||||
} | } | ||||
// Invalid key | // Invalid key | ||||
else | else | ||||
{ | { | ||||
warn_msg("USB Code not within 4-164 (0x4-0xA4) or 176-221 (0xB0-0xDD) NKRO Mode: "); | |||||
warn_msg("USB Code not within 4-49 (0x4-0x31), 51-155 (0x33-0x9B), 157-164 (0x9D-0xA4), 176-221 (0xB0-0xDD) or 224-231 (0xE0-0xE7) NKRO Mode: "); | |||||
printHex( key ); | printHex( key ); | ||||
print( NL ); | print( NL ); | ||||
break; | break; | ||||
{ | { | ||||
// Setup UART | // Setup UART | ||||
uart_serial_setup(); | uart_serial_setup(); | ||||
print("\033[2J"); // Clear screen | |||||
// Initialize the USB, and then wait for the host to set configuration. | |||||
// This will hang forever if USB does not initialize | |||||
// Initialize the USB | |||||
// If a USB connection does not exist, just ignore it | |||||
// All usb related functions will non-fatally fail if called | |||||
// If the USB initialization is delayed, then functionality will just be delayed | |||||
usb_init(); | usb_init(); | ||||
while ( !usb_configured() ); | |||||
// Register USB Output CLI dictionary | // Register USB Output CLI dictionary | ||||
CLI_registerDictionary( outputCLIDict, outputCLIDictName ); | CLI_registerDictionary( outputCLIDict, outputCLIDictName ); | ||||
// Zero out USBKeys_Keys array | |||||
for ( uint8_t c = 0; c < USB_NKRO_BITFIELD_SIZE_KEYS; c++ ) | |||||
USBKeys_Keys[ c ] = 0; | |||||
// Flush key buffers | |||||
Output_flushBuffers(); | |||||
} | } | ||||
while ( USBKeys_Changed ) | while ( USBKeys_Changed ) | ||||
usb_keyboard_send(); | usb_keyboard_send(); | ||||
// Clear modifiers and keys | |||||
USBKeys_Modifiers = 0; | |||||
USBKeys_Sent = 0; | |||||
// Clear keys sent | |||||
USBKeys_Sent = 0; | |||||
// Signal Scan Module we are finished | // Signal Scan Module we are finished | ||||
switch ( USBKeys_Protocol ) | switch ( USBKeys_Protocol ) | ||||
{ | { | ||||
case 0: // Boot Mode | case 0: // Boot Mode | ||||
// Clear modifiers only in boot mode | |||||
USBKeys_Modifiers = 0; | |||||
Scan_finishedWithOutput( USBKeys_Sent <= USB_BOOT_MAX_KEYS ? USBKeys_Sent : USB_BOOT_MAX_KEYS ); | Scan_finishedWithOutput( USBKeys_Sent <= USB_BOOT_MAX_KEYS ? USBKeys_Sent : USB_BOOT_MAX_KEYS ); | ||||
break; | break; | ||||
case 1: // NKRO Mode | case 1: // NKRO Mode | ||||
// Sets the device into firmware reload mode | // Sets the device into firmware reload mode | ||||
inline void Output_firmwareReload() | |||||
void Output_firmwareReload() | |||||
{ | { | ||||
uart_device_reload(); | |||||
usb_device_reload(); | |||||
} | } | ||||
} | } | ||||
void cliFunc_outputDebug( char* args ) | |||||
{ | |||||
// Parse number from argument | |||||
// NOTE: Only first argument is used | |||||
char* arg1Ptr; | |||||
char* arg2Ptr; | |||||
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr ); | |||||
// Default to 1 if no argument is given | |||||
Output_DebugMode = 1; | |||||
if ( arg1Ptr[0] != '\0' ) | |||||
{ | |||||
Output_DebugMode = (uint16_t)numToInt( arg1Ptr ); | |||||
} | |||||
} | |||||
void cliFunc_readLEDs( char* args ) | void cliFunc_readLEDs( char* args ) | ||||
{ | { | ||||
print( NL ); | print( NL ); |
// Initialize SPI | // Initialize SPI | ||||
SPI_setup(); | SPI_setup(); | ||||
// Setup Register Control Signal (A0) | // Setup Register Control Signal (A0) | ||||
// Start in display register mode (1) | // Start in display register mode (1) | ||||
GPIOC_PDDR |= (1<<7); | GPIOC_PDDR |= (1<<7); | ||||
// Run LCD intialization sequence | // Run LCD intialization sequence | ||||
LCD_initialize(); | LCD_initialize(); | ||||
// Setup Backlight | |||||
// TODO Expose default settings | |||||
// TODO Setup PWM | |||||
GPIOC_PDDR |= (1<<1); | |||||
PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PCOR |= (1<<1); | |||||
GPIOC_PDDR |= (1<<2); | |||||
PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PCOR |= (1<<2); | |||||
GPIOC_PDDR |= (1<<3); | |||||
PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | |||||
GPIOC_PCOR |= (1<<3); | |||||
} | } | ||||
# Thus baud setting = 26 | # Thus baud setting = 26 | ||||
# NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet | # NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet | ||||
# Baud fine setting = 0x02 | # Baud fine setting = 0x02 | ||||
UARTConnectBaud = 26; | |||||
UARTConnectBaudFine = 0x02; | |||||
UARTConnectBaud = 1; # 4.5 Mbps @ 72 MHz | |||||
UARTConnectBaudFine = 0x0; | |||||
# Cable Check Command Length | |||||
# This defines the length of the cable command | |||||
# 0xD2 11010010 is used for each check byte | |||||
# | |||||
# For example: | |||||
# Length: 4 | |||||
# Args: 0xD2 0xD2 0xD2 0xD2 | |||||
UARTConnectCableCheckLength => UARTConnectCableCheckLength_define; | |||||
UARTConnectCableCheckLength = 2; | |||||
} \ | } \ | ||||
for ( uint8_t c = 0; c < count; c++ ) \ | for ( uint8_t c = 0; c < count; c++ ) \ | ||||
{ \ | { \ | ||||
printHex( buffer[ c ] ); \ | |||||
print( " +" #uartNum NL ); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
printHex( buffer[ c ] ); \ | |||||
print( " +" #uartNum NL ); \ | |||||
} \ | |||||
uart##uartNum##_buffer[ uart##uartNum##_buffer_tail++ ] = buffer[ c ]; \ | uart##uartNum##_buffer[ uart##uartNum##_buffer_tail++ ] = buffer[ c ]; \ | ||||
uart##uartNum##_buffer_items++; \ | uart##uartNum##_buffer_items++; \ | ||||
if ( uart##uartNum##_buffer_tail >= uart_buffer_size ) \ | if ( uart##uartNum##_buffer_tail >= uart_buffer_size ) \ | ||||
while ( available-- > 0 ) \ | while ( available-- > 0 ) \ | ||||
{ \ | { \ | ||||
uint8_t byteRead = UART##uartNum##_D; \ | uint8_t byteRead = UART##uartNum##_D; \ | ||||
printHex( byteRead ); \ | |||||
print( "(" ); \ | |||||
printInt8( available ); \ | |||||
print( ") <-" ); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
printHex( byteRead ); \ | |||||
print( "(" ); \ | |||||
printInt8( available ); \ | |||||
print( ") <-" ); \ | |||||
} \ | |||||
switch ( uart##uartNum##_rx_status ) \ | switch ( uart##uartNum##_rx_status ) \ | ||||
{ \ | { \ | ||||
case UARTStatus_Wait: \ | case UARTStatus_Wait: \ | ||||
print(" SYN "); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
print(" SYN "); \ | |||||
} \ | |||||
uart##uartNum##_rx_status = byteRead == 0x16 ? UARTStatus_SYN : UARTStatus_Wait; \ | uart##uartNum##_rx_status = byteRead == 0x16 ? UARTStatus_SYN : UARTStatus_Wait; \ | ||||
break; \ | break; \ | ||||
case UARTStatus_SYN: \ | case UARTStatus_SYN: \ | ||||
print(" SOH "); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
print(" SOH "); \ | |||||
} \ | |||||
uart##uartNum##_rx_status = byteRead == 0x01 ? UARTStatus_SOH : UARTStatus_Wait; \ | uart##uartNum##_rx_status = byteRead == 0x01 ? UARTStatus_SOH : UARTStatus_Wait; \ | ||||
break; \ | break; \ | ||||
case UARTStatus_SOH: \ | case UARTStatus_SOH: \ | ||||
{ \ | { \ | ||||
print(" CMD "); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
print(" CMD "); \ | |||||
} \ | |||||
uint8_t byte = byteRead; \ | uint8_t byte = byteRead; \ | ||||
if ( byte <= Animation ) \ | if ( byte <= Animation ) \ | ||||
{ \ | { \ | ||||
uart##uartNum##_rx_status = UARTStatus_Wait; \ | uart##uartNum##_rx_status = UARTStatus_Wait; \ | ||||
break; \ | break; \ | ||||
default: \ | default: \ | ||||
print("###"); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
print("###"); \ | |||||
} \ | |||||
break; \ | break; \ | ||||
} \ | } \ | ||||
break; \ | break; \ | ||||
} \ | } \ | ||||
case UARTStatus_Command: \ | case UARTStatus_Command: \ | ||||
{ \ | { \ | ||||
print(" CMD "); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
print(" CMD "); \ | |||||
} \ | |||||
uint8_t (*rcvFunc)(uint8_t, uint16_t(*), uint8_t) = (uint8_t(*)(uint8_t, uint16_t(*), uint8_t))(Connect_receiveFunctions[ uart##uartNum##_rx_command ]); \ | uint8_t (*rcvFunc)(uint8_t, uint16_t(*), uint8_t) = (uint8_t(*)(uint8_t, uint16_t(*), uint8_t))(Connect_receiveFunctions[ uart##uartNum##_rx_command ]); \ | ||||
if ( rcvFunc( byteRead, (uint16_t*)&uart##uartNum##_rx_bytes_waiting, uartNum ) ) \ | if ( rcvFunc( byteRead, (uint16_t*)&uart##uartNum##_rx_bytes_waiting, uartNum ) ) \ | ||||
uart##uartNum##_rx_status = UARTStatus_Wait; \ | uart##uartNum##_rx_status = UARTStatus_Wait; \ | ||||
available++; \ | available++; \ | ||||
continue; \ | continue; \ | ||||
} \ | } \ | ||||
print( NL ); \ | |||||
if ( Connect_debug ) \ | |||||
{ \ | |||||
print( NL ); \ | |||||
} \ | |||||
} \ | } \ | ||||
} | } | ||||
uint8_t Connect_master = 0; | uint8_t Connect_master = 0; | ||||
// -- Control Variables -- | |||||
uint32_t Connect_lastCheck = 0; // Cable Check scheduler | |||||
uint8_t Connect_debug = 0; // Set 1 for debug | |||||
// -- Rx Status Variables -- | // -- Rx Status Variables -- | ||||
volatile UARTStatus uart0_rx_status; | volatile UARTStatus uart0_rx_status; | ||||
// Check if this is the first byte | // Check if this is the first byte | ||||
if ( *pending_bytes == 0xFFFF ) | if ( *pending_bytes == 0xFFFF ) | ||||
{ | { | ||||
dbug_msg("PENDING SET -> "); | |||||
printHex( byte ); | |||||
print(" "); | |||||
*pending_bytes = byte; | *pending_bytes = byte; | ||||
printHex( *pending_bytes ); | |||||
print( NL ); | |||||
if ( Connect_debug ) | |||||
{ | |||||
dbug_msg("PENDING SET -> "); | |||||
printHex( byte ); | |||||
print(" "); | |||||
printHex( *pending_bytes ); | |||||
print( NL ); | |||||
} | |||||
} | } | ||||
// Verify byte | // Verify byte | ||||
else | else | ||||
Connect_cableOkSlave = 1; | Connect_cableOkSlave = 1; | ||||
} | } | ||||
} | } | ||||
dbug_msg("CABLECHECK RECEIVE - "); | |||||
printHex( byte ); | |||||
print(" "); | |||||
printHex( *pending_bytes ); | |||||
print(NL); | |||||
if ( Connect_debug ) | |||||
{ | |||||
dbug_msg("CABLECHECK RECEIVE - "); | |||||
printHex( byte ); | |||||
print(" "); | |||||
printHex( *pending_bytes ); | |||||
print( NL ); | |||||
} | |||||
// Check whether the cable check has finished | // Check whether the cable check has finished | ||||
return *pending_bytes == 0 ? 1 : 0; | return *pending_bytes == 0 ? 1 : 0; | ||||
{ | { | ||||
dbug_print("IdRequest"); | dbug_print("IdRequest"); | ||||
// Check the directionality | // Check the directionality | ||||
if ( !to_master ) | |||||
if ( to_master ) | |||||
{ | { | ||||
erro_print("Invalid IdRequest direction..."); | erro_print("Invalid IdRequest direction..."); | ||||
} | } | ||||
{ | { | ||||
dbug_print("IdEnumeration"); | dbug_print("IdEnumeration"); | ||||
// Check the directionality | // Check the directionality | ||||
if ( to_master ) | |||||
if ( !to_master ) | |||||
{ | { | ||||
erro_print("Invalid IdEnumeration direction..."); | erro_print("Invalid IdEnumeration direction..."); | ||||
} | } | ||||
{ | { | ||||
dbug_print("IdReport"); | dbug_print("IdReport"); | ||||
// Check the directionality | // Check the directionality | ||||
if ( !to_master ) | |||||
if ( to_master ) | |||||
{ | { | ||||
erro_print("Invalid IdRequest direction..."); | erro_print("Invalid IdRequest direction..."); | ||||
} | } | ||||
// Register Connect CLI dictionary | // Register Connect CLI dictionary | ||||
CLI_registerDictionary( uartConnectCLIDict, uartConnectCLIDictName ); | CLI_registerDictionary( uartConnectCLIDict, uartConnectCLIDictName ); | ||||
// Check if master | |||||
Connect_master = master; | Connect_master = master; | ||||
if ( Connect_master ) | |||||
Connect_id = 0; // 0x00 is always the master Id | |||||
// Master / UART0 setup | // Master / UART0 setup | ||||
// Slave / UART1 setup | // Slave / UART1 setup | ||||
SIM_SCGC4 |= SIM_SCGC4_UART1; // Disable clock gating | SIM_SCGC4 |= SIM_SCGC4_UART1; // Disable clock gating | ||||
// Pin Setup for UART0 / UART1 | // Pin Setup for UART0 / UART1 | ||||
// XXX TODO Set to actual (Teensy 3.1s don't have the correct pins available) | |||||
PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin | |||||
PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin | |||||
PORTC_PCR3 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin | |||||
PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin | |||||
//PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin | |||||
//PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin | |||||
//PORTE_PCR0 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin | |||||
//PORTE_PCR1 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin | |||||
PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin | |||||
PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin | |||||
PORTE_PCR0 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin | |||||
PORTE_PCR1 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin | |||||
// Baud Rate setting | // Baud Rate setting | ||||
UART0_BDH = (uint8_t)(Connect_baud >> 8); | UART0_BDH = (uint8_t)(Connect_baud >> 8); | ||||
// - SyncEvent is also blocking until sent | // - SyncEvent is also blocking until sent | ||||
void Connect_scan() | void Connect_scan() | ||||
{ | { | ||||
// Check if Tx Buffers are empty and the Tx Ring buffers have data to send | |||||
// This happens if there was previously nothing to send | |||||
if ( uart0_buffer_items > 0 && UART0_TCFIFO == 0 ) | |||||
uart_fillTxFifo( 0 ); | |||||
if ( uart1_buffer_items > 0 && UART1_TCFIFO == 0 ) | |||||
uart_fillTxFifo( 1 ); | |||||
// Check if initially configured as a slave and usb comes up | |||||
// Then reconfigure as a master | |||||
if ( !Connect_master && Output_Available ) | |||||
{ | |||||
Connect_setup( Output_Available ); | |||||
} | |||||
// Limit how often we do cable checks | |||||
uint32_t time_compare = 0x7FF; // Must be all 1's, 0x3FF is valid, 0x4FF is not | |||||
uint32_t current_time = systick_millis_count; | |||||
if ( Connect_lastCheck != current_time | |||||
&& ( current_time & time_compare ) == time_compare | |||||
) | |||||
{ | |||||
// Make sure we don't double check if the clock speed is too high | |||||
Connect_lastCheck = current_time; | |||||
// Send a cable check command of 2 bytes | |||||
Connect_send_CableCheck( UARTConnectCableCheckLength_define ); | |||||
// If this is a slave, and we don't have an id yeth | |||||
// Don't bother sending if there are cable issues | |||||
if ( !Connect_master && Connect_id == 0xFF && Connect_cableOkMaster ) | |||||
{ | |||||
Connect_send_IdRequest(); | |||||
} | |||||
} | |||||
// Only process commands if uarts have been configured | |||||
if ( uarts_configured ) | |||||
{ | |||||
// Check if Tx Buffers are empty and the Tx Ring buffers have data to send | |||||
// This happens if there was previously nothing to send | |||||
if ( uart0_buffer_items > 0 && UART0_TCFIFO == 0 ) | |||||
uart_fillTxFifo( 0 ); | |||||
if ( uart1_buffer_items > 0 && UART1_TCFIFO == 0 ) | |||||
uart_fillTxFifo( 1 ); | |||||
} | |||||
} | } | ||||
switch ( numToInt( &arg1Ptr[0] ) ) | switch ( numToInt( &arg1Ptr[0] ) ) | ||||
{ | { | ||||
case CableCheck: | case CableCheck: | ||||
Connect_send_CableCheck( 2 ); | |||||
Connect_send_CableCheck( UARTConnectCableCheckLength_define ); | |||||
break; | break; | ||||
case IdRequest: | case IdRequest: |
UARTStatus_SYN = 1, // Rx: SYN Received, waiting for SOH | UARTStatus_SYN = 1, // Rx: SYN Received, waiting for SOH | ||||
UARTStatus_SOH = 2, // Rx: SOH Received, waiting for Command | UARTStatus_SOH = 2, // Rx: SOH Received, waiting for Command | ||||
UARTStatus_Command = 3, // Rx: Command Received, waiting for data | UARTStatus_Command = 3, // Rx: Command Received, waiting for data | ||||
UARTStatus_Ready = 4, // Tx: Ready to receive commands | |||||
UARTStatus_Ready = 4, // Tx: Ready to send commands | |||||
} UARTStatus; | } UARTStatus; | ||||