]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Scan/UARTConnect/connect_scan.c
Fixing CMake dependency checking for kll_defs.h
[kiibohd-controller.git] / Scan / UARTConnect / connect_scan.c
index 03691a36b930d6a5b37467db72e7ea1fb1ff32f8..587a5e7c3b0ed783f79c1854c6fc2a16a8472d7b 100644 (file)
@@ -21,6 +21,7 @@
 
 // Project Includes
 #include <cli.h>
+#include <kll_defs.h>
 #include <led.h>
 #include <print.h>
 #include <macro.h>
@@ -42,8 +43,11 @@ case uartNum: \
        } \
        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_items++; \
                if ( uart##uartNum##_buffer_tail >= uart_buffer_size ) \
@@ -87,23 +91,35 @@ case uartNum: \
        while ( available-- > 0 ) \
        { \
                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 ) \
                { \
                case UARTStatus_Wait: \
-                       print(" SYN "); \
+                       if ( Connect_debug ) \
+                       { \
+                               print(" SYN "); \
+                       } \
                        uart##uartNum##_rx_status = byteRead == 0x16 ? UARTStatus_SYN : UARTStatus_Wait; \
                        break; \
                case UARTStatus_SYN: \
-                       print(" SOH "); \
+                       if ( Connect_debug ) \
+                       { \
+                               print(" SOH "); \
+                       } \
                        uart##uartNum##_rx_status = byteRead == 0x01 ? UARTStatus_SOH : UARTStatus_Wait; \
                        break; \
                case UARTStatus_SOH: \
                { \
-                       print(" CMD "); \
+                       if ( Connect_debug ) \
+                       { \
+                               print(" CMD "); \
+                       } \
                        uint8_t byte = byteRead; \
                        if ( byte <= Animation ) \
                        { \
@@ -122,14 +138,20 @@ case uartNum: \
                                uart##uartNum##_rx_status = UARTStatus_Wait; \
                                break; \
                        default: \
-                               print("###"); \
+                               if ( Connect_debug ) \
+                               { \
+                                       print("###"); \
+                               } \
                                break; \
                        } \
                        break; \
                } \
                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 ]); \
                        if ( rcvFunc( byteRead, (uint16_t*)&uart##uartNum##_rx_bytes_waiting, uartNum ) ) \
                                uart##uartNum##_rx_status = UARTStatus_Wait; \
@@ -141,7 +163,10 @@ case uartNum: \
                        available++; \
                        continue; \
                } \
-               print( NL ); \
+               if ( Connect_debug ) \
+               { \
+                       print( NL ); \
+               } \
        } \
 }
 
@@ -164,6 +189,7 @@ case uartNum: \
 // CLI Functions
 void cliFunc_connectCmd ( char *args );
 void cliFunc_connectIdl ( char *args );
+void cliFunc_connectLst ( char *args );
 void cliFunc_connectMst ( char *args );
 void cliFunc_connectRst ( char *args );
 void cliFunc_connectSts ( char *args );
@@ -175,12 +201,14 @@ void cliFunc_connectSts ( char *args );
 // Connect Module command dictionary
 CLIDict_Entry( connectCmd,  "Sends a command via UART Connect, first arg is which uart, next arg is the command, rest are the arguments." );
 CLIDict_Entry( connectIdl,  "Sends N number of Idle commands, 2 is the default value, and should be sufficient in most cases." );
+CLIDict_Entry( connectLst,  "Lists available UARTConnect commands and index id" );
 CLIDict_Entry( connectMst,  "Sets the device as master. Use argument of s to set as slave." );
 CLIDict_Entry( connectRst,  "Resets both Rx and Tx connect buffers and state variables." );
 CLIDict_Entry( connectSts,  "UARTConnect status." );
 CLIDict_Def( uartConnectCLIDict, "UARTConnect Module Commands" ) = {
        CLIDict_Item( connectCmd ),
        CLIDict_Item( connectIdl ),
+       CLIDict_Item( connectLst ),
        CLIDict_Item( connectMst ),
        CLIDict_Item( connectRst ),
        CLIDict_Item( connectSts ),
@@ -193,6 +221,11 @@ uint8_t Connect_id = 255; // Invalid, unset
 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 --
 
 volatile UARTStatus uart0_rx_status;
@@ -398,12 +431,16 @@ uint8_t Connect_receive_CableCheck( uint8_t byte, uint16_t *pending_bytes, uint8
        // Check if this is the first byte
        if ( *pending_bytes == 0xFFFF )
        {
-               dbug_msg("PENDING SET -> ");
-               printHex( byte );
-               print(" ");
                *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
        else
@@ -448,11 +485,15 @@ uint8_t Connect_receive_CableCheck( uint8_t byte, uint16_t *pending_bytes, uint8
                        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
        return *pending_bytes == 0 ? 1 : 0;
@@ -462,7 +503,7 @@ uint8_t Connect_receive_IdRequest( uint8_t byte, uint16_t *pending_bytes, uint8_
 {
        dbug_print("IdRequest");
        // Check the directionality
-       if ( !to_master )
+       if ( to_master )
        {
                erro_print("Invalid IdRequest direction...");
        }
@@ -487,7 +528,7 @@ uint8_t Connect_receive_IdEnumeration( uint8_t id, uint16_t *pending_bytes, uint
 {
        dbug_print("IdEnumeration");
        // Check the directionality
-       if ( to_master )
+       if ( !to_master )
        {
                erro_print("Invalid IdEnumeration direction...");
        }
@@ -511,7 +552,7 @@ uint8_t Connect_receive_IdReport( uint8_t id, uint16_t *pending_bytes, uint8_t t
 {
        dbug_print("IdReport");
        // Check the directionality
-       if ( !to_master )
+       if ( to_master )
        {
                erro_print("Invalid IdRequest direction...");
        }
@@ -690,7 +731,10 @@ void Connect_setup( uint8_t master )
        // Register Connect CLI dictionary
        CLI_registerDictionary( uartConnectCLIDict, uartConnectCLIDictName );
 
+       // Check if master
        Connect_master = master;
+       if ( Connect_master )
+               Connect_id = 0; // 0x00 is always the master Id
 
        // Master / UART0 setup
        // Slave  / UART1 setup
@@ -699,15 +743,10 @@ void Connect_setup( uint8_t master )
        SIM_SCGC4 |= SIM_SCGC4_UART1; // Disable clock gating
 
        // 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
        UART0_BDH = (uint8_t)(Connect_baud >> 8);
@@ -771,12 +810,44 @@ void Connect_setup( uint8_t master )
 // - SyncEvent is also blocking until sent
 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 );
+       }
 }
 
 
@@ -796,7 +867,7 @@ void cliFunc_connectCmd( char* args )
        switch ( numToInt( &arg1Ptr[0] ) )
        {
        case CableCheck:
-               Connect_send_CableCheck( 2 );
+               Connect_send_CableCheck( UARTConnectCableCheckLength_define );
                break;
 
        case IdRequest:
@@ -818,6 +889,20 @@ void cliFunc_connectCmd( char* args )
                break;
        }
        case Animation:
+               break;
+
+       case RemoteCapability:
+               // TODO
+               break;
+
+       case RemoteOutput:
+               // TODO
+               break;
+
+       case RemoteInput:
+               // TODO
+               break;
+
        default:
                break;
        }
@@ -842,6 +927,31 @@ void cliFunc_connectIdl( char* args )
        Connect_send_Idle( count );
 }
 
+void cliFunc_connectLst( char* args )
+{
+       const char *Command_strs[] = {
+               "CableCheck",
+               "IdRequest",
+               "IdEnumeration",
+               "IdReport",
+               "ScanCode",
+               "Animation",
+               "RemoteCapability",
+               "RemoteOutput",
+               "RemoteInput",
+       };
+
+       print( NL );
+       info_msg("List of UARTConnect commands");
+       for ( uint8_t cmd = 0; cmd < Command_TOP; cmd++ )
+       {
+               print( NL );
+               printInt8( cmd );
+               print(" - ");
+               dPrint( (char*)Command_strs[ cmd ] );
+       }
+}
+
 void cliFunc_connectMst( char* args )
 {
        // Parse number from argument
@@ -877,7 +987,8 @@ void cliFunc_connectRst( char* args )
        info_msg("Resetting UARTConnect state...");
        Connect_reset();
 
-       // TODO - Argument for re-sync
+       // Reset node id
+       Connect_id = 0xFF;
 }
 
 void cliFunc_connectSts( char* args )