Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

port_scan.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright (C) 2015 by Jacob Alexander
  2. *
  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.
  7. *
  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.
  12. *
  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/>.
  15. */
  16. // ----- Includes -----
  17. // Compiler Includes
  18. #include <Lib/ScanLib.h>
  19. // Project Includes
  20. #include <cli.h>
  21. #include <kll_defs.h>
  22. #include <led.h>
  23. #include <print.h>
  24. // USB Includes
  25. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  26. #include <avr/usb_keyboard_serial.h>
  27. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_)
  28. #include <arm/usb_dev.h>
  29. #endif
  30. // Interconnect Includes
  31. #include <connect_scan.h>
  32. // Local Includes
  33. #include "port_scan.h"
  34. // ----- Defines -----
  35. // ----- Structs -----
  36. // ----- Function Declarations -----
  37. // CLI Functions
  38. void cliFunc_portCross( char* args );
  39. void cliFunc_portSwap ( char* args );
  40. // ----- Variables -----
  41. uint32_t Port_lastcheck_ms;
  42. // Scan Module command dictionary
  43. CLIDict_Entry( portCross, "Cross interconnect pins." );
  44. CLIDict_Entry( portSwap, "Swap USB ports manually, forces usb and interconnect to re-negotiate if necessary." );
  45. CLIDict_Def( portCLIDict, "Port Swap Module Commands" ) = {
  46. CLIDict_Item( portCross ),
  47. CLIDict_Item( portSwap ),
  48. { 0, 0, 0 } // Null entry for dictionary end
  49. };
  50. // ----- Functions -----
  51. void Port_swap()
  52. {
  53. info_print("USB Port Swap");
  54. // PTA13 - USB Swap
  55. GPIOA_PTOR |= (1<<13);
  56. // Re-initialize usb
  57. // Call usb_configured() to check if usb is ready
  58. usb_init();
  59. }
  60. void Port_cross()
  61. {
  62. info_print("Interconnect Line Cross");
  63. // PTA12 - UART Tx/Rx cross-over
  64. GPIOA_PTOR |= (1<<12);
  65. // Reset interconnects
  66. Connect_reset();
  67. }
  68. // Setup
  69. inline void Port_setup()
  70. {
  71. // Register Scan CLI dictionary
  72. CLI_registerDictionary( portCLIDict, portCLIDictName );
  73. // PTA12 - UART Tx/Rx cross-over
  74. // Start, disabled
  75. GPIOA_PDDR |= (1<<12);
  76. PORTA_PCR12 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  77. GPIOA_PCOR |= (1<<12);
  78. // PTA13 - USB Swap
  79. // Start, disabled
  80. GPIOA_PDDR |= (1<<13);
  81. PORTA_PCR13 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  82. GPIOA_PCOR |= (1<<13);
  83. // Starting point for automatic port swapping
  84. Port_lastcheck_ms = systick_millis_count;
  85. }
  86. // Port State processing loop
  87. inline uint8_t Port_scan()
  88. {
  89. // TODO Add in interconnect line cross
  90. #define USBPortSwapDelay_ms 1000
  91. // Wait 1000 ms before checking
  92. // Only check for swapping after delay
  93. uint32_t wait_ms = systick_millis_count - Port_lastcheck_ms;
  94. if ( wait_ms > USBPortSwapDelay_ms )
  95. {
  96. // Update timeout
  97. Port_lastcheck_ms = systick_millis_count;
  98. // USB not initialized, attempt to swap
  99. if ( !usb_configured() )
  100. {
  101. Port_swap();
  102. }
  103. }
  104. return 0;
  105. }
  106. // Signal from parent Scan Module that available current has changed
  107. // current - mA
  108. void Port_currentChange( unsigned int current )
  109. {
  110. // TODO - Power savings?
  111. }
  112. // ----- Capabilities -----
  113. void Port_swap_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  114. {
  115. // Display capability name
  116. if ( stateType == 0xFF && state == 0xFF )
  117. {
  118. print("Port_swap_capability()");
  119. return;
  120. }
  121. // Only only release
  122. // TODO Analog
  123. if ( state != 0x03 )
  124. return;
  125. Port_swap();
  126. }
  127. void Port_cross_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  128. {
  129. // Display capability name
  130. if ( stateType == 0xFF && state == 0xFF )
  131. {
  132. print("Port_cross_capability()");
  133. return;
  134. }
  135. // Only only release
  136. // TODO Analog
  137. if ( state != 0x03 )
  138. return;
  139. Port_cross();
  140. }
  141. // ----- CLI Command Functions -----
  142. void cliFunc_portSwap( char* args )
  143. {
  144. print( NL );
  145. Port_swap();
  146. }
  147. void cliFunc_portCross( char* args )
  148. {
  149. print( NL );
  150. Port_cross();
  151. }