Versão do protegemed que calcula o tempo em ms da fuga, calcula o numero de onverflow (valores muito baixo) e underflow (valores muito altos). Além disso, calcula um valor médio a partir dos valores capturados e não apenas pela fft.

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
rebonatto
Date:
Wed Jul 09 21:16:23 2014 +0000
Revision:
0:c64e1194230b
Child:
1:917ca6b5d9d9
Vers?o do Protegemed com calculo de tempo de fuga, overflow, underflow e novo valor m?dio (manual).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:c64e1194230b 1 /*
rebonatto 0:c64e1194230b 2 * Capture.h
rebonatto 0:c64e1194230b 3 *
rebonatto 0:c64e1194230b 4 * Created on:
rebonatto 0:c64e1194230b 5 * Author:
rebonatto 0:c64e1194230b 6 */
rebonatto 0:c64e1194230b 7 #include "Capture.h"
rebonatto 0:c64e1194230b 8
rebonatto 0:c64e1194230b 9 Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture
rebonatto 0:c64e1194230b 10 int Capture::m_BufferIndex;
rebonatto 0:c64e1194230b 11 dmaLinkedListNode Capture::m_Nodes[2] __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral
rebonatto 0:c64e1194230b 12 unsigned short int Capture::m_AdcBuffers[2][NUMBER_OF_SAMPLES][NUMBER_OF_CHANNELS] __attribute__((section("AHBSRAM0")));
rebonatto 0:c64e1194230b 13
rebonatto 0:c64e1194230b 14 //This function prepares the capture buffers and starts the DMA peripheral
rebonatto 0:c64e1194230b 15 void Capture::Start()
rebonatto 0:c64e1194230b 16 {
rebonatto 0:c64e1194230b 17 m_Nodes[0].destAddr = (unsigned int)m_AdcBuffers[0];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval;
rebonatto 0:c64e1194230b 18 m_Nodes[0].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
rebonatto 0:c64e1194230b 19 m_Nodes[0].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
rebonatto 0:c64e1194230b 20 m_Nodes[0].nextNode = (unsigned int)&m_Nodes[1];
rebonatto 0:c64e1194230b 21
rebonatto 0:c64e1194230b 22 m_Nodes[1].destAddr = (unsigned int)m_AdcBuffers[1];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval2;
rebonatto 0:c64e1194230b 23 m_Nodes[1].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
rebonatto 0:c64e1194230b 24 m_Nodes[1].dmaControl = (NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS)| DMA_DST_WIDTH_HALFWORD | DMA_SRC_WIDTH_HALFWORD | DMA_DST_INCREMENT | DMA_TC_INT;
rebonatto 0:c64e1194230b 25 m_Nodes[1].nextNode = (unsigned int)&m_Nodes[0];
rebonatto 0:c64e1194230b 26
rebonatto 0:c64e1194230b 27 m_BufferIndex=0;
rebonatto 0:c64e1194230b 28
rebonatto 0:c64e1194230b 29 while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes
rebonatto 0:c64e1194230b 30 while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes
rebonatto 0:c64e1194230b 31
rebonatto 0:c64e1194230b 32 setup_channel(&m_Nodes[0],0,DMA_PERIPHERAL_ADC,DMA_MEMORY);
rebonatto 0:c64e1194230b 33 }
rebonatto 0:c64e1194230b 34
rebonatto 0:c64e1194230b 35 //This function initializes the ADC and DMA peripherals
rebonatto 0:c64e1194230b 36 void Capture::Initialize()
rebonatto 0:c64e1194230b 37 {
rebonatto 0:c64e1194230b 38 init_adc(Settings::get_FreqBase()*NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS);
rebonatto 0:c64e1194230b 39 select_channels(ADC_CH_0|ADC_CH_1|ADC_CH_2|ADC_CH_3|ADC_CH_4|ADC_CH_5);
rebonatto 0:c64e1194230b 40 LPC_ADC->ADCR |= ADC_MODE_BURST;
rebonatto 0:c64e1194230b 41
rebonatto 0:c64e1194230b 42 init_dma();
rebonatto 0:c64e1194230b 43
rebonatto 0:c64e1194230b 44 Start();
rebonatto 0:c64e1194230b 45
rebonatto 0:c64e1194230b 46 m_CaptureSemaphore.wait();
rebonatto 0:c64e1194230b 47 }
rebonatto 0:c64e1194230b 48
rebonatto 0:c64e1194230b 49 void Capture::Stop()
rebonatto 0:c64e1194230b 50 {
rebonatto 0:c64e1194230b 51 m_CaptureSemaphore.release(); //release semaphore
rebonatto 0:c64e1194230b 52 stop_channel();
rebonatto 0:c64e1194230b 53 }
rebonatto 0:c64e1194230b 54
rebonatto 0:c64e1194230b 55 void Capture::Wait()
rebonatto 0:c64e1194230b 56 {
rebonatto 0:c64e1194230b 57 m_CaptureSemaphore.wait(osWaitForever);
rebonatto 0:c64e1194230b 58 }
rebonatto 0:c64e1194230b 59
rebonatto 0:c64e1194230b 60 unsigned short int Capture::GetValue(int nsamples, int nchannel)
rebonatto 0:c64e1194230b 61 {
rebonatto 0:c64e1194230b 62 return ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
rebonatto 0:c64e1194230b 63 }
rebonatto 0:c64e1194230b 64
rebonatto 0:c64e1194230b 65 void Capture::CopyBuffer(int channel, unsigned short int *dest)
rebonatto 0:c64e1194230b 66 {
rebonatto 0:c64e1194230b 67 for(int i=0;i<Settings::get_Samples();i++)
rebonatto 0:c64e1194230b 68 {
rebonatto 0:c64e1194230b 69 dest[i] = GetValue(i,channel);
rebonatto 0:c64e1194230b 70 }
rebonatto 0:c64e1194230b 71 }
rebonatto 0:c64e1194230b 72
rebonatto 0:c64e1194230b 73 //DMA ISR signals the capture thread about the end of capture event
rebonatto 0:c64e1194230b 74 extern "C" void DMA_IRQHandler(void)
rebonatto 0:c64e1194230b 75 {
rebonatto 0:c64e1194230b 76 Capture::ISRHandler();
rebonatto 0:c64e1194230b 77 }
rebonatto 0:c64e1194230b 78
rebonatto 0:c64e1194230b 79 void Capture::ISRHandler()
rebonatto 0:c64e1194230b 80 {
rebonatto 0:c64e1194230b 81 Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1;
rebonatto 0:c64e1194230b 82
rebonatto 0:c64e1194230b 83 Capture::m_CaptureSemaphore.release();
rebonatto 0:c64e1194230b 84 LPC_GPDMA->DMACIntTCClear = 0xFF;
rebonatto 0:c64e1194230b 85 }