eichi kowata / Mbed 2 deprecated geiger

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example2.h Source File

example2.h

00001 /*
00002  * This example was provided to support Mbed forum thread:-
00003  * http://mbed.org/forum/mbed/topic/1798
00004  */
00005  
00006 #include "mbed.h"
00007 #include "MODDMA.h"
00008 
00009 #define SAMPLE_BUFFER_LENGTH 32
00010 
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 
00014 MODDMA dma;
00015 Serial pc(USBTX, USBRX);
00016 
00017 // ISR set's this when transfer complete.
00018 bool dmaTransferComplete = false;
00019 
00020 // Function prototypes for IRQ callbacks.
00021 // See definitions following main() below.
00022 void TC0_callback(void);
00023 void ERR0_callback(void);
00024 
00025 int main() {
00026 
00027     // Create a buffer to hold the ADC samples and clear it.
00028     // Note, we are going to sample two ADC inputs so they
00029     // end up in this buffer "interleaved". So you will want
00030     // a buffer twice this size to a real life given sample
00031     // frequency. See the printf() output for details.
00032     uint32_t adcInputBuffer[SAMPLE_BUFFER_LENGTH];    
00033     memset(adcInputBuffer, 0, sizeof(adcInputBuffer));
00034     
00035     // We use the ADC irq to trigger DMA and the manual says
00036     // that in this case the NVIC for ADC must be disabled.
00037     NVIC_DisableIRQ(ADC_IRQn);
00038     
00039     // Power up the ADC and set PCLK
00040     LPC_SC->PCONP    |=  (1UL << 12);
00041     LPC_SC->PCLKSEL0 &= ~(3UL << 24); // PCLK = CCLK/4 96M/4 = 24MHz
00042     
00043     // Enable the ADC, 12MHz,  ADC0.0 & .1
00044     LPC_ADC->ADCR  = (1UL << 21) | (1UL << 8) | (3UL << 0); 
00045     
00046     // Set the pin functions to ADC
00047     LPC_PINCON->PINSEL1 &= ~(3UL << 14);  /* P0.23, Mbed p15. */
00048     LPC_PINCON->PINSEL1 |=  (1UL << 14);
00049     LPC_PINCON->PINSEL1 &= ~(3UL << 16);  /* P0.24, Mbed p16. */
00050     LPC_PINCON->PINSEL1 |=  (1UL << 16);
00051     
00052     // Setup the serial port to print out results.
00053 //    pc.baud(115200);
00054     pc.baud(460800);
00055     pc.printf("ADC with DMA example\n");
00056     pc.printf("====================\n");
00057     
00058     // Prepare an ADC configuration.
00059     MODDMA_Config *conf = new MODDMA_Config;
00060     conf
00061      ->channelNum    ( MODDMA::Channel_0 )
00062      ->srcMemAddr    ( 0 )
00063      ->dstMemAddr    ( (uint32_t)adcInputBuffer )
00064      ->transferSize  ( SAMPLE_BUFFER_LENGTH )
00065      ->transferType  ( MODDMA::p2m )
00066      ->transferWidth ( MODDMA::word )
00067      ->srcConn       ( MODDMA::ADC )
00068      ->dstConn       ( 0 )
00069      ->dmaLLI        ( 0 )
00070      ->attach_tc     ( &TC0_callback )
00071      ->attach_err    ( &ERR0_callback )
00072     ; // end conf.
00073     
00074     // Prepare configuration.
00075     dma.Setup( conf );
00076     
00077     // Enable configuration.
00078     dma.Enable( conf );
00079     
00080     // Enable ADC irq flag (to DMA).
00081     // Note, don't set the individual flags,
00082     // just set the global flag.
00083     LPC_ADC->ADINTEN = 0x100;
00084 
00085     // Enable burst mode on inputs 0 and 1.
00086     LPC_ADC->ADCR |= (1UL << 16); 
00087     
00088     while (1) {
00089         // When transfer complete do this block.
00090         if (dmaTransferComplete) {
00091             delete conf; // No memory leaks, delete the configuration.
00092             dmaTransferComplete = false;
00093             for (int i = 0; i < SAMPLE_BUFFER_LENGTH; i++) {
00094                 int channel = (adcInputBuffer[i] >> 24) & 0x7;
00095                 int iVal = (adcInputBuffer[i] >> 4) & 0xFFF;
00096                 double fVal = 3.3 * (double)((double)iVal) / ((double)0x1000); // scale to 0v to 3.3v
00097                 pc.printf("Array index %02d : ADC input channel %d = 0x%03x %01.3f volts\n", i, channel, iVal, fVal);                  
00098             }
00099         }
00100         
00101         // Just flash LED1 for something to do.
00102         led1 = !led1;
00103         wait(0.25);        
00104     }
00105 }
00106 
00107 // Configuration callback on TC
00108 void TC0_callback(void) {
00109     
00110     MODDMA_Config *config = dma.getConfig();
00111     
00112     // Disbale burst mode and switch off the IRQ flag.
00113     LPC_ADC->ADCR &= ~(1UL << 16);
00114     LPC_ADC->ADINTEN = 0;    
00115     
00116     // Finish the DMA cycle by shutting down the channel.
00117     dma.haltAndWaitChannelComplete( (MODDMA::CHANNELS)config->channelNum());
00118     dma.Disable( (MODDMA::CHANNELS)config->channelNum() );
00119     
00120     // Tell main() while(1) loop to print the results.
00121     dmaTransferComplete = true;            
00122     
00123     // Switch on LED2 to show transfer complete.
00124     led2 = 1;        
00125     
00126     // Clear DMA IRQ flags.
00127     if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq();    
00128     if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq();
00129 }
00130 
00131 // Configuration callback on Error
00132 void ERR0_callback(void) {
00133     // Switch off burst conversions.
00134     LPC_ADC->ADCR |= ~(1UL << 16);
00135     LPC_ADC->ADINTEN = 0;
00136     error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem");
00137 }
00138