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>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include QMK_KEYBOARD_H
21 #include "protocol/serial.h"
23 #include "pincontrol.h"
41 static uint8_t matrix[MATRIX_ROWS];
44 // we're going to need a sleep timer
45 static uint16_t last_activity ;
46 // and a byte to track duplicate up events signalling all keys up.
47 static uint16_t last_upKey ;
48 // serial device can disconnect. Check every MAXDROP characters.
49 static uint16_t disconnect_counter = 0;
53 #define KEY_MASK 0b10000000
54 #define COL_MASK 0b00000111
55 #define ROW_MASK 0b01111000
58 #define ROW(code) (( code & ROW_MASK ) >>3)
59 #define COL(code) ((code & COL_MASK) )
60 #define KEYUP(code) ((code & KEY_MASK) >>7 )
62 static bool is_modified = false;
64 __attribute__ ((weak))
65 void matrix_init_kb(void) {
69 __attribute__ ((weak))
70 void matrix_scan_kb(void) {
74 __attribute__ ((weak))
75 void matrix_init_user(void) {
78 __attribute__ ((weak))
79 void matrix_scan_user(void) {
83 uint8_t matrix_rows(void)
89 uint8_t matrix_cols(void)
95 void pins_init(void) {
96 // set pins for pullups, Rts , power &etc.
98 //print ("pins setup\n");
99 pinMode(VCC_PIN, PinDirectionOutput);
100 digitalWrite(VCC_PIN, PinLevelLow);
102 #if ( HANDSPRING == 0)
105 pinMode(GND_PIN, PinDirectionOutput);
106 digitalWrite(GND_PIN, PinLevelLow);
108 pinMode(PULLDOWN_PIN, PinDirectionOutput);
109 digitalWrite(PULLDOWN_PIN, PinLevelLow);
112 pinMode(DCD_PIN, PinDirectionInput);
113 pinMode(RTS_PIN, PinDirectionInput);
116 /* check that the other side isn't powered up.
117 test=digitalRead(DCD_PIN);
118 xprintf("b%02X:", test);
119 test=digitalRead(RTS_PIN);
120 xprintf("%02X\n", test);
125 uint8_t rts_reset(void) {
126 static uint8_t firstread ;
127 /* bounce RTS so device knows it is rebooted */
129 // On boot, we keep rts as input, then switch roles here
130 // on leaving sleep, we toggle the same way
132 firstread=digitalRead(RTS_PIN);
133 // printf("r%02X:", firstread);
135 pinMode(RTS_PIN, PinDirectionOutput);
137 if (firstread == PinLevelHigh) {
138 digitalWrite(RTS_PIN, PinLevelLow);
141 digitalWrite(RTS_PIN, PinLevelHigh);
145 if (palReadPad(RTS_PIN_IOPRT) == PinLevelLow)
148 palSetPadMode(RTS_PINn_IOPORT, PinDirectionOutput_PUSHPULL);
149 palSetPad(RTS_PORT, RTS_PIN);
153 palSetPadMode(RTS_PIN_RTS_PORT, PinDirectionOutput_PUSHPULL);
154 palSetPad(RTS_PORT, RTS_PIN);
155 palClearPad(RTS_PORT, RTS_PIN);
157 palSetPad(RTS_PORT, RTS_PIN);
167 uint8_t get_serial_byte(void) {
170 code = serial_recv();
172 debug_hex(code); debug(" ");
178 uint8_t palm_handshake(void) {
179 // assumes something has seen DCD go high, we've toggled RTS
180 // and we now need to verify handshake.
181 // listen for up to 4 packets before giving up.
182 // usually I get the sequence FF FA FD
183 static uint8_t codeA=0;
185 for (uint8_t i=0; i < 5; i++) {
186 codeA=get_serial_byte();
187 if ( 0xFA == codeA) {
188 if( 0xFD == get_serial_byte()) {
196 uint8_t palm_reset(void) {
198 rts_reset(); // shouldn't need to power cycle.
200 if ( palm_handshake() ) {
201 last_activity = timer_read();
204 print("failed reset");
210 uint8_t handspring_handshake(void) {
211 // should be sent 15 ms after power up.
212 // listen for up to 4 packets before giving up.
213 static uint8_t codeA=0;
215 for (uint8_t i=0; i < 5; i++) {
216 codeA=get_serial_byte();
217 if ( 0xF9 == codeA) {
218 if( 0xFB == get_serial_byte()) {
226 uint8_t handspring_reset(void) {
227 digitalWrite(VCC_PIN, PinLevelLow);
229 digitalWrite(VCC_PIN, PinLevelHigh);
231 if ( handspring_handshake() ) {
232 last_activity = timer_read();
233 disconnect_counter=0;
241 void matrix_init(void)
244 //debug_matrix =true;
246 serial_init(); // arguments all #defined
248 #if (HANDSPRING == 0)
249 pins_init(); // set all inputs and outputs.
253 digitalWrite(VCC_PIN, PinLevelHigh);
255 // wait for DCD strobe from keyboard - it will do this
256 // up to 3 times, then the board needs the RTS toggled to try again
258 #if ( HANDSPRING == 1)
259 if ( handspring_handshake() ) {
260 last_activity = timer_read();
262 print("failed handshake");
264 //BUG /should/ power cycle or toggle RTS & reset, but this usually works.
267 #else /// Palm / HP device with DCD
268 while( digitalRead(DCD_PIN) != PinLevelHigh ) {;}
271 rts_reset(); // at this point the keyboard should think all is well.
273 if ( palm_handshake() ) {
274 last_activity = timer_read();
276 print("failed handshake");
278 //BUG /should/ power cycle or toggle RTS & reset, but this usually works.
283 // initialize matrix state: all keys off
284 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
286 matrix_init_quantum();
293 uint8_t matrix_scan(void)
296 code = serial_recv();
299 disconnect_counter ++;
300 if (disconnect_counter > MAXDROP) {
302 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
305 // check if the keyboard is asleep.
306 if (timer_elapsed(last_activity) > SLEEP_TIMEOUT) {
317 last_activity = timer_read();
318 disconnect_counter=0; // if we are getting serial data, we're connected.
320 debug_hex(code); debug(" ");
324 case 0xFD: // unexpected reset byte 2
327 case 0xFA: // unexpected reset
333 if (code == last_upKey) {
334 // all keys are not pressed.
335 // Manual says to disable all modifiers left open now.
336 // but that could defeat sticky keys.
337 // BUG? dropping this byte.
342 if (matrix_is_on(ROW(code), COL(code))) {
343 matrix[ROW(code)] &= ~(1<<COL(code));
348 if (!matrix_is_on(ROW(code), COL(code))) {
349 matrix[ROW(code)] |= (1<<COL(code));
354 matrix_scan_quantum();
358 bool matrix_is_modified(void)
364 bool matrix_has_ghost(void)
370 bool matrix_is_on(uint8_t row, uint8_t col)
372 return (matrix[row] & (1<<col));
376 uint8_t matrix_get_row(uint8_t row)
381 void matrix_print(void)
383 print("\nr/c 01234567\n");
384 for (uint8_t row = 0; row < matrix_rows(); row++) {
385 phex(row); print(": ");
386 pbin_reverse(matrix_get_row(row));
391 uint8_t matrix_key_count(void)
394 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
395 count += bitpop(matrix[i]);