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