]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/timer_dma.c
Fix rising/falling edge capture
[~andy/csm213a-hw] / hw2 / timer_dma.c
index 529fd7e2a59d2873563995cf5d5800f698df91ba..36fdc979e567527600268f1c27360c187460041b 100644 (file)
@@ -49,6 +49,10 @@ static tdma_t tdma_ports[TDMA_NUM_CHAN];
 /* Global timer initialization */\r
 void tdma_init(void)\r
 {\r
+       static int tdma_init_done = 0;\r
+       if (tdma_init_done)\r
+               return;\r
+\r
        // Enable DMA Cock\r
        SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;\r
        SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;\r
@@ -73,6 +77,9 @@ void tdma_init(void)
        // Start timers\r
        PIT->CHANNEL[0].TCTRL |= PIT_TCTRL_TEN_MASK;\r
        PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_TEN_MASK;\r
+\r
+       // Done\r
+       tdma_init_done = 1;\r
 }\r
 \r
 /* DMA Functions */\r
@@ -82,7 +89,10 @@ tdma_t *tdma_open(tdma_chan_t chan, int alt, PinName pin, PinMode mode)
                   pin >= PTC0 ? TDMA_REQ_PTC :\r
                   pin >= PTA0 ? TDMA_REQ_PTA : 0;\r
 \r
-       int irqc = mode == PullUp ? 1 : 2;\r
+       int irqc = mode == PullDown ? 1 : 2;\r
+\r
+       // Initialize global registers\r
+       tdma_init();\r
 \r
        // Allocate port\r
        tdma_t *port    = &tdma_ports[chan];\r
@@ -158,13 +168,17 @@ void tdma_start(tdma_t *port)
        //sirq_printf("isfr3: %08x\r\n", PORTD->ISFR);\r
 }\r
 \r
-void tdma_stop(tdma_t *port)\r
+void tdma_stop(tdma_t *port, int wait)\r
 {\r
        if (!port)\r
                return;\r
 \r
        //sirq_printf("isfr0: %08x\r\n", PORTD->ISFR);\r
 \r
+       for (int i = 0; port->dma->dsr & DMA_DSR_BCR_BCR_MASK; i++)\r
+               if (i > wait)\r
+                       return;\r
+\r
        // Disable DMA Mux\r
        port->mux->cfg &= DMAMUX_CHCFG_ENBL_MASK;\r
 \r
@@ -211,7 +225,7 @@ uint64_t tdma_time(void)
                        | ((uint64_t)~tml) << 0;\r
 \r
        // Convert to nanoseconds\r
-       return clocks * 1000 / 24;\r
+       return clocks * 125 / 3;\r
 }\r
 \r
 void tdma_debug(tdma_t *port)\r