]> git.donarmstrong.com Git - tmk_firmware.git/blobdiff - protocol/ps2.c
Add ps2_usart.c and fix set_led at USB wake
[tmk_firmware.git] / protocol / ps2.c
index cf7b1f43ce42076e9b6eb876b844180afb427cc3..4886b16d0356b521ed895a5c46ac9b4f1ec2e14f 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright 2010,2011 Jun WAKO <wakojun@gmail.com>
+Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
 
 This software is licensed with a Modified BSD License.
 All of this is supposed to be Free Software, Open Source, DFSG-free,
@@ -43,7 +43,9 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "debug.h"
 
 
+#ifndef PS2_USE_INT
 static uint8_t recv_data(void);
+#endif
 static inline void clock_lo(void);
 static inline void clock_hi(void);
 static inline bool clock_in(void);
@@ -89,6 +91,9 @@ uint8_t ps2_error = PS2_ERR_NONE;
 
 void ps2_host_init(void)
 {
+    // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
+    _delay_ms(2500);
+
 #ifdef PS2_USE_INT
     PS2_INT_INIT();
     PS2_INT_ON();
@@ -109,12 +114,12 @@ uint8_t ps2_host_send(uint8_t data)
 #endif
     /* terminate a transmission if we have */
     inhibit();
-    _delay_us(100);
+    _delay_us(200); // at least 100us
 
     /* start bit [1] */
     data_lo();
     clock_hi();
-    WAIT(clock_lo, 15000, 1);
+    WAIT(clock_lo, 20000, 10);   // may take 15ms at most until device starts clocking
     /* data [2-9] */
     for (uint8_t i = 0; i < 8; i++) {
         _delay_us(15);
@@ -143,6 +148,9 @@ uint8_t ps2_host_send(uint8_t data)
     WAIT(clock_hi, 50, 8);
     WAIT(data_hi, 50, 9);
 
+#ifdef PS2_USE_INT
+    PS2_INT_ON();
+#endif
     res = ps2_host_recv_response();
 ERROR:
 #ifdef PS2_USE_INT
@@ -154,42 +162,42 @@ ERROR:
     return res;
 }
 
+#ifndef PS2_USE_INT
 /* receive data when host want else inhibit communication */
 uint8_t ps2_host_recv_response(void)
 {
+    // Command might take 20ms to response([3]p.21)
+    // TrackPoint might take 25ms ([5]2.7)
     uint8_t data = 0;
+    uint8_t try = 200;
+    while (try-- && (data = ps2_host_recv())) ;
+    return data;
+}
+#endif
 
-    /* terminate a transmission if we have */
-    inhibit();
-    _delay_us(100);
+#ifndef PS2_USE_INT
+uint8_t ps2_host_recv(void)
+{
+    uint8_t data = 0;
 
     /* release lines(idle state) */
     idle();
 
     /* wait start bit */
-    wait_clock_lo(2000);
+    wait_clock_lo(100); // TODO: this is enough?
     data = recv_data();
 
     inhibit();
     return data;
 }
-
-#ifndef PS2_USE_INT
-uint8_t ps2_host_recv(void)
-{
-    return ps2_host_recv_response();
-}
 #else
 /* ring buffer to store ps/2 key data */
-#define PBUF_SIZE 8
+#define PBUF_SIZE 32
 static uint8_t pbuf[PBUF_SIZE];
 static uint8_t pbuf_head = 0;
 static uint8_t pbuf_tail = 0;
 static inline void pbuf_enqueue(uint8_t data)
 {
-    if (!data)
-        return;
-
     uint8_t sreg = SREG;
     cli();
     uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
@@ -215,27 +223,34 @@ static inline uint8_t pbuf_dequeue(void)
 
     return val;
 }
+static inline bool pbuf_has_data(void)
+{
+    uint8_t sreg = SREG;
+    cli();
+    bool has_data = (pbuf_head != pbuf_tail);
+    SREG = sreg;
+    return has_data;
+}
+static inline void pbuf_clear(void)
+{
+    uint8_t sreg = SREG;
+    cli();
+    pbuf_head = pbuf_tail = 0;
+    SREG = sreg;
+}
 
 /* get data received by interrupt */
 uint8_t ps2_host_recv(void)
 {
-    if (ps2_error) {
-        print("x");
-        phex(ps2_error);
-        ps2_host_send(0xFE);    // request to resend
-        ps2_error = PS2_ERR_NONE;
-    }
-    idle();
     return pbuf_dequeue();
 }
 
-#if 0
-#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
-#define DEBUGP(x) do { PORTC = x; } while (0)
-#else
-#define DEBUGP_INIT()
-#define DEBUGP(x)
-#endif
+uint8_t ps2_host_recv_response(void)
+{
+    while (!pbuf_has_data()) ;
+    return pbuf_dequeue();
+}
+
 ISR(PS2_INT_VECT)
 {
     static enum {
@@ -256,7 +271,6 @@ ISR(PS2_INT_VECT)
     }
 
     state++;
-    DEBUGP(state);
     switch (state) {
         case START:
             if (data_in())
@@ -289,6 +303,7 @@ ISR(PS2_INT_VECT)
             if (!data_in())
                 goto ERROR;
             pbuf_enqueue(data);
+//phex(data);
             goto DONE;
             break;
         default:
@@ -296,7 +311,6 @@ ISR(PS2_INT_VECT)
     }
     goto RETURN;
 ERROR:
-    DEBUGP(0x0F);
     inhibit();
     ps2_error = state;
 DONE:
@@ -309,11 +323,6 @@ RETURN:
 #endif
 
 
-static void ps2_reset(void)
-{
-    ps2_host_send(0xFF);
-}
-
 /* send LED state to keyboard */
 void ps2_host_set_led(uint8_t led)
 {
@@ -322,6 +331,7 @@ void ps2_host_set_led(uint8_t led)
 }
 
 
+#ifndef PS2_USE_INT
 /* called after start bit comes */
 static uint8_t recv_data(void)
 {
@@ -361,6 +371,7 @@ static uint8_t recv_data(void)
 ERROR:
     return 0;
 }
+#endif
 
 static inline void clock_lo()
 {
@@ -433,3 +444,26 @@ static inline void inhibit(void)
     clock_lo();
     data_hi();
 }
+
+
+/* PS/2 Resources
+ *
+ * [1] The PS/2 Mouse/Keyboard Protocol
+ * http://www.computer-engineering.org/ps2protocol/
+ * Concise and thorough primer of PS/2 protocol.
+ *
+ * [2] Keyboard and Auxiliary Device Controller
+ * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
+ * Signal Timing and Format
+ *
+ * [3] Keyboards(101- and 102-key)
+ * http://www.mcamafia.de/pdf/ibm_hitrc11.pdf
+ * Keyboard Layout, Scan Code Set, POR, and Commands.
+ *
+ * [4] PS/2 Reference Manuals
+ * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
+ * Collection of IBM Personal System/2 documents.
+ *
+ * [5] TrackPoint Engineering Specifications for version 3E
+ * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html
+ */