]> git.donarmstrong.com Git - tmk_firmware.git/blobdiff - protocol/ps2_usart.c
Fix rn42.h API
[tmk_firmware.git] / protocol / ps2_usart.c
index d4911428680e31f94354b82a0ef207fe8613cf99..c2d9d0a208d301cc71d8b2bdfcce6f652615fa5a 100644 (file)
@@ -40,11 +40,10 @@ POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <stdbool.h>
-#include <avr/io.h>
 #include <avr/interrupt.h>
 #include <util/delay.h>
 #include "ps2.h"
-#include "debug.h"
+#include "print.h"
 
 
 #define WAIT(stat, us, err) do { \
@@ -82,13 +81,14 @@ uint8_t ps2_host_send(uint8_t data)
 
     /* terminate a transmission if we have */
     inhibit();
-    _delay_us(100);
+    _delay_us(100); // [4]p.13
 
-    /* start bit [1] */
+    /* 'Request to Send' and Start bit */
     data_lo();
     clock_hi();
-    WAIT(clock_lo, 15000, 1);
-    /* data [2-9] */
+    WAIT(clock_lo, 10000, 10);   // 10ms [5]p.50
+
+    /* Data bit[2-9] */
     for (uint8_t i = 0; i < 8; i++) {
         _delay_us(15);
         if (data&(1<<i)) {
@@ -100,15 +100,18 @@ uint8_t ps2_host_send(uint8_t data)
         WAIT(clock_hi, 50, 2);
         WAIT(clock_lo, 50, 3);
     }
-    /* parity [10] */
+
+    /* Parity bit */
     _delay_us(15);
     if (parity) { data_hi(); } else { data_lo(); }
     WAIT(clock_hi, 50, 4);
     WAIT(clock_lo, 50, 5);
-    /* stop bit [11] */
+
+    /* Stop bit */
     _delay_us(15);
     data_hi();
-    /* ack [12] */
+
+    /* Ack */
     WAIT(data_lo, 50, 6);
     WAIT(clock_lo, 50, 7);
 
@@ -127,12 +130,12 @@ ERROR:
     return 0;
 }
 
-// Do polling data from keyboard to get response to last command.
 uint8_t ps2_host_recv_response(void)
 {
+    // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
     uint8_t retry = 25;
     while (retry-- && !pbuf_has_data()) {
-        _delay_ms(1);   // without this delay it seems to fall into deadlock
+        _delay_ms(1);
     }
     return pbuf_dequeue();
 }
@@ -184,11 +187,10 @@ static inline void pbuf_enqueue(uint8_t data)
         pbuf[pbuf_head] = data;
         pbuf_head = next;
     } else {
-        debug("pbuf: full\n");
+        print("pbuf: full\n");
     }
     SREG = sreg;
 }
-
 static inline uint8_t pbuf_dequeue(void)
 {
     uint8_t val = 0;