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

Dependencies:   EthernetInterface mbed-rtos mbed NTPClient

Committer:
rebonatto
Date:
Thu Jul 30 21:42:06 2015 +0000
Revision:
37:51fed2a3e009
Parent:
31:647771325538
Child:
39:9fd8397cbef9
Altera??es no formato do RFID colocado quando n?o ocorre leitura.

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 31:647771325538 86 void Capture::CopyBufferFloat(int channel, float *dest)
rebonatto 31:647771325538 87 {
rebonatto 31:647771325538 88 for(int i=0;i<NUMBER_OF_SAMPLES;i++)
rebonatto 31:647771325538 89 {
rebonatto 31:647771325538 90 dest[i] = (float) GetValue(i,channel);
rebonatto 31:647771325538 91 }
rebonatto 31:647771325538 92 }
rebonatto 31:647771325538 93
rebonatto 0:66d8bd1f9d4c 94 //DMA ISR signals the capture thread about the end of capture event
rebonatto 0:66d8bd1f9d4c 95 extern "C" void DMA_IRQHandler(void)
rebonatto 0:66d8bd1f9d4c 96 {
rebonatto 0:66d8bd1f9d4c 97 Capture::ISRHandler();
rebonatto 0:66d8bd1f9d4c 98 }
rebonatto 0:66d8bd1f9d4c 99
rebonatto 0:66d8bd1f9d4c 100 void Capture::ISRHandler()
rebonatto 0:66d8bd1f9d4c 101 {
rebonatto 0:66d8bd1f9d4c 102 Capture::m_BufferIndex = (~Capture::m_BufferIndex)&1;
rebonatto 0:66d8bd1f9d4c 103
rebonatto 0:66d8bd1f9d4c 104 Capture::m_CaptureSemaphore.release();
rebonatto 0:66d8bd1f9d4c 105 LPC_GPDMA->DMACIntTCClear = 0xFF;
rebonatto 0:66d8bd1f9d4c 106 }
rebonatto 37:51fed2a3e009 107 /*
rebonatto 1:fe2d4530da1b 108 bool Capture::ReadRFID(int channel,char *rfid)
rebonatto 0:66d8bd1f9d4c 109 {
rebonatto 1:fe2d4530da1b 110
rebonatto 0:66d8bd1f9d4c 111 char cmd[4];
rebonatto 0:66d8bd1f9d4c 112 cmd[0] = 'S';
rebonatto 0:66d8bd1f9d4c 113 cmd[1] = '0'+channel;
rebonatto 0:66d8bd1f9d4c 114 cmd[2] = '\n';
rebonatto 0:66d8bd1f9d4c 115 cmd[3] = '\0';
rebonatto 0:66d8bd1f9d4c 116
rebonatto 0:66d8bd1f9d4c 117 //send
rebonatto 0:66d8bd1f9d4c 118 rfid_serial.puts(cmd);
rebonatto 0:66d8bd1f9d4c 119
rebonatto 0:66d8bd1f9d4c 120 //receive
rebonatto 0:66d8bd1f9d4c 121 char ch=0;
rebonatto 0:66d8bd1f9d4c 122 char ans[10];
rebonatto 0:66d8bd1f9d4c 123 int cnt=0;
rebonatto 0:66d8bd1f9d4c 124 int tmout=1000;
rebonatto 0:66d8bd1f9d4c 125 while(ch != '\n' && tmout-- && cnt<9)
rebonatto 0:66d8bd1f9d4c 126 {
rebonatto 0:66d8bd1f9d4c 127 if(rfid_serial.readable())
rebonatto 0:66d8bd1f9d4c 128 {
rebonatto 0:66d8bd1f9d4c 129 ch = rfid_serial.getc();
rebonatto 0:66d8bd1f9d4c 130 if(!((ch>='0' && ch<='9') || (ch >= 'A' && ch <= 'F')))ch='0';
rebonatto 0:66d8bd1f9d4c 131 ans[cnt++] = ch;
rebonatto 0:66d8bd1f9d4c 132 }
rebonatto 0:66d8bd1f9d4c 133 else
rebonatto 0:66d8bd1f9d4c 134 wait_ms(1);
rebonatto 0:66d8bd1f9d4c 135
rebonatto 0:66d8bd1f9d4c 136 }
rebonatto 1:fe2d4530da1b 137 if (cnt > 0){
rebonatto 1:fe2d4530da1b 138 ans[cnt-1] = '\0';
rebonatto 1:fe2d4530da1b 139 for(int i=0;i<10;i++)
rebonatto 1:fe2d4530da1b 140 rfid[i] = ans[i];
rebonatto 1:fe2d4530da1b 141 return true;
rebonatto 1:fe2d4530da1b 142 }
rebonatto 1:fe2d4530da1b 143 return false;
rebonatto 1:fe2d4530da1b 144
rebonatto 37:51fed2a3e009 145 }
rebonatto 37:51fed2a3e009 146 */
rebonatto 37:51fed2a3e009 147
rebonatto 37:51fed2a3e009 148 void Capture::ReadRFID(int channel,char *rfid)
rebonatto 37:51fed2a3e009 149 {
rebonatto 37:51fed2a3e009 150
rebonatto 37:51fed2a3e009 151 char cmd[4];
rebonatto 37:51fed2a3e009 152 cmd[0] = 'S';
rebonatto 37:51fed2a3e009 153 cmd[1] = '0'+channel;
rebonatto 37:51fed2a3e009 154 cmd[2] = '\n';
rebonatto 37:51fed2a3e009 155 cmd[3] = '\0';
rebonatto 37:51fed2a3e009 156
rebonatto 37:51fed2a3e009 157 //send
rebonatto 37:51fed2a3e009 158 rfid_serial.puts(cmd);
rebonatto 37:51fed2a3e009 159
rebonatto 37:51fed2a3e009 160 //receive
rebonatto 37:51fed2a3e009 161 char ch=0;
rebonatto 37:51fed2a3e009 162 char ans[10];
rebonatto 37:51fed2a3e009 163 int cnt=0;
rebonatto 37:51fed2a3e009 164 int tmout=1000;
rebonatto 37:51fed2a3e009 165 while(ch != '\n' && tmout-- && cnt<9)
rebonatto 37:51fed2a3e009 166 {
rebonatto 37:51fed2a3e009 167 if(rfid_serial.readable())
rebonatto 37:51fed2a3e009 168 {
rebonatto 37:51fed2a3e009 169 ch = rfid_serial.getc();
rebonatto 37:51fed2a3e009 170 if(!((ch>='0' && ch<='9') || (ch >= 'A' && ch <= 'F')))ch='0';
rebonatto 37:51fed2a3e009 171 ans[cnt++] = ch;
rebonatto 37:51fed2a3e009 172 }
rebonatto 37:51fed2a3e009 173 else
rebonatto 37:51fed2a3e009 174 wait_ms(1);
rebonatto 37:51fed2a3e009 175
rebonatto 37:51fed2a3e009 176 }
rebonatto 37:51fed2a3e009 177 ans[cnt-1] = '\0';
rebonatto 37:51fed2a3e009 178 for(int i=0;i<9;i++)
rebonatto 37:51fed2a3e009 179 rfid[i] = ans[i];
rebonatto 37:51fed2a3e009 180
rebonatto 0:66d8bd1f9d4c 181 }