Marcelo Rebonatto / Mbed 2 deprecated PMED_Tempo

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 
00012 //extern char LargeBuffer[1024];
00013 
00014 __attribute((section("AHBSRAM1"),aligned)) dmaLinkedListNode Capture::m_Nodes[2];// __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral
00015 __attribute((section("AHBSRAM1"),aligned)) unsigned short int Capture::m_AdcBuffers[2][NUMBER_OF_SAMPLES][NUMBER_OF_CHANNELS];// __attribute__((section("AHBSRAM0")));
00016 //Serial rfid_serial(p28,p27);
00017 
00018 //This function prepares the capture buffers and starts the DMA peripheral
00019 void Capture::Start()
00020 {
00021     m_Nodes[0].destAddr = (unsigned int)m_AdcBuffers[0];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval;
00022     m_Nodes[0].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
00023     m_Nodes[0].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
00024     m_Nodes[0].nextNode = (unsigned int)&m_Nodes[1];
00025 
00026     m_Nodes[1].destAddr = (unsigned int)m_AdcBuffers[1];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval2;
00027     m_Nodes[1].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
00028     m_Nodes[1].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
00029     m_Nodes[1].nextNode = (unsigned int)&m_Nodes[0];
00030 
00031     m_BufferIndex=0;
00032 
00033     while(!(LPC_ADC->ADDR0&(1U<<31)));  //waits the completion of the ADC Channel 0 capture - for synchronization purposes
00034     while(!(LPC_ADC->ADDR0&(1U<<31)));  //waits the completion of the ADC Channel 0 capture - for synchronization purposes
00035 
00036     setup_channel(&m_Nodes[0],0,DMA_PERIPHERAL_ADC,DMA_MEMORY);
00037 }
00038 
00039 //This function initializes the ADC and DMA peripherals
00040 void Capture::Initialize()
00041 {   
00042     //printf("0x%lx\n",  LargeBuffer);
00043     init_adc(Settings::get_FreqBase()*NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS);
00044     select_channels(ADC_CH_0|ADC_CH_1|ADC_CH_2|ADC_CH_3|ADC_CH_4|ADC_CH_5);
00045     LPC_ADC->ADCR |= ADC_MODE_BURST;
00046 
00047     init_dma();
00048     
00049     Start();
00050     
00051     m_CaptureSemaphore.wait();
00052 }
00053 
00054 void Capture::Stop()
00055 {
00056     m_CaptureSemaphore.release();   //release semaphore
00057     stop_channel();
00058 }
00059 
00060 void Capture::Wait()
00061 {
00062     m_CaptureSemaphore.wait(osWaitForever);
00063 }
00064 
00065 unsigned short int Capture::GetValue(int nsamples, int nchannel)
00066 {
00067     return ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
00068 }
00069 
00070 void Capture::CopyBuffer(int channel, unsigned short int *dest)
00071 {
00072     for(int i=0;i<Settings::get_Samples();i++)
00073     {
00074         dest[i] = GetValue(i,channel);
00075     }
00076 }
00077 
00078 //DMA ISR signals the capture thread about the end of capture event
00079 extern "C" void DMA_IRQHandler(void)
00080 {
00081     Capture::ISRHandler();
00082 }
00083 
00084 void Capture::ISRHandler()
00085 {
00086     Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1;
00087 
00088     Capture::m_CaptureSemaphore.release();
00089     LPC_GPDMA->DMACIntTCClear = 0xFF;
00090 }
00091 
00092 void Capture::ReadRFID(int channel,char *rfid)
00093 {
00094     /*
00095     char cmd[4];
00096     cmd[0] = 'S';
00097     cmd[1] = '0'+channel;
00098     cmd[2] = '\n';
00099     cmd[3] = '\0';
00100     
00101     //send
00102     rfid_serial.puts(cmd);
00103     
00104     //receive
00105     char ch=0;
00106     char ans[10];
00107     int cnt=0;
00108     int tmout=1000;
00109     while(ch != '\n' && tmout-- && cnt<9)
00110     {
00111         if(rfid_serial.readable())
00112         {
00113             ch = rfid_serial.getc();
00114             if(!((ch>='0' && ch<='9') || (ch >= 'A' && ch <= 'F')))ch='0';
00115             ans[cnt++] = ch;
00116         }
00117         else
00118             wait_ms(1);
00119         
00120     }
00121     ans[cnt-1] = '\0';
00122     for(int i=0;i<8;i++)
00123         rfid[i] = ans[i];
00124     */
00125 }