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