]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - tmk_core/protocol/xt_interrupt.c
xt_usb: Fix warning on switch-case
[max/tmk_keyboard.git] / tmk_core / protocol / xt_interrupt.c
index 2a351c5b35a093aaa4c9242c63bb07cc7880419c..525dbcf2c318b451177a6aa18253c1a02beaf81b 100644 (file)
@@ -51,6 +51,22 @@ POSSIBILITY OF SUCH DAMAGE.
 void xt_host_init(void)
 {
     XT_INT_INIT();
+    XT_INT_OFF();
+
+    /* hard reset */
+#ifdef XT_RESET
+    XT_RESET();
+#endif
+
+    /* soft reset: pull clock line down for 20ms */
+    XT_DATA_LO();
+    XT_CLOCK_LO();
+    _delay_ms(20);
+
+    /* input mode with pullup */
+    XT_CLOCK_IN();
+    XT_DATA_IN();
+
     XT_INT_ON();
 }
 
@@ -66,48 +82,42 @@ uint8_t xt_host_recv(void)
 
 ISR(XT_INT_VECT)
 {
+    /*
+     * XT signal format consits of 10 or 9 clocks and sends start bits and 8-bit data,
+     * which should be read on falling edge of clock.
+     *
+     *  start(0), start(1), bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7
+     *
+     * Original IBM XT keyboard sends start(0) bit while some of clones don't.
+     * Start(0) bit is read as low on data line while start(1) as high.
+     *
+     * https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
+     */
     static enum {
-        INIT,
-        BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
-        STOP,
-    } state = INIT;
+        START, BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7
+    } state = START;
     static uint8_t data = 0;
-    // wait for clock falling edge
-    if(state != INIT)
-        wait_clock_lo(70);
+
+    uint8_t dbit = XT_DATA_READ();
+
+    // This is needed if using PCINT which can be called on both falling and rising edge
+    //if (clock_in()) return;
 
     switch (state) {
-        case INIT:
-            if (data_in())
-                state++;
+        case START:
+            // ignore start(0) bit
+            if (!dbit) return;
             break;
-        case BIT0:
-        case BIT1:
-        case BIT2:
-        case BIT3:
-        case BIT4:
-        case BIT5:
-        case BIT6:
-        case BIT7:
-            state++;
+        case BIT0 ... BIT7:
             data >>= 1;
-            if (data_in()) {
+            if (dbit)
                 data |= 0x80;
-            }
             break;
-        case STOP:
-            if (!data_in())
-                goto DONE;
-            pbuf_enqueue(data);
-            goto DONE;
-            break;
-        default:
-            goto DONE;
     }
-    goto RETURN;
-DONE:
-    state = INIT;
-    data = 0;
-RETURN:
+    if (state++ == BIT7) {
+        pbuf_enqueue(data);
+        state = START;
+        data = 0;
+    }
     return;
 }