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

Dependencies:   EthernetInterface mbed-rtos mbed NTPClient

Committer:
rebonatto
Date:
Thu Jan 07 18:44:44 2016 +0000
Revision:
38:132e83a591d0
Parent:
33:3abe9d906312
Child:
41:a413c26109c2
Child:
42:d6f9ce115eaf
Vers?o est?vel com DMA e FFT.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rebonatto 0:66d8bd1f9d4c 1 /*
rebonatto 0:66d8bd1f9d4c 2 Novo projeto MBED_Limpo
rebonatto 0:66d8bd1f9d4c 3 Criado para: Tentar reduzir o consumo de memória.
rebonatto 0:66d8bd1f9d4c 4 - Tirei do Seetings
rebonatto 0:66d8bd1f9d4c 5 - m_MaxChannels --> tem constante (NUMBER_OF_CHANNELS). Se mudar tem de recompilar
rebonatto 0:66d8bd1f9d4c 6 - m_MaxOutlets --> tem constante (NUMBER_OF_OUTLETS). Se mudar tem de recompilar
rebonatto 0:66d8bd1f9d4c 7 - m_Samples --> tem constante (NUMBER_OF_SAMPLES). Se mudar tem de recompilar
rebonatto 0:66d8bd1f9d4c 8 - m_FreqBase --> Adicionei constante (FREQBASE). Se mudar tem de recompilar.
rebonatto 0:66d8bd1f9d4c 9 - m_NumNeighbors --> Não utilizado, sem acompanhamento
rebonatto 0:66d8bd1f9d4c 10 - m_Neighbor[] --> Não Utilizado, sem acompanhamento
rebonatto 0:66d8bd1f9d4c 11 - m_PortTCP --> Não Utilizado, sem acompanhamento
rebonatto 0:66d8bd1f9d4c 12 - m_ServerSocket --> Não Utilizado, sem acompanhamento
rebonatto 0:66d8bd1f9d4c 13 - m_Socket[] --> Não Utilizado, sem acompanhamento
rebonatto 0:66d8bd1f9d4c 14 - m_MaxTries --> Adicionei constante (MAXTRIES). Se mudar tem de recompilar.
rebonatto 0:66d8bd1f9d4c 15 */
rebonatto 0:66d8bd1f9d4c 16 #include <stdio.h>
rebonatto 0:66d8bd1f9d4c 17
rebonatto 0:66d8bd1f9d4c 18 #include "mbed.h"
rebonatto 0:66d8bd1f9d4c 19 #include "rtos.h"
rebonatto 0:66d8bd1f9d4c 20 #include "cmsis_os.h"
rebonatto 0:66d8bd1f9d4c 21 //#include "EthernetIf.h"
rebonatto 0:66d8bd1f9d4c 22 #include "EthernetInterface.h"
rebonatto 0:66d8bd1f9d4c 23 #include "Settings.h"
rebonatto 0:66d8bd1f9d4c 24 #include "Capture.h"
rebonatto 0:66d8bd1f9d4c 25 #include "Http_post.h"
rebonatto 0:66d8bd1f9d4c 26 //#include "CommTCP.h"
rebonatto 0:66d8bd1f9d4c 27 #include "SignalProcessor.h"
rebonatto 0:66d8bd1f9d4c 28 #include "EventDetector.h"
rebonatto 0:66d8bd1f9d4c 29 #include "limites.h"
rebonatto 0:66d8bd1f9d4c 30
rebonatto 0:66d8bd1f9d4c 31 #include "TelnetServer.h"
rebonatto 0:66d8bd1f9d4c 32
mlucas82 3:52419e510163 33 #include "tftpsrv.h"
mlucas82 3:52419e510163 34 #include "ntpc.h"
mlucas82 3:52419e510163 35
mlucas82 14:f756167f791f 36 #include "PmedLog.h"
mlucas82 14:f756167f791f 37
mlucas82 26:7e215fc0c118 38 #include "NTPClient.h"
mlucas82 26:7e215fc0c118 39
rebonatto 38:132e83a591d0 40 #include "whatchdog.h"
rebonatto 38:132e83a591d0 41
rebonatto 38:132e83a591d0 42
rebonatto 38:132e83a591d0 43
rebonatto 0:66d8bd1f9d4c 44 //__attribute((section("AHBSRAM0"),aligned)) char LargeBuffer[1024];
rebonatto 0:66d8bd1f9d4c 45 EthernetInterface eth;
rebonatto 38:132e83a591d0 46 Watchdog wdt;
rebonatto 0:66d8bd1f9d4c 47
rebonatto 0:66d8bd1f9d4c 48 void thread1(void const *args)
rebonatto 0:66d8bd1f9d4c 49 {
rebonatto 0:66d8bd1f9d4c 50 DigitalOut led1(LED1);
rebonatto 0:66d8bd1f9d4c 51 int n = 0;
rebonatto 0:66d8bd1f9d4c 52 //int tatual, tnovo;
rebonatto 0:66d8bd1f9d4c 53 float rms[NUMBER_OF_CHANNELS], mv2[NUMBER_OF_CHANNELS];
rebonatto 0:66d8bd1f9d4c 54 int under[NUMBER_OF_CHANNELS], over[NUMBER_OF_CHANNELS];
rebonatto 0:66d8bd1f9d4c 55
rebonatto 0:66d8bd1f9d4c 56 Capture::Initialize();
rebonatto 0:66d8bd1f9d4c 57
rebonatto 0:66d8bd1f9d4c 58 //Timer t;
rebonatto 0:66d8bd1f9d4c 59 //t.start();
rebonatto 0:66d8bd1f9d4c 60 //tatual = 0;
rebonatto 0:66d8bd1f9d4c 61
rebonatto 0:66d8bd1f9d4c 62 while(1)
rebonatto 0:66d8bd1f9d4c 63 {
rebonatto 0:66d8bd1f9d4c 64 Capture::Wait();
rebonatto 0:66d8bd1f9d4c 65
rebonatto 0:66d8bd1f9d4c 66 // Calcula o RMS dos 6 canais
rebonatto 0:66d8bd1f9d4c 67 SignalProcessor::CalculateRMSBulk(rms, mv2, under, over);
rebonatto 0:66d8bd1f9d4c 68 //printf("Tempo ms %d\n", t.read_ms());
rebonatto 0:66d8bd1f9d4c 69 //t.reset();
rebonatto 0:66d8bd1f9d4c 70
rebonatto 0:66d8bd1f9d4c 71 //rms[0] = 2050;
rebonatto 0:66d8bd1f9d4c 72 //rms[1]=rms[2]=rms[3]=rms[4]=rms[5]=2000;
rebonatto 0:66d8bd1f9d4c 73
rebonatto 0:66d8bd1f9d4c 74 for(int i=0;i<6;i++){
rebonatto 0:66d8bd1f9d4c 75 //printf("Main %d\n", i);
rebonatto 0:66d8bd1f9d4c 76 EventDetector::get_Detector(i).ProcessEvent(rms[i], mv2[i], under[i], over[i]);
rebonatto 0:66d8bd1f9d4c 77 //wait_ms(2);
rebonatto 0:66d8bd1f9d4c 78 }
rebonatto 0:66d8bd1f9d4c 79 //Thread::yield();
rebonatto 0:66d8bd1f9d4c 80
rebonatto 0:66d8bd1f9d4c 81
rebonatto 0:66d8bd1f9d4c 82 /*
rebonatto 0:66d8bd1f9d4c 83 for(int i =0; i < 6; i++)
rebonatto 0:66d8bd1f9d4c 84 printf("%5.2f\t", rms[i]);
rebonatto 0:66d8bd1f9d4c 85 printf("\n");
rebonatto 0:66d8bd1f9d4c 86
rebonatto 0:66d8bd1f9d4c 87 wait(5);
rebonatto 0:66d8bd1f9d4c 88 */
rebonatto 0:66d8bd1f9d4c 89 n++;
rebonatto 0:66d8bd1f9d4c 90 if(n==60)
rebonatto 0:66d8bd1f9d4c 91 {
rebonatto 0:66d8bd1f9d4c 92 printf("%.2f %.0f %.2f %.0f\t%.2f %.0f %.2f %.0f\t%.2f %.0f %.2f %.0f\n",rms[0], mv2[0],rms[1],mv2[1],rms[2],mv2[2],rms[3],mv2[3],rms[4],mv2[4],rms[5],mv2[5]);
rebonatto 0:66d8bd1f9d4c 93 led1 = !led1;
rebonatto 0:66d8bd1f9d4c 94 n=0;
rebonatto 0:66d8bd1f9d4c 95 //t.stop();
rebonatto 0:66d8bd1f9d4c 96
rebonatto 0:66d8bd1f9d4c 97 /*
rebonatto 0:66d8bd1f9d4c 98 tnovo = t.read_us();
rebonatto 0:66d8bd1f9d4c 99 printf("MAIN: The time XXX taken loop %d\n", tnovo - tatual);
rebonatto 0:66d8bd1f9d4c 100 tatual = tnovo;
rebonatto 0:66d8bd1f9d4c 101 t.reset();
rebonatto 0:66d8bd1f9d4c 102 */
rebonatto 0:66d8bd1f9d4c 103 //Thread::wait(1000); //1000
rebonatto 33:3abe9d906312 104
mlucas82 14:f756167f791f 105 PmedLog::Mark();
rebonatto 33:3abe9d906312 106
rebonatto 33:3abe9d906312 107 //wait(1);
rebonatto 0:66d8bd1f9d4c 108 }
rebonatto 0:66d8bd1f9d4c 109 }
rebonatto 0:66d8bd1f9d4c 110 }
rebonatto 0:66d8bd1f9d4c 111
rebonatto 0:66d8bd1f9d4c 112 /*
rebonatto 0:66d8bd1f9d4c 113 void InitializeEthernetLink()
rebonatto 0:66d8bd1f9d4c 114 {
rebonatto 0:66d8bd1f9d4c 115 if(Settings::get_Dhcp())
rebonatto 0:66d8bd1f9d4c 116 eth.init(); //Use DHCP
rebonatto 0:66d8bd1f9d4c 117 else
rebonatto 0:66d8bd1f9d4c 118 eth.init(Settings::get_IpAddress(),Settings::get_Netmask(),Settings::get_Gateway());
rebonatto 0:66d8bd1f9d4c 119
rebonatto 0:66d8bd1f9d4c 120 eth.connect();
rebonatto 0:66d8bd1f9d4c 121 printf("IP Address is %s\n", eth.getIPAddress());
rebonatto 0:66d8bd1f9d4c 122 }
rebonatto 0:66d8bd1f9d4c 123 */
rebonatto 0:66d8bd1f9d4c 124 void InitializeEthernetLink()
rebonatto 0:66d8bd1f9d4c 125 {
rebonatto 0:66d8bd1f9d4c 126 if(Settings::get_Dhcp())
rebonatto 0:66d8bd1f9d4c 127 //EthernetIf::Initialize(); //Use DHCP
rebonatto 0:66d8bd1f9d4c 128 eth.init(); //Use DHCP
rebonatto 0:66d8bd1f9d4c 129 else
rebonatto 0:66d8bd1f9d4c 130 //EthernetIf::Initialize(Settings::get_IpAddress(),Settings::get_Netmask(),Settings::get_Gateway());
rebonatto 0:66d8bd1f9d4c 131 eth.init(Settings::get_IpAddress(),Settings::get_Netmask(),Settings::get_Gateway());
rebonatto 0:66d8bd1f9d4c 132
rebonatto 0:66d8bd1f9d4c 133 //EthernetIf::Connect();
rebonatto 0:66d8bd1f9d4c 134 eth.connect();
rebonatto 0:66d8bd1f9d4c 135 //printf("IP Address is %s\n", EthernetIf::get_IpAddress());
rebonatto 0:66d8bd1f9d4c 136 printf("IP Address is NEW %s\n", eth.getIPAddress());
rebonatto 0:66d8bd1f9d4c 137 }
rebonatto 0:66d8bd1f9d4c 138
rebonatto 0:66d8bd1f9d4c 139 int main() {
rebonatto 38:132e83a591d0 140 //DigitalOut myled3(LED3); //The pushbutton or power on caused a reset
rebonatto 38:132e83a591d0 141 //DigitalOut myled4(LED4); //The watchdog timer caused a reset
rebonatto 38:132e83a591d0 142
rebonatto 38:132e83a591d0 143 if ((LPC_WDT->WDMOD >> 2) & 1){
rebonatto 38:132e83a591d0 144 //myled4 = 1;
rebonatto 38:132e83a591d0 145 PmedLog::WriteEntry(PMEDLOG_INITIALIZINGWDT);
rebonatto 38:132e83a591d0 146 }
rebonatto 38:132e83a591d0 147 else{
rebonatto 38:132e83a591d0 148 //myled3 = 1;
rebonatto 38:132e83a591d0 149 PmedLog::WriteEntry(PMEDLOG_INITIALIZING);
rebonatto 38:132e83a591d0 150 }
mlucas82 18:3e9ad4c03f85 151
rebonatto 38:132e83a591d0 152 printf("\r\nNova versao 70 ( 20151028.1 )... Com WhatchDog\r\n");
rebonatto 0:66d8bd1f9d4c 153 FILE *f;
rebonatto 0:66d8bd1f9d4c 154 //Set Highest Priority
rebonatto 0:66d8bd1f9d4c 155 //osThreadSetPriority(osThreadGetId(),osPriorityHigh);
rebonatto 0:66d8bd1f9d4c 156
rebonatto 0:66d8bd1f9d4c 157 Settings::ReadFile();
rebonatto 0:66d8bd1f9d4c 158 //printf("Passou Settings, carregou arquivo\n");
rebonatto 0:66d8bd1f9d4c 159 //Settings::ShowValues();
rebonatto 0:66d8bd1f9d4c 160
rebonatto 0:66d8bd1f9d4c 161 InitializeEthernetLink();
rebonatto 32:8b108d8089e8 162 printf("Inicializou link Ethernet\n");
rebonatto 0:66d8bd1f9d4c 163
rebonatto 0:66d8bd1f9d4c 164 //Start HTTP POST service
rebonatto 0:66d8bd1f9d4c 165 Thread http_post(HttpPost::HttpPost_Thread);
rebonatto 0:66d8bd1f9d4c 166
rebonatto 0:66d8bd1f9d4c 167 //Start TCP daemon service
rebonatto 0:66d8bd1f9d4c 168 //Thread TcpService(CommTCP::CommTCP_Thread);
rebonatto 0:66d8bd1f9d4c 169
rebonatto 0:66d8bd1f9d4c 170 //Start Telnet Service
rebonatto 0:66d8bd1f9d4c 171 Thread telnetserver(TelnetServer::TelnetServer_Thread);
rebonatto 0:66d8bd1f9d4c 172
rebonatto 5:6860bed2d096 173 //Start TFTP Service
rebonatto 5:6860bed2d096 174 Thread tftpsrv(tftpsrv::tftpsrv_Thread);
rebonatto 0:66d8bd1f9d4c 175
rebonatto 5:6860bed2d096 176 //Start NTP Client for refresh time
rebonatto 5:6860bed2d096 177 //Thread ntpc(ntpc::ntpc_Thread);
mlucas82 22:a06c7bb05008 178 time_t tempo = time(NULL);
mlucas82 22:a06c7bb05008 179 if ((tempo+60) < 1420077600) set_time(1420077600);
rebonatto 0:66d8bd1f9d4c 180
rebonatto 29:fcfcb31a6362 181 // ntpClient.setTime("192.168.103.101");
mlucas82 26:7e215fc0c118 182
rebonatto 0:66d8bd1f9d4c 183 DisplayRAMBanks();
rebonatto 0:66d8bd1f9d4c 184
rebonatto 0:66d8bd1f9d4c 185 /*
rebonatto 0:66d8bd1f9d4c 186 unsigned short vet[256] = {2105,2105,2113,2127,2127,2125,2112,2113,2130,2130,2123,2112,2112,2128,2128,2123,2112,2113,2136,2136,2374,2551,2671,2869,2887,3036,2964,2964,2964,3145,3145,3206,3209,3298,3298,3264,3261,3208,3239,3239,3197,3197,3113,3032,3065,3065,3000,2901,2943,2943,2900,2852,2844,2863,2863,2838,2764,2791,2724,2724,2668,2710,2636,2658,2658,2606,2527,2443,2434,2434,2258,2066,2061,2080,2080,2063,2055,2055,2070,2070,2064,2051,2054,2069,2069,2062,2054,2058,2066,2309,2062,2052,2054,2067,2067,2063,2051,2049,2068,2068,2060,2053,2050,2067,2066,2069,2051,2053,2070,2070,2064,2050,2053,2070,2070,2062,2052,2055,2068,2068,2065,2052,2057,2072,2072,2064,2054,2054,2072,2072,2064,2053,2052,2069,2069,2064,2052,2053,2064,2064,2062,2049,2051,2067,2067,2059,2051,2050,2068,2068,2058,2046,2050,2068,2068,2061,2052,2058,2068,2068,2059,2052,2053,2067,2067,1744,1526,1471,1289,1289,1137,1142,1055,1120,1120,997,967,894,941,941,928,887,1001,949,949,1028,1105,1079,1191,1191,1223,1211,1223,1267,1267,1325,1267,1356,1327,1327,1369,1439,1381,1498,1498,1503,1503,1527,1545,1545,1635,1650,1778,1792,1792,1971,2108,2109,2126,2126,2124,2117,2118,2131,2131,2126,2118,2118,2138,2138,2134,2124,2114,2135,2135,2129,2121,2120,2136,2136,2128,2122,2122,2143,2120,2130,2120,2121,2139,2139,2130,2119,2121,2136,2136,2129};
rebonatto 0:66d8bd1f9d4c 187 float sen[12],cos[12],vm;
rebonatto 0:66d8bd1f9d4c 188 SignalProcessor::CalculateFFT(vet,sen,cos,&vm,1);
rebonatto 0:66d8bd1f9d4c 189
rebonatto 0:66d8bd1f9d4c 190 printf("VM = %f\n",vm);
rebonatto 0:66d8bd1f9d4c 191 for(int i=0;i<12;i++)
rebonatto 0:66d8bd1f9d4c 192 {
rebonatto 0:66d8bd1f9d4c 193 printf("SEN%d = %f, COS%d = %f\n",i,sen[i],i,cos[i]);
rebonatto 0:66d8bd1f9d4c 194 }
rebonatto 0:66d8bd1f9d4c 195 */
rebonatto 38:132e83a591d0 196 printf(PMEDLOG_INITIALIZINGWDT);
rebonatto 38:132e83a591d0 197 printf("\n\n");
rebonatto 0:66d8bd1f9d4c 198
rebonatto 0:66d8bd1f9d4c 199 //printf("0x%lx\n", LargeBuffer);
rebonatto 0:66d8bd1f9d4c 200
mlucas82 18:3e9ad4c03f85 201 PmedLog::WriteEntry(PMEDLOG_STARTED);
mlucas82 18:3e9ad4c03f85 202
rebonatto 38:132e83a591d0 203 /* Teste com WhatchDog
rebonatto 0:66d8bd1f9d4c 204 //Jump to the capture routine(will run on this thread)
rebonatto 0:66d8bd1f9d4c 205 thread1(NULL);
rebonatto 38:132e83a591d0 206 */
rebonatto 38:132e83a591d0 207
rebonatto 38:132e83a591d0 208 /* start test WahtchDog */
rebonatto 38:132e83a591d0 209 DigitalOut led1(LED1);
rebonatto 38:132e83a591d0 210 int n = 0;
rebonatto 38:132e83a591d0 211 //int tatual, tnovo;
rebonatto 38:132e83a591d0 212 float rms[NUMBER_OF_CHANNELS], mv2[NUMBER_OF_CHANNELS];
rebonatto 38:132e83a591d0 213 int under[NUMBER_OF_CHANNELS], over[NUMBER_OF_CHANNELS];
rebonatto 38:132e83a591d0 214
rebonatto 38:132e83a591d0 215 wdt.kick(10.0);
rebonatto 38:132e83a591d0 216
rebonatto 38:132e83a591d0 217 Capture::Initialize();
rebonatto 38:132e83a591d0 218
rebonatto 38:132e83a591d0 219 while(1)
rebonatto 38:132e83a591d0 220 {
rebonatto 38:132e83a591d0 221 Capture::Wait();
rebonatto 38:132e83a591d0 222
rebonatto 38:132e83a591d0 223 // Calcula o RMS dos 6 canais
rebonatto 38:132e83a591d0 224 SignalProcessor::CalculateRMSBulk(rms, mv2, under, over);
rebonatto 38:132e83a591d0 225
rebonatto 38:132e83a591d0 226 for(int i=0;i<6;i++){
rebonatto 38:132e83a591d0 227 EventDetector::get_Detector(i).ProcessEvent(rms[i], mv2[i], under[i], over[i]);
rebonatto 38:132e83a591d0 228 }
rebonatto 38:132e83a591d0 229
rebonatto 38:132e83a591d0 230 n++;
rebonatto 38:132e83a591d0 231 if(n==60)
rebonatto 38:132e83a591d0 232 {
rebonatto 38:132e83a591d0 233 printf("%.2f %.0f %.2f %.0f\t%.2f %.0f %.2f %.0f\t%.2f %.0f %.2f %.0f\n",rms[0], mv2[0],rms[1],mv2[1],rms[2],mv2[2],rms[3],mv2[3],rms[4],mv2[4],rms[5],mv2[5]);
rebonatto 38:132e83a591d0 234 led1 = !led1;
rebonatto 38:132e83a591d0 235 n=0;
rebonatto 38:132e83a591d0 236
rebonatto 38:132e83a591d0 237 PmedLog::Mark();
rebonatto 38:132e83a591d0 238 }
rebonatto 38:132e83a591d0 239
rebonatto 38:132e83a591d0 240 // End of main loop so "kick" to reset watchdog timer and avoid a reset
rebonatto 38:132e83a591d0 241 wdt.kick();
rebonatto 38:132e83a591d0 242
rebonatto 38:132e83a591d0 243 }
rebonatto 38:132e83a591d0 244 /* end test WahtchDog */
rebonatto 38:132e83a591d0 245
rebonatto 0:66d8bd1f9d4c 246 while(1){//never reaches here
mlucas82 18:3e9ad4c03f85 247 PmedLog::WriteEntry(PMEDLOG_WRONGCODE);
rebonatto 0:66d8bd1f9d4c 248 printf("Reset\n");
rebonatto 0:66d8bd1f9d4c 249 f = fopen(FILENAMERESET, "a");
rebonatto 0:66d8bd1f9d4c 250 if (f == NULL)
rebonatto 0:66d8bd1f9d4c 251 f = fopen(FILENAMERESET, "w");
rebonatto 0:66d8bd1f9d4c 252 fprintf(f, "Laco Errado\n");
rebonatto 0:66d8bd1f9d4c 253 fclose(f);
rebonatto 0:66d8bd1f9d4c 254 Thread::yield();
rebonatto 0:66d8bd1f9d4c 255 }
rebonatto 0:66d8bd1f9d4c 256 }
mlucas82 19:682cb884ec00 257