GPDMA (Direct Memory Access) and LLI (Link List Item) test see: http://mbed.org/users/okini3939/notebook/dma_jp/
MODDMA/example2.h@0:de79d4a48e63, 2013-09-13 (annotated)
- Committer:
- okini3939
- Date:
- Fri Sep 13 14:49:52 2013 +0000
- Revision:
- 0:de79d4a48e63
GPDMA (Direct Memory Access) and LLI (Link List Item) test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:de79d4a48e63 | 1 | /* |
okini3939 | 0:de79d4a48e63 | 2 | * This example was provided to support Mbed forum thread:- |
okini3939 | 0:de79d4a48e63 | 3 | * http://mbed.org/forum/mbed/topic/1798 |
okini3939 | 0:de79d4a48e63 | 4 | */ |
okini3939 | 0:de79d4a48e63 | 5 | |
okini3939 | 0:de79d4a48e63 | 6 | #include "mbed.h" |
okini3939 | 0:de79d4a48e63 | 7 | #include "MODDMA.h" |
okini3939 | 0:de79d4a48e63 | 8 | |
okini3939 | 0:de79d4a48e63 | 9 | #define SAMPLE_BUFFER_LENGTH 32 |
okini3939 | 0:de79d4a48e63 | 10 | |
okini3939 | 0:de79d4a48e63 | 11 | DigitalOut led1(LED1); |
okini3939 | 0:de79d4a48e63 | 12 | DigitalOut led2(LED2); |
okini3939 | 0:de79d4a48e63 | 13 | |
okini3939 | 0:de79d4a48e63 | 14 | MODDMA dma; |
okini3939 | 0:de79d4a48e63 | 15 | Serial pc(USBTX, USBRX); |
okini3939 | 0:de79d4a48e63 | 16 | |
okini3939 | 0:de79d4a48e63 | 17 | // ISR set's this when transfer complete. |
okini3939 | 0:de79d4a48e63 | 18 | bool dmaTransferComplete = false; |
okini3939 | 0:de79d4a48e63 | 19 | |
okini3939 | 0:de79d4a48e63 | 20 | // Function prototypes for IRQ callbacks. |
okini3939 | 0:de79d4a48e63 | 21 | // See definitions following main() below. |
okini3939 | 0:de79d4a48e63 | 22 | void TC0_callback(void); |
okini3939 | 0:de79d4a48e63 | 23 | void ERR0_callback(void); |
okini3939 | 0:de79d4a48e63 | 24 | |
okini3939 | 0:de79d4a48e63 | 25 | int main() { |
okini3939 | 0:de79d4a48e63 | 26 | |
okini3939 | 0:de79d4a48e63 | 27 | // Create a buffer to hold the ADC samples and clear it. |
okini3939 | 0:de79d4a48e63 | 28 | // Note, we are going to sample two ADC inputs so they |
okini3939 | 0:de79d4a48e63 | 29 | // end up in this buffer "interleaved". So you will want |
okini3939 | 0:de79d4a48e63 | 30 | // a buffer twice this size to a real life given sample |
okini3939 | 0:de79d4a48e63 | 31 | // frequency. See the printf() output for details. |
okini3939 | 0:de79d4a48e63 | 32 | uint32_t adcInputBuffer[SAMPLE_BUFFER_LENGTH]; |
okini3939 | 0:de79d4a48e63 | 33 | memset(adcInputBuffer, 0, sizeof(adcInputBuffer)); |
okini3939 | 0:de79d4a48e63 | 34 | |
okini3939 | 0:de79d4a48e63 | 35 | // We use the ADC irq to trigger DMA and the manual says |
okini3939 | 0:de79d4a48e63 | 36 | // that in this case the NVIC for ADC must be disabled. |
okini3939 | 0:de79d4a48e63 | 37 | NVIC_DisableIRQ(ADC_IRQn); |
okini3939 | 0:de79d4a48e63 | 38 | |
okini3939 | 0:de79d4a48e63 | 39 | // Power up the ADC and set PCLK |
okini3939 | 0:de79d4a48e63 | 40 | LPC_SC->PCONP |= (1UL << 12); |
okini3939 | 0:de79d4a48e63 | 41 | LPC_SC->PCLKSEL0 &= ~(3UL << 24); // PCLK = CCLK/4 96M/4 = 24MHz |
okini3939 | 0:de79d4a48e63 | 42 | |
okini3939 | 0:de79d4a48e63 | 43 | // Enable the ADC, 12MHz, ADC0.0 & .1 |
okini3939 | 0:de79d4a48e63 | 44 | LPC_ADC->ADCR = (1UL << 21) | (1UL << 8) | (3UL << 0); |
okini3939 | 0:de79d4a48e63 | 45 | |
okini3939 | 0:de79d4a48e63 | 46 | // Set the pin functions to ADC |
okini3939 | 0:de79d4a48e63 | 47 | LPC_PINCON->PINSEL1 &= ~(3UL << 14); /* P0.23, Mbed p15. */ |
okini3939 | 0:de79d4a48e63 | 48 | LPC_PINCON->PINSEL1 |= (1UL << 14); |
okini3939 | 0:de79d4a48e63 | 49 | LPC_PINCON->PINSEL1 &= ~(3UL << 16); /* P0.24, Mbed p16. */ |
okini3939 | 0:de79d4a48e63 | 50 | LPC_PINCON->PINSEL1 |= (1UL << 16); |
okini3939 | 0:de79d4a48e63 | 51 | |
okini3939 | 0:de79d4a48e63 | 52 | // Setup the serial port to print out results. |
okini3939 | 0:de79d4a48e63 | 53 | pc.baud(115200); |
okini3939 | 0:de79d4a48e63 | 54 | pc.printf("ADC with DMA example\n"); |
okini3939 | 0:de79d4a48e63 | 55 | pc.printf("====================\n"); |
okini3939 | 0:de79d4a48e63 | 56 | |
okini3939 | 0:de79d4a48e63 | 57 | // Prepare an ADC configuration. |
okini3939 | 0:de79d4a48e63 | 58 | MODDMA_Config *conf = new MODDMA_Config; |
okini3939 | 0:de79d4a48e63 | 59 | conf |
okini3939 | 0:de79d4a48e63 | 60 | ->channelNum ( MODDMA::Channel_0 ) |
okini3939 | 0:de79d4a48e63 | 61 | ->srcMemAddr ( 0 ) |
okini3939 | 0:de79d4a48e63 | 62 | ->dstMemAddr ( (uint32_t)adcInputBuffer ) |
okini3939 | 0:de79d4a48e63 | 63 | ->transferSize ( SAMPLE_BUFFER_LENGTH ) |
okini3939 | 0:de79d4a48e63 | 64 | ->transferType ( MODDMA::p2m ) |
okini3939 | 0:de79d4a48e63 | 65 | ->transferWidth ( MODDMA::word ) |
okini3939 | 0:de79d4a48e63 | 66 | ->srcConn ( MODDMA::ADC ) |
okini3939 | 0:de79d4a48e63 | 67 | ->dstConn ( 0 ) |
okini3939 | 0:de79d4a48e63 | 68 | ->dmaLLI ( 0 ) |
okini3939 | 0:de79d4a48e63 | 69 | ->attach_tc ( &TC0_callback ) |
okini3939 | 0:de79d4a48e63 | 70 | ->attach_err ( &ERR0_callback ) |
okini3939 | 0:de79d4a48e63 | 71 | ; // end conf. |
okini3939 | 0:de79d4a48e63 | 72 | |
okini3939 | 0:de79d4a48e63 | 73 | // Prepare configuration. |
okini3939 | 0:de79d4a48e63 | 74 | dma.Setup( conf ); |
okini3939 | 0:de79d4a48e63 | 75 | |
okini3939 | 0:de79d4a48e63 | 76 | // Enable configuration. |
okini3939 | 0:de79d4a48e63 | 77 | dma.Enable( conf ); |
okini3939 | 0:de79d4a48e63 | 78 | |
okini3939 | 0:de79d4a48e63 | 79 | // Enable ADC irq flag (to DMA). |
okini3939 | 0:de79d4a48e63 | 80 | // Note, don't set the individual flags, |
okini3939 | 0:de79d4a48e63 | 81 | // just set the global flag. |
okini3939 | 0:de79d4a48e63 | 82 | LPC_ADC->ADINTEN = 0x100; |
okini3939 | 0:de79d4a48e63 | 83 | |
okini3939 | 0:de79d4a48e63 | 84 | // Enable burst mode on inputs 0 and 1. |
okini3939 | 0:de79d4a48e63 | 85 | LPC_ADC->ADCR |= (1UL << 16); |
okini3939 | 0:de79d4a48e63 | 86 | |
okini3939 | 0:de79d4a48e63 | 87 | while (1) { |
okini3939 | 0:de79d4a48e63 | 88 | // When transfer complete do this block. |
okini3939 | 0:de79d4a48e63 | 89 | if (dmaTransferComplete) { |
okini3939 | 0:de79d4a48e63 | 90 | delete conf; // No memory leaks, delete the configuration. |
okini3939 | 0:de79d4a48e63 | 91 | dmaTransferComplete = false; |
okini3939 | 0:de79d4a48e63 | 92 | for (int i = 0; i < SAMPLE_BUFFER_LENGTH; i++) { |
okini3939 | 0:de79d4a48e63 | 93 | int channel = (adcInputBuffer[i] >> 24) & 0x7; |
okini3939 | 0:de79d4a48e63 | 94 | int iVal = (adcInputBuffer[i] >> 4) & 0xFFF; |
okini3939 | 0:de79d4a48e63 | 95 | double fVal = 3.3 * (double)((double)iVal) / ((double)0x1000); // scale to 0v to 3.3v |
okini3939 | 0:de79d4a48e63 | 96 | pc.printf("Array index %02d : ADC input channel %d = 0x%03x %01.3f volts\n", i, channel, iVal, fVal); |
okini3939 | 0:de79d4a48e63 | 97 | } |
okini3939 | 0:de79d4a48e63 | 98 | } |
okini3939 | 0:de79d4a48e63 | 99 | |
okini3939 | 0:de79d4a48e63 | 100 | // Just flash LED1 for something to do. |
okini3939 | 0:de79d4a48e63 | 101 | led1 = !led1; |
okini3939 | 0:de79d4a48e63 | 102 | wait(0.25); |
okini3939 | 0:de79d4a48e63 | 103 | } |
okini3939 | 0:de79d4a48e63 | 104 | } |
okini3939 | 0:de79d4a48e63 | 105 | |
okini3939 | 0:de79d4a48e63 | 106 | // Configuration callback on TC |
okini3939 | 0:de79d4a48e63 | 107 | void TC0_callback(void) { |
okini3939 | 0:de79d4a48e63 | 108 | |
okini3939 | 0:de79d4a48e63 | 109 | MODDMA_Config *config = dma.getConfig(); |
okini3939 | 0:de79d4a48e63 | 110 | |
okini3939 | 0:de79d4a48e63 | 111 | // Disbale burst mode and switch off the IRQ flag. |
okini3939 | 0:de79d4a48e63 | 112 | LPC_ADC->ADCR &= ~(1UL << 16); |
okini3939 | 0:de79d4a48e63 | 113 | LPC_ADC->ADINTEN = 0; |
okini3939 | 0:de79d4a48e63 | 114 | |
okini3939 | 0:de79d4a48e63 | 115 | // Finish the DMA cycle by shutting down the channel. |
okini3939 | 0:de79d4a48e63 | 116 | dma.haltAndWaitChannelComplete( (MODDMA::CHANNELS)config->channelNum()); |
okini3939 | 0:de79d4a48e63 | 117 | dma.Disable( (MODDMA::CHANNELS)config->channelNum() ); |
okini3939 | 0:de79d4a48e63 | 118 | |
okini3939 | 0:de79d4a48e63 | 119 | // Tell main() while(1) loop to print the results. |
okini3939 | 0:de79d4a48e63 | 120 | dmaTransferComplete = true; |
okini3939 | 0:de79d4a48e63 | 121 | |
okini3939 | 0:de79d4a48e63 | 122 | // Switch on LED2 to show transfer complete. |
okini3939 | 0:de79d4a48e63 | 123 | led2 = 1; |
okini3939 | 0:de79d4a48e63 | 124 | |
okini3939 | 0:de79d4a48e63 | 125 | // Clear DMA IRQ flags. |
okini3939 | 0:de79d4a48e63 | 126 | if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); |
okini3939 | 0:de79d4a48e63 | 127 | if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq(); |
okini3939 | 0:de79d4a48e63 | 128 | } |
okini3939 | 0:de79d4a48e63 | 129 | |
okini3939 | 0:de79d4a48e63 | 130 | // Configuration callback on Error |
okini3939 | 0:de79d4a48e63 | 131 | void ERR0_callback(void) { |
okini3939 | 0:de79d4a48e63 | 132 | // Switch off burst conversions. |
okini3939 | 0:de79d4a48e63 | 133 | LPC_ADC->ADCR |= ~(1UL << 16); |
okini3939 | 0:de79d4a48e63 | 134 | LPC_ADC->ADINTEN = 0; |
okini3939 | 0:de79d4a48e63 | 135 | error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem"); |
okini3939 | 0:de79d4a48e63 | 136 | } |
okini3939 | 0:de79d4a48e63 | 137 |