![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
First Version
Dependencies: EthernetInterface mbed-rtos mbed
Codes/Capture.cpp@0:9df41090ba33, 2015-07-21 (annotated)
- Committer:
- rebonatto
- Date:
- Tue Jul 21 21:29:49 2015 +0000
- Revision:
- 0:9df41090ba33
- Child:
- 2:bf4bbf7d6793
First Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rebonatto | 0:9df41090ba33 | 1 | /* |
rebonatto | 0:9df41090ba33 | 2 | * Capture.h |
rebonatto | 0:9df41090ba33 | 3 | * |
rebonatto | 0:9df41090ba33 | 4 | * Created on: |
rebonatto | 0:9df41090ba33 | 5 | * Author: |
rebonatto | 0:9df41090ba33 | 6 | */ |
rebonatto | 0:9df41090ba33 | 7 | #include "Capture.h" |
rebonatto | 0:9df41090ba33 | 8 | |
rebonatto | 0:9df41090ba33 | 9 | Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture |
rebonatto | 0:9df41090ba33 | 10 | int Capture::m_BufferIndex; |
rebonatto | 0:9df41090ba33 | 11 | dmaLinkedListNode Capture::m_Nodes[2] __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral |
rebonatto | 0:9df41090ba33 | 12 | unsigned short int Capture::m_AdcBuffers[2][NUMBER_OF_SAMPLES][NUMBER_OF_CHANNELS] __attribute__((section("AHBSRAM0"))); |
rebonatto | 0:9df41090ba33 | 13 | Serial rfid_serial(p28,p27); |
rebonatto | 0:9df41090ba33 | 14 | |
rebonatto | 0:9df41090ba33 | 15 | //This function prepares the capture buffers and starts the DMA peripheral |
rebonatto | 0:9df41090ba33 | 16 | void Capture::Start() |
rebonatto | 0:9df41090ba33 | 17 | { |
rebonatto | 0:9df41090ba33 | 18 | m_Nodes[0].destAddr = (unsigned int)m_AdcBuffers[0];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval; |
rebonatto | 0:9df41090ba33 | 19 | m_Nodes[0].sourceAddr = (unsigned int)&LPC_ADC->ADGDR; |
rebonatto | 0:9df41090ba33 | 20 | 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:9df41090ba33 | 21 | m_Nodes[0].nextNode = (unsigned int)&m_Nodes[1]; |
rebonatto | 0:9df41090ba33 | 22 | |
rebonatto | 0:9df41090ba33 | 23 | m_Nodes[1].destAddr = (unsigned int)m_AdcBuffers[1];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval2; |
rebonatto | 0:9df41090ba33 | 24 | m_Nodes[1].sourceAddr = (unsigned int)&LPC_ADC->ADGDR; |
rebonatto | 0:9df41090ba33 | 25 | 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:9df41090ba33 | 26 | m_Nodes[1].nextNode = (unsigned int)&m_Nodes[0]; |
rebonatto | 0:9df41090ba33 | 27 | |
rebonatto | 0:9df41090ba33 | 28 | m_BufferIndex=0; |
rebonatto | 0:9df41090ba33 | 29 | |
rebonatto | 0:9df41090ba33 | 30 | while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes |
rebonatto | 0:9df41090ba33 | 31 | while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes |
rebonatto | 0:9df41090ba33 | 32 | |
rebonatto | 0:9df41090ba33 | 33 | setup_channel(&m_Nodes[0],0,DMA_PERIPHERAL_ADC,DMA_MEMORY); |
rebonatto | 0:9df41090ba33 | 34 | } |
rebonatto | 0:9df41090ba33 | 35 | |
rebonatto | 0:9df41090ba33 | 36 | //This function initializes the ADC and DMA peripherals |
rebonatto | 0:9df41090ba33 | 37 | void Capture::Initialize() |
rebonatto | 0:9df41090ba33 | 38 | { |
rebonatto | 0:9df41090ba33 | 39 | init_adc(Settings::get_FreqBase()*NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS); |
rebonatto | 0:9df41090ba33 | 40 | select_channels(ADC_CH_0|ADC_CH_1|ADC_CH_2|ADC_CH_3|ADC_CH_4|ADC_CH_5); |
rebonatto | 0:9df41090ba33 | 41 | LPC_ADC->ADCR |= ADC_MODE_BURST; |
rebonatto | 0:9df41090ba33 | 42 | |
rebonatto | 0:9df41090ba33 | 43 | init_dma(); |
rebonatto | 0:9df41090ba33 | 44 | |
rebonatto | 0:9df41090ba33 | 45 | Start(); |
rebonatto | 0:9df41090ba33 | 46 | |
rebonatto | 0:9df41090ba33 | 47 | m_CaptureSemaphore.wait(); |
rebonatto | 0:9df41090ba33 | 48 | } |
rebonatto | 0:9df41090ba33 | 49 | |
rebonatto | 0:9df41090ba33 | 50 | void Capture::Stop() |
rebonatto | 0:9df41090ba33 | 51 | { |
rebonatto | 0:9df41090ba33 | 52 | m_CaptureSemaphore.release(); //release semaphore |
rebonatto | 0:9df41090ba33 | 53 | stop_channel(); |
rebonatto | 0:9df41090ba33 | 54 | } |
rebonatto | 0:9df41090ba33 | 55 | |
rebonatto | 0:9df41090ba33 | 56 | void Capture::Wait() |
rebonatto | 0:9df41090ba33 | 57 | { |
rebonatto | 0:9df41090ba33 | 58 | m_CaptureSemaphore.wait(osWaitForever); |
rebonatto | 0:9df41090ba33 | 59 | } |
rebonatto | 0:9df41090ba33 | 60 | |
rebonatto | 0:9df41090ba33 | 61 | unsigned short int Capture::GetValue(int nsamples, int nchannel) |
rebonatto | 0:9df41090ba33 | 62 | { |
rebonatto | 0:9df41090ba33 | 63 | return ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]); |
rebonatto | 0:9df41090ba33 | 64 | } |
rebonatto | 0:9df41090ba33 | 65 | |
rebonatto | 0:9df41090ba33 | 66 | void Capture::CopyBuffer(int channel, unsigned short int *dest) |
rebonatto | 0:9df41090ba33 | 67 | { |
rebonatto | 0:9df41090ba33 | 68 | for(int i=0;i<Settings::get_Samples();i++) |
rebonatto | 0:9df41090ba33 | 69 | { |
rebonatto | 0:9df41090ba33 | 70 | dest[i] = GetValue(i,channel); |
rebonatto | 0:9df41090ba33 | 71 | } |
rebonatto | 0:9df41090ba33 | 72 | } |
rebonatto | 0:9df41090ba33 | 73 | |
rebonatto | 0:9df41090ba33 | 74 | //DMA ISR signals the capture thread about the end of capture event |
rebonatto | 0:9df41090ba33 | 75 | extern "C" void DMA_IRQHandler(void) |
rebonatto | 0:9df41090ba33 | 76 | { |
rebonatto | 0:9df41090ba33 | 77 | Capture::ISRHandler(); |
rebonatto | 0:9df41090ba33 | 78 | } |
rebonatto | 0:9df41090ba33 | 79 | |
rebonatto | 0:9df41090ba33 | 80 | void Capture::ISRHandler() |
rebonatto | 0:9df41090ba33 | 81 | { |
rebonatto | 0:9df41090ba33 | 82 | Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1; |
rebonatto | 0:9df41090ba33 | 83 | |
rebonatto | 0:9df41090ba33 | 84 | Capture::m_CaptureSemaphore.release(); |
rebonatto | 0:9df41090ba33 | 85 | LPC_GPDMA->DMACIntTCClear = 0xFF; |
rebonatto | 0:9df41090ba33 | 86 | } |
rebonatto | 0:9df41090ba33 | 87 | |
rebonatto | 0:9df41090ba33 | 88 | void Capture::ReadRFID(int channel,char *rfid) |
rebonatto | 0:9df41090ba33 | 89 | { |
rebonatto | 0:9df41090ba33 | 90 | |
rebonatto | 0:9df41090ba33 | 91 | char cmd[4]; |
rebonatto | 0:9df41090ba33 | 92 | cmd[0] = 'S'; |
rebonatto | 0:9df41090ba33 | 93 | cmd[1] = '0'+channel; |
rebonatto | 0:9df41090ba33 | 94 | cmd[2] = '\n'; |
rebonatto | 0:9df41090ba33 | 95 | cmd[3] = '\0'; |
rebonatto | 0:9df41090ba33 | 96 | |
rebonatto | 0:9df41090ba33 | 97 | //send |
rebonatto | 0:9df41090ba33 | 98 | rfid_serial.puts(cmd); |
rebonatto | 0:9df41090ba33 | 99 | |
rebonatto | 0:9df41090ba33 | 100 | //receive |
rebonatto | 0:9df41090ba33 | 101 | char ch=0; |
rebonatto | 0:9df41090ba33 | 102 | char ans[10]; |
rebonatto | 0:9df41090ba33 | 103 | int cnt=0; |
rebonatto | 0:9df41090ba33 | 104 | int tmout=1000; |
rebonatto | 0:9df41090ba33 | 105 | while(ch != '\n' && tmout-- && cnt<9) |
rebonatto | 0:9df41090ba33 | 106 | { |
rebonatto | 0:9df41090ba33 | 107 | if(rfid_serial.readable()) |
rebonatto | 0:9df41090ba33 | 108 | { |
rebonatto | 0:9df41090ba33 | 109 | ch = rfid_serial.getc(); |
rebonatto | 0:9df41090ba33 | 110 | if(!((ch>='0' && ch<='9') || (ch >= 'A' && ch <= 'F')))ch='0'; |
rebonatto | 0:9df41090ba33 | 111 | ans[cnt++] = ch; |
rebonatto | 0:9df41090ba33 | 112 | } |
rebonatto | 0:9df41090ba33 | 113 | else |
rebonatto | 0:9df41090ba33 | 114 | wait_ms(1); |
rebonatto | 0:9df41090ba33 | 115 | |
rebonatto | 0:9df41090ba33 | 116 | } |
rebonatto | 0:9df41090ba33 | 117 | ans[cnt-1] = '\0'; |
rebonatto | 0:9df41090ba33 | 118 | for(int i=0;i<9;i++) |
rebonatto | 0:9df41090ba33 | 119 | rfid[i] = ans[i]; |
rebonatto | 0:9df41090ba33 | 120 | |
rebonatto | 0:9df41090ba33 | 121 | } |