]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - converter/ibmpc_usb/ibmpc_usb.c
ibmpc_usb: Update prebuilt firmware files
[max/tmk_keyboard.git] / converter / ibmpc_usb / ibmpc_usb.c
index 98bba1f6f03b556bf282f6ae669fda9c098183bb..f31e83dd4457c6778d9767440272a05e803369f3 100644 (file)
@@ -113,6 +113,8 @@ uint8_t matrix_scan(void)
     // scan code reading states
     static enum {
         INIT,
+        WAIT_SETTLE,
+        AT_RESET,
         XT_RESET,
         XT_RESET_WAIT,
         XT_RESET_DONE,
@@ -127,35 +129,45 @@ uint8_t matrix_scan(void)
 
 
     if (ibmpc_error) {
-        xprintf("\nERR:%02X\n", ibmpc_error);
+        xprintf("\nERR:%02X ISR:%04X ", ibmpc_error, ibmpc_isr_debug);
 
         // when recv error, neither send error nor buffer full
         if (!(ibmpc_error & (IBMPC_ERR_SEND | IBMPC_ERR_FULL))) {
             // keyboard init again
             if (state == LOOP) {
-                xprintf("init\n");
+                xprintf("[RST] ");
                 state = INIT;
             }
         }
 
         // clear or process error
         ibmpc_error = IBMPC_ERR_NONE;
-    }
-
-    // check ISR state debug
-    if (ibmpc_isr_debug) {
-        xprintf("\nISR:%04X\n", ibmpc_isr_debug);
         ibmpc_isr_debug = 0;
     }
 
-    // check protocol AT/XT
-    if (ibmpc_protocol != current_protocol) {
-        xprintf("\nPROTO:%02X\n", ibmpc_protocol);
+    // check protocol change AT/XT
+    if (ibmpc_protocol && ibmpc_protocol != current_protocol) {
+        xprintf("\nPRT:%02X ISR:%04X ", ibmpc_protocol, ibmpc_isr_debug);
+
+        // protocol change between AT and XT indicates that
+        // keyboard is hotswapped or something goes wrong.
+        // This requires initializing keyboard again probably.
+        if (((current_protocol&IBMPC_PROTOCOL_XT) && (ibmpc_protocol&IBMPC_PROTOCOL_AT)) ||
+            ((current_protocol&IBMPC_PROTOCOL_AT) && (ibmpc_protocol&IBMPC_PROTOCOL_XT))) {
+            if (state == LOOP) {
+                xprintf("[CHG] ");
+                state = INIT;
+            }
+        }
+
         current_protocol = ibmpc_protocol;
+        ibmpc_isr_debug = 0;
     }
 
     switch (state) {
         case INIT:
+            ibmpc_host_disable();
+
             xprintf("I%u ", timer_read());
             keyboard_kind = NONE;
             keyboard_id = 0x0000;
@@ -163,14 +175,39 @@ uint8_t matrix_scan(void)
             matrix_clear();
             clear_keyboard();
 
-            state = XT_RESET;
+            init_time = timer_read();
+            state = WAIT_SETTLE;
+            break;
+        case WAIT_SETTLE:
+            // wait for keyboard to settle after plugin
+            if (timer_elapsed(init_time) > 1000) {
+                state = AT_RESET;
+            }
+            break;
+        case AT_RESET:
+            ibmpc_host_isr_clear();
+            ibmpc_host_enable();
+            wait_ms(1); // keyboard can't respond to command without this
+
+            // SKIDATA-2-DE(and some other keyboards?) stores 'Code Set' setting in nonvlatile memory
+            // and keeps it until receiving reset. Sending reset here may be useful to clear it, perhaps.
+            // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#select-alternate-scan-codesf0
+
+            // reset command
+            if (0xFA == ibmpc_host_send(0xFF)) {
+                state = WAIT_AA;
+            } else {
+                state = XT_RESET;
+            }
+            xprintf("A%u ", timer_read());
             break;
         case XT_RESET:
             // Reset XT-initialize keyboard
             // XT: hard reset 500ms for IBM XT Type-1 keyboard and clones
-            // XT: soft reset 20ms min(clock Lo)
-            ibmpc_host_disable();   // soft reset: inihibit(clock Lo/Data Hi)
-            IBMPC_RST_LO();         // hard reset: reset pin Lo
+            // XT: soft reset 20ms min
+            // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol#keyboard-soft-reset
+            ibmpc_host_disable();   // soft reset: Clock Lo/Data Hi
+            IBMPC_RST_LO();         // hard reset: Reset pin Lo
 
             init_time = timer_read();
             state = XT_RESET_WAIT;
@@ -181,9 +218,9 @@ uint8_t matrix_scan(void)
             }
             break;
         case XT_RESET_DONE:
-            IBMPC_RST_HIZ();        // hard reset: reset pin HiZ
+            IBMPC_RST_HIZ();        // hard reset: Reset pin HiZ
             ibmpc_host_isr_clear();
-            ibmpc_host_enable();    // soft reset: idle(clock Hi/Data Hi)
+            ibmpc_host_enable();    // soft reset: idle(Clock Hi/Data Hi)
 
             xprintf("X%u ", timer_read());
             init_time = timer_read();
@@ -230,19 +267,8 @@ uint8_t matrix_scan(void)
             }
             break;
         case READ_ID:
-            xprintf("R%u ", timer_read());
-
-            // SKIDATA-2-DE(and some other keyboards?) stores 'Code Set' setting in nonvlatile memory
-            // and keeps it until receiving reset. Sending reset here may be useful to clear it, perhaps.
-            // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#select-alternate-scan-codesf0
-            //ibmpc_host_send(0xFF);  // reset command
-            //read_wait(500);         // BAT takes 600-900ms(84-key) or 300-500ms(101/102-key) [8] 4-7, 4-39
-
             keyboard_id = read_keyboard_id();
-            if (ibmpc_error) {
-                xprintf("\nERR:%02X\n", ibmpc_error);
-                ibmpc_error = IBMPC_ERR_NONE;
-            }
+            xprintf("R%u ", timer_read());
 
             if (0x0000 == keyboard_id) {            // CodeSet2 AT(IBM PC AT 84-key)
                 keyboard_kind = PC_AT;
@@ -280,7 +306,7 @@ uint8_t matrix_scan(void)
                 keyboard_kind = PC_AT;
             }
 
-            xprintf("ID:%04X(%d)\n", keyboard_id, keyboard_kind);
+            xprintf("\nID:%04X(%d) ", keyboard_id, keyboard_kind);
 
             state = SETUP;
             break;
@@ -316,17 +342,11 @@ uint8_t matrix_scan(void)
                 // Scan Code Set 2 and 3: 0x00
                 // Buffer full(IBMPC_ERR_FULL): 0xFF
                 if (code == 0x00 || code == 0xFF) {
-                    xprintf("\n!OVERRUN![");
-
-                    // read and ignore data
-                    do {
-                        wait_ms(10);
-                    } while ((code = ibmpc_host_recv()) != -1);
-                    xprintf("]\n");
-
                     // clear stuck keys
                     matrix_clear();
                     clear_keyboard();
+
+                    xprintf("\n[OVR] ");
                     break;
                 }
 
@@ -502,7 +522,7 @@ static int8_t process_cs1(uint8_t code)
     static enum {
         INIT,
         E0,
-        // Pause: E1 1D 45, E1 9D C5 [a] (TODO: test)
+        // Pause: E1 1D 45, E1 9D C5 [a]
         E1,
         E1_1D,
         E1_9D,
@@ -559,7 +579,8 @@ static int8_t process_cs1(uint8_t code)
         case E1_1D:
             switch (code) {
                 case 0x45:
-                    matrix_make(0x55);
+                    matrix_make(0x55); // Pause
+                    state = INIT;
                     break;
                 default:
                     state = INIT;
@@ -568,8 +589,9 @@ static int8_t process_cs1(uint8_t code)
             break;
         case E1_9D:
             switch (code) {
-                case 0x45:
-                    matrix_break(0x55);
+                case 0xC5:
+                    matrix_break(0x55); // Pause
+                    state = INIT;
                     break;
                 default:
                     state = INIT;
@@ -742,10 +764,7 @@ static int8_t process_cs2(uint8_t code)
                     break;
                 case 0xAA:  // Self-test passed
                 case 0xFC:  // Self-test failed
-                    // reset or plugin-in new keyboard
-                    state = INIT;
-                    return -1;
-                    break;
+                    // replug or unstable connection probably
                 default:    // normal key make
                     state = INIT;
                     if (code < 0x80) {
@@ -931,7 +950,7 @@ static int8_t process_cs3(uint8_t code)
                         matrix_make(code);
                     } else {
                         xprintf("!CS3_READY!\n");
-                        //return -1;
+                        return -1;
                     }
             }
             break;
@@ -975,7 +994,7 @@ static int8_t process_cs3(uint8_t code)
                         matrix_break(code);
                     } else {
                         xprintf("!CS3_F0!\n");
-                        //return -1;
+                        return -1;
                     }
             }
             break;