Marcelo Rebonatto / Mbed 2 deprecated PM_COPIA

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Capture.cpp Source File

Capture.cpp

00001 /*
00002  * Capture.h
00003  *
00004  *  Created on: 
00005  *      Author: 
00006  */
00007 #include "Capture.h"
00008 
00009 Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture 
00010 int Capture::m_BufferIndex;
00011 dmaLinkedListNode Capture::m_Nodes[2] __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral
00012 unsigned short int Capture::m_AdcBuffers[2][NUMBER_OF_SAMPLES][NUMBER_OF_CHANNELS] __attribute__((section("AHBSRAM0")));
00013 
00014 //This function prepares the capture buffers and starts the DMA peripheral
00015 void Capture::Start()
00016 {
00017     m_Nodes[0].destAddr = (unsigned int)m_AdcBuffers[0];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval;
00018     m_Nodes[0].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
00019     m_Nodes[0].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
00020     m_Nodes[0].nextNode = (unsigned int)&m_Nodes[1];
00021 
00022     m_Nodes[1].destAddr = (unsigned int)m_AdcBuffers[1];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval2;
00023     m_Nodes[1].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
00024     m_Nodes[1].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
00025     m_Nodes[1].nextNode = (unsigned int)&m_Nodes[0];
00026 
00027     m_BufferIndex=0;
00028 
00029     while(!(LPC_ADC->ADDR0&(1U<<31)));  //waits the completion of the ADC Channel 0 capture - for synchronization purposes
00030     while(!(LPC_ADC->ADDR0&(1U<<31)));  //waits the completion of the ADC Channel 0 capture - for synchronization purposes
00031 
00032     setup_channel(&m_Nodes[0],0,DMA_PERIPHERAL_ADC,DMA_MEMORY);
00033 }
00034 
00035 //This function initializes the ADC and DMA peripherals
00036 void Capture::Initialize()
00037 {   
00038     init_adc(Settings::get_FreqBase()*NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS);
00039     select_channels(ADC_CH_0|ADC_CH_1|ADC_CH_2|ADC_CH_3|ADC_CH_4|ADC_CH_5);
00040     LPC_ADC->ADCR |= ADC_MODE_BURST;
00041 
00042     init_dma();
00043     
00044     Start();
00045     
00046     m_CaptureSemaphore.wait();
00047 }
00048 
00049 void Capture::Stop()
00050 {
00051     m_CaptureSemaphore.release();   //release semaphore
00052     stop_channel();
00053 }
00054 
00055 void Capture::Wait()
00056 {
00057     m_CaptureSemaphore.wait(osWaitForever);
00058 }
00059 
00060 unsigned short int Capture::GetValue(int nsamples, int nchannel)
00061 {
00062     return ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
00063 }
00064 
00065 void Capture::CopyBuffer(int channel, unsigned short int *dest)
00066 {
00067     for(int i=0;i<Settings::get_Samples();i++)
00068     {
00069         dest[i] = GetValue(i,channel);
00070     }
00071 }
00072 
00073 //DMA ISR signals the capture thread about the end of capture event
00074 extern "C" void DMA_IRQHandler(void)
00075 {
00076     Capture::ISRHandler();
00077 }
00078 
00079 void Capture::ISRHandler()
00080 {
00081     Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1;
00082 
00083     Capture::m_CaptureSemaphore.release();
00084     LPC_GPDMA->DMACIntTCClear = 0xFF;
00085 }