]> git.friedersdorff.com Git - max/tmk_keyboard.git/commitdiff
ibmpc: Add ibmpc_host_clear_isr
authortmk <hasu@tmk-kbd.com>
Wed, 22 Jan 2020 05:52:01 +0000 (14:52 +0900)
committertmk <hasu@tmk-kbd.com>
Sat, 29 Feb 2020 08:29:54 +0000 (17:29 +0900)
tmk_core/protocol/ibmpc.c
tmk_core/protocol/ibmpc.h

index f9374089a92bc322fffdcde8b9855c492259902e..32977e1802664295ca7fe40ee44587f0165d13bf 100644 (file)
@@ -88,6 +88,7 @@ void ibmpc_host_enable(void)
 
 void ibmpc_host_disable(void)
 {
+    // TODO: test order? uneeded interrupt happens by making clock lo
     inhibit();
     IBMPC_INT_OFF();
 }
@@ -143,6 +144,10 @@ int16_t ibmpc_host_send(uint8_t data)
     WAIT(clock_hi, 50, 8);
     WAIT(data_hi, 50, 9);
 
+    // clear buffer to get response correctly
+    ringbuf_reset(&rb);
+    ibmpc_host_isr_clear();
+
     idle();
     IBMPC_INT_ON();
     return ibmpc_host_recv_response();
@@ -173,30 +178,41 @@ int16_t ibmpc_host_recv(void)
     return data;
 }
 
-ISR(IBMPC_INT_VECT)
+
+/*
+ * Receive data from keyboard with ISR
+ */
+static enum {
+    START,
+    BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
+    PARITY,
+    STOP, } isr_state = START;
+static uint8_t isr_data = 0;
+static uint8_t isr_parity = 1;
+static uint16_t isr_time = 0;
+
+void ibmpc_host_isr_clear(void)
 {
-    static uint16_t last_time = 0;
-    static enum {
-        START,
-        BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
-        PARITY,
-        STOP,
-    } state = START;
-    static uint8_t data = 0;
-    static uint8_t parity = 1;
+    isr_state = START;
+    isr_data = 0;
+    isr_parity = 1;
+    isr_time = 0;
+}
 
+ISR(IBMPC_INT_VECT)
+{
     uint8_t dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
 
     // Reset state when taking more than 1ms
-    if (last_time && timer_elapsed(last_time) > 10) {
-        ibmpc_error = IBMPC_ERR_TIMEOUT | IBMPC_ERR_RECV | state;
-        state = START;
-        data = 0;
-        parity = 1;
+    if (isr_time && timer_elapsed(isr_time) > 1) {
+        ibmpc_error = IBMPC_ERR_TIMEOUT | IBMPC_ERR_RECV | isr_state;
+        isr_state = START;
+        isr_data = 0;
+        isr_parity = 1;
     }
-    last_time = timer_read();
+    isr_time = timer_read();
 
-    switch (state) {
+    switch (isr_state) {
         case START:
             if (ibmpc_protocol == IBMPC_PROTOCOL_XT) {
                 // ignore start(0) bit
@@ -214,13 +230,13 @@ ISR(IBMPC_INT_VECT)
         case BIT5:
         case BIT6:
         case BIT7:
-            data >>= 1;
+            isr_data >>= 1;
             if (dbit) {
-                data |= 0x80;
-                parity++;
+                isr_data |= 0x80;
+                isr_parity++;
             }
-            if (state == BIT7 && ibmpc_protocol == IBMPC_PROTOCOL_XT) {
-                if (!ringbuf_put(&rb, data)) {
+            if (isr_state == BIT7 && ibmpc_protocol == IBMPC_PROTOCOL_XT) {
+                if (!ringbuf_put(&rb, isr_data)) {
                     ibmpc_error = IBMPC_ERR_FULL;
                     goto ERROR;
                 }
@@ -230,17 +246,17 @@ ISR(IBMPC_INT_VECT)
             break;
         case PARITY:
             if (dbit) {
-                if (!(parity & 0x01))
+                if (!(isr_parity & 0x01))
                     goto ERROR;
             } else {
-                if (parity & 0x01)
+                if (isr_parity & 0x01)
                     goto ERROR;
             }
             break;
         case STOP:
             if (!dbit)
                 goto ERROR;
-            if (!ringbuf_put(&rb, data)) {
+            if (!ringbuf_put(&rb, isr_data)) {
                 ibmpc_error = IBMPC_ERR_FULL;
                 goto ERROR;
             }
@@ -253,17 +269,17 @@ ISR(IBMPC_INT_VECT)
     goto NEXT;
 
 ERROR:
-    ibmpc_error |= state;
+    ibmpc_error |= isr_state;
     ibmpc_error |= IBMPC_ERR_RECV;
     ringbuf_reset(&rb);
 DONE:
-    last_time = 0;
-    state = START;
-    data = 0;
-    parity = 1;
+    isr_state = START;
+    isr_data = 0;
+    isr_parity = 1;
+    isr_time = 0;
     return;
 NEXT:
-    state++;
+    isr_state++;
     return;
 }
 
index ebaac137d720ee16eb98fb5e7e335b6ae4d0136c..fc117f23c174cd8b7d09d0a4df4fd68e6d7419f6 100644 (file)
@@ -94,6 +94,7 @@ void ibmpc_host_disable(void);
 int16_t ibmpc_host_send(uint8_t data);
 int16_t ibmpc_host_recv_response(void);
 int16_t ibmpc_host_recv(void);
+void ibmpc_host_isr_clear(void);
 void ibmpc_host_set_led(uint8_t usb_led);