]> git.friedersdorff.com Git - max/tmk_keyboard.git/blobdiff - tmk_core/protocol/ibmpc.c
ibmpc: Fix debug print and wait time
[max/tmk_keyboard.git] / tmk_core / protocol / ibmpc.c
index f6c5e5c3727193e5da0606c4b4fe15ed520db3f0..374e7fa4b7341e2308aa827607526aeec240dcba 100644 (file)
@@ -42,7 +42,6 @@ POSSIBILITY OF SUCH DAMAGE.
 #include <stdbool.h>
 #include <avr/interrupt.h>
 #include <util/atomic.h>
-#include "ringbuf.h"
 #include "ibmpc.h"
 #include "debug.h"
 #include "timer.h"
@@ -57,18 +56,20 @@ POSSIBILITY OF SUCH DAMAGE.
 } while (0)
 
 
-#define BUF_SIZE 16
-static uint8_t buf[BUF_SIZE];
-static ringbuf_t rb = {
-    .buffer = buf,
-    .head = 0,
-    .tail = 0,
-    .size_mask = BUF_SIZE - 1
-};
-
 volatile uint8_t ibmpc_protocol = IBMPC_PROTOCOL_AT;
 volatile uint8_t ibmpc_error = IBMPC_ERR_NONE;
 
+/* 2-byte buffer for data received from keyhboard
+ * buffer states:
+ *      FFFF: empty
+ *      FFss: one data
+ *      sstt: two data(full)
+ *  0xFF can not be stored as data in buffer because it means empty or no data.
+ */
+static volatile uint16_t recv_data = 0xFFFF;
+/* internal state of receiving data */
+static volatile uint16_t isr_data = 0x8000;
+
 void ibmpc_host_init(void)
 {
     // initialize reset pin to HiZ
@@ -142,7 +143,7 @@ int16_t ibmpc_host_send(uint8_t data)
     WAIT(data_hi, 50, 9);
 
     // clear buffer to get response correctly
-    ringbuf_reset(&rb);
+    recv_data = 0xFFFF;
     ibmpc_host_isr_clear();
 
     idle();
@@ -156,27 +157,27 @@ ERROR:
 }
 
 /*
- * Receive data from keyboard with ISR
+ * Receive data from keyboard
  */
-static volatile int16_t recv_data = -1;
-static volatile uint16_t isr_data = 0x8000;
-
-void ibmpc_host_isr_clear(void)
-{
-    isr_data = 0x8000;
-}
-
 int16_t ibmpc_host_recv(void)
 {
-    int16_t data = 0;
+    uint16_t data = 0;
+    uint8_t ret = 0xFF;
+
     ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
         data = recv_data;
-        recv_data = -1;
-    }
-    if (data != -1) {
-        dprintf("r%04X ", data);
+        if ((data&0xFF00) != 0xFF00) {      // recv_data:sstt -> recv_data:FFtt, ret:ss
+            ret = (data>>8)&0x00FF;
+            recv_data = data | 0xFF00;
+        } else if (data != 0xFFFF) {        // recv_data:FFss -> recv_data:FFFF, ret:ss
+            ret = data&0x00FF;
+            recv_data = data | 0x00FF;
+        }
     }
-    return data;
+
+    if (ret != 0xFF) dprintf("r%02X ", ret);
+    if (recv_data != 0xFFFF) dprintf("b%04X ", recv_data);
+    return ((ret != 0xFF) ? ret : -1);
 
 }
 
@@ -191,17 +192,27 @@ int16_t ibmpc_host_recv_response(void)
     return data;
 }
 
+void ibmpc_host_isr_clear(void)
+{
+    isr_data = 0x8000;
+    recv_data = 0xFFFF;
+}
+
+// NOTE: With this ISR data line can be read within 2us after clock falling edge.
+// To read data line early as possible:
+// write naked ISR with asembly code to read the line and call C func to do other job?
 ISR(IBMPC_INT_VECT)
 {
     uint8_t dbit;
     dbit = IBMPC_DATA_PIN&(1<<IBMPC_DATA_BIT);
+PINB|=0x01;
     isr_data = isr_data>>1;
     if (dbit) isr_data |= 0x8000;
 
-    // isr_data:
+    // isr_data: state of receiving data from keyboard
     //            15 14 13 12   11 10  9  8    7  6  5  4    3  2  1  0
     //            -----------------------------------------------------
-    // Initial:   *1  0  0  0    0  0  0  0 |  0  0  0  0    0  0  0  0
+    // Initial:   *1  0  0  0    0  0  0  0 |  0  0  0  0    0  0  0  0     MSB sentinel
     // XT IBM:    b7 b6 b5 b4   b3 b2 b1 b0 | s1 s0 *1  0    0  0  0  0     after receiving **
     // XT Clone:  b7 b6 b5 b4   b3 b2 b1 b0 | s1 *1  0  0    0  0  0  0     after receiving
     // AT:        st pr b7 b6   b5 b4 b3 b2 | b1 b0 s0 *1    0  0  0  0     after receiving
@@ -236,27 +247,37 @@ ISR(IBMPC_INT_VECT)
             break;
         case 0b11000000:
             // XT Clone-done
-            recv_data = (isr_data>>8) & 0xFF;
+            recv_data = recv_data<<8;
+            recv_data |= (isr_data>>8) & 0xFF;
             goto DONE;
             break;
         case 0b10100000:
-            // XT IBM-done or AT-midway
-            // wait and check for clock of AT stop bit
-            if (wait_clock_hi(100) && wait_clock_lo(100)) { // FIXME this makes ISR prologe long
-                // AT-midway
-                return;
-            } else {
-                // XT-IBM-done
-                recv_data = (isr_data>>8) & 0xFF;
-                goto DONE;
-            }
+            {
+                uint8_t us = 100;
+                // wait for rising and falling edge of AT stop bit
+                while (!(IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)) && us) { wait_us(1); us--; }
+                while (  IBMPC_CLOCK_PIN&(1<<IBMPC_CLOCK_BIT)  && us) { wait_us(1); us--; }
+
+                if (us) {
+                    // found stop bit: return immediately and process the stop bit in ISR
+                    // AT-midway
+                    return;
+                } else {
+                    // no stop bit
+                    // XT-IBM-done
+                    recv_data = recv_data<<8;
+                    recv_data |= (isr_data>>8) & 0xFF;
+                    goto DONE;
+                }
+             }
             break;
         case 0b00010000:
         case 0b10010000:
         case 0b01010000:
         case 0b11010000:
             // AT-done
-            recv_data = (isr_data>>6) & 0xFF;
+            recv_data = recv_data<<8;
+            recv_data |= (isr_data>>6) & 0xFF;
             goto DONE;
             break;
         case 0b01100000:
@@ -270,7 +291,9 @@ ISR(IBMPC_INT_VECT)
             break;
     }
 DONE:
-    // TODO: buffer for recv_data
+    // TODO: check protocol change to support keyboard howswap
+    //       not correct if there is clock edge within short time like 100us after receving data
+    // TODO: process error code: 0x00(AT), 0xFF(XT) in particular
     isr_data = 0x8000;  // clear to next data
     return;
 }