Kiibohd Controller
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

scan_loop.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #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 CLK_READ PIND
  34. #define CLK_PORT PORTD
  35. #define CLK_DDR DDRD
  36. #define CLK_PIN 1
  37. #define DATA_READ PIND
  38. #define DATA_PORT PORTD
  39. #define DATA_DDR DDRD
  40. #define DATA_PIN 0
  41. #define INTR_PORT PORTD
  42. #define INTR_DDR DDRD
  43. #define INTR_PIN 0
  44. // ----- Macros -----
  45. #define READ_CLK CLK_READ & (1 << CLK_PIN) ? 1 : 0
  46. #define READ_DATA DATA_READ & (1 << DATA_PIN) ? 0 : 1
  47. #define UNSET_INTR() INTR_DDR &= ~(1 << INTR_PIN)
  48. #define SET_INTR() INTR_DDR |= (1 << INTR_PIN)
  49. #define bufferAdd(byte) \
  50. if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
  51. KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
  52. // ----- Variables -----
  53. // Buffer used to inform the macro processing module which keys have been detected as pressed
  54. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  55. volatile uint8_t KeyIndex_BufferUsed;
  56. // Scan Code Retrieval Variables
  57. uint8_t inputData = 0xFF;
  58. uint8_t packet_index = 0;
  59. // ----- Functions -----
  60. // Setup
  61. inline void scan_setup()
  62. {
  63. // Initially reset the keyboard (just in case we are in a wierd state)
  64. scan_resetKeyboard();
  65. // Setup SPI for data input using the clock and data inputs
  66. // TODO
  67. /*
  68. // Setup inputs
  69. CLK_DDR &= ~(1 << CLK_PIN);
  70. DATA_DDR &= ~(1 << DATA_PIN);
  71. // Setup Pull-up's
  72. CLK_PORT &= ~(1 << CLK_PIN); // (CLK)
  73. DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
  74. */
  75. // Setup Keyboard Interrupt
  76. INTR_DDR &= ~(1 << INTR_PIN);
  77. INTR_PORT &= ~(1 << INTR_PIN);
  78. // Setup Keyboard Reset Line
  79. // TODO
  80. }
  81. // Main Detection Loop
  82. inline uint8_t scan_loop()
  83. {
  84. /*
  85. // Packet Read
  86. if ( packet_index == 8 )
  87. {
  88. // Disable Error LED, proper key found
  89. errorLED( 0 );
  90. //#ifdef MAX_DEBUG
  91. // Crazy Debug (Read the Scan Code)
  92. char tmpStr[3];
  93. hexToStr_op( inputData, tmpStr, 2 );
  94. dPrintStrsNL( "Read Data: 0x", tmpStr );
  95. //#endif
  96. // - Map the scan code to the index array -
  97. // If the 8th bit is high, remove the keypress, else, add the keypress
  98. // The lower 7 bits are the array index
  99. KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
  100. // Reset Containers
  101. packet_index = 0;
  102. inputData = 0xFF;
  103. }
  104. // Bad Packet
  105. else if ( packet_index > 8 )
  106. {
  107. // Signal Error
  108. errorLED( 1 );
  109. char tmpStr[3];
  110. int8ToStr( packet_index, tmpStr );
  111. erro_dPrint( "Big packet? Mismatched... ", tmpStr );
  112. packet_index = 0;
  113. inputData = 0xFF;
  114. }
  115. */
  116. // Disable keyboard interrupt (does nothing if already off)
  117. UNSET_INTR();
  118. // Read the clock 8 times
  119. if ( READ_CLK )
  120. {
  121. // Mis-read packet, set back to 0
  122. if ( packet_index == -1 )
  123. packet_index = 0;
  124. // Append 1 bit of data
  125. inputData &= ~(READ_DATA << packet_index);
  126. packet_index++;
  127. // 8 Bits have been read
  128. if ( packet_index == 8 )
  129. {
  130. // Wait till clock edge falls
  131. while ( READ_CLK );
  132. // Sample both lines to make sure this is not a data value
  133. // and definitely the end of packet data blip
  134. uint16_t badDataCounter = 0;
  135. while ( !( READ_DATA ) && !( READ_CLK ) )
  136. badDataCounter++;
  137. if ( badDataCounter < 25 )
  138. {
  139. //#ifdef MAX_DEBUG
  140. // Crazy Debug (Read the Scan Code)
  141. char tmpStr[3];
  142. hexToStr_op( inputData, tmpStr, 2 );
  143. dbug_dPrint( "Read Data: 0x", tmpStr );
  144. //#endif
  145. // - Map the scan code to the index array -
  146. // If the 8th bit is high, remove the keypress, else, add the keypress
  147. // The lower 7 bits are the array index
  148. KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
  149. }
  150. // Even though this is a mis-read packet, we still know what the value is
  151. else
  152. {
  153. // Signal Error
  154. errorLED( 1 );
  155. char tmpStr[3];
  156. hexToStr_op( inputData, tmpStr, 2 );
  157. erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
  158. }
  159. // Reset Containers
  160. inputData = 0xFF;
  161. packet_index = 0;
  162. // Interrupt the keyboard, so we don't get packet pieces...
  163. SET_INTR();
  164. // Do not wait for next clock, let USB do it's thing (if desired)
  165. return packet_index;
  166. }
  167. // Wait till clock edge falls
  168. while ( READ_CLK );
  169. }
  170. // Interrupt keyboard if there is no pending packet
  171. SET_INTR();
  172. return packet_index;
  173. }
  174. // Send data
  175. // XXX Not used with the Tandy1000
  176. uint8_t scan_sendData( uint8_t dataPayload )
  177. {
  178. return 0;
  179. }
  180. // Signal KeyIndex_Buffer that it has been properly read
  181. // TODO
  182. void scan_finishedWithBuffer( void )
  183. {
  184. }
  185. // Reset/Hold keyboard
  186. // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
  187. // The Tandy 1000 keyboard has a dedicated hold/processor interrupt line
  188. void scan_lockKeyboard( void )
  189. {
  190. UNSET_INTR();
  191. }
  192. void scan_unlockKeyboard( void )
  193. {
  194. SET_INTR();
  195. }
  196. // Reset Keyboard
  197. void scan_resetKeyboard( void )
  198. {
  199. // TODO Tandy1000 has a dedicated reset line
  200. }