]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/serial_irq.c
Improve time sync accuracy
[~andy/csm213a-hw] / hw2 / serial_irq.c
index 67beeab353ff9ac707eb13696a678925f41443e9..68945cfcbb759e8e46361046f7c40c7ce7734e0f 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdio.h>\r
 #include "serial_api.h"\r
 #include "serial_irq.h"\r
+#include "timer_dma.h"\r
 \r
 /* Defines */\r
 #define SIRQ_LEN  1024\r
@@ -21,6 +22,14 @@ struct sirq_t {
        queue_t  rcv;\r
 };\r
 \r
+/* Test data */\r
+extern uint32_t test_xmt_enab;\r
+extern uint64_t test_xmt_time0;\r
+extern uint64_t test_xmt_time1;\r
+\r
+extern uint32_t test_rcv_enab;\r
+extern uint64_t test_rcv_time;\r
+\r
 /* Port data */\r
 static sirq_t sirq_ports[SIRQ_NUM_UART];\r
 \r
@@ -32,7 +41,17 @@ void sirq_handler(uint32_t _port, SerialIrq event)
        // Handle transmit\r
        if (event == TxIrq && port->xmt.rix != port->xmt.wix) {\r
                int byte = port->xmt.buf[port->xmt.rix];\r
+\r
+               uint64_t time0 = tdma_time();\r
                serial_putc(&port->uart, byte);\r
+               uint64_t time1 = tdma_time();\r
+\r
+               if (test_xmt_enab) {\r
+                       test_xmt_time0 = time0;\r
+                       test_xmt_time1 = time1;\r
+                       test_xmt_enab  = 0;\r
+               }\r
+\r
                port->xmt.rix = (port->xmt.rix+1) % SIRQ_LEN;\r
        } else {\r
                serial_irq_set(&port->uart, TxIrq, 0);\r
@@ -75,7 +94,7 @@ void sirq_putc(sirq_t *port, int byte)
 int sirq_getc(sirq_t *port)\r
 {\r
        int byte = 0;\r
-       if (port->rcv.rix < port->rcv.wix) {\r
+       if (port->rcv.rix != port->rcv.wix) {\r
                byte = port->rcv.buf[port->rcv.rix];\r
                port->rcv.rix = (port->rcv.rix+1) % SIRQ_LEN;\r
        }\r
@@ -93,7 +112,29 @@ void sirq_write(sirq_t *port, void *data, int len)
 /* Check if port is writable */\r
 int sirq_ready(sirq_t *port)\r
 {\r
-       return port->rcv.rix < port->rcv.wix;\r
+       return port->rcv.rix != port->rcv.wix;\r
+}\r
+\r
+/* Debug print */\r
+void sirq_debug(sirq_t *port)\r
+{\r
+       sirq_printf("xmt  - wix:%03x rix:%03x\r\n", port->xmt.wix, port->xmt.rix);\r
+       sirq_printf("rcv  - wix:%03x rix:%03x\r\n", port->rcv.wix, port->rcv.rix);\r
+       sirq_printf("irq  - ??\r\n");\r
+       sirq_printf("uart - ??\r\n");\r
+\r
+       // __IO uint8_t BDH;     \r
+       // __IO uint8_t BDL;     \r
+       // __IO uint8_t C1;      \r
+       // __IO uint8_t C2;      \r
+       // __IO uint8_t S1;      \r
+       // __IO uint8_t S2;      \r
+       // __IO uint8_t C3;      \r
+       // __IO uint8_t D;       \r
+       // __IO uint8_t MA1;     \r
+       // __IO uint8_t MA2;     \r
+       // __IO uint8_t C4;      \r
+       // __IO uint8_t C5;      \r
 }\r
 \r
 /* Write ASCII data to the output queue */\r