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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Copyright (C) 2011 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. // Project Includes
  25. #include <led.h>
  26. #include <print.h>
  27. // Local Includes
  28. #include "scan_loop.h"
  29. // ----- Defines -----
  30. // Pinout Defines
  31. #define CLK_READ PIND
  32. #define CLK_PORT PORTD
  33. #define CLK_DDR DDRD
  34. #define CLK_PIN 1
  35. #define DATA_READ PIND
  36. #define DATA_PORT PORTD
  37. #define DATA_DDR DDRD
  38. #define DATA_PIN 0
  39. #define INTR_PORT PORTD
  40. #define INTR_DDR DDRD
  41. #define INTR_PIN 0
  42. // ----- Macros -----
  43. #define READ_CLK CLK_READ & (1 << CLK_PIN) ? 1 : 0
  44. #define READ_DATA DATA_READ & (1 << DATA_PIN) ? 0 : 1
  45. #define UNSET_INTR() INTR_DDR &= ~(1 << INTR_PIN)
  46. #define SET_INTR() INTR_DDR |= (1 << INTR_PIN)
  47. // ----- Variables -----
  48. uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
  49. // Scan Code Retrieval Variables
  50. uint8_t inputData = 0xFF;
  51. uint8_t packet_index = 0;
  52. // ----- Functions -----
  53. // Setup
  54. inline void scan_setup()
  55. {
  56. // Setup inputs
  57. CLK_DDR &= ~(1 << CLK_PIN);
  58. DATA_DDR &= ~(1 << DATA_PIN);
  59. // Setup Pull-up's
  60. CLK_PORT &= ~(1 << CLK_PIN); // (CLK)
  61. DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
  62. // Setup Keyboard Interrupt
  63. INTR_DDR &= ~(1 << INTR_PIN);
  64. INTR_PORT &= ~(1 << INTR_PIN);
  65. /* Interrupt Style (Not working fully)
  66. cli();
  67. // Setup interrupt on the CLK pin TODO Better defines
  68. EICRA |= 0x03; // Rising Edge Interrupt
  69. EIMSK |= (1 << INT0);
  70. // Setup interrupt on the DATA pin TODO Better defines
  71. EICRA |= 0x08; // Falling Edge Interrupt
  72. EIMSK |= (1 << INT1);
  73. sei();
  74. */
  75. }
  76. // Main Detection Loop
  77. inline uint8_t scan_loop()
  78. {
  79. /*
  80. // Packet Read
  81. if ( packet_index == 8 )
  82. {
  83. // Disable Error LED, proper key found
  84. errorLED( 0 );
  85. //#ifdef MAX_DEBUG
  86. // Crazy Debug (Read the Scan Code)
  87. char tmpStr[3];
  88. hexToStr_op( inputData, tmpStr, 2 );
  89. dPrintStrsNL( "Read Data: 0x", tmpStr );
  90. //#endif
  91. // - Map the scan code to the index array -
  92. // If the 8th bit is high, remove the keypress, else, add the keypress
  93. // The lower 7 bits are the array index
  94. KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
  95. // Reset Containers
  96. packet_index = 0;
  97. inputData = 0xFF;
  98. }
  99. // Bad Packet
  100. else if ( packet_index > 8 )
  101. {
  102. // Signal Error
  103. errorLED( 1 );
  104. char tmpStr[3];
  105. int8ToStr( packet_index, tmpStr );
  106. erro_dPrint( "Big packet? Mismatched... ", tmpStr );
  107. packet_index = 0;
  108. inputData = 0xFF;
  109. }
  110. */
  111. // Disable keyboard interrupt (does nothing if already off)
  112. UNSET_INTR();
  113. // Read the clock 8 times
  114. if ( READ_CLK )
  115. {
  116. // Mis-read packet, set back to 0
  117. if ( packet_index == -1 )
  118. packet_index = 0;
  119. // Append 1 bit of data
  120. inputData &= ~(READ_DATA << packet_index);
  121. packet_index++;
  122. // 8 Bits have been read
  123. if ( packet_index == 8 )
  124. {
  125. // Wait till clock edge falls
  126. while ( READ_CLK );
  127. // Sample both lines to make sure this is not a data value
  128. // and definitely the end of packet data blip
  129. uint16_t badDataCounter = 0;
  130. while ( !( READ_DATA ) && !( READ_CLK ) )
  131. badDataCounter++;
  132. if ( badDataCounter < 25 )
  133. {
  134. //#ifdef MAX_DEBUG
  135. // Crazy Debug (Read the Scan Code)
  136. char tmpStr[3];
  137. hexToStr_op( inputData, tmpStr, 2 );
  138. dbug_dPrint( "Read Data: 0x", tmpStr );
  139. //#endif
  140. // - Map the scan code to the index array -
  141. // If the 8th bit is high, remove the keypress, else, add the keypress
  142. // The lower 7 bits are the array index
  143. KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
  144. }
  145. // Even though this is a mis-read packet, we still know what the value is
  146. else
  147. {
  148. // Signal Error
  149. errorLED( 1 );
  150. char tmpStr[3];
  151. hexToStr_op( inputData, tmpStr, 2 );
  152. erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
  153. }
  154. // Reset Containers
  155. inputData = 0xFF;
  156. packet_index = 0;
  157. // Interrupt the keyboard, so we don't get packet pieces...
  158. SET_INTR();
  159. // Do not wait for next clock, let USB do it's thing (if desired)
  160. return packet_index;
  161. }
  162. // Wait till clock edge falls
  163. while ( READ_CLK );
  164. }
  165. // Interrupt keyboard if there is no pending packet
  166. SET_INTR();
  167. return packet_index;
  168. }
  169. // Detection interrupt, signalled by a clock pulse from CLK_PIN
  170. ISR(INT0_vect)
  171. {
  172. //cli(); // Disable Interrupts
  173. // Append 1 bit of data
  174. //inputData &= ~(READ_DATA << packet_index);
  175. packet_index++;
  176. //sei(); // Re-enable Interrupts
  177. }
  178. // Data Detected
  179. ISR(INT1_vect)
  180. {
  181. // Append 1 bit of data
  182. inputData &= ~(1 << packet_index);
  183. packet_index++;
  184. // Disable Clk Signal (Not needed if there's a data signal)
  185. EIFR |= (1 << INTF0);
  186. }