2
0

matrix.c 8.6 KB

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