First Version

Dependencies:   EthernetInterface mbed-rtos mbed

Revision:
2:bf4bbf7d6793
Parent:
0:9df41090ba33
--- a/Codes/Capture.cpp	Fri Jul 24 16:39:11 2015 +0000
+++ b/Codes/Capture.cpp	Fri Mar 04 21:18:48 2016 +0000
@@ -6,83 +6,136 @@
  */
 #include "Capture.h"
 
-Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture 
-int Capture::m_BufferIndex;
-dmaLinkedListNode Capture::m_Nodes[2] __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral
-unsigned short int Capture::m_AdcBuffers[2][NUMBER_OF_SAMPLES][NUMBER_OF_CHANNELS] __attribute__((section("AHBSRAM0")));
+
+__attribute((section("AHBSRAM1"),aligned)) float Capture::m_AdcBuffers[NUMBER_OF_CHANNELS][NUMBER_OF_SAMPLES];// __attribute__((section("AHBSRAM0")));
+
+bool Capture::flag_capture = 0;
+int Capture::cnt_buffer=0;
+int Capture::channel_number=0;
+int Capture::cnt_interrupt=0;
+
 Serial rfid_serial(p28,p27);
 
-//This function prepares the capture buffers and starts the DMA peripheral
-void Capture::Start()
-{
-    m_Nodes[0].destAddr = (unsigned int)m_AdcBuffers[0];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval;
-    m_Nodes[0].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
-    m_Nodes[0].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
-    m_Nodes[0].nextNode = (unsigned int)&m_Nodes[1];
+Ticker timer_interrupt;
+
+DigitalOut led1(LED1);
 
-    m_Nodes[1].destAddr = (unsigned int)m_AdcBuffers[1];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval2;
-    m_Nodes[1].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
-    m_Nodes[1].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
-    m_Nodes[1].nextNode = (unsigned int)&m_Nodes[0];
 
-    m_BufferIndex=0;
-
-    while(!(LPC_ADC->ADDR0&(1U<<31)));  //waits the completion of the ADC Channel 0 capture - for synchronization purposes
-    while(!(LPC_ADC->ADDR0&(1U<<31)));  //waits the completion of the ADC Channel 0 capture - for synchronization purposes
-
-    setup_channel(&m_Nodes[0],0,DMA_PERIPHERAL_ADC,DMA_MEMORY);
+float Capture::GetValue(int nsamples, int nchannel)
+{
+    return m_AdcBuffers[nchannel][nsamples];
 }
 
-//This function initializes the ADC and DMA peripherals
-void Capture::Initialize()
-{   
-    init_adc(Settings::get_FreqBase()*NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS);
-    select_channels(ADC_CH_0|ADC_CH_1|ADC_CH_2|ADC_CH_3|ADC_CH_4|ADC_CH_5);
-    LPC_ADC->ADCR |= ADC_MODE_BURST;
-
-    init_dma();
-    
-    Start();
-    
-    m_CaptureSemaphore.wait();
+void Capture::PutValue(int nsamples, int nchannel, float value)
+{
+    m_AdcBuffers[nchannel][nsamples] = value;
 }
 
-void Capture::Stop()
-{
-    m_CaptureSemaphore.release();   //release semaphore
-    stop_channel();
-}
-
-void Capture::Wait()
+void Capture::CopyBuffer(int channel, float *dest)
 {
-    m_CaptureSemaphore.wait(osWaitForever);
-}
-
-unsigned short int Capture::GetValue(int nsamples, int nchannel)
-{
-    return ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
-}
-
-void Capture::CopyBuffer(int channel, unsigned short int *dest)
-{
-    for(int i=0;i<Settings::get_Samples();i++)
+    memcpy(dest, &m_AdcBuffers[channel][0], sizeof(float) * NUMBER_OF_SAMPLES);
+    /*
+    for(int i=0;i<NUMBER_OF_SAMPLES;i++)
     {
         dest[i] = GetValue(i,channel);
     }
+    */
+}
+
+void Capture::InitializeAD()
+{
+    
+    init_adc(200000);                   // Inicializa o AD com a frequencia de amostragem de 200 kHz
+    LPC_ADC->ADCR &= ~ADC_MODE_BURST;   // Desabilito o modo burst de captura para utilizar apenas a captura simples
+    NVIC_DisableIRQ(TIMER3_IRQn);   // Desabilita a interrupção do Timer3
+    
+}
+
+void Capture::AcquireValues()
+{
+
+    NVIC_DisableIRQ(TIMER3_IRQn);   // Desabilita a interrupção do Timer3
+    timer_interrupt.attach_us(&Capture::FcnTimerInt,11);    // Seta a interrupção do timer3 para cada 11us apontando para a função FcnTimerInt
+    cnt_buffer = 0;
+    cnt_interrupt = 0;
+    flag_capture = 0;   
+    channel_number = 0;
+    LPC_ADC->ADCR |= ADC_START_NOW; // Habilita a captura do A/D -> Captura Simples
+    //printf("\r\nPASSOU do init \n"); 
+     
+    select_channels(1<<channel_number); //         
+    
+    NVIC_EnableIRQ(TIMER3_IRQn); // Habilita a interrupção do timer 3
+    for(volatile int i=0; i<100000; i++); // Gasta tempo
+
+    while(!flag_capture);
+    
+    /*for (int i=0;i<NUMBER_OF_SAMPLES;i++)
+    {
+        printf("\r\n%d", i);
+        
+        for (int j=0;j<6;j++)
+        {
+            float val = 0;
+            val = (float)m_AdcBuffers[j][i] - (float)Settings::get_Offset(j);
+            val /= (float)Settings::get_Gain(j);
+            //printf("\t%f", m_AdcBuffers[j][i]);
+            printf("\t%2.2f", val);
+        }
+    }*/
+    //printf("\n");
 }
 
-//DMA ISR signals the capture thread about the end of capture event
-extern "C" void DMA_IRQHandler(void)
+extern "C" void Capture::FcnTimerInt(void)
 {
-    Capture::ISRHandler();
-}
-
-void Capture::ISRHandler()
-{
-    Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1;
-
-    Capture::m_CaptureSemaphore.release();
-    LPC_GPDMA->DMACIntTCClear = 0xFF;
+    if (cnt_interrupt < 120)    // Descarta as primeiras leituras para estabilizar as leituras do A/D
+    {
+        while(!(LPC_ADC->ADGDR>>31));
+        m_AdcBuffers[channel_number][0] = ((LPC_ADC->ADGDR>>4)&0xfff);
+        if (channel_number < 5)
+            {
+                channel_number++;
+            }
+            else
+            {
+                channel_number = 0;
+            }
+        cnt_buffer = 0;   
+        cnt_interrupt++;
+        select_channels(1<<channel_number);
+        LPC_ADC->ADCR |= ADC_START_NOW;
+                
+    }
+    else    //Armazena as leituras no buffer dos canais
+    {
+        while(!(LPC_ADC->ADGDR>>31));  // Aguarda o bit de DONE ser setado no registrador ADGDR sinalizando o final de uma captura
+        
+        if (cnt_buffer < NUMBER_OF_SAMPLES)
+        {
+            
+            //Pega o valor do AD e adapta de 16 bits para 12 bits
+            m_AdcBuffers[channel_number][cnt_buffer] = (float) ((LPC_ADC->ADGDR>>4)&0xfff);
+                        
+            if (channel_number < 5)
+            {
+                channel_number++;
+            }
+            else
+            {
+                channel_number = 0;
+                cnt_buffer++;
+            }   
+        }
+        else
+        {
+            
+            flag_capture = 1;
+            NVIC_DisableIRQ(TIMER3_IRQn);
+            //timer_interrupt.detach();
+        }
+        select_channels(1<<channel_number);
+        LPC_ADC->ADCR |= ADC_START_NOW;
+   }
 }
 
 void Capture::ReadRFID(int channel,char *rfid)