Versão limpa em 04/09/2014. Telnet funcionando.

Dependencies:   EthernetInterface mbed-rtos mbed NTPClient

Committer:
rebonatto
Date:
Tue Apr 28 14:19:03 2015 +0000
Revision:
29:fcfcb31a6362
Parent:
1:fe2d4530da1b
Child:
31:647771325538
Fun??es auxiliares para trabalhar com signed int;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:66d8bd1f9d4c 1 /*
rebonatto 0:66d8bd1f9d4c 2 * Capture.h
rebonatto 0:66d8bd1f9d4c 3 *
rebonatto 0:66d8bd1f9d4c 4 * Created on:
rebonatto 0:66d8bd1f9d4c 5 * Author:
rebonatto 0:66d8bd1f9d4c 6 */
rebonatto 0:66d8bd1f9d4c 7 #include "Capture.h"
rebonatto 0:66d8bd1f9d4c 8
rebonatto 0:66d8bd1f9d4c 9 Semaphore Capture::m_CaptureSemaphore(1); //used to alert the capture thread about a ready capture
rebonatto 0:66d8bd1f9d4c 10 int Capture::m_BufferIndex;
rebonatto 0:66d8bd1f9d4c 11
rebonatto 0:66d8bd1f9d4c 12 //extern char LargeBuffer[1024];
rebonatto 0:66d8bd1f9d4c 13
rebonatto 0:66d8bd1f9d4c 14 __attribute((section("AHBSRAM1"),aligned)) dmaLinkedListNode Capture::m_Nodes[2];// __attribute__((section("AHBSRAM0"))); //this list holds the buffer configuration for the DMA peripheral
rebonatto 0:66d8bd1f9d4c 15 __attribute((section("AHBSRAM1"),aligned)) unsigned short int Capture::m_AdcBuffers[2][NUMBER_OF_SAMPLES][NUMBER_OF_CHANNELS];// __attribute__((section("AHBSRAM0")));
rebonatto 1:fe2d4530da1b 16 Serial rfid_serial(p28,p27);
rebonatto 0:66d8bd1f9d4c 17
rebonatto 0:66d8bd1f9d4c 18 //This function prepares the capture buffers and starts the DMA peripheral
rebonatto 0:66d8bd1f9d4c 19 void Capture::Start()
rebonatto 0:66d8bd1f9d4c 20 {
rebonatto 0:66d8bd1f9d4c 21 m_Nodes[0].destAddr = (unsigned int)m_AdcBuffers[0];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval;
rebonatto 0:66d8bd1f9d4c 22 m_Nodes[0].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
rebonatto 0:66d8bd1f9d4c 23 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:66d8bd1f9d4c 24 m_Nodes[0].nextNode = (unsigned int)&m_Nodes[1];
rebonatto 0:66d8bd1f9d4c 25
rebonatto 0:66d8bd1f9d4c 26 m_Nodes[1].destAddr = (unsigned int)m_AdcBuffers[1];// HERE GOES THE BUFFER ADDRESS(unsigned int)adval2;
rebonatto 0:66d8bd1f9d4c 27 m_Nodes[1].sourceAddr = (unsigned int)&LPC_ADC->ADGDR;
rebonatto 0:66d8bd1f9d4c 28 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:66d8bd1f9d4c 29 m_Nodes[1].nextNode = (unsigned int)&m_Nodes[0];
rebonatto 0:66d8bd1f9d4c 30
rebonatto 0:66d8bd1f9d4c 31 m_BufferIndex=0;
rebonatto 0:66d8bd1f9d4c 32
rebonatto 0:66d8bd1f9d4c 33 while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes
rebonatto 0:66d8bd1f9d4c 34 while(!(LPC_ADC->ADDR0&(1U<<31))); //waits the completion of the ADC Channel 0 capture - for synchronization purposes
rebonatto 0:66d8bd1f9d4c 35
rebonatto 0:66d8bd1f9d4c 36 setup_channel(&m_Nodes[0],0,DMA_PERIPHERAL_ADC,DMA_MEMORY);
rebonatto 0:66d8bd1f9d4c 37 }
rebonatto 0:66d8bd1f9d4c 38
rebonatto 0:66d8bd1f9d4c 39 //This function initializes the ADC and DMA peripherals
rebonatto 0:66d8bd1f9d4c 40 void Capture::Initialize()
rebonatto 0:66d8bd1f9d4c 41 {
rebonatto 0:66d8bd1f9d4c 42 //printf("0x%lx\n", LargeBuffer);
rebonatto 0:66d8bd1f9d4c 43 init_adc(FREQBASE*NUMBER_OF_SAMPLES*NUMBER_OF_CHANNELS);
rebonatto 0:66d8bd1f9d4c 44 select_channels(ADC_CH_0|ADC_CH_1|ADC_CH_2|ADC_CH_3|ADC_CH_4|ADC_CH_5);
rebonatto 0:66d8bd1f9d4c 45 LPC_ADC->ADCR |= ADC_MODE_BURST;
rebonatto 0:66d8bd1f9d4c 46
rebonatto 0:66d8bd1f9d4c 47 init_dma();
rebonatto 0:66d8bd1f9d4c 48
rebonatto 0:66d8bd1f9d4c 49 Start();
rebonatto 0:66d8bd1f9d4c 50
rebonatto 0:66d8bd1f9d4c 51 m_CaptureSemaphore.wait();
rebonatto 0:66d8bd1f9d4c 52 }
rebonatto 0:66d8bd1f9d4c 53
rebonatto 0:66d8bd1f9d4c 54 void Capture::Stop()
rebonatto 0:66d8bd1f9d4c 55 {
rebonatto 0:66d8bd1f9d4c 56 m_CaptureSemaphore.release(); //release semaphore
rebonatto 0:66d8bd1f9d4c 57 stop_channel();
rebonatto 0:66d8bd1f9d4c 58 }
rebonatto 0:66d8bd1f9d4c 59
rebonatto 0:66d8bd1f9d4c 60 void Capture::Wait()
rebonatto 0:66d8bd1f9d4c 61 {
rebonatto 0:66d8bd1f9d4c 62 m_CaptureSemaphore.wait(osWaitForever);
rebonatto 0:66d8bd1f9d4c 63 }
rebonatto 0:66d8bd1f9d4c 64
rebonatto 0:66d8bd1f9d4c 65 unsigned short int Capture::GetValue(int nsamples, int nchannel)
rebonatto 0:66d8bd1f9d4c 66 {
rebonatto 0:66d8bd1f9d4c 67 return ADC_CONVERT(m_AdcBuffers[m_BufferIndex][nsamples][nchannel]);
rebonatto 0:66d8bd1f9d4c 68 }
rebonatto 0:66d8bd1f9d4c 69
rebonatto 0:66d8bd1f9d4c 70 void Capture::CopyBuffer(int channel, unsigned short int *dest)
rebonatto 0:66d8bd1f9d4c 71 {
rebonatto 0:66d8bd1f9d4c 72 for(int i=0;i<NUMBER_OF_SAMPLES;i++)
rebonatto 0:66d8bd1f9d4c 73 {
rebonatto 0:66d8bd1f9d4c 74 dest[i] = GetValue(i,channel);
rebonatto 0:66d8bd1f9d4c 75 }
rebonatto 0:66d8bd1f9d4c 76 }
rebonatto 0:66d8bd1f9d4c 77
rebonatto 29:fcfcb31a6362 78 void Capture::CopyBufferSigned(int channel, short int *dest)
rebonatto 29:fcfcb31a6362 79 {
rebonatto 29:fcfcb31a6362 80 for(int i=0;i<NUMBER_OF_SAMPLES;i++)
rebonatto 29:fcfcb31a6362 81 {
rebonatto 29:fcfcb31a6362 82 dest[i] = GetValue(i,channel);
rebonatto 29:fcfcb31a6362 83 }
rebonatto 29:fcfcb31a6362 84 }
rebonatto 29:fcfcb31a6362 85
rebonatto 0:66d8bd1f9d4c 86 //DMA ISR signals the capture thread about the end of capture event
rebonatto 0:66d8bd1f9d4c 87 extern "C" void DMA_IRQHandler(void)
rebonatto 0:66d8bd1f9d4c 88 {
rebonatto 0:66d8bd1f9d4c 89 Capture::ISRHandler();
rebonatto 0:66d8bd1f9d4c 90 }
rebonatto 0:66d8bd1f9d4c 91
rebonatto 0:66d8bd1f9d4c 92 void Capture::ISRHandler()
rebonatto 0:66d8bd1f9d4c 93 {
rebonatto 0:66d8bd1f9d4c 94 Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1;
rebonatto 0:66d8bd1f9d4c 95
rebonatto 0:66d8bd1f9d4c 96 Capture::m_CaptureSemaphore.release();
rebonatto 0:66d8bd1f9d4c 97 LPC_GPDMA->DMACIntTCClear = 0xFF;
rebonatto 0:66d8bd1f9d4c 98 }
rebonatto 0:66d8bd1f9d4c 99
rebonatto 1:fe2d4530da1b 100 bool Capture::ReadRFID(int channel,char *rfid)
rebonatto 0:66d8bd1f9d4c 101 {
rebonatto 1:fe2d4530da1b 102
rebonatto 0:66d8bd1f9d4c 103 char cmd[4];
rebonatto 0:66d8bd1f9d4c 104 cmd[0] = 'S';
rebonatto 0:66d8bd1f9d4c 105 cmd[1] = '0'+channel;
rebonatto 0:66d8bd1f9d4c 106 cmd[2] = '\n';
rebonatto 0:66d8bd1f9d4c 107 cmd[3] = '\0';
rebonatto 0:66d8bd1f9d4c 108
rebonatto 0:66d8bd1f9d4c 109 //send
rebonatto 0:66d8bd1f9d4c 110 rfid_serial.puts(cmd);
rebonatto 0:66d8bd1f9d4c 111
rebonatto 0:66d8bd1f9d4c 112 //receive
rebonatto 0:66d8bd1f9d4c 113 char ch=0;
rebonatto 0:66d8bd1f9d4c 114 char ans[10];
rebonatto 0:66d8bd1f9d4c 115 int cnt=0;
rebonatto 0:66d8bd1f9d4c 116 int tmout=1000;
rebonatto 0:66d8bd1f9d4c 117 while(ch != '\n' && tmout-- && cnt<9)
rebonatto 0:66d8bd1f9d4c 118 {
rebonatto 0:66d8bd1f9d4c 119 if(rfid_serial.readable())
rebonatto 0:66d8bd1f9d4c 120 {
rebonatto 0:66d8bd1f9d4c 121 ch = rfid_serial.getc();
rebonatto 0:66d8bd1f9d4c 122 if(!((ch>='0' && ch<='9') || (ch >= 'A' && ch <= 'F')))ch='0';
rebonatto 0:66d8bd1f9d4c 123 ans[cnt++] = ch;
rebonatto 0:66d8bd1f9d4c 124 }
rebonatto 0:66d8bd1f9d4c 125 else
rebonatto 0:66d8bd1f9d4c 126 wait_ms(1);
rebonatto 0:66d8bd1f9d4c 127
rebonatto 0:66d8bd1f9d4c 128 }
rebonatto 1:fe2d4530da1b 129 if (cnt > 0){
rebonatto 1:fe2d4530da1b 130 ans[cnt-1] = '\0';
rebonatto 1:fe2d4530da1b 131 for(int i=0;i<10;i++)
rebonatto 1:fe2d4530da1b 132 rfid[i] = ans[i];
rebonatto 1:fe2d4530da1b 133 return true;
rebonatto 1:fe2d4530da1b 134 }
rebonatto 1:fe2d4530da1b 135 return false;
rebonatto 1:fe2d4530da1b 136
rebonatto 0:66d8bd1f9d4c 137 }