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.

scan_loop.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Copyright (C) 2012,2014,2016 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/ScanLib.h>
  24. // Project Includes
  25. #include <kll.h>
  26. #include <kll_defs.h>
  27. #include <led.h>
  28. #include <macro.h>
  29. #include <print.h>
  30. // Local Includes
  31. #include "scan_loop.h"
  32. // ----- Defines -----
  33. // Pinout Defines
  34. #define HOLD_PORT PORTD
  35. #define HOLD_DDR DDRD
  36. #define HOLD_PIN 3
  37. // ----- Macros -----
  38. // ----- Enums -----
  39. // Keypress States
  40. typedef enum KeyPosition {
  41. KeyState_Off = 0,
  42. KeyState_Press = 1,
  43. KeyState_Hold = 2,
  44. KeyState_Release = 3,
  45. KeyState_Invalid,
  46. } KeyPosition;
  47. // ----- Variables -----
  48. // Buffer used to inform the macro processing module which keys have been detected as pressed
  49. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  50. volatile uint8_t KeyIndex_BufferUsed;
  51. volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
  52. // Buffer Signals
  53. volatile uint8_t BufferReadyToClear;
  54. // ----- Function Declarations -----
  55. void processKeyValue( uint8_t keyValue );
  56. void removeKeyValue( uint8_t keyValue );
  57. // ----- Interrupt Functions -----
  58. // USART Receive Buffer Full Interrupt
  59. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  60. ISR(USART1_RX_vect)
  61. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  62. void uart0_status_isr()
  63. #endif
  64. {
  65. cli(); // Disable Interrupts
  66. uint8_t keyValue = 0x00;
  67. uint8_t keyState = 0x00;
  68. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  69. // Read the scancode packet from the USART (1st to 8th bits)
  70. keyValue = UDR1;
  71. // Read the release/press bit (9th bit) XXX Unnecessary, and wrong it seems, parity bit? or something else?
  72. keyState = UCSR1B & 0x02;
  73. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  74. // UART0_S1 must be read for the interrupt to be cleared
  75. if ( UART0_S1 & UART_S1_RDRF )
  76. {
  77. // Only doing single byte FIFO here
  78. keyValue = UART0_D;
  79. }
  80. #endif
  81. // High bit of keyValue, also represents press/release
  82. keyState = keyValue & 0x80 ? 0x00 : 0x02;
  83. // Debug
  84. char tmpStr[6];
  85. hexToStr( keyValue & 0x7F, tmpStr );
  86. // Process the scancode
  87. switch ( keyState )
  88. {
  89. case 0x00: // Released
  90. dPrintStrs( tmpStr, "R " ); // Debug
  91. break;
  92. case 0x02: // Pressed
  93. dPrintStrs( tmpStr, "P " ); // Debug
  94. break;
  95. }
  96. // Add key event to macro key buffer
  97. TriggerGuide guide = {
  98. .type = 0x00,
  99. .state = keyState == 0x02 ? 0x01 : 0x03,
  100. .scanCode = keyValue & 0x7F,
  101. };
  102. Macro_pressReleaseAdd( &guide );
  103. sei(); // Re-enable Interrupts
  104. }
  105. // ----- Functions -----
  106. // Setup
  107. inline void Scan_setup()
  108. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  109. {
  110. // Setup the the USART interface for keyboard data input
  111. // NOTE: The input data signal needs to be inverted for the Teensy USART to properly work
  112. // Setup baud rate
  113. // 16 MHz / ( 16 * Baud ) = UBRR
  114. // Baud <- 0.823284 ms per bit, thus 1000 / 0.823284 = 1214.65004 -> 823.2824
  115. // Thus baud setting = 823
  116. uint16_t baud = 823; // Max setting of 4095
  117. UBRR1H = (uint8_t)(baud >> 8);
  118. UBRR1L = (uint8_t)baud;
  119. // Enable the receiver, and RX Complete Interrupt as well as 9 bit data
  120. UCSR1B = 0x94;
  121. // The transmitter is only to be enabled when needed
  122. // Set the pin to be pull-up otherwise (use the lowered voltage inverter in order to sink)
  123. HOLD_DDR &= ~(1 << HOLD_PIN);
  124. HOLD_PORT |= (1 << HOLD_PIN);
  125. // Set frame format: 9 data, 1 stop bit, no parity
  126. // Asynchrounous USART mode
  127. UCSR1C = 0x06;
  128. // Initially buffer doesn't need to be cleared (it's empty...)
  129. BufferReadyToClear = 0;
  130. // InputSignal is off by default
  131. KeyIndex_Add_InputSignal = 0x00;
  132. // Reset the keyboard before scanning, we might be in a wierd state
  133. scan_resetKeyboard();
  134. }
  135. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  136. {
  137. // Setup the the UART interface for keyboard data input
  138. SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
  139. // Pin Setup for UART0
  140. PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  141. PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  142. // Setup baud rate - 1205 Baud
  143. // 48 MHz / ( 16 * Baud ) = BDH/L
  144. // Baud: 1215 -> 48 MHz / ( 16 * 1215 ) = 2469.1358
  145. // Thus baud setting = 2469
  146. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  147. uint16_t baud = 2469; // Max setting of 8191
  148. UART0_BDH = (uint8_t)(baud >> 8);
  149. UART0_BDL = (uint8_t)baud;
  150. // 8 bit, Even Parity, Idle Character bit after stop
  151. // NOTE: For 8 bit with Parity you must enable 9 bit transmission (pg. 1065)
  152. // You only need to use UART0_D for 8 bit reading/writing though
  153. // UART_C1_M UART_C1_PE UART_C1_PT UART_C1_ILT
  154. UART0_C1 = UART_C1_M | UART_C1_PE | UART_C1_ILT;
  155. // Number of bytes in FIFO before TX Interrupt
  156. UART0_TWFIFO = 1;
  157. // Number of bytes in FIFO before RX Interrupt
  158. UART0_RWFIFO = 1;
  159. // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
  160. // TX/RX FIFO Size:
  161. // 0x0 - 1 dataword
  162. // 0x1 - 4 dataword
  163. // 0x2 - 8 dataword
  164. //UART0_PFIFO = UART_PFIFO_TXFE | /*TXFIFOSIZE*/ (0x0 << 4) | UART_PFIFO_RXFE | /*RXFIFOSIZE*/ (0x0);
  165. // Reciever Inversion Disabled, LSBF
  166. // UART_S2_RXINV UART_S2_MSBF
  167. UART0_S2 |= 0x00;
  168. // Transmit Inversion Disabled
  169. // UART_C3_TXINV
  170. UART0_C3 |= 0x00;
  171. // TX Disabled, RX Enabled, RX Interrupt Enabled
  172. // UART_C2_TE UART_C2_RE UART_C2_RIE
  173. UART0_C2 = UART_C2_RE | UART_C2_RIE | UART_C2_TE;
  174. // Add interrupt to the vector table
  175. NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
  176. // Reset the keyboard before scanning, we might be in a wierd state
  177. Scan_resetKeyboard();
  178. }
  179. #endif
  180. // Main Detection Loop
  181. // Not needed for the BETKB, this is just a busy loop
  182. inline uint8_t Scan_loop()
  183. {
  184. return 0;
  185. }
  186. // Send data
  187. uint8_t scan_sendData( uint8_t dataPayload )
  188. {
  189. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  190. // Enable the USART Transmitter
  191. UCSR1B |= (1 << 3);
  192. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  193. #endif
  194. // Debug
  195. char tmpStr[6];
  196. hexToStr( dataPayload, tmpStr );
  197. info_dPrint( "Sending - ", tmpStr );
  198. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  199. UDR1 = dataPayload;
  200. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  201. UART0_D = dataPayload;
  202. #endif
  203. // Wait for the payload
  204. _delay_us( 800 );
  205. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  206. // Disable the USART Transmitter
  207. UCSR1B &= ~(1 << 3);
  208. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  209. #endif
  210. return 0;
  211. }
  212. // Signal KeyIndex_Buffer that it has been properly read
  213. void Scan_finishedWithMacro( uint8_t sentKeys )
  214. {
  215. }
  216. // Signal that the keys have been properly sent over USB
  217. void Scan_finishedWithOutput( uint8_t sentKeys )
  218. {
  219. }
  220. // Reset/Hold keyboard
  221. // NOTE: Does nothing with the BETKB
  222. void Scan_lockKeyboard()
  223. {
  224. }
  225. // NOTE: Does nothing with the BETKB
  226. void Scan_unlockKeyboard()
  227. {
  228. }
  229. // Reset Keyboard
  230. void Scan_resetKeyboard()
  231. {
  232. // Not a calculated valued...
  233. _delay_ms( 50 );
  234. }
  235. // NOTE: Does nothing with the BETKB
  236. void Scan_currentChange( unsigned int current )
  237. {
  238. }