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.

382 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 "matrix.h"
  17. #include "debug.h"
  18. #include "wait.h"
  19. #include "uart.h"
  20. #include "timer.h"
  21. /*
  22. * Matrix Array usage:
  23. *
  24. * ROW: 12(4bits)
  25. * COL: 8(3bits)
  26. *
  27. * +---------+
  28. * 0|00 ... 07|
  29. * 1|00 ... 07|
  30. * :| ... |
  31. * :| ... |
  32. * A| |
  33. * B| |
  34. * +---------+
  35. */
  36. static uint8_t matrix[MATRIX_ROWS];
  37. // we're going to need a sleep timer
  38. static uint16_t last_activity ;
  39. // and a byte to track duplicate up events signalling all keys up.
  40. static uint16_t last_upKey ;
  41. // serial device can disconnect. Check every MAXDROP characters.
  42. static uint16_t disconnect_counter = 0;
  43. // bitmath masks.
  44. #define KEY_MASK 0b10000000
  45. #define COL_MASK 0b00000111
  46. #define ROW_MASK 0b01111000
  47. #define ROW(code) (( code & ROW_MASK ) >>3)
  48. #define COL(code) ((code & COL_MASK) )
  49. #define KEYUP(code) ((code & KEY_MASK) >>7 )
  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. setPinOutput(VCC_PIN);
  78. writePinLow(VCC_PIN);
  79. #if ( HANDSPRING == 0)
  80. #ifdef CY835
  81. setPinOutput(GND_PIN);
  82. writePinLow(GND_PIN);
  83. setPinOutput(PULLDOWN_PIN);
  84. writePinLow(PULLDOWN_PIN);
  85. #endif
  86. setPinInput(DCD_PIN);
  87. setPinInput(RTS_PIN);
  88. #endif
  89. /* check that the other side isn't powered up.
  90. test=gpio_read_pin(DCD_PIN);
  91. xprintf("b%02X:", test);
  92. test=gpio_read_pin(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=gpio_read_pin(RTS_PIN);
  102. // printf("r%02X:", firstread);
  103. setPinOutput(RTS_PIN);
  104. if (firstread) {
  105. writePinLow(RTS_PIN);
  106. }
  107. wait_ms(10);
  108. gpio_write_pin_high(RTS_PIN);
  109. /* the future is Arm
  110. if (!palReadPad(RTS_PIN_IOPRT))
  111. {
  112. wait_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. wait_ms(10);
  122. palSetPad(RTS_PORT, RTS_PIN);
  123. }
  124. */
  125. wait_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 = uart_read();
  133. if (code) {
  134. dprintf("%02X ", code);
  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. writePinLow(VCC_PIN);
  182. wait_ms(5);
  183. gpio_write_pin_high(VCC_PIN);
  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. uart_init(9600); // arguments all #defined
  198. #if (HANDSPRING == 0)
  199. pins_init(); // set all inputs and outputs.
  200. #endif
  201. print("power up\n");
  202. gpio_write_pin_high(VCC_PIN);
  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. wait_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( !gpio_read_pin(DCD_PIN) ) {;}
  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. wait_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_kb();
  228. return;
  229. }
  230. uint8_t matrix_scan(void)
  231. {
  232. uint8_t code;
  233. code = uart_read();
  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. dprintf("%02X ", code);
  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_kb();
  284. return code;
  285. }
  286. inline
  287. bool matrix_has_ghost(void)
  288. {
  289. return false;
  290. }
  291. inline
  292. bool matrix_is_on(uint8_t row, uint8_t col)
  293. {
  294. return (matrix[row] & (1<<col));
  295. }
  296. inline
  297. uint8_t matrix_get_row(uint8_t row)
  298. {
  299. return matrix[row];
  300. }
  301. void matrix_print(void)
  302. {
  303. print("\nr/c 01234567\n");
  304. for (uint8_t row = 0; row < matrix_rows(); row++) {
  305. print_hex8(row); print(": ");
  306. print_bin_reverse8(matrix_get_row(row));
  307. print("\n");
  308. }
  309. }