eichi kowata / Mbed 2 deprecated geiger

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers adcdma.c Source File

adcdma.c

00001 /*************************************************************
00002 
00003  History
00004  2011/07/26 - Add "shape_mode" option. 
00005 
00006 
00007 *************************************************************/
00008 #include "MODDMA.h"
00009 
00010 #define SAMPLE_BUFFER_LENGTH 256
00011 
00012 extern int shape_mode;
00013 
00014 // for ADC DMA read
00015 static MODDMA dma;
00016 static MODDMA_Config *conf = new MODDMA_Config;
00017 static uint32_t adcInputBuffer[SAMPLE_BUFFER_LENGTH];   
00018 
00019 /******************
00020 To read ADC DMA
00021 *******************/
00022 // Configuration callback on TC
00023 void TC0_callback(void) {
00024     
00025   MODDMA_Config *config = dma.getConfig();
00026 
00027   // Disbale burst mode and switch off the IRQ flag.
00028   LPC_ADC->ADCR &= ~(1UL << 16);
00029   LPC_ADC->ADINTEN = 0;    
00030     
00031   // Finish the DMA cycle by shutting down the channel.
00032   dma.haltAndWaitChannelComplete( (MODDMA::CHANNELS)config->channelNum());
00033   dma.Disable( (MODDMA::CHANNELS)config->channelNum() );
00034 
00035   // Clear DMA IRQ flags.
00036   if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq();    
00037   if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq();
00038 
00039   int m=0,max=0,n=0;
00040   int downtrend=0;
00041   int errata=0;
00042 
00043   for (int i = 0; i < SAMPLE_BUFFER_LENGTH; i++) {
00044       int channel = (adcInputBuffer[i] >> 24) & 0x7;
00045       int iVal = (adcInputBuffer[i] >> 4) & 0xFFF;
00046 //      double fVal = 3.3 * (double)((double)iVal) / ((double)0x1000); // scale to 0v to 3.3v
00047 //      pc.printf("Array index %02d : ADC input channel %d = 0x%03x %01.3f volts\n", i, channel, iVal, fVal);
00048 
00049     if( channel == 0 ){
00050 
00051         n = iVal;
00052 
00053 //PEAKMAX
00054         if(!downtrend){
00055             max= n>max ? n:max;
00056             if(n<max){
00057                 downtrend=1;
00058             }
00059         }else{
00060             if (n>max+100)
00061                 errata=1;
00062         }
00063 
00064 //Accumulation
00065         m+=n;
00066 
00067         if (shape_mode) {       //  2011/07/26
00068             double fVal = 3.3 * (double)((double)n) / ((double)0x1000);
00069             printf("%01.3f\n",fVal);
00070         }
00071      }    
00072 
00073       if(n==0 && max !=0) break;
00074   }
00075 
00076     if(!errata){
00077         if (shape_mode) {       //  2011/07/26
00078             double fm = 3.3 * (double)((double)m) / ((double)0x1000);
00079             double fmax = 3.3 * (double)((double)max) / ((double)0x1000);
00080             printf("%01.3f,%01.3f\n",fm,fmax);
00081         }
00082         printf("%d,%d\n",m,max);
00083 
00084         if (shape_mode) {       //  2011/07/26
00085             printf("-----\n");
00086         }
00087     }
00088 }
00089 
00090 // Configuration callback on Error
00091 void ERR0_callback(void) {
00092     // Switch off burst conversions.
00093     LPC_ADC->ADCR |= ~(1UL << 16);
00094     LPC_ADC->ADINTEN = 0;
00095     error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem");
00096 }
00097 
00098 void read_dma_data()
00099 {
00100     memset(adcInputBuffer, 0, sizeof(adcInputBuffer));
00101     
00102     // Prepare configuration.
00103     dma.Setup( conf );
00104     
00105     // Enable configuration.
00106     dma.Enable( conf );
00107     
00108     // Enable ADC irq flag (to DMA).
00109     // Note, don't set the individual flags,
00110     // just set the global flag.
00111     LPC_ADC->ADINTEN = 0x100;
00112 
00113     // Enable burst mode on inputs 0 and 1.
00114     LPC_ADC->ADCR |= (1UL << 16); 
00115     return;
00116 }
00117 
00118 void setup_adcdma()
00119 {
00120   // We use the ADC irq to trigger DMA and the manual says
00121   // that in this case the NVIC for ADC must be disabled.
00122   NVIC_DisableIRQ(ADC_IRQn);
00123     
00124   // Power up the ADC and set PCLK
00125   LPC_SC->PCONP    |=  (1UL << 12);
00126   LPC_SC->PCLKSEL0 &= ~(3UL << 24); // PCLK = CCLK/4 96M/4 = 24MHz
00127    
00128   // Enable the ADC, 12MHz
00129   LPC_ADC->ADCR  = (1UL << 21) | (1UL << 8) | (1UL << 0);
00130   
00131   // Set the pin functions to ADC
00132   LPC_PINCON->PINSEL1 &= ~(3UL << 14);  /* P0.23, Mbed p15. */
00133   LPC_PINCON->PINSEL1 |=  (1UL << 14);
00134 
00135 // Prepare an ADC configuration.
00136 //  MODDMA_Config *conf = new MODDMA_Config;
00137 
00138   conf
00139    ->channelNum    ( MODDMA::Channel_0 )
00140    ->srcMemAddr    ( 0 )
00141    ->dstMemAddr    ( (uint32_t)adcInputBuffer )
00142    ->transferSize  ( SAMPLE_BUFFER_LENGTH )
00143    ->transferType  ( MODDMA::p2m )
00144    ->transferWidth ( MODDMA::word )
00145    ->srcConn       ( MODDMA::ADC )
00146    ->dstConn       ( 0 )
00147    ->dmaLLI        ( 0 )
00148    ->attach_tc     ( &TC0_callback )
00149    ->attach_err    ( &ERR0_callback )
00150   ; // end conf.
00151 }
00152