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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Copyright (C) 2013 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 <led.h>
  26. #include <print.h>
  27. // Local Includes
  28. #include "scan_loop.h"
  29. // ----- Defines -----
  30. // ----- Macros -----
  31. // Make sure we haven't overflowed the buffer
  32. #define bufferAdd(byte) \
  33. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  34. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  35. // ----- Variables -----
  36. // Buffer used to inform the macro processing module which keys have been detected as pressed
  37. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  38. volatile uint8_t KeyIndex_BufferUsed;
  39. // ----- Function Declarations -----
  40. void processKeyValue( uint8_t valueType );
  41. void removeKeyValue( uint8_t keyValue );
  42. // ----- Interrupt Functions -----
  43. // UART Receive Buffer Full Interrupt
  44. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  45. ISR(USART1_RX_vect)
  46. #elif defined(_mk20dx128_) // ARM
  47. void uart0_status_isr(void)
  48. #endif
  49. {
  50. cli(); // Disable Interrupts
  51. // Read part of the scan code (3 8bit chunks) from USART
  52. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  53. uint8_t tmp = UDR1;
  54. #elif defined(_mk20dx128_) // ARM
  55. // Exit out if nothing to do
  56. /*
  57. if ( !(UART0_S1 & UART_S1_RDRF ) )
  58. {
  59. sei();
  60. return;
  61. }
  62. */
  63. // Only doing single byte FIFO here
  64. uint8_t tmp = UART0_D;
  65. print("YAYA");
  66. #endif
  67. // Debug
  68. char tmpStr[6];
  69. hexToStr( tmp, tmpStr );
  70. dPrintStrsNL( tmpStr, " " ); // Debug
  71. // TODO
  72. sei(); // Re-enable Interrupts
  73. }
  74. // ----- Functions -----
  75. // Setup
  76. inline void scan_setup()
  77. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  78. {
  79. // Setup the the USART interface for keyboard data input
  80. // Setup baud rate - 1205 Baud
  81. // 16 MHz / ( 16 * Baud ) = UBRR
  82. // Baud: 1205 -> 16 MHz / ( 16 * 1205 ) = 829.8755
  83. // Thus baud setting = 830
  84. uint16_t baud = 830; // Max setting of 4095
  85. UBRR1H = (uint8_t)(baud >> 8);
  86. UBRR1L = (uint8_t)baud;
  87. // Enable the receiver, transmitter, and RX Complete Interrupt
  88. // TODO - Only receiver, and rx interrupt
  89. UCSR1B = 0x98;
  90. // Set frame format: 8 data, 1 stop bit, odd parity
  91. // Asynchrounous USART mode
  92. // TODO - Even parity
  93. UCSR1C = 0x36;
  94. // Reset the keyboard before scanning, we might be in a wierd state
  95. scan_resetKeyboard();
  96. }
  97. #elif defined(_mk20dx128_) // ARM
  98. {
  99. // Setup the the UART interface for keyboard data input
  100. SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
  101. // Pin Setup for UART0
  102. PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  103. PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  104. // Setup baud rate - 1205 Baud
  105. // 48 MHz / ( 16 * Baud ) = BDH/L
  106. // Baud: 1205 -> 48 MHz / ( 16 * 1205 ) = 2489.6266
  107. // Thus baud setting = 2490
  108. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  109. uint16_t baud = 2490; // Max setting of 8191
  110. UART0_BDH = (uint8_t)(baud >> 8);
  111. UART0_BDL = (uint8_t)baud;
  112. // 8 bit, Even Parity, Idle Character bit after stop
  113. UART0_C1 = ~UART_C1_M | ~UART_C1_PE | UART_C1_PT | UART_C1_ILT;
  114. // Number of bytes in FIFO before TX Interrupt
  115. UART0_TWFIFO = 1;
  116. // Number of bytes in FIFO before RX Interrupt
  117. UART0_RWFIFO = 1;
  118. // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
  119. // TX/RX FIFO Size:
  120. // 0x0 - 1 dataword
  121. // 0x1 - 4 dataword
  122. // 0x2 - 8 dataword
  123. UART0_PFIFO = ~UART_PFIFO_TXFE | /*TXFIFOSIZE*/ (0x0 << 4) | ~UART_PFIFO_RXFE | /*RXFIFOSIZE*/ (0x0);
  124. // Reciever Inversion Disabled, LSBF
  125. UART0_S2 = ~UART_S2_RXINV | UART_S2_MSBF;
  126. // Transmit Inversion Disabled
  127. UART0_C3 = ~UART_S2_TXINV;
  128. // TX Disabled, RX Enabled, RX Interrupt Enabled
  129. UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE;
  130. // Add interrupt to the vector table
  131. NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
  132. // Reset the keyboard before scanning, we might be in a wierd state
  133. scan_resetKeyboard();
  134. }
  135. #endif
  136. // Main Detection Loop
  137. inline uint8_t scan_loop()
  138. {
  139. UART0_D = 0x56;
  140. UART0_D = 0x1C;
  141. _delay_ms( 100 );
  142. return 0;
  143. }
  144. void processKeyValue( uint8_t keyValue )
  145. {
  146. // TODO Process ASCII
  147. // Make sure the key isn't already in the buffer
  148. for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
  149. {
  150. // Key isn't in the buffer yet
  151. if ( c == KeyIndex_BufferUsed )
  152. {
  153. bufferAdd( keyValue );
  154. break;
  155. }
  156. // Key already in the buffer
  157. if ( KeyIndex_Buffer[c] == keyValue )
  158. break;
  159. }
  160. }
  161. void removeKeyValue( uint8_t keyValue )
  162. {
  163. // Check for the released key, and shift the other keys lower on the buffer
  164. uint8_t c;
  165. for ( c = 0; c < KeyIndex_BufferUsed; c++ )
  166. {
  167. // Key to release found
  168. if ( KeyIndex_Buffer[c] == keyValue )
  169. {
  170. // Shift keys from c position
  171. for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
  172. KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
  173. // Decrement Buffer
  174. KeyIndex_BufferUsed--;
  175. break;
  176. }
  177. }
  178. // Error case (no key to release)
  179. if ( c == KeyIndex_BufferUsed + 1 )
  180. {
  181. errorLED( 1 );
  182. char tmpStr[6];
  183. hexToStr( keyValue, tmpStr );
  184. erro_dPrint( "Could not find key to release: ", tmpStr );
  185. }
  186. }
  187. // Send data
  188. // NOTE: Example only, MBC-55X cannot receive user data
  189. uint8_t scan_sendData( uint8_t dataPayload )
  190. {
  191. // Debug
  192. char tmpStr[6];
  193. hexToStr( dataPayload, tmpStr );
  194. info_dPrint( "Sending - ", tmpStr );
  195. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  196. UDR1 = dataPayload;
  197. #elif defined(_mk20dx128_) // ARM
  198. UART0_D = dataPayload;
  199. #endif
  200. return 0;
  201. }
  202. // Signal KeyIndex_Buffer that it has been properly read
  203. void scan_finishedWithBuffer( uint8_t sentKeys )
  204. {
  205. }
  206. // Signal that the keys have been properly sent over USB
  207. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  208. {
  209. }
  210. // Reset/Hold keyboard
  211. // NOTE: Does nothing with the FACOM6684
  212. void scan_lockKeyboard( void )
  213. {
  214. }
  215. // NOTE: Does nothing with the FACOM6684
  216. void scan_unlockKeyboard( void )
  217. {
  218. }
  219. // Reset Keyboard
  220. void scan_resetKeyboard( void )
  221. {
  222. // Not a calculated valued...
  223. _delay_ms( 50 );
  224. KeyIndex_BufferUsed = 0;
  225. }