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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /* XXX OLD CODE - Somewhat worked, has glitches, and is not compatible with the current API
  119. // Read the clock 8 times
  120. if ( READ_CLK )
  121. {
  122. // Mis-read packet, set back to 0
  123. if ( packet_index == -1 )
  124. packet_index = 0;
  125. // Append 1 bit of data
  126. inputData &= ~(READ_DATA << packet_index);
  127. packet_index++;
  128. // 8 Bits have been read
  129. if ( packet_index == 8 )
  130. {
  131. // Wait till clock edge falls
  132. while ( READ_CLK );
  133. // Sample both lines to make sure this is not a data value
  134. // and definitely the end of packet data blip
  135. uint16_t badDataCounter = 0;
  136. while ( !( READ_DATA ) && !( READ_CLK ) )
  137. badDataCounter++;
  138. if ( badDataCounter < 25 )
  139. {
  140. //#ifdef MAX_DEBUG
  141. // Crazy Debug (Read the Scan Code)
  142. char tmpStr[3];
  143. hexToStr_op( inputData, tmpStr, 2 );
  144. dbug_dPrint( "Read Data: 0x", tmpStr );
  145. //#endif
  146. // - Map the scan code to the index array -
  147. // If the 8th bit is high, remove the keypress, else, add the keypress
  148. // The lower 7 bits are the array index
  149. KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
  150. }
  151. // Even though this is a mis-read packet, we still know what the value is
  152. else
  153. {
  154. // Signal Error
  155. errorLED( 1 );
  156. char tmpStr[3];
  157. hexToStr_op( inputData, tmpStr, 2 );
  158. erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
  159. }
  160. // Reset Containers
  161. inputData = 0xFF;
  162. packet_index = 0;
  163. // Interrupt the keyboard, so we don't get packet pieces...
  164. SET_INTR();
  165. // Do not wait for next clock, let USB do it's thing (if desired)
  166. return packet_index;
  167. }
  168. // Wait till clock edge falls
  169. while ( READ_CLK );
  170. }
  171. */
  172. // Interrupt keyboard if there is no pending packet
  173. SET_INTR();
  174. return packet_index;
  175. }
  176. // Send data
  177. // XXX Not used with the Tandy1000
  178. uint8_t scan_sendData( uint8_t dataPayload )
  179. {
  180. return 0;
  181. }
  182. // Signal KeyIndex_Buffer that it has been properly read
  183. // TODO
  184. void scan_finishedWithBuffer( uint8_t sentKeys )
  185. {
  186. }
  187. // Signal that the keys have been properly sent over USB
  188. void scan_finishedWithUSBBuffer( uint8_t sentKeys )
  189. {
  190. }
  191. // Reset/Hold keyboard
  192. // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
  193. // The Tandy 1000 keyboard has a dedicated hold/processor interrupt line
  194. void scan_lockKeyboard( void )
  195. {
  196. UNSET_INTR();
  197. }
  198. void scan_unlockKeyboard( void )
  199. {
  200. SET_INTR();
  201. }
  202. // Reset Keyboard
  203. void scan_resetKeyboard( void )
  204. {
  205. // TODO Tandy1000 has a dedicated reset line
  206. }