Zoltan Hudak / MODDMA
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example3.h Source File

example3.h

00001 /*
00002  * Demonstrates capturing the GPIO P0.4 to P0.7 "nibble" to memory
00003  * using GPDMA. The transfers from port pins to memory buffer are
00004  * triggered using Timer1 MAT1.0 match compare.
00005  *
00006  * In this example all inputs have pullups. So with nothing connected
00007  * the P0.4/7 reads as 0xF. Connecting a wire from one or more of the four
00008  * inputs to ground will show up in the captured buffer sequence.
00009  */
00010 #include "mbed.h"
00011 #include "MODDMA.h"
00012 #include "iomacros.h"   // within MODDMA library.
00013 
00014 // How long between grabbing GPIO FIO0PIN register.
00015 
00016 // Value is in microseconds. (500000 is half a second).
00017 #define SAMPLE_PERIOD   500000
00018 
00019 #define NUM_OF_SAMPLES  5
00020 
00021 DigitalOut      led1(LED1);
00022 DigitalOut      led2(LED2);
00023 DigitalOut      led3(LED3);
00024 
00025 uint32_t        buffer[NUM_OF_SAMPLES];
00026 bool            dmaTransferComplete;
00027 
00028 MODDMA          dma;
00029 MODDMA_Config*  conf;
00030 
00031 void            TC0_callback(void);
00032 void            ERR0_callback(void);
00033 
00034 /**
00035  * @brief
00036  * @note
00037  * @param
00038  * @retval
00039  */
00040 int main()
00041 {
00042     volatile int    life_counter = 0;
00043 
00044     // Macros defined in iomacros.h, saves messing with DigitalIn
00045     p30_AS_INPUT;
00046     p30_MODE(PIN_PULLUP);               // P0.4
00047     p29_AS_INPUT;
00048     p29_MODE(PIN_PULLUP);               // P0.5
00049     p8_AS_INPUT;
00050     p8_MODE(PIN_PULLUP);                // P0.6
00051     p7_AS_INPUT;
00052     p7_MODE(PIN_PULLUP);                // P0.7
00053 
00054     // Clear the buffer.
00055     memset(buffer, 0, sizeof(buffer));
00056 
00057     printf("Starting up...\n");
00058 
00059     // Set-up timer1 as a periodic timer.
00060     LPC_SC->PCONP |= (1UL << 2);        // TIM1 On
00061     LPC_SC->PCLKSEL0 |= (3UL << 4);     // CCLK/8 = 12MHz
00062     LPC_TIM1->PR = 11;                  // TC clocks at 1MHz.
00063     LPC_TIM1->MCR = 2;                  // Reset TCR to zero on match.
00064     LPC_TIM1->MR0 = SAMPLE_PERIOD;
00065 
00066     // Prepare the GPDMA system.
00067     conf = new MODDMA_Config;
00068     conf->channelNum(MODDMA::Channel_0);
00069     conf->srcMemAddr((uint32_t) & LPC_GPIO0->FIOPIN);
00070     conf->dstMemAddr((uint32_t) & buffer[0]);
00071     conf->transferSize(NUM_OF_SAMPLES);
00072     conf->transferType(MODDMA::g2m);    // pseudo transfer code MODDMA understands.
00073     conf->transferWidth(MODDMA::word);
00074     conf->srcConn(MODDMA::MAT1_0);
00075     conf->dmacSync(MODDMA::MAT1_0);
00076     conf->attach_tc(TC0_callback);
00077     conf->attach_err(ERR0_callback);
00078 
00079     // Prepare configuration.
00080     if (!dma.Setup(conf)) {
00081         error("Doh!");
00082     }
00083 
00084     // Enable GPDMA to be ready for the TIM1 "ticks".
00085     dma.Enable(conf);
00086 
00087     // Begin.
00088     LPC_TIM1->TCR = 1;
00089 
00090     while (1) {
00091         if (life_counter++ > 1000000) {
00092             led1 = !led1;               // Show some sort of life.
00093             life_counter = 0;
00094         }
00095 
00096         if (dmaTransferComplete) {
00097             dmaTransferComplete = false;
00098             for (int i = 0; i < NUM_OF_SAMPLES; i++) {
00099                 int val = (buffer[i] >> 4) & 0xF;
00100                 printf("Buffer index %d = 0x%x\n", i, val);
00101             }
00102 
00103             printf("Done.\n");
00104 
00105             // Schedule another grab.
00106             if (dma.Setup(conf)) {
00107                 dma.Enable(conf);
00108             }
00109         }
00110     }
00111 }
00112 
00113 // Configuration callback on TC
00114 void TC0_callback(void)
00115 {
00116     // Just show sample sequence grab complete.
00117     led3 = !led3;
00118 
00119     // Get configuration pointer.
00120     MODDMA_Config*  config = dma.getConfig();
00121 
00122     // Finish the DMA cycle by shutting down the channel.
00123     dma.Disable((MODDMA::CHANNELS) config->channelNum());
00124 
00125     // Tell main() while(1) loop to print the results.
00126     dmaTransferComplete = true;
00127 
00128     // Clear DMA IRQ flags.
00129     if (dma.irqType() == MODDMA::TcIrq)
00130         dma.clearTcIrq();
00131     if (dma.irqType() == MODDMA::ErrIrq)
00132         dma.clearErrIrq();
00133 }
00134 
00135 // Configuration callback on Error
00136 void ERR0_callback(void)
00137 {
00138     error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem");
00139 }