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.3KB

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