GPDMA (Direct Memory Access) and LLI (Link List Item) test see: http://mbed.org/users/okini3939/notebook/dma_jp/
MODDMA/example3.h@1:1a77fa863282, 2013-09-13 (annotated)
- Committer:
- okini3939
- Date:
- Fri Sep 13 15:19:09 2013 +0000
- Revision:
- 1:1a77fa863282
- Parent:
- 0:de79d4a48e63
fix more lli
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:de79d4a48e63 | 1 | /* |
okini3939 | 0:de79d4a48e63 | 2 | * Demonstrates capturing the GPIO P0.4 to P0.7 "nibble" to memory |
okini3939 | 0:de79d4a48e63 | 3 | * using GPDMA. The transfers from port pins to memory buffer are |
okini3939 | 0:de79d4a48e63 | 4 | * triggered using Timer1 MAT1.0 match compare. |
okini3939 | 0:de79d4a48e63 | 5 | * |
okini3939 | 0:de79d4a48e63 | 6 | * In this example all inputs have pullups. So with nothing connected |
okini3939 | 0:de79d4a48e63 | 7 | * the P0.4/7 reads as 0xF. Connecting a wire from one or more of the four |
okini3939 | 0:de79d4a48e63 | 8 | * inputs to ground will show up in the captured buffer sequence. |
okini3939 | 0:de79d4a48e63 | 9 | */ |
okini3939 | 0:de79d4a48e63 | 10 | |
okini3939 | 0:de79d4a48e63 | 11 | #include "mbed.h" |
okini3939 | 0:de79d4a48e63 | 12 | #include "MODDMA.h" |
okini3939 | 0:de79d4a48e63 | 13 | #include "iomacros.h" // within MODDMA library. |
okini3939 | 0:de79d4a48e63 | 14 | |
okini3939 | 0:de79d4a48e63 | 15 | // How long between grabbing GPIO FIO0PIN register. |
okini3939 | 0:de79d4a48e63 | 16 | // Value is in microseconds. (500000 is half a second). |
okini3939 | 0:de79d4a48e63 | 17 | #define SAMPLE_PERIOD 500000 |
okini3939 | 0:de79d4a48e63 | 18 | |
okini3939 | 0:de79d4a48e63 | 19 | #define NUM_OF_SAMPLES 5 |
okini3939 | 0:de79d4a48e63 | 20 | |
okini3939 | 0:de79d4a48e63 | 21 | Serial pc(USBTX, USBRX); |
okini3939 | 0:de79d4a48e63 | 22 | |
okini3939 | 0:de79d4a48e63 | 23 | DigitalOut led1(LED1); |
okini3939 | 0:de79d4a48e63 | 24 | DigitalOut led2(LED2); |
okini3939 | 0:de79d4a48e63 | 25 | DigitalOut led3(LED3); |
okini3939 | 0:de79d4a48e63 | 26 | |
okini3939 | 0:de79d4a48e63 | 27 | uint32_t buffer[NUM_OF_SAMPLES]; |
okini3939 | 0:de79d4a48e63 | 28 | |
okini3939 | 0:de79d4a48e63 | 29 | bool dmaTransferComplete; |
okini3939 | 0:de79d4a48e63 | 30 | |
okini3939 | 0:de79d4a48e63 | 31 | MODDMA dma; |
okini3939 | 0:de79d4a48e63 | 32 | MODDMA_Config *conf; |
okini3939 | 0:de79d4a48e63 | 33 | |
okini3939 | 0:de79d4a48e63 | 34 | void TC0_callback(void); |
okini3939 | 0:de79d4a48e63 | 35 | void ERR0_callback(void); |
okini3939 | 0:de79d4a48e63 | 36 | |
okini3939 | 0:de79d4a48e63 | 37 | int main() { |
okini3939 | 0:de79d4a48e63 | 38 | volatile int life_counter = 0; |
okini3939 | 0:de79d4a48e63 | 39 | |
okini3939 | 0:de79d4a48e63 | 40 | // Macros defined in iomacros.h, saves messing with DigitalIn |
okini3939 | 0:de79d4a48e63 | 41 | p30_AS_INPUT; p30_MODE( PIN_PULLUP ); // P0.4 |
okini3939 | 0:de79d4a48e63 | 42 | p29_AS_INPUT; p29_MODE( PIN_PULLUP ); // P0.5 |
okini3939 | 0:de79d4a48e63 | 43 | p8_AS_INPUT; p8_MODE( PIN_PULLUP ); // P0.6 |
okini3939 | 0:de79d4a48e63 | 44 | p7_AS_INPUT; p7_MODE( PIN_PULLUP ); // P0.7 |
okini3939 | 0:de79d4a48e63 | 45 | |
okini3939 | 0:de79d4a48e63 | 46 | // Clear the buffer. |
okini3939 | 0:de79d4a48e63 | 47 | memset(buffer, 0, sizeof(buffer)); |
okini3939 | 0:de79d4a48e63 | 48 | |
okini3939 | 0:de79d4a48e63 | 49 | // Setup the serial port to print out results. |
okini3939 | 0:de79d4a48e63 | 50 | pc.baud(115200); |
okini3939 | 0:de79d4a48e63 | 51 | pc.printf("Starting up...\n"); |
okini3939 | 0:de79d4a48e63 | 52 | |
okini3939 | 0:de79d4a48e63 | 53 | // Set-up timer1 as a periodic timer. |
okini3939 | 0:de79d4a48e63 | 54 | LPC_SC->PCONP |= (1UL << 2); // TIM1 On |
okini3939 | 0:de79d4a48e63 | 55 | LPC_SC->PCLKSEL0 |= (3UL << 4); // CCLK/8 = 12MHz |
okini3939 | 0:de79d4a48e63 | 56 | LPC_TIM1->PR = 11; // TC clocks at 1MHz. |
okini3939 | 0:de79d4a48e63 | 57 | LPC_TIM1->MCR = 2; // Reset TCR to zero on match. |
okini3939 | 0:de79d4a48e63 | 58 | LPC_TIM1->MR0 = SAMPLE_PERIOD; |
okini3939 | 0:de79d4a48e63 | 59 | |
okini3939 | 0:de79d4a48e63 | 60 | // Prepare the GPDMA system. |
okini3939 | 0:de79d4a48e63 | 61 | conf = new MODDMA_Config; |
okini3939 | 0:de79d4a48e63 | 62 | conf |
okini3939 | 0:de79d4a48e63 | 63 | ->channelNum ( MODDMA::Channel_0 ) |
okini3939 | 0:de79d4a48e63 | 64 | ->srcMemAddr ( (uint32_t)&LPC_GPIO0->FIOPIN ) |
okini3939 | 0:de79d4a48e63 | 65 | ->dstMemAddr ( (uint32_t)&buffer[0] ) |
okini3939 | 0:de79d4a48e63 | 66 | ->transferSize ( NUM_OF_SAMPLES ) |
okini3939 | 0:de79d4a48e63 | 67 | ->transferType ( MODDMA::g2m ) // pseudo transfer code MODDMA understands. |
okini3939 | 0:de79d4a48e63 | 68 | ->transferWidth ( MODDMA::word ) |
okini3939 | 0:de79d4a48e63 | 69 | ->srcConn ( MODDMA::MAT1_0 ) |
okini3939 | 0:de79d4a48e63 | 70 | ->dmacSync ( MODDMA::MAT1_0 ) |
okini3939 | 0:de79d4a48e63 | 71 | ->attach_tc ( TC0_callback ) |
okini3939 | 0:de79d4a48e63 | 72 | ->attach_err ( ERR0_callback ) |
okini3939 | 0:de79d4a48e63 | 73 | ; // end conf. |
okini3939 | 0:de79d4a48e63 | 74 | |
okini3939 | 0:de79d4a48e63 | 75 | // Prepare configuration. |
okini3939 | 0:de79d4a48e63 | 76 | if (!dma.Setup( conf )) { |
okini3939 | 0:de79d4a48e63 | 77 | error("Doh!"); |
okini3939 | 0:de79d4a48e63 | 78 | } |
okini3939 | 0:de79d4a48e63 | 79 | |
okini3939 | 0:de79d4a48e63 | 80 | // Enable GPDMA to be ready for the TIM1 "ticks". |
okini3939 | 0:de79d4a48e63 | 81 | dma.Enable( conf ); |
okini3939 | 0:de79d4a48e63 | 82 | |
okini3939 | 0:de79d4a48e63 | 83 | // Begin. |
okini3939 | 0:de79d4a48e63 | 84 | LPC_TIM1->TCR = 1; |
okini3939 | 0:de79d4a48e63 | 85 | |
okini3939 | 0:de79d4a48e63 | 86 | while (1) { |
okini3939 | 0:de79d4a48e63 | 87 | if (life_counter++ > 1000000) { |
okini3939 | 0:de79d4a48e63 | 88 | led1 = !led1; // Show some sort of life. |
okini3939 | 0:de79d4a48e63 | 89 | life_counter = 0; |
okini3939 | 0:de79d4a48e63 | 90 | } |
okini3939 | 0:de79d4a48e63 | 91 | |
okini3939 | 0:de79d4a48e63 | 92 | if (dmaTransferComplete) { |
okini3939 | 0:de79d4a48e63 | 93 | dmaTransferComplete = false; |
okini3939 | 0:de79d4a48e63 | 94 | for (int i = 0; i < NUM_OF_SAMPLES; i++) { |
okini3939 | 0:de79d4a48e63 | 95 | int val = (buffer[i] >> 4) & 0xF; |
okini3939 | 0:de79d4a48e63 | 96 | pc.printf("Buffer index %d = 0x%x\n", i, val); |
okini3939 | 0:de79d4a48e63 | 97 | } |
okini3939 | 0:de79d4a48e63 | 98 | pc.printf("Done.\n"); |
okini3939 | 0:de79d4a48e63 | 99 | |
okini3939 | 0:de79d4a48e63 | 100 | // Schedule another grab. |
okini3939 | 0:de79d4a48e63 | 101 | if (dma.Setup( conf )) { |
okini3939 | 0:de79d4a48e63 | 102 | dma.Enable( conf ); |
okini3939 | 0:de79d4a48e63 | 103 | } |
okini3939 | 0:de79d4a48e63 | 104 | } |
okini3939 | 0:de79d4a48e63 | 105 | } |
okini3939 | 0:de79d4a48e63 | 106 | } |
okini3939 | 0:de79d4a48e63 | 107 | |
okini3939 | 0:de79d4a48e63 | 108 | // Configuration callback on TC |
okini3939 | 0:de79d4a48e63 | 109 | void TC0_callback(void) { |
okini3939 | 0:de79d4a48e63 | 110 | |
okini3939 | 0:de79d4a48e63 | 111 | // Just show sample sequence grab complete. |
okini3939 | 0:de79d4a48e63 | 112 | led3 = !led3; |
okini3939 | 0:de79d4a48e63 | 113 | |
okini3939 | 0:de79d4a48e63 | 114 | // Get configuration pointer. |
okini3939 | 0:de79d4a48e63 | 115 | MODDMA_Config *config = dma.getConfig(); |
okini3939 | 0:de79d4a48e63 | 116 | |
okini3939 | 0:de79d4a48e63 | 117 | // Finish the DMA cycle by shutting down the channel. |
okini3939 | 0:de79d4a48e63 | 118 | dma.Disable( (MODDMA::CHANNELS)config->channelNum() ); |
okini3939 | 0:de79d4a48e63 | 119 | |
okini3939 | 0:de79d4a48e63 | 120 | // Tell main() while(1) loop to print the results. |
okini3939 | 0:de79d4a48e63 | 121 | dmaTransferComplete = true; |
okini3939 | 0:de79d4a48e63 | 122 | |
okini3939 | 0:de79d4a48e63 | 123 | // Clear DMA IRQ flags. |
okini3939 | 0:de79d4a48e63 | 124 | if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); |
okini3939 | 0:de79d4a48e63 | 125 | if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq(); |
okini3939 | 0:de79d4a48e63 | 126 | } |
okini3939 | 0:de79d4a48e63 | 127 | |
okini3939 | 0:de79d4a48e63 | 128 | // Configuration callback on Error |
okini3939 | 0:de79d4a48e63 | 129 | void ERR0_callback(void) { |
okini3939 | 0:de79d4a48e63 | 130 | error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem"); |
okini3939 | 0:de79d4a48e63 | 131 | } |