Kiibohd Controller
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

scan_loop.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Copyright (C) 2012 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. // AVR Includes
  23. #include <avr/interrupt.h>
  24. #include <avr/io.h>
  25. #include <util/delay.h>
  26. // Project Includes
  27. #include <led.h>
  28. #include <print.h>
  29. // Local Includes
  30. #include "scan_loop.h"
  31. // ----- Defines -----
  32. // Pinout Defines
  33. #define DATA_PORT PORTC
  34. #define DATA_DDR DDRC
  35. #define DATA_PIN 7
  36. #define DATA_OUT PINC
  37. #define CLOCK_PORT PORTC
  38. #define CLOCK_DDR DDRC
  39. #define CLOCK_PIN 6
  40. #define RESET_PORT PORTF
  41. #define RESET_DDR DDRF
  42. #define RESET_PIN 7
  43. // ----- Macros -----
  44. // Make sure we haven't overflowed the buffer
  45. #define bufferAdd(byte) \
  46. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  47. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  48. // ----- Variables -----
  49. // Buffer used to inform the macro processing module which keys have been detected as pressed
  50. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  51. volatile uint8_t KeyIndex_BufferUsed;
  52. volatile uint8_t KeyIndex_Add_InputSignal; // Used to pass the (click/input value) to the keyboard for the clicker
  53. volatile uint8_t currentWaveState = 0;
  54. volatile uint8_t currentWaveDone = 0;
  55. volatile uint8_t positionCounter = 0;
  56. // Buffer Signals
  57. volatile uint8_t BufferReadyToClear;
  58. // ----- Function Declarations -----
  59. void processKeyValue( uint8_t keyValue );
  60. void removeKeyValue( uint8_t keyValue );
  61. // ----- Interrupt Functions -----
  62. // Generates a constant external clock
  63. ISR( TIMER1_COMPA_vect )
  64. {
  65. if ( currentWaveState )
  66. {
  67. CLOCK_PORT &= ~(1 << CLOCK_PIN);
  68. currentWaveState--; // Keeps track of the clock value (for direct clock output)
  69. currentWaveDone--; // Keeps track of whether the current falling edge has been processed
  70. positionCounter++; // Counts the number of falling edges, reset is done by the controlling section (reset, or main scan)
  71. }
  72. else
  73. {
  74. CLOCK_PORT |= (1 << CLOCK_PIN);
  75. currentWaveState++;
  76. }
  77. }
  78. // ----- Functions -----
  79. // Setup
  80. inline void scan_setup()
  81. {
  82. // Setup Timer Pulse (16 bit)
  83. // TODO Clock can be adjusted to whatever (read chip datasheets for limits)
  84. // This seems like a good scan speed, as there don't seem to be any periodic
  85. // de-synchronization events, and is fast enough for scanning keys
  86. // Anything much more (100k baud), tends to cause a lot of de-synchronization
  87. // 16 MHz / (2 * Prescaler * (1 + OCR1A)) = 10k baud
  88. // Prescaler is 1
  89. cli();
  90. TCCR1B = 0x09;
  91. OCR1AH = 0x03;
  92. OCR1AL = 0x1F;
  93. TIMSK1 = (1 << OCIE1A);
  94. CLOCK_DDR = (1 << CLOCK_PIN);
  95. sei();
  96. // Initially buffer doesn't need to be cleared (it's empty...)
  97. BufferReadyToClear = 0;
  98. // Reset the keyboard before scanning, we might be in a wierd state
  99. scan_resetKeyboard();
  100. }
  101. // Main Detection Loop
  102. // Since this function is non-interruptable, we can do checks here on what stage of the
  103. // output clock we are at (0 or 1)
  104. // We are looking for a start of packet
  105. // If detected, all subsequent bits are then logged into a variable
  106. // Once the end of the packet has been detected (always the same length), decode the pressed keys
  107. inline uint8_t scan_loop()
  108. {
  109. // Read on each falling edge/after the falling edge of the clock
  110. if ( !currentWaveDone )
  111. {
  112. // Sample the current value 50 times
  113. // If there is a signal for 40/50 of the values, then it is active
  114. // This works as a very simple debouncing mechanism
  115. // XXX Could be done more intelligently:
  116. // Take into account the frequency of the clock + overhead, and space out the reads
  117. // Or do something like "dual edge" statistics, where you query the stats from both rising and falling edges
  118. // then make a decision (probably won't do much better against the last source of noise, but would do well for debouncing)
  119. uint8_t total = 0;
  120. uint8_t c = 0;
  121. for ( ; c < 50; c++ )
  122. if ( DATA_OUT & (1 << DATA_PIN) )
  123. total++;
  124. // Only use as a valid signal
  125. if ( total >= 40 )
  126. {
  127. // Reset the scan counter, all the keys have been iterated over
  128. // Ideally this should reset at 128, however
  129. // due to noise in the cabling, this often moves around
  130. // The minimum this can possibly set to is 124 as there
  131. // are keys to service at 123 (0x78)
  132. // Usually, unless there is lots of interference,
  133. // this should limit most of the noise.
  134. if ( positionCounter >= 124 )
  135. {
  136. positionCounter = 0;
  137. // Clear key buffer
  138. KeyIndex_BufferUsed = 0;
  139. }
  140. // Key Press Detected
  141. else
  142. {
  143. char tmp[15];
  144. hexToStr( positionCounter, tmp );
  145. dPrintStrsNL( "Key: ", tmp );
  146. bufferAdd( positionCounter );
  147. }
  148. }
  149. // Wait until the next falling clock edge for the next DATA scan
  150. currentWaveDone++;
  151. }
  152. // Check if the clock de-synchronized
  153. // And reset
  154. if ( positionCounter > 128 )
  155. {
  156. char tmp[15];
  157. hexToStr( positionCounter, tmp );
  158. erro_dPrint( "De-synchronization detected at: ", tmp );
  159. errorLED( 1 );
  160. positionCounter = 0;
  161. KeyIndex_BufferUsed = 0;
  162. // A keyboard reset requires interrupts to be enabled
  163. sei();
  164. scan_resetKeyboard();
  165. cli();
  166. }
  167. // Regardless of what happens, always return 0
  168. return 0;
  169. }
  170. // Send data
  171. uint8_t scan_sendData( uint8_t dataPayload )
  172. {
  173. return 0;
  174. }
  175. // Signal KeyIndex_Buffer that it has been properly read
  176. void scan_finishedWithBuffer( void )
  177. {
  178. }
  179. // Signal that the keys have been properly sent over USB
  180. void scan_finishedWithUSBBuffer( void )
  181. {
  182. }
  183. // Reset/Hold keyboard
  184. // NOTE: Does nothing with the HP150
  185. void scan_lockKeyboard( void )
  186. {
  187. }
  188. // NOTE: Does nothing with the HP150
  189. void scan_unlockKeyboard( void )
  190. {
  191. }
  192. // Reset Keyboard
  193. void scan_resetKeyboard( void )
  194. {
  195. info_print("Attempting to synchronize the keyboard, do not press any keys...");
  196. errorLED( 1 );
  197. // Do a proper keyboard reset (flushes the ripple counters)
  198. RESET_PORT |= (1 << RESET_PIN);
  199. _delay_us(10);
  200. RESET_PORT &= ~(1 << RESET_PIN);
  201. // Delay main keyboard scanning, until the bit counter is synchronized
  202. uint8_t synchronized = 0;
  203. while ( !synchronized )
  204. {
  205. // Read on each falling edge/after the falling edge of the clock
  206. if ( !currentWaveDone )
  207. {
  208. // Read the current data value
  209. if ( DATA_OUT & (1 << DATA_PIN) )
  210. {
  211. // Check if synchronized
  212. // There are 128 positions to scan for with the HP150 keyboard protocol
  213. if ( positionCounter == 128 )
  214. synchronized = 1;
  215. positionCounter = 0;
  216. }
  217. // Wait until the next falling clock edge for the next DATA scan
  218. currentWaveDone++;
  219. }
  220. }
  221. info_print("Keyboard Synchronized!");
  222. }