matrix.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. Copyright 2018 milestogo
  3. with elements Copyright 2014 cy384 under a modified BSD license
  4. building on qmk structure Copyright 2012 Jun Wako <wakojun@gmail.com>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include QMK_KEYBOARD_H
  17. #include "protocol/serial.h"
  18. #include "timer.h"
  19. /*
  20. * Matrix Array usage:
  21. *
  22. * ROW: 12(4bits)
  23. * COL: 8(3bits)
  24. *
  25. * +---------+
  26. * 0|00 ... 07|
  27. * 1|00 ... 07|
  28. * :| ... |
  29. * :| ... |
  30. * A| |
  31. * B| |
  32. * +---------+
  33. */
  34. static uint8_t matrix[MATRIX_ROWS];
  35. // we're going to need a sleep timer
  36. static uint16_t last_activity ;
  37. // and a byte to track duplicate up events signalling all keys up.
  38. static uint16_t last_upKey ;
  39. // serial device can disconnect. Check every MAXDROP characters.
  40. static uint16_t disconnect_counter = 0;
  41. // bitmath masks.
  42. #define KEY_MASK 0b10000000
  43. #define COL_MASK 0b00000111
  44. #define ROW_MASK 0b01111000
  45. #define ROW(code) (( code & ROW_MASK ) >>3)
  46. #define COL(code) ((code & COL_MASK) )
  47. #define KEYUP(code) ((code & KEY_MASK) >>7 )
  48. static bool is_modified = false;
  49. __attribute__ ((weak))
  50. void matrix_init_kb(void) {
  51. matrix_init_user();
  52. }
  53. __attribute__ ((weak))
  54. void matrix_scan_kb(void) {
  55. matrix_scan_user();
  56. }
  57. __attribute__ ((weak))
  58. void matrix_init_user(void) {
  59. }
  60. __attribute__ ((weak))
  61. void matrix_scan_user(void) {
  62. }
  63. inline
  64. uint8_t matrix_rows(void)
  65. {
  66. return MATRIX_ROWS;
  67. }
  68. inline
  69. uint8_t matrix_cols(void)
  70. {
  71. return MATRIX_COLS;
  72. }
  73. void pins_init(void) {
  74. // set pins for pullups, Rts , power &etc.
  75. //print ("pins setup\n");
  76. setPinOutput(VCC_PIN);
  77. writePinLow(VCC_PIN);
  78. #if ( HANDSPRING == 0)
  79. #ifdef CY835
  80. setPinOutput(GND_PIN);
  81. writePinLow(GND_PIN);
  82. setPinOutput(PULLDOWN_PIN);
  83. writePinLow(PULLDOWN_PIN);
  84. #endif
  85. setPinInput(DCD_PIN);
  86. setPinInput(RTS_PIN);
  87. #endif
  88. /* check that the other side isn't powered up.
  89. test=readPin(DCD_PIN);
  90. xprintf("b%02X:", test);
  91. test=readPin(RTS_PIN);
  92. xprintf("%02X\n", test);
  93. */
  94. }
  95. uint8_t rts_reset(void) {
  96. static uint8_t firstread ;
  97. /* bounce RTS so device knows it is rebooted */
  98. // On boot, we keep rts as input, then switch roles here
  99. // on leaving sleep, we toggle the same way
  100. firstread=readPin(RTS_PIN);
  101. // printf("r%02X:", firstread);
  102. setPinOutput(RTS_PIN);
  103. if (firstread) {
  104. writePinLow(RTS_PIN);
  105. }
  106. _delay_ms(10);
  107. writePinHigh(RTS_PIN);
  108. /* the future is Arm
  109. if (!palReadPad(RTS_PIN_IOPRT))
  110. {
  111. _delay_ms(10);
  112. palSetPadMode(RTS_PINn_IOPORT, PinDirectionOutput_PUSHPULL);
  113. palSetPad(RTS_PORT, RTS_PIN);
  114. }
  115. else
  116. {
  117. palSetPadMode(RTS_PIN_RTS_PORT, PinDirectionOutput_PUSHPULL);
  118. palSetPad(RTS_PORT, RTS_PIN);
  119. palClearPad(RTS_PORT, RTS_PIN);
  120. _delay_ms(10);
  121. palSetPad(RTS_PORT, RTS_PIN);
  122. }
  123. */
  124. _delay_ms(5);
  125. //print("rts\n");
  126. return 1;
  127. }
  128. uint8_t get_serial_byte(void) {
  129. static uint8_t code;
  130. while(1) {
  131. code = serial_recv();
  132. if (code) {
  133. debug_hex(code); debug(" ");
  134. return code;
  135. }
  136. }
  137. }
  138. uint8_t palm_handshake(void) {
  139. // assumes something has seen DCD go high, we've toggled RTS
  140. // and we now need to verify handshake.
  141. // listen for up to 4 packets before giving up.
  142. // usually I get the sequence FF FA FD
  143. static uint8_t codeA=0;
  144. for (uint8_t i=0; i < 5; i++) {
  145. codeA=get_serial_byte();
  146. if ( 0xFA == codeA) {
  147. if( 0xFD == get_serial_byte()) {
  148. return 1;
  149. }
  150. }
  151. }
  152. return 0;
  153. }
  154. uint8_t palm_reset(void) {
  155. print("@");
  156. rts_reset(); // shouldn't need to power cycle.
  157. if ( palm_handshake() ) {
  158. last_activity = timer_read();
  159. return 1;
  160. } else {
  161. print("failed reset");
  162. return 0;
  163. }
  164. }
  165. uint8_t handspring_handshake(void) {
  166. // should be sent 15 ms after power up.
  167. // listen for up to 4 packets before giving up.
  168. static uint8_t codeA=0;
  169. for (uint8_t i=0; i < 5; i++) {
  170. codeA=get_serial_byte();
  171. if ( 0xF9 == codeA) {
  172. if( 0xFB == get_serial_byte()) {
  173. return 1;
  174. }
  175. }
  176. }
  177. return 0;
  178. }
  179. uint8_t handspring_reset(void) {
  180. writePinLow(VCC_PIN);
  181. _delay_ms(5);
  182. writePinHigh(VCC_PIN);
  183. if ( handspring_handshake() ) {
  184. last_activity = timer_read();
  185. disconnect_counter=0;
  186. return 1;
  187. } else {
  188. print("-HSreset");
  189. return 0;
  190. }
  191. }
  192. void matrix_init(void)
  193. {
  194. debug_enable = true;
  195. //debug_matrix =true;
  196. serial_init(); // arguments all #defined
  197. #if (HANDSPRING == 0)
  198. pins_init(); // set all inputs and outputs.
  199. #endif
  200. print("power up\n");
  201. writePinHigh(VCC_PIN);
  202. // wait for DCD strobe from keyboard - it will do this
  203. // up to 3 times, then the board needs the RTS toggled to try again
  204. #if ( HANDSPRING == 1)
  205. if ( handspring_handshake() ) {
  206. last_activity = timer_read();
  207. } else {
  208. print("failed handshake");
  209. _delay_ms(1000);
  210. //BUG /should/ power cycle or toggle RTS & reset, but this usually works.
  211. }
  212. #else /// Palm / HP device with DCD
  213. while( !readPin(DCD_PIN) ) {;}
  214. print("dcd\n");
  215. rts_reset(); // at this point the keyboard should think all is well.
  216. if ( palm_handshake() ) {
  217. last_activity = timer_read();
  218. } else {
  219. print("failed handshake");
  220. _delay_ms(1000);
  221. //BUG /should/ power cycle or toggle RTS & reset, but this usually works.
  222. }
  223. #endif
  224. // initialize matrix state: all keys off
  225. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  226. matrix_init_quantum();
  227. return;
  228. }
  229. uint8_t matrix_scan(void)
  230. {
  231. uint8_t code;
  232. code = serial_recv();
  233. if (!code) {
  234. /*
  235. disconnect_counter ++;
  236. if (disconnect_counter > MAXDROP) {
  237. // set all keys off
  238. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  239. }
  240. */
  241. // check if the keyboard is asleep.
  242. if (timer_elapsed(last_activity) > SLEEP_TIMEOUT) {
  243. #if(HANDSPRING ==0 )
  244. palm_reset();
  245. #else
  246. handspring_reset();
  247. #endif
  248. return 0;
  249. }
  250. }
  251. last_activity = timer_read();
  252. disconnect_counter=0; // if we are getting serial data, we're connected.
  253. debug_hex(code); debug(" ");
  254. switch (code) {
  255. case 0xFD: // unexpected reset byte 2
  256. print("rstD ");
  257. return 0;
  258. case 0xFA: // unexpected reset
  259. print("rstA ");
  260. return 0;
  261. }
  262. if (KEYUP(code)) {
  263. if (code == last_upKey) {
  264. // all keys are not pressed.
  265. // Manual says to disable all modifiers left open now.
  266. // but that could defeat sticky keys.
  267. // BUG? dropping this byte.
  268. last_upKey=0;
  269. return 0;
  270. }
  271. // release
  272. if (matrix_is_on(ROW(code), COL(code))) {
  273. matrix[ROW(code)] &= ~(1<<COL(code));
  274. last_upKey=code;
  275. }
  276. } else {
  277. // press
  278. if (!matrix_is_on(ROW(code), COL(code))) {
  279. matrix[ROW(code)] |= (1<<COL(code));
  280. }
  281. }
  282. matrix_scan_quantum();
  283. return code;
  284. }
  285. bool matrix_is_modified(void)
  286. {
  287. return is_modified;
  288. }
  289. inline
  290. bool matrix_has_ghost(void)
  291. {
  292. return false;
  293. }
  294. inline
  295. bool matrix_is_on(uint8_t row, uint8_t col)
  296. {
  297. return (matrix[row] & (1<<col));
  298. }
  299. inline
  300. uint8_t matrix_get_row(uint8_t row)
  301. {
  302. return matrix[row];
  303. }
  304. void matrix_print(void)
  305. {
  306. print("\nr/c 01234567\n");
  307. for (uint8_t row = 0; row < matrix_rows(); row++) {
  308. print_hex8(row); print(": ");
  309. print_bin_reverse8(matrix_get_row(row));
  310. print("\n");
  311. }
  312. }
  313. uint8_t matrix_key_count(void)
  314. {
  315. uint8_t count = 0;
  316. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  317. count += bitpop(matrix[i]);
  318. }
  319. return count;
  320. }