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.

381 lines
8.1 KiB

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