]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/timer_dma.c
Split start and period set functions
[~andy/csm213a-hw] / hw2 / timer_dma.c
index 7d06bac50e49cc7ad84c42e6ef1acc2225c0f538..a5eb78e211f4fc17420f2d4c4c3751cf27e406f5 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
@@ -84,6 +91,9 @@ tdma_t *tdma_open(tdma_chan_t chan, int alt, PinName pin, PinMode mode)
 \r
        int irqc = mode == PullUp ? 1 : 2;\r
 \r
+       // Initialize global registers\r
+       tdma_init();\r
+\r
        // Allocate port\r
        tdma_t *port    = &tdma_ports[chan];\r
 \r
@@ -167,7 +177,7 @@ void tdma_stop(tdma_t *port, int wait)
 \r
        for (int i = 0; port->dma->dsr & DMA_DSR_BCR_BCR_MASK; i++)\r
                if (i > wait)\r
-                       return 0;\r
+                       return;\r
 \r
        // Disable DMA Mux\r
        port->mux->cfg &= DMAMUX_CHCFG_ENBL_MASK;\r
@@ -215,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