Kevin Tseng / MODDMA

Dependents:   DSHOT_test

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  
00011 #include "mbed.h"
00012 #include "MODDMA.h"
00013 #include "iomacros.h" // within MODDMA library.
00014 
00015 // How long between grabbing GPIO FIO0PIN register.
00016 // Value is in microseconds. (500000 is half a second).
00017 #define SAMPLE_PERIOD   500000
00018 
00019 #define NUM_OF_SAMPLES  16
00020 
00021 Serial pc(USBTX, USBRX);
00022 
00023 DigitalOut led1(LED1);
00024 DigitalOut led2(LED2);
00025 DigitalOut led3(LED3);
00026 
00027 uint32_t buffer[NUM_OF_SAMPLES];
00028 
00029 bool dmaTransferComplete;
00030 
00031 MODDMA dma;
00032 MODDMA_Config *conf;
00033 
00034 void TC0_callback(void);
00035 void ERR0_callback(void);
00036 
00037 int main() {
00038     volatile int life_counter = 0;
00039      
00040     // Sets bits 4-7 on port 0 to pulled down outputs.
00041     p30_AS_OUTPUT; p30_MODE( PIN_PULLDOWN ); // P0.4
00042     p29_AS_OUTPUT; p29_MODE( PIN_PULLDOWN ); // P0.5
00043     p8_AS_OUTPUT;  p8_MODE( PIN_PULLDOWN );  // P0.6
00044     p7_AS_OUTPUT;  p7_MODE( PIN_PULLDOWN );  // P0.7
00045     
00046     // Clear the buffer.
00047     memset(buffer, 0, sizeof(buffer));
00048     
00049     // Set-up timer1 as a periodic timer.
00050     LPC_SC->PCONP    |= (1UL << 2); // TIM1 On
00051     LPC_SC->PCLKSEL0 |= (3UL << 4); // CCLK/8 = 12MHz
00052     LPC_TIM1->PR      = 11;         // TC clocks at 1MHz.
00053     LPC_TIM1->MCR     = 2;          // Reset TCR to zero on match.
00054     LPC_TIM1->MR0     = SAMPLE_PERIOD;
00055     
00056     // Prepare the GPDMA system.
00057     conf = new MODDMA_Config;
00058     conf
00059      ->channelNum    ( MODDMA::Channel_0 )
00060      ->srcMemAddr    ( (uint32_t)&buffer[0] ) //Buffer data to be transferred
00061      ->dstMemAddr    ( (uint32_t)&LPC_GPIO0->FIOPIN ) //GPIO to be transferred
00062      ->transferSize  ( NUM_OF_SAMPLES )
00063      ->transferType  ( MODDMA::m2g ) // pseudo transfer code MODDMA understands.
00064      ->transferWidth ( MODDMA::word )
00065      ->srcConn       ( MODDMA::MAT1_0 )
00066      ->dmacSync      ( MODDMA::MAT1_0 ) 
00067      ->attach_tc     ( TC0_callback )
00068      ->attach_err    ( ERR0_callback )
00069     ; // end conf.
00070     
00071     // Prepare configuration.
00072     if (!dma.Setup( conf )) {
00073         error("Doh!");
00074     }
00075     
00076     // Enable GPDMA to be ready for the TIM1 "ticks".       
00077     dma.Enable( conf );
00078     
00079     // Begin.
00080     LPC_TIM1->TCR = 1;
00081        
00082     while (1) { 
00083         if (life_counter++ > 1000000) {
00084             led1 = !led1; // Show some sort of life.
00085             life_counter = 0;
00086         }
00087         
00088         if (dmaTransferComplete) {
00089             dmaTransferComplete = false;
00090             for (int i = 0; i < NUM_OF_SAMPLES; i++) {
00091                 int val = (buffer[i] >> 4) & 0xF; 
00092                 pc.printf("Buffer index %d = 0x%x\n", i, val);
00093             }
00094             pc.printf("Done.\n");
00095             
00096             // Schedule another grab.
00097             if (dma.Setup( conf )) {        
00098                 dma.Enable( conf );                
00099             }            
00100         }
00101     }       
00102 }
00103 
00104 // Configuration callback on TC
00105 void TC0_callback(void) {
00106     
00107     // Just show sample sequence grab complete.
00108     led3 = !led3; 
00109         
00110     // Get configuration pointer.
00111     MODDMA_Config *config = dma.getConfig();
00112     
00113     // Finish the DMA cycle by shutting down the channel.
00114     dma.Disable( (MODDMA::CHANNELS)config->channelNum() );
00115     
00116     // Tell main() while(1) loop to print the results.
00117     dmaTransferComplete = true;            
00118     
00119     // Clear DMA IRQ flags.
00120     if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq();    
00121     if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq();    
00122 }
00123 
00124 // Configuration callback on Error
00125 void ERR0_callback(void) {
00126     error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem");
00127 }