Kiibohd Controller
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

main.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright (C) 2011-2014 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. // ----- Includes -----
  22. // Compiler Includes
  23. #include <Lib/MainLib.h>
  24. // Project Includes
  25. #include <macro.h>
  26. #include <scan_loop.h>
  27. #include <output_com.h>
  28. #include <cli.h>
  29. #include <led.h>
  30. #include <print.h>
  31. // ----- Defines -----
  32. // Verified Keypress Defines
  33. #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
  34. // ----- Macros -----
  35. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  36. #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
  37. #endif
  38. // ----- Function Declarations -----
  39. void cliFunc_free ( char* args );
  40. void cliFunc_gaugeHelp ( char* args );
  41. void cliFunc_single ( char* args );
  42. void cliFunc_start ( char* args );
  43. void cliFunc_zeroForce ( char* args );
  44. void cliFunc_zeroPosition( char* args );
  45. // ----- Variables -----
  46. // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
  47. uint16_t sendKeypressCounter = 0;
  48. // Flag generated by the timer interrupt
  49. volatile uint8_t sendKeypresses = 0;
  50. // ----- Functions -----
  51. // Initial Pin Setup, make sure they are sane
  52. inline void pinSetup(void)
  53. {
  54. // AVR
  55. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  56. // For each pin, 0=input, 1=output
  57. #if defined(__AVR_AT90USB1286__)
  58. DDRA = 0x00;
  59. #endif
  60. DDRB = 0x00;
  61. DDRC = 0x00;
  62. DDRD = 0x00;
  63. DDRE = 0x00;
  64. DDRF = 0x00;
  65. // Setting pins to either high or pull-up resistor
  66. #if defined(__AVR_AT90USB1286__)
  67. PORTA = 0x00;
  68. #endif
  69. PORTB = 0x00;
  70. PORTC = 0x00;
  71. PORTD = 0x00;
  72. PORTE = 0x00;
  73. PORTF = 0x00;
  74. // ARM
  75. #elif defined(_mk20dx128_)
  76. // TODO - Should be cleared, but not that necessary due to the pin layout
  77. #endif
  78. }
  79. inline void usbTimerSetup(void)
  80. {
  81. // AVR
  82. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  83. // Setup with 16 MHz clock
  84. CPU_PRESCALE( 0 );
  85. // Setup ISR Timer for flagging a kepress send to USB
  86. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  87. TCCR0A = 0x00;
  88. TCCR0B = 0x03;
  89. TIMSK0 = (1 << TOIE0);
  90. // ARM
  91. #elif defined(_mk20dx128_)
  92. // 48 MHz clock by default
  93. // System Clock Gating Register Disable
  94. SIM_SCGC6 |= SIM_SCGC6_PIT;
  95. // Enable Timers
  96. PIT_MCR = 0x00;
  97. // Setup ISR Timer for flagging a kepress send to USB
  98. // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
  99. PIT_LDVAL0 = 0x0000BB7F;
  100. PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
  101. // Insert the required vector for Timer 0
  102. NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
  103. #endif
  104. }
  105. int main(void)
  106. {
  107. // Configuring Pins
  108. pinSetup();
  109. init_errorLED();
  110. // Setup Output Module
  111. output_setup();
  112. // Enable CLI
  113. init_cli();
  114. // Setup ISR Timer for flagging a kepress send to USB
  115. usbTimerSetup();
  116. // Main Detection Loop
  117. uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
  118. while ( 1 )
  119. {
  120. // Setup the scanning module
  121. scan_setup();
  122. while ( 1 )
  123. {
  124. // Acquire Key Indices
  125. // Loop continuously until scan_loop returns 0
  126. cli();
  127. while ( scan_loop() );
  128. sei();
  129. // Run Macros over Key Indices and convert to USB Keys
  130. process_macros();
  131. // Send keypresses over USB if the ISR has signalled that it's time
  132. if ( !sendKeypresses )
  133. continue;
  134. // Send USB Data
  135. usb_send();
  136. // Clear sendKeypresses Flag
  137. sendKeypresses = 0;
  138. // Indicate Error, if valid
  139. errorLED( ledTimer );
  140. if ( ledTimer > 0 )
  141. ledTimer--;
  142. }
  143. // Loop should never get here (indicate error)
  144. ledTimer = 255;
  145. // HID Debug Error message
  146. erro_print("Detection loop error, this is very bad...bug report!");
  147. }
  148. }
  149. // ----- Interrupts -----
  150. // USB Keyboard Data Send Counter Interrupt
  151. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  152. ISR( TIMER0_OVF_vect )
  153. #elif defined(_mk20dx128_) // ARM
  154. void pit0_isr(void)
  155. #endif
  156. {
  157. sendKeypressCounter++;
  158. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  159. sendKeypressCounter = 0;
  160. sendKeypresses = 1;
  161. }
  162. #if defined(_mk20dx128_) // ARM
  163. // Clear the interrupt flag
  164. PIT_TFLG0 = 1;
  165. #endif
  166. }
  167. // ----- CLI Command Functions -----
  168. void cliFunc_free( char* args )
  169. {
  170. }
  171. void cliFunc_gaugeHelp( char* args )
  172. {
  173. }
  174. void cliFunc_single( char* args )
  175. {
  176. }
  177. void cliFunc_start( char* args )
  178. {
  179. }
  180. void cliFunc_zeroForce( char* args )
  181. {
  182. }
  183. void cliFunc_zeroPosition( char* args )
  184. {
  185. }