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.

main.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // ----- Variables -----
  39. // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
  40. uint16_t sendKeypressCounter = 0;
  41. // Flag generated by the timer interrupt
  42. volatile uint8_t sendKeypresses = 0;
  43. // ----- Functions -----
  44. inline void usbTimerSetup()
  45. {
  46. // AVR
  47. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  48. // Setup with 16 MHz clock
  49. CPU_PRESCALE( 0 );
  50. // Setup ISR Timer for flagging a kepress send to USB
  51. // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
  52. TCCR0A = 0x00;
  53. TCCR0B = 0x03;
  54. TIMSK0 = (1 << TOIE0);
  55. // ARM
  56. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
  57. // 48 MHz clock by default
  58. // System Clock Gating Register Disable
  59. SIM_SCGC6 |= SIM_SCGC6_PIT;
  60. // Enable Timers
  61. PIT_MCR = 0x00;
  62. // Setup ISR Timer for flagging a kepress send to USB
  63. // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
  64. PIT_LDVAL0 = 0x0000BB7F;
  65. PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
  66. // Insert the required vector for Timer 0
  67. NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
  68. #endif
  69. }
  70. int main()
  71. {
  72. // Enable CLI
  73. CLI_init();
  74. // Setup Modules
  75. Output_setup();
  76. Macro_setup();
  77. Scan_setup();
  78. // Setup ISR Timer for flagging a kepress send to USB
  79. usbTimerSetup();
  80. // Main Detection Loop
  81. while ( 1 )
  82. {
  83. // Process CLI
  84. CLI_process();
  85. // Acquire Key Indices
  86. // Loop continuously until scan_loop returns 0
  87. cli();
  88. while ( Scan_loop() );
  89. sei();
  90. // Run Macros over Key Indices and convert to USB Keys
  91. Macro_process();
  92. // Send keypresses over USB if the ISR has signalled that it's time
  93. if ( !sendKeypresses )
  94. continue;
  95. // Send USB Data
  96. Output_send();
  97. // Clear sendKeypresses Flag
  98. sendKeypresses = 0;
  99. }
  100. }
  101. // ----- Interrupts -----
  102. // USB Keyboard Data Send Counter Interrupt
  103. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  104. ISR( TIMER0_OVF_vect )
  105. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
  106. void pit0_isr()
  107. #endif
  108. {
  109. sendKeypressCounter++;
  110. if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
  111. sendKeypressCounter = 0;
  112. sendKeypresses = 1;
  113. }
  114. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
  115. // Clear the interrupt flag
  116. PIT_TFLG0 = 1;
  117. #endif
  118. }