Kiibohd Controller
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

port_scan.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (C) 2015-2016 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_portUART ( char* args );
  40. void cliFunc_portUSB ( char* args );
  41. // ----- Variables -----
  42. uint32_t Port_lastcheck_ms;
  43. // Scan Module command dictionary
  44. CLIDict_Entry( portCross, "Cross interconnect pins." );
  45. CLIDict_Entry( portUSB, "Swap USB ports manually, forces usb and interconnect to re-negotiate if necessary." );
  46. CLIDict_Entry( portUART, "Swap interconnect ports." );
  47. CLIDict_Def( portCLIDict, "Port Swap Module Commands" ) = {
  48. CLIDict_Item( portCross ),
  49. CLIDict_Item( portUART ),
  50. CLIDict_Item( portUSB ),
  51. { 0, 0, 0 } // Null entry for dictionary end
  52. };
  53. // ----- Functions -----
  54. void Port_usb_swap()
  55. {
  56. info_print("USB Port Swap");
  57. // PTA4 - USB Swap
  58. GPIOA_PTOR |= (1<<4);
  59. // Re-initialize usb
  60. // Call usb_configured() to check if usb is ready
  61. usb_init();
  62. }
  63. void Port_uart_swap()
  64. {
  65. info_print("Interconnect Line Swap");
  66. // PTA13 - UART Swap
  67. GPIOA_PTOR |= (1<<13);
  68. }
  69. void Port_cross()
  70. {
  71. info_print("Interconnect Line Cross");
  72. // PTA12 - UART Tx/Rx cross-over
  73. GPIOA_PTOR |= (1<<12);
  74. // Reset interconnects
  75. Connect_reset();
  76. }
  77. // Setup
  78. inline void Port_setup()
  79. {
  80. // Register Scan CLI dictionary
  81. CLI_registerDictionary( portCLIDict, portCLIDictName );
  82. // PTA4 - USB Swap
  83. // Start, disabled
  84. GPIOA_PDDR |= (1<<4);
  85. PORTA_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  86. GPIOA_PCOR |= (1<<4);
  87. // PTA12 - UART Tx/Rx cross-over
  88. // Start, disabled
  89. GPIOA_PDDR |= (1<<12);
  90. PORTA_PCR12 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  91. GPIOA_PCOR |= (1<<12);
  92. // PTA13 - UART Swap
  93. // Start, disabled
  94. GPIOA_PDDR |= (1<<13);
  95. PORTA_PCR13 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  96. GPIOA_PCOR |= (1<<13);
  97. // Starting point for automatic port swapping
  98. Port_lastcheck_ms = systick_millis_count;
  99. }
  100. // Port State processing loop
  101. inline uint8_t Port_scan()
  102. {
  103. // TODO Add in interconnect line cross
  104. #define USBPortSwapDelay_ms 1000
  105. // Wait 1000 ms before checking
  106. // Only check for swapping after delay
  107. uint32_t wait_ms = systick_millis_count - Port_lastcheck_ms;
  108. if ( wait_ms > USBPortSwapDelay_ms )
  109. {
  110. // Update timeout
  111. Port_lastcheck_ms = systick_millis_count;
  112. // USB not initialized, attempt to swap
  113. if ( !usb_configured() )
  114. {
  115. Port_usb_swap();
  116. }
  117. }
  118. return 0;
  119. }
  120. // Signal from parent Scan Module that available current has changed
  121. // current - mA
  122. void Port_currentChange( unsigned int current )
  123. {
  124. // TODO - Power savings?
  125. }
  126. // ----- Capabilities -----
  127. void Port_uart_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_uart_capability()");
  133. return;
  134. }
  135. // Only only release
  136. // TODO Analog
  137. if ( state != 0x03 )
  138. return;
  139. Port_uart_swap();
  140. }
  141. void Port_usb_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  142. {
  143. // Display capability name
  144. if ( stateType == 0xFF && state == 0xFF )
  145. {
  146. print("Port_usb_capability()");
  147. return;
  148. }
  149. // Only only release
  150. // TODO Analog
  151. if ( state != 0x03 )
  152. return;
  153. Port_usb_swap();
  154. }
  155. void Port_cross_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  156. {
  157. // Display capability name
  158. if ( stateType == 0xFF && state == 0xFF )
  159. {
  160. print("Port_cross_capability()");
  161. return;
  162. }
  163. // Only only release
  164. // TODO Analog
  165. if ( state != 0x03 )
  166. return;
  167. Port_cross();
  168. }
  169. // ----- CLI Command Functions -----
  170. void cliFunc_portUART( char* args )
  171. {
  172. print( NL );
  173. Port_uart_swap();
  174. }
  175. void cliFunc_portUSB( char* args )
  176. {
  177. print( NL );
  178. Port_usb_swap();
  179. }
  180. void cliFunc_portCross( char* args )
  181. {
  182. print( NL );
  183. Port_cross();
  184. }